コード例 #1
0
        public async Task <Artist> SearchArtistOnline(string name)
        {
            Artist foundArtist;

            if (SearchArtist(name, out foundArtist))
            {
                return(foundArtist);
            }

            using (var wc = new WebClient {
                Proxy = null
            })
            {
                ArtistSearchResult searchedArtists;

                var s = (await
                         wc.DownloadStringTaskAsync(
                             $"http://ws.audioscrobbler.com/2.0/?method=artist.search&artist={Uri.EscapeDataString(name)}&api_key={SensitiveInformation.LastfmKey}&format=json&limit=1"))
                        .FixJsonString();
                try
                {
                    searchedArtists = JsonConvert.DeserializeObject <ArtistSearchResult>(s);
                }
                catch (JsonException)
                {
                    return(null);
                }

                var match = searchedArtists?.results?.ArtistMatches?.artist;
                if (match == null)
                {
                    return(null);
                }

                if (SearchArtist(match.name, out foundArtist))
                {
                    if (ArtistKeywords.ContainsKey(foundArtist))
                    {
                        ArtistKeywords[foundArtist].Add(name);
                    }
                    else
                    {
                        ArtistKeywords.Add(foundArtist, new List <string> {
                            name
                        });
                    }

                    return(foundArtist);
                }

                var artist = new Artist(match.name)
                {
                    MusicBrainzId = match.mbid,
                    Url           = match.url
                };
                SetImages(artist, match.image);

                await Artists.AddArtist(artist);

                ArtistKeywords.Add(artist, new List <string> {
                    name
                });
                return(artist);
            }
        }
コード例 #2
0
        public async Task SetAdvancedInfoAboutArtist(Artist artist, CultureInfo culture)
        {
            if (artist.ProvidesAdvancedInfo)
            {
                return;
            }

            using (var wc = new WebClient {
                Proxy = null
            })
            {
                GetArtistInfoResult artistInfo;
                try
                {
                    var jsonString = (await
                                      wc.DownloadStringTaskAsync(string.IsNullOrEmpty(artist.MusicBrainzId)
                            ? $"http://ws.audioscrobbler.com/2.0/?method=artist.getinfo&artist={Uri.EscapeDataString(artist.Name)}&autocorrect=1&api_key={SensitiveInformation.LastfmKey}&format=json&lang={culture.TwoLetterISOLanguageName}"
                            : $"http://ws.audioscrobbler.com/2.0/?method=artist.getinfo&mbid={artist.MusicBrainzId}&api_key={SensitiveInformation.LastfmKey}&format=json&lang={culture.TwoLetterISOLanguageName}"))
                                     .FixJsonString();
                    artistInfo = JsonConvert.DeserializeObject <GetArtistInfoResult>(jsonString);
                }
                catch (JsonException)
                {
                    return;
                }

                if (string.IsNullOrEmpty(artist.MusicBrainzId))
                {
                    artist.MusicBrainzId = artistInfo.artist.mbid;
                }

                var topTracks = await
                                wc.DownloadStringTaskAsync(string.IsNullOrEmpty(artist.MusicBrainzId)
                                                           ?$"http://ws.audioscrobbler.com/2.0/?method=artist.gettoptracks&artist={artist.Name}&api_key={SensitiveInformation.LastfmKey}&limit=10&format=json"
                                                           : $"http://ws.audioscrobbler.com/2.0/?method=artist.gettoptracks&mbid={artist.MusicBrainzId}&api_key={SensitiveInformation.LastfmKey}&limit=10&format=json");

                artist.Biography = Regex.Match(artistInfo.artist.bio.summary.Trim(), @"^(?<content>(.+))<a", RegexOptions.Singleline).Groups["content"].Value.Trim();
                if (string.IsNullOrWhiteSpace(artist.Biography))
                {
                    artist.Biography = null;
                }

                if (artistInfo.artist?.similar?.artist != null)
                {
                    var similarArtists = new List <Artist>();
                    foreach (var similarArtist in artistInfo.artist.similar.artist)
                    {
                        Artist existingArtist;
                        if (SearchArtist(similarArtist.name, out existingArtist))
                        {
                            similarArtists.Add(existingArtist);
                            break;
                        }

                        var newArtist = new Artist(similarArtist.name)
                        {
                            Url = similarArtist.url
                        };

                        SetImages(newArtist, similarArtist.image);
                        await Artists.AddArtist(newArtist);

                        similarArtists.Add(newArtist);
                    }
                    artist.SimilarArtists = similarArtists;
                }

                GetTopTracksResult topTrackResult;
                try
                {
                    topTrackResult =
                        JsonConvert.DeserializeObject <GetTopTracksResult>(topTracks.FixJsonString());
                }
                catch (JsonException)
                {
                    return;
                }
                artist.TopTracks = topTrackResult.toptracks?.track?.Where(x => !string.IsNullOrWhiteSpace(x.duration)).Select(x => new TopTrack
                {
                    Artist        = artist,
                    Duration      = TimeSpan.FromMilliseconds(int.Parse(x.duration)),
                    MusicbrainzId = x.mbid,
                    Name          = x.name,
                    Url           = x.url,
                    Thumbnail     =
                        x.image?.Count > 0
                            ? GetBestImage(x.image)
                            : null
                }).ToList();
            }
            artist.ProvidesAdvancedInfo = true;
        }