Esempio n. 1
0
        // Methods :: Private :: DownloadFromAmazonViaMusicBrainz
        /// <summary>
        ///	Get the cover URL from amazon with the help of libmusicbrainz
        /// </summary>
        /// <remarks>
        ///	This should only be called from <see cref="GetWebThread" />
        ///     and <see cref="DownloadFromAmazon" />. Normally,
        ///	<see cref="GetWeb" /> should be used instead.
        /// </remarks>
        /// <param name="album">
        ///	The <see cref="Album"> for which a cover needs to be downloaded.
        /// </param>
        /// <returns>
        ///	A <see cref="Gdk.Pixbuf" /> if a cover is found, null otherwise.
        /// </returns>
        /// <exception cref="WebException">
        ///	Thrown if an error occurred while downloading.
        /// </exception>
        /// <exception cref="GLib.GException">
        ///	Thrown if loading file fails.
        /// </exception>
        private Pixbuf DownloadFromAmazonViaMusicBrainz(Album album)
        {
            // Rather than do the lib search and catch the
            // DllNotFoundException every single time,
            // we check a simple bool as a performance helper.
            if (musicbrainz_lib_missing)
            {
                return(null);
            }

            Pixbuf pix = null;

            try {
                // Sane album title
                string sane_album_title;
                if (album.Name != null)
                {
                    sane_album_title = SanitizeString(album.Name);
                }
                else
                {
                    sane_album_title = String.Empty;
                }

                // Sane artist name
                string sane_artist_name;
                if (album.Artists != null && album.Artists.Length > 0)
                {
                    sane_artist_name = album.Artists [0].ToLower();
                }
                else
                {
                    sane_artist_name = String.Empty;
                }

                //
                string asin = null;

                // TODO: Move to constant
                string AmazonImageUri =
                    "http://images.amazon.com/images/P/{0}.01._SCMZZZZZZZ_.jpg";


                // remove "disc 1" and family
                //  TODO: Make the regexes translatable?
                //  (e.g. "uno|dos|tres...", "les|los..)
                string sane_album_title_regex = @"[,:]?\s*";
                sane_album_title_regex += @"(cd|dis[ck])\s*";
                sane_album_title_regex += @"(\d+|one|two|three|four|five|six|seven|eight|nine|ten)\s*$";
                sane_album_title        = Regex.Replace(sane_album_title, sane_album_title_regex, String.Empty);

                // Remove "The " and "the " from artist names
                string sane_artist_name_regex = @"^the\s+";
                sane_artist_name = Regex.Replace(sane_artist_name, sane_artist_name_regex, String.Empty);

                MusicBrainz c = new MusicBrainz();

                // set the depth of the query
                //   (see http://wiki.musicbrainz.org/ClientHOWTO)
                c.SetDepth(4);

                string [] album_name = new string [] { sane_album_title };

                bool match =
                    c.Query(MusicBrainz.MBQ_FindAlbumByName, album_name);

                if (match)
                {
                    int num_albums =
                        c.GetResultInt(MusicBrainz.MBE_GetNumAlbums);

                    string fetched_artist_name;
                    for (int i = 1; i <= num_albums; i++)
                    {
                        c.Select(MusicBrainz.MBS_SelectAlbum, i);

                        // gets the artist from the first track of the album
                        c.GetResultData
                            (MusicBrainz.MBE_AlbumGetArtistName, 1,
                            out fetched_artist_name);

                        // Remove "The " here as well
                        if (fetched_artist_name != null)
                        {
                            string tmp = fetched_artist_name.ToLower();
                            string fetched_artist_name_regex = @"^the\s+";
                            fetched_artist_name = Regex.Replace(tmp, fetched_artist_name_regex, String.Empty);
                        }
                        else
                        {
                            fetched_artist_name = String.Empty;
                        }

                        if (fetched_artist_name == sane_artist_name)
                        {
                            c.GetResultData
                                (MusicBrainz.MBE_AlbumGetAmazonAsin, out asin);

                            break;
                        }

                        // go back one level so we can select the next album
                        c.Select(MusicBrainz.MBS_Back);
                    }
                }

                if (asin == null)
                {
                    pix = null;
                }
                else
                {
                    string uri = String.Format(AmazonImageUri, asin);
                    pix = Download(uri);
                }
            } catch (DllNotFoundException) {
                // We catch this exception so we can always include the
                // musicbrainz support but not have a strict compile/runtime
                // requirement on it.
                musicbrainz_lib_missing = true;
            }

            return(pix);
        }
Esempio n. 2
0
        //////////////////////////////////////////////////////////////////////////
        // Method:    GetMusicBrainzReleases
        // FullName:  MusicDataminer.MusicDBParser.GetMusicBrainzReleases
        // Access:    public
        // Returns:   bool
        // Parameter: string artist
        // Parameter: string albumName
        // Parameter: MusicBrainz o
        // Parameter: List<MusicBrainzAlbum> releasesList
        // Parameter: out string retrievedName
        //////////////////////////////////////////////////////////////////////////
        public bool GetMusicBrainzReleases(string artist, string albumName,
                                           List <MusicBrainzRelease> releasesList, out string retrievedName, string aStyle)
        {
            MusicBrainz o;

            // Create the musicbrainz object, which will be needed for subsequent calls
            o = new MusicBrainz();
            // Set the proper server to use. Defaults to mm.musicbrainz.org:80
            if (Environment.GetEnvironmentVariable("MB_SERVER") != null)
            {
                o.SetServer(Environment.GetEnvironmentVariable("MB_SERVER"), 80);
            }
            // Check to see if the debug env var has been set
            if (Environment.GetEnvironmentVariable("MB_DEBUG") != null)
            {
                o.SetDebug(Environment.GetEnvironmentVariable("MB_DEBUG") != "0");
            }
            // Tell the server to only return 4 levels of data, unless the MB_DEPTH env var is set
            if (Environment.GetEnvironmentVariable("MB_DEPTH") != null)
            {
                o.SetDepth(int.Parse(Environment.GetEnvironmentVariable("MB_DEPTH")));
            }
            else
            {
                o.SetDepth(4);
            }

            retrievedName = albumName;
            bool foundRelevantRelease = false;

            //Console.WriteLine("Searching for occurrences for: " + artist + " / " + albumName);
            iForm.PrintLine("(" + aStyle + ") LOOK: " + artist + " / " + albumName);
            bool found = o.Query(MusicBrainz.MBQ_FileInfoLookup, new String[] { "", artist, albumName, "", "", "" });

            if (!found)
            {
                return(found);
            }

            // Select the first album
            o.Select(MusicBrainz.MBS_SelectLookupResult, 1);

            string type;

            o.GetResultData(MusicBrainz.MBE_LookupGetType, out type);
            string fragment;

            o.GetFragmentFromURL(type, out fragment);

            // iterate through all the results
            o.Select(MusicBrainz.MBS_Rewind);

            if (!o.Select(MusicBrainz.MBS_SelectLookupResult, 1))
            {
                return(foundRelevantRelease);
            }

            // NOTE: must be done before the next Select
            int relevance = o.GetResultInt(MusicBrainz.MBE_LookupGetRelevance);

            // if not sure about it, quit
            if (relevance < 80)
            {
                return(foundRelevantRelease);
            }

            // select the album
            o.Select(MusicBrainz.MBS_SelectLookupResultAlbum);

            o.GetResultData(MusicBrainz.MBE_AlbumGetAlbumName, out retrievedName);

            int nReleases = o.GetResultInt(MusicBrainz.MBE_AlbumGetNumReleaseDates);

            if (nReleases != 0)
            {
                albumName = retrievedName;

                for (int i = 1; i <= nReleases; i++)
                {
                    if (o.Select(MusicBrainz.MBS_SelectReleaseDate, i))
                    {
                        foundRelevantRelease = true;

                        string country;
                        o.GetResultData(MusicBrainz.MBE_ReleaseGetCountry, out country);

                        string date;
                        o.GetResultData(MusicBrainz.MBE_ReleaseGetDate, out date);

                        if (dataBase.countries.ContainsKey(country.ToUpper()))
                        {
                            // add it to the list
                            MusicBrainzRelease release = new MusicBrainzRelease();

                            release.country = (Country)dataBase.countries[country];

                            if (date.Length >= 4)
                            {
                                release.date = int.Parse(date.Substring(0, 4));
                            }
                            else
                            {
                                return(false); //release.date = int.Parse(date);
                            }

                            // Add the release both to the Album and Country
                            releasesList.Add(release);
                            release.country.releases.Add(release);
                        }
                    }

                    o.Select(MusicBrainz.MBS_Back);
                }
            }
            return(foundRelevantRelease);
        }