コード例 #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);
        }