Ejemplo n.º 1
0
        /// <summary>
        /// Get album by MusicBrainz ID (Avoid using this)
        /// </summary>
        /// <param name="mbid">MusicBrainz ID</param>
        /// <returns></returns>
        public async Task <LastFmAlbum> GetAlbumByMbid(string mbid)
        {
            string args = string.Format("?method=album.getinfo&mbid={0}&{1}&format=json", mbid, _credentials);
            Uri    url  = new Uri(Domain + args);

            HttpClient webClient = new HttpClient();

            string jsonSource = await webClient.GetStringAsync(url);

            JObject json = JObject.Parse(jsonSource);
            JToken  temp;

            if (json.TryGetValue("error", out temp))
            {
                LastFmApiExceptionArgs arg = new LastFmApiExceptionArgs(temp.ToObject <int>(),
                                                                        json.GetValue("message").ToString());
                throw new LastFmApiException(arg);
            }

            LastFmAlbum album = LastFmAlbum.Parse(json);

            return(album);
        }
Ejemplo n.º 2
0
        public static LastFmAlbum Parse(JObject jObject)
        {
            LastFmAlbum alb  = new LastFmAlbum();
            JToken      data = jObject["album"];

            alb.Title    = data.GetStringValue("name");
            alb.Artist   = data.GetStringValue("artist");
            alb.LastFmId = data.GetStringValue("id");
            alb.Mbid     = data.GetStringValue("mbid");
            alb.Url      = data.GetStringValue("url");
            alb.Release  = data.GetStringValue("releasedate");

            alb.Stats = new LastFmStats
            {
                Listeners = data.GetStringValue("listeners"),
                PlayCount = data.GetStringValue("playcount")
            };

            alb.Images = new List <ImageInfo>();
            if (data.HasProperty("image"))
            {
                foreach (var img in data["image"])
                {
                    ImageInfoSizes size = ImageInfoSizes.Small;
                    Enum.TryParse(img.Value <string>("size"), true, out size);

                    alb.Images.Add(new ImageInfo
                    {
                        Size = new ImageSize(size),
                        Link = img.Value <string>("#text"),
                    });
                }
            }

            alb.Tags = new List <string>();
            if (data.HasProperty("toptags"))
            {
                foreach (var tag in data["toptags"]["tag"])
                {
                    alb.Tags.Add(tag["name"].ToString());
                }
            }

            alb.Tracks = new List <LastFmAlbumTrack>();
            if (data.HasProperty("tracks"))
            {
                foreach (var t in data["tracks"]["track"])
                {
                    int r;
                    if (!int.TryParse(t["@attr"]["rank"].ToString(), out r))
                    {
                        r = -1;
                    }

                    int d;
                    if (!int.TryParse(t["duration"].ToString(), out d))
                    {
                        d = 0;
                    }

                    alb.Tracks.Add(new LastFmAlbumTrack
                    {
                        Rank       = r,
                        AlbumName  = alb.Title,
                        ArtistMbid = t["artist"]["mbid"].ToString(),
                        ArtistName = t["artist"]["name"].ToString(),
                        Duration   = d,
                        Title      = t["name"].ToString()
                    });
                }
            }

            return(alb);
        }