예제 #1
0
        private SimpleAlbum DebugLookupAlbumById(string id)
        {
            SimpleAlbum album = null;

            StringBuilder output = new StringBuilder();

            string data, temp = null;
            bool   ret, isMultipleArtist = false;
            int    numTracks, trackNum = 0;

            //string id = //"cCQzv12PgDzszIK8_ZLHSK7oHJc-";


            // Create the musicbrainz object, which will be needed for subsequent calls
            //MusicBrainzClient o = new MusicBrainzClient();
            //Client o = new Client();

            // Set the proper server to use. Defaults to mm.musicbrainz.org:80
            if (Environment.GetEnvironmentVariable("MB_SERVER") != null)
            {
                client.Server = new ServerInfo(Environment.GetEnvironmentVariable("MB_SERVER"), 80);
            }

            // Check to see if the debug env var has been set
            if (Environment.GetEnvironmentVariable("MB_DEBUG") != null)
            {
                client.Debug = (Environment.GetEnvironmentVariable("MB_DEBUG") != "0");
            }

            // Tell the server to only return 2 levels of data, unless the MB_DEPTH env var is set
            if (Environment.GetEnvironmentVariable("MB_DEPTH") != null)
            {
                client.QueryDepth = (int.Parse(Environment.GetEnvironmentVariable("MB_DEPTH"), System.Globalization.CultureInfo.InvariantCulture));
            }
            else
            {
                client.QueryDepth = 4;
            }

            // Set up the args for the find album query
            string[] args = new string[] { id };

            if (id.Length != MusicBrainzConstants.CDINDEX_ID_LEN)
            {
                // Execute the MBQ_GetAlbumById query
                ret = client.Query(rdf.QueryGetAlbumById, args);
            }
            else
            {
                // Execute the MBQ_GetCDInfoFromCDIndexId
                ret = client.Query(rdf.QueryGetCDInfoFromCDIndexId, args);
            }

            if (!ret)
            {
                //o.GetQueryError(out error);
                System.Diagnostics.Debug.WriteLine("Query failed: {0}", client.QueryError);
                return(album);                //o.QueryError;
            }

            // Select the first album
            client.Select(rdf.SelectAlbum, 1);

            // Pull back the album id to see if we got the album
            data = client.GetResultData(rdf.ExpressionAlbumGetAlbumId);
            if (client.CurrentResult == 0)
            {
                System.Diagnostics.Debug.WriteLine("Album not found.");
                return(album);                //string.Empty;
            }
            temp = client.GetIdFromUrl(data);
            System.Diagnostics.Debug.WriteLine("    AlbumId: {0}", temp);
            output.AppendLine("AlbumId: " + temp);

            // Extract the album name
            data = client.GetResultData(rdf.ExpressionAlbumGetAlbumName);
            if (client.CurrentResult != 0)
            {
                System.Diagnostics.Debug.WriteLine("       Name: {0}", data);
                output.AppendLine("Name: " + data);
            }

            // Extract the number of tracks
            numTracks = client.GetResultInt(rdf.ExpressionAlbumGetNumberTracks);
            if (numTracks > 0 && numTracks < 100)
            {
                System.Diagnostics.Debug.WriteLine("  NumTracks: {0}", numTracks.ToString(System.Globalization.NumberFormatInfo.InvariantInfo));
                output.AppendLine("Tracks: " + numTracks.ToString(System.Globalization.NumberFormatInfo.InvariantInfo));
            }

            // Check to see if there is more than one artist for this album
            for (int i = 1; i <= numTracks; i++)
            {
                data = client.GetResultData(rdf.ExpressionAlbumGetArtistId, i);
                if (client.CurrentResult == 0)
                {
                    break;
                }

                if (i == 1)
                {
                    temp = data;
                }

                if (temp != data)
                {
                    isMultipleArtist = true;
                    break;
                }
            }

            if (!isMultipleArtist)
            {
                // Extract the artist name from the album
                data = client.GetResultData(rdf.ExpressionAlbumGetArtistName, 1);
                if (client.CurrentResult != 0)
                {
                    System.Diagnostics.Debug.WriteLine("AlbumArtist: {0}", data);
                    output.AppendLine("Album Artist: " + data);
                }

                // Extract the artist id from the album
                data = client.GetResultData(rdf.ExpressionAlbumGetArtistId, 1);
                if (client.CurrentResult != 0)
                {
                    temp = client.GetIdFromUrl(data);
                    System.Diagnostics.Debug.WriteLine("   ArtistId: {0}", temp);
                    output.AppendLine("ArtistId: " + temp);
                }
            }

            System.Diagnostics.Debug.WriteLine(string.Empty);

            for (int i = 1; i <= numTracks; i++)
            {
                // Extract the track name from the album.
                data = client.GetResultData(rdf.ExpressionAlbumGetTrackName, i);
                if (client.CurrentResult != 0)
                {
                    System.Diagnostics.Debug.WriteLine("      Track: {0}", data);
                    output.AppendLine("Track: " + data);
                }
                else
                {
                    break;
                }

                // Extract the album id from the track. Just use the
                // first album that this track appears on
                data = client.GetResultData(rdf.ExpressionAlbumGetTrackId, i);
                if (client.CurrentResult != 0)
                {
                    temp = client.GetIdFromUrl(data);
                    System.Diagnostics.Debug.WriteLine("    TrackId: {0}", temp);
                    output.AppendLine("TrackId: " + temp);

                    // Extract the track number
                    trackNum = client.GetOrdinalFromList(rdf.ExpressionAlbumGetTrackList, data);
                    if (trackNum > 0 && trackNum < 100)
                    {
                        System.Diagnostics.Debug.WriteLine("  TrackNum: {0}", trackNum.ToString(System.Globalization.NumberFormatInfo.InvariantInfo));
                        output.AppendLine("Track Number: " + trackNum.ToString(System.Globalization.NumberFormatInfo.InvariantInfo));
                    }
                }

                // If its a multple artist album, print out the artist for each track
                if (isMultipleArtist)
                {
                    // Extract the artist name from this track
                    data = client.GetResultData(rdf.ExpressionAlbumGetArtistName, i);
                    if (client.CurrentResult != 0)
                    {
                        System.Diagnostics.Debug.WriteLine("TrackArtist: {0}", data);
                        output.AppendLine("Track Artist: " + data);
                    }

                    // Extract the artist id from this track
                    data = client.GetResultData(rdf.ExpressionAlbumGetArtistId, i);
                    if (client.CurrentResult != 0)
                    {
                        temp = client.GetIdFromUrl(data);
                        System.Diagnostics.Debug.WriteLine("   ArtistId: {0}", temp);
                        output.AppendLine("Track ArtistId: " + temp);
                    }
                }
                System.Diagnostics.Debug.WriteLine(string.Empty);
            }

            //return output.ToString();
            return(album);
        }
예제 #2
0
        public void QueryCDMetadata()
        {
            if (!client.Query(rdf.QueryGetCDInfo))
            {
                // "Could not query CD"
                Debug.WriteLine("Could not query CD");
                throw new ApplicationException("Could not query CD");
            }

            int disc_count = client.GetResultInt(rdf.ExpressionGetNumberAlbums);

            if (disc_count <= 0)
            {
                // "No Discs Found"
                Debug.WriteLine("No Discs Found");
                throw new ApplicationException("No Discs Found");
            }

            // handle more than 1 disc? not sure how this works? for multi-disc sets?
            // if that's the case, we only care about the first one I guess... I hope

            if (!client.Select(rdf.SelectAlbum, 1))
            {
                // "Could not select disc"
                Debug.WriteLine("Could not select disc");
                throw new ApplicationException("Could not select disc");
            }

            amazonAsin  = client.GetResultData(rdf.ExpressionAlbumGetAmazonAsin, 1);
            coverArtUrl = new Uri(rdf.AmazonCoverArtUrlPrefix + amazonAsin);
            artistName  = client.GetResultData(rdf.ExpressionAlbumGetArtistName, 1);
            albumUrl    = new Uri(client.GetResultData(rdf.ExpressionAlbumGetAlbumId));
            albumId     = client.GetIdFromUrl(albumUrl);
            albumName   = client.GetResultData(rdf.ExpressionAlbumGetAlbumName, 1);

            int num_releases = client.GetResultInt(rdf.ExpressionAlbumGetNumberReleaseDates);

            if (num_releases > 0)
            {
                client.Select(rdf.SelectReleaseDate, 1);

                string raw_date = client.GetResultData(rdf.ExpressionReleaseGetDate);
                if (raw_date != null && raw_date.Length > 0)
                {
                    string [] parts = raw_date.Split('-');
                    if (parts.Length == 3)
                    {
                        int year  = Convert.ToInt32(parts[0], System.Globalization.NumberFormatInfo.InvariantInfo);
                        int month = Convert.ToInt32(parts[1], System.Globalization.NumberFormatInfo.InvariantInfo);
                        int day   = Convert.ToInt32(parts[2], System.Globalization.NumberFormatInfo.InvariantInfo);

                        releaseDate = new DateTime(year, month, day);
                    }
                }

                client.Select(rdf.SelectBack);
            }

            int trackCount = client.GetResultInt(rdf.ExpressionAlbumGetNumberTracks, 1);

            if (trackCount <= 0)
            {
                // "Invalid track count from album query"
                Debug.WriteLine("Invalid track count from album query");
                throw new ApplicationException("Invalid track count from album query");
            }

            tracks = new List <MediaItem>(trackCount);

            string pathRoot = string.Format("file://{0}{1}", client.Device, System.IO.Path.DirectorySeparatorChar);

            for (int i = 1; i <= trackCount; i++)
            {
                client.Select(rdf.SelectTrack, i);

                //[i - 1] = new SimpleTrack(); //new SimpleTrack(i, lengths[i - 1]);
                string artist      = client.GetResultData(rdf.ExpressionTrackGetArtistName);
                string title       = client.GetResultData(rdf.ExpressionTrackGetTrackName);
                int    trackNumber = i;
                Uri    path        = new Uri(string.Format("{0}Track {1}", pathRoot, i));

                TimeSpan duration     = TimeSpan.Zero;
                int      milliseconds = client.GetResultInt(rdf.ExpressionTrackGetTrackDuration);
                if (milliseconds > 0)
                {
                    //duration = duration / 1000;
                    duration = new TimeSpan(0, 0, 0, 0, milliseconds);
                }

                MediaItem track = new MediaItem(Guid.NewGuid(), ModelConstants.MEDIA_SOURCE_CD, ModelConstants.MEDIA_TYPE_AUDIO, trackNumber, title, artist, albumName, duration, releaseDate, MusicBrainzConstants.FORMAT_CD, path);
                tracks.Add(track);

                client.Select(rdf.SelectBack);
            }

            client.Select(rdf.SelectBack);
        }
예제 #3
0
        public static SimpleTrack FileLookup(MusicBrainzClient client, string artistName, string albumName, string trackName, int trackNumber, int duration)
        {
            Rdf rdf = new Rdf();

            SimpleTrack track = null;

            if (client != null)
            {
                client.QueryDepth = 4;

                if (!client.Query(rdf.QueryFileInfoLookup, new string[]
                                  { string.Empty,             // trmid
                                    artistName,
                                    albumName,
                                    trackName,
                                    trackNumber.ToString(System.Globalization.CultureInfo.InvariantCulture),
                                    duration.ToString(System.Globalization.CultureInfo.InvariantCulture),
                                    string.Empty,             // filename
                                    string.Empty,             // artistid
                                    string.Empty,             // albumid
                                    string.Empty }))
                {
                    //"File Lookup Query unsuccessful"
                    throw new ApplicationException("File Lookup Query unsuccessful");
                }

                client.Select(rdf.SelectRewind);

                if (!client.Select(rdf.SelectLookupResult, 1))
                {
                    // "Selection failed"
                    throw new ApplicationException("Selection failed");
                }

                track = new SimpleTrack();
                string result_type = client.GetId(client.GetResultData(rdf.ExpressionLookupGetType));

                switch (result_type)
                {
                case "AlbumTrackResult":
                    client.Select(rdf.SelectLookupResultTrack);
                    //track.Title = client.GetResultData(rdf.ExpressionTrackGetTrackName);
                    track.Title  = client.GetResultData(rdf.ExpressionTrackGetTrackName);
                    track.Artist = client.GetResultData(rdf.ExpressionTrackGetArtistName);
                    int length = (client.GetResultInt(rdf.ExpressionTrackGetTrackDuration) / 1000);
                    track.Duration = new TimeSpan(0, 0, 0, 0, length);
                    //track.Length = client.GetResultInt(rdf.ExpressionTrackGetTrackDuration);
                    client.Select(rdf.SelectBack);

                    client.Select(rdf.SelectLookupResultAlbum, 1);
                    track.Album      = client.GetResultData(rdf.ExpressionAlbumGetAlbumName);
                    track.TrackCount = client.GetResultInt(rdf.ExpressionAlbumGetNumberTracks);
                    track.Number     = client.GetResultInt(rdf.ExpressionAlbumGetTrackNumber);
                    track.Asin       = client.GetResultData(rdf.ExpressionAlbumGetAmazonAsin);
                    client.Select(rdf.SelectBack);
                    break;

                case "AlbumResult":
                    client.Select(rdf.SelectLookupResultAlbum, 1);
                    track.TrackCount = client.GetResultInt(rdf.ExpressionAlbumGetNumberTracks);
                    track.Album      = client.GetResultData(rdf.ExpressionAlbumGetAlbumName);
                    track.Asin       = client.GetResultData(rdf.ExpressionAlbumGetAmazonAsin);

                    string track_id = client.GetResultData(rdf.ExpressionAlbumGetTrackId, trackNumber);

                    if (client.Query(rdf.QueryGetTrackById, new string[] { client.GetId(track_id) }))
                    {
                        client.Select(rdf.SelectTrack, 1);
                        //track.Title = client.GetResultData(rdf.ExpressionTrackGetTrackName);
                        track.Title  = client.GetResultData(rdf.ExpressionTrackGetTrackName);
                        track.Artist = client.GetResultData(rdf.ExpressionTrackGetArtistName);
                        track.Number = client.GetResultInt(rdf.ExpressionTrackGetTrackNumber);
                        track.Length = client.GetResultInt(rdf.ExpressionTrackGetTrackDuration);
                        client.Select(rdf.SelectBack);
                    }

                    client.Select(rdf.SelectBack);
                    break;

                default:
                    //"Invalid result type: " + result_type
                    throw new ApplicationException("Invalid result type: " + result_type);
                }
            }
            return(track);
        }
예제 #4
0
        public static SimpleTrack FileLookup(MusicBrainzClient client, string artistName, string albumName, string trackName, int trackNumber, int duration)
        {
			Rdf rdf = new Rdf();
        
			SimpleTrack track = null;
        
			if (client != null)
			{
				client.QueryDepth = 4;

				if (!client.Query(rdf.QueryFileInfoLookup, new string[]
					{	string.Empty, // trmid
						artistName,
						albumName,
						trackName,
						trackNumber.ToString(System.Globalization.CultureInfo.InvariantCulture),
						duration.ToString(System.Globalization.CultureInfo.InvariantCulture),
						string.Empty, // filename
						string.Empty, // artistid
						string.Empty, // albumid
						string.Empty
					}))
				{
					//"File Lookup Query unsuccessful"
					throw new ApplicationException("File Lookup Query unsuccessful");
				}

				client.Select(rdf.SelectRewind);

				if (!client.Select(rdf.SelectLookupResult, 1))
				{
					// "Selection failed"
					throw new ApplicationException("Selection failed");
				}
				
				track = new SimpleTrack();
				string result_type = client.GetId(client.GetResultData(rdf.ExpressionLookupGetType));
            
				switch(result_type)
				{
					case "AlbumTrackResult":
						client.Select(rdf.SelectLookupResultTrack);
						//track.Title = client.GetResultData(rdf.ExpressionTrackGetTrackName);
						track.Title = client.GetResultData(rdf.ExpressionTrackGetTrackName);
						track.Artist = client.GetResultData(rdf.ExpressionTrackGetArtistName);
						int length = (client.GetResultInt(rdf.ExpressionTrackGetTrackDuration) / 1000);
						track.Duration = new TimeSpan(0, 0, 0, 0, length);
						//track.Length = client.GetResultInt(rdf.ExpressionTrackGetTrackDuration);
						client.Select(rdf.SelectBack);

						client.Select(rdf.SelectLookupResultAlbum, 1);
						track.Album = client.GetResultData(rdf.ExpressionAlbumGetAlbumName);
						track.TrackCount = client.GetResultInt(rdf.ExpressionAlbumGetNumberTracks);
						track.Number = client.GetResultInt(rdf.ExpressionAlbumGetTrackNumber);
						track.Asin = client.GetResultData(rdf.ExpressionAlbumGetAmazonAsin);
						client.Select(rdf.SelectBack);
						break;
					case "AlbumResult":
						client.Select(rdf.SelectLookupResultAlbum, 1);
						track.TrackCount = client.GetResultInt(rdf.ExpressionAlbumGetNumberTracks);
						track.Album = client.GetResultData(rdf.ExpressionAlbumGetAlbumName);
						track.Asin = client.GetResultData(rdf.ExpressionAlbumGetAmazonAsin);

						string track_id = client.GetResultData(rdf.ExpressionAlbumGetTrackId, trackNumber);

						if (client.Query(rdf.QueryGetTrackById, new string[] { client.GetId(track_id) }))
						{
							client.Select(rdf.SelectTrack, 1);
							//track.Title = client.GetResultData(rdf.ExpressionTrackGetTrackName);
							track.Title = client.GetResultData(rdf.ExpressionTrackGetTrackName);
							track.Artist = client.GetResultData(rdf.ExpressionTrackGetArtistName);
							track.Number = client.GetResultInt(rdf.ExpressionTrackGetTrackNumber);
							track.Length = client.GetResultInt(rdf.ExpressionTrackGetTrackDuration);
							client.Select(rdf.SelectBack);
						}

						client.Select(rdf.SelectBack);
						break;
					default:
						//"Invalid result type: " + result_type
						throw new ApplicationException("Invalid result type: " + result_type);
				}
            }
            return track;
        }