private async Task <string> GetWikiDataAsync(MusicBrainz musicBrainz) { foreach (var relation in musicBrainz.Relations) { if (relation.Type.ToLower() == "wikidata") { var url = relation.Url.Resource.ToString(); var id = url.Split('/').LastOrDefault(); var wikiUrl = $"https://www.wikidata.org/w/api.php?action=wbgetentities&ids={id}&format=json&props=sitelinks"; var response = await _httpClient.GetStringAsync(wikiUrl); var wikiData = DecodeWikiData(response, id); return(await GetWikipediaAsync(wikiData.Title)); } if (relation.Type.ToLower() == "wikipedia") { // ToDo: Where do I get the artist title here? // Have not seen any result which have a wikipedia link. Guessing that this is correct. var url = relation.Url.Resource.ToString(); var title = url.Split('/').LastOrDefault(); return(await GetWikipediaAsync(title)); } } return(""); }
/// <summary> /// /// </summary> /// <param name="query"></param> /// <returns></returns> private static Boolean IsArtist(String query) { MusicBrainz mb = new MusicBrainz(); MBArtist artist = null; return(mb.IsArtist(query, out artist)); }
public async Task <MusicBrainz> GetMusicBrainz(string mbId) { var httpResponseMessage = await _musicBrainAdapter.GetMusicBrainzArtistByMbId(mbId); var json = await httpResponseMessage.Content.ReadAsStringAsync(); if (!httpResponseMessage.IsSuccessStatusCode) { throw new BadRequestException("Invalid request: MBId does not exist"); } var artistResponse = JsonConvert.DeserializeObject <MbArtistResponse>(json); if (artistResponse.ReleaseGroups == null || !artistResponse.ReleaseGroups.Any()) { throw new NotFoundException("Not Found request: Artist"); } var relations = artistResponse.Relations; var musicBrainz = new MusicBrainz { MbId = mbId, Description = await GetDescriptionIfUrlIsEmpty(relations.FirstOrDefault()?.Url, mbId), Albums = new List <Album>() }; var wikiDataQId = GetWikidataQId(relations); var wikiTitle = await _wikidataAdapter.GetWikiDataTitleByQId(wikiDataQId); musicBrainz.Description = await _wikipediaAdapter.GetWikiDescriptionByTitle(HttpUtility.UrlPathEncode(wikiTitle)); await Task.Run(() => { Parallel.ForEach(artistResponse.ReleaseGroups, (releaseGroup) => { var album = new Album { Id = releaseGroup.CoverArtId, Title = releaseGroup.Title, Image = _coverArtAdapter .GetCoverArtImageLinkByMbId(releaseGroup.CoverArtId) .Result }; musicBrainz.Albums.Add(album); }); } ); return(musicBrainz); }
public void GetReleases() { Console.WriteLine("Enter artist name or enter to exit."); Console.Write(">"); string line; line = Console.ReadLine(); if (line == null) { return; } Console.WriteLine("Looking up: " + line); var newReleases = new MusicBrainz(); var results = newReleases.GetArtistListFromMusicBrainz(line); //var results = newReleases.DiscogArtistSearch("Motley Crue"); if (results.Count > 0) { int count = 1; Console.WriteLine("Possible Artists:"); foreach (Artist result in results) { result.Count = count; Console.WriteLine(result.Count + "\t" + result.Name + "\t" + result.Description); count++; if (count == 10) { break; } } Console.Write("Please choose the correct artist: "); int choice; bool valid = int.TryParse(Console.ReadLine(), out choice); if (valid && choice > 0 && choice <= count) { var artist = results.FirstOrDefault(x => choice.Equals(x.Count)); artist.Releases = newReleases.GetAsinDataFromMusicBrainz(artist.Id); var amazonRequest = new AmazonProductAPI(); amazonRequest.GetAmazonArtistId(artist); } else { Console.WriteLine("Invalid choice"); } } Console.ReadLine(); }
private async Task <List <AlbumData> > GetAlbumListWithCoverAsync(MusicBrainz musicBrainz) { var albumDataList = new List <AlbumData>(); var listOfTasks = new List <Task <AlbumData> >(); foreach (var album in musicBrainz.ReleaseGroups) { if (album.PrimaryType.ToLower() == "album") { listOfTasks.Add(GetAlbumDataAsync(album)); } } var list = await Task.WhenAll <AlbumData>(listOfTasks); albumDataList.AddRange(list); return(albumDataList); }
static void Main(string[] args) { //NounCollection coll = NounCollection.Instance; //String [] related = null; //Console.WriteLine(coll.Nouns["hello"].GetDescription(0, out related)); //using (StreamWriter writer = new StreamWriter(File.Create(@"\1.txt"))) //{ // for (int index = 0; index < 256; index++) // writer.WriteLine(String.Concat(index, " ", (char)index)); //} //Console.WriteLine((int)'e');//'é'); //BingSearch search = new BingSearch(); //search.SearchWeb("apj kalam"); //Wikipedia wClass = new Wikipedia(new WikiQueryEngine()); //Console.WriteLine(wClass.IsArticleAvailable("Plain white T's")); //SearchSuggestion searchSuggest = wClass.SearchArticles("Plain white T's", 10); //foreach (SearchSuggestionItem sItem in searchSuggest.Section) //{ // Console.WriteLine(sItem.Text + "\n\t" + sItem.Description + "\n" + sItem.Url); //} //Console.WriteLine(wClass.GetArticle("Seattle", false).Introduction); OmukSemantics semantics = new OmukSemantics(); //YSearch search = new YSearch(); //YSearchResultCollection resCollection = search.SearchWeb("me genera serotonina listening to Viva la Vida by Coldplay", 0, 10, ref semantics); BingSearch bsearch = new BingSearch(); bsearch.SearchWeb("Am I Dreaming by Xscape", 0, 10, ref semantics); semantics.PostSearch(); semantics.FormContext(); LyricWiki wiki = new LyricWiki(); LyricsResult lyrics = wiki.getSong("Rihanna", "Umbrella"); Console.WriteLine(lyrics.lyrics); //Console.WriteLine(""); LastFmService service = new LastFmService(); //service.GetArtistInfo( //MusicBrainz mb = new MusicBrainz(); // mb.GetAlbums //Grooveshark gshark = new Grooveshark(); //GSSong[] song = gshark.SearchSong("Sugar", 2); //LyricWiki wiki = new LyricWiki(); //LyricsResult lyrics = wiki.getSong("R.e.m", "Moon river"); //lyrics.GetLyricsHtml(); //SongResult songs = wiki.searchSongs("R.e.m", "Moon river"); //String artist = "Flo rida"; //String album = "Sugar"; //int year = 2009; //String[] albums = null; //String res = wiki.getAlbum(ref artist, ref album, ref year, out albums); //AlbumData[] album = (AlbumData[])wiki.getArtist(ref artist); //LWLyrics lyrics = wiki.GetLyricsForSong("Flo rida", "sugar"); //Console.WriteLine(wiki.IsArtist("rihanna")); //String[] albums = wiki.GetAlbums("rihanna"); //LastFmService service = new LastFmService(); //LFMArtist artist = service.GetArtistInfo("Rihanna"); //List<LFMArtist> albums = service.SearchArtist("Kid Cudi", 0); //Immem immem = new Immem(); //List<ImmemSearchItem> items = immem.SearchMedia("Rihanna good girl gone bad", "music", 0, 10); MusicBrainz mb = new MusicBrainz(); List <MBAlbum> albums = mb.GetAlbums(String.Empty, "Rihanna", "73e5e69d-3554-40d8-8516-00cb38737a1c", 0, 10); List <MBSong> songs = mb.GetSongs("oh Carol", "Neil", "", "", "", 0, 2); //YouTube yt = new YouTube(); //List<YTVideo> videos = yt.SearchVideos("oh carol", 0, 3); //RottenTomatoes rt = new RottenTomatoes(); //String val = rt.GetTomatometerScore("http://www.rottentomatoes.com/m/1009437-heidi/"); /* * Twitter twitter = new Twitter(); * ////twitter.SearchTwitter("i gotta feeling", 0, 10); * TMovieTrendsClass mTrends = new TMovieTrendsClass(); * twitter.GetMovieTrends("The Martian", ref mTrends); * Console.WriteLine(""); */ //twitter.GetMovieTrends("\"the proposal\"",ref mTrends); //twitter.GetMovieTrends("\"year one\"", ref mTrends); //twitter.GetMovieTrends("\"Terminator Salvation\"", ref mTrends); //twitter.GetMovieTrends("\"Next day air\"", ref mTrends); //twitter.GetMovieTrends("\"Imagine That\"", ref mTrends); //twitter.GetMovieTrends("\"Taking of Pelham 1 2 3", ref mTrends); //twitter.GetMovieTrends("Wolverine", ref mTrends); //using (StreamWriter writer = new StreamWriter(File.Create("trend.txt"))) //{ // foreach (KeyValuePair<String, int> keyval in mTrends.Trend) // { // if (keyval.Value > 3) // writer.WriteLine(String.Format("{0} {1}", keyval.Key, keyval.Value)); // } //} //TSearchResultCollection twRes = twitter.SearchTwitter("palm pre", 0, 15); //Eventful evntful = new Eventful (); //EventCollection coll = evntful.SearchEvents("San Jose", 0, 10); //Digg digg = new Digg(); //List<DiggResult> res = digg.SearchDigg("Hello", 0, 10); return; }
/// <summary> /// /// </summary> public void FormContext() { if (this.Category.Equals("music")) { MusicBrainz mb = new MusicBrainz(); String artist = String.Empty; String song = String.Empty; string mbid = String.Empty; int wStrength = Int32.MaxValue; int artistIndex = 0; String[] kWArr = this.KeyWords.ToArray(); this.keyWords.Clear(); for (int index = 0; index < kWArr.Length; index++) { String txt = kWArr[index].ToLower(); List<MBArtist> artists = mb.GetArtists(txt, 0, 2); if (artists != null) { foreach (MBArtist mbartist in artists) { String largeTxttmp = mbartist.Name.ToLower(); int weight = this.MapReplace(txt, largeTxttmp); if (weight <= 3 && weight <= wStrength) { wStrength = weight; artist = mbartist.Name; mbid = mbartist.MBID; artistIndex = index; } } } } artistIndex = artistIndex == 0 ? 1 : 0; if (!String.IsNullOrEmpty(artist) && artistIndex < kWArr.Length) { List<MBSong> songs = mb.GetSongs(kWArr[artistIndex], artist, String.Empty, String.Empty, String.Empty, 0, 3); if (songs != null && songs.Count > 0) song = songs[0].Title; else { org.lyricwiki.www.service.LyricWiki lw = new org.lyricwiki.www.service.LyricWiki(); org.lyricwiki.www.service.LyricsResult lyrics = lw.getSong(artist, kWArr[artistIndex]); if (lyrics != null) { song = lyrics.song; } } } this.keyWords.Add("artist:" + artist); this.keyWords.Add("album:" + String.Empty); this.keyWords.Add("song:" + song); this.keyWords.Add("mbid:" + mbid); } return; }
// 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); }
////////////////////////////////////////////////////////////////////////// // 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); }