public async Task LastFmAgentGetAlbumByMbid()
        {
            /*
             * MBID:5daf013f-25fd-3275-a9a6-e7101ff0efe9
             * Master of reality
             */

            LastFmAgent agent = new LastFmAgent(Secret.LastFmAuth);

            LastFmAlbum album = await agent.GetAlbumByMbid("5daf013f-25fd-3275-a9a6-e7101ff0efe9");

            LastFmApiExceptionArgs error = null;

            try
            {
                await agent.GetAlbumByMbid("datra");
            }
            catch (LastFmApiException exception)
            {
                error = exception.Args;
            }

            Assert.IsNotNull(error, "error not raised");
            Assert.IsNotNull(album, "wrongly initiated");

            Assert.AreEqual("Master of Reality", album.Title);
            Assert.AreEqual("Black Sabbath", album.Artist);
        }
Exemple #2
0
 private string FormatAlbumLink(LastFmAlbum album, bool trim = false)
 {
     if (!string.IsNullOrEmpty(album?.Url))
     {
         return(FormatLink(album?.Name ?? "Unknown", album?.Url, trim));
     }
     else
     {
         return(FormatLink(album?.Name ?? "Unknown", album?.Artist?.Url, trim)); // Just for the formatting...
     }
 }
Exemple #3
0
        public async Task <string> GetAlbumImageAsync(string albumTitle, IList <string> albumArtists, string trackTitle = "", IList <string> trackArtists = null)
        {
            string        title   = string.Empty;
            List <string> artists = new List <string>();

            // Title
            if (!string.IsNullOrEmpty(albumTitle))
            {
                title = albumTitle;
            }
            else if (!string.IsNullOrEmpty(trackTitle))
            {
                title = trackTitle;
            }

            // Artist
            if (albumArtists != null && albumArtists.Count > 0)
            {
                artists.AddRange(albumArtists.Where(a => !string.IsNullOrEmpty(a)));
            }

            if (trackArtists != null && trackArtists.Count > 0)
            {
                artists.AddRange(trackArtists.Where(a => !string.IsNullOrEmpty(a)));
            }

            if (string.IsNullOrEmpty(title) || artists == null)
            {
                return(null);
            }

            foreach (string artist in artists)
            {
                LastFmAlbum lfmAlbum = await LastfmApi.AlbumGetInfo(artist, title, false, "EN");

                if (!string.IsNullOrEmpty(lfmAlbum.LargestImage()))
                {
                    return(lfmAlbum.LargestImage());
                }
            }

            return(null);
        }
Exemple #4
0
        public async Task <LastFmTrack> GetTrackInfo(string artistName, string name)
        {
            var request = WebRequest.CreateHttp($"{ApiBase}/?method=track.getInfo&api_key={Key}&artist={Uri.EscapeDataString(artistName)}&track={Uri.EscapeDataString(name)}&format=json&username={User}");

            request.Timeout = (int)RequestTimeout.TotalMilliseconds;
            using (var response = (HttpWebResponse)await request.GetResponseAsync())
                using (var reader = new StreamReader(response.GetResponseStream()))
                {
                    var text = await reader.ReadToEndAsync();

                    var track = JObject.Parse(text)?["track"];
                    if (track == null)
                    {
                        return(null);
                    }

                    var artist = new LastFmArtist((string)track["artist"]["name"]);
                    var album  = new LastFmAlbum((string)track["album"]?["title"], artist, imageUri: GetLargestImage(track["album"]?["image"]));
                    return(new LastFmTrack((string)track["name"], album, (int?)track["userplaycount"]));
                }
        }
        public async Task <List <LastFmAlbum> > GetTopAlbums(string mbid, string artist, int count = 0)
        {
            var parameters = new Dictionary <string, string>();

            if (!string.IsNullOrEmpty(mbid))
            {
                parameters.Add("mbid", mbid);
            }
            else
            {
                parameters.Add("artist", artist);
            }
            if (count > 0)
            {
                parameters.Add("limit", count.ToString());
            }
            parameters.Add("api_key", _lastFm.ApiKey);

            var response = await(new CoreRequest(new Uri(LastFmConst.MethodBase + "artist.getTopAlbums"), parameters).Execute());

            LastFmErrorProcessor.ProcessError(response);


            if (response.SelectToken("topalbums.album") != null)
            {
                var albumToken = response.SelectToken("topalbums.album");
                if (albumToken.GetType() == typeof(JArray))
                {
                    return((from a in albumToken select LastFmAlbum.FromJson(a)).ToList());
                }
                else if (albumToken.GetType() == typeof(JObject))
                {
                    var result = new List <LastFmAlbum>();
                    result.Add(LastFmAlbum.FromJson(albumToken));
                    return(result);
                }
            }

            return(null);
        }
Exemple #6
0
        public async Task AlbumGetInfoTest()
        {
            LastFmAlbum lfmAlbum = await Core.Api.Lastfm.LastfmApi.AlbumGetInfo("Coldplay", "Viva la Vida or Death and All His Friends", false, string.Empty);

            Assert.IsTrue(!string.IsNullOrEmpty(lfmAlbum.Name) & !string.IsNullOrEmpty(lfmAlbum.Url));
        }