Beispiel #1
0
        /// <summary>
        /// Get artist by MusicBrainz ID (Avoid using this)
        /// </summary>
        /// <param name="mbid">MusicBrainz ID</param>
        /// <returns></returns>
        public async Task <LastFmArtist> GetArtistbyMbid(string mbid)
        {
            string args = string.Format("?method=artist.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);
            }

            LastFmArtist art = LastFmArtist.Parse(json);

            return(art);
        }
Beispiel #2
0
        public static LastFmArtist Parse(JObject jsonObj)
        {
            LastFmArtist art      = new LastFmArtist();
            JToken       artToken = jsonObj["artist"];

            art.Name = artToken.HasProperty("name") ? artToken.Value <string>("name") : "Undefined";
            art.Mbid = artToken.HasProperty("mbid") ? artToken.Value <string>("mbid") : "Undefined";

            art.Members = new List <LastFmMembers>();
            if (artToken.HasProperty("bandmembers"))
            {
                foreach (JToken m in artToken["bandmembers"]["member"].Children())
                {
                    art.Members.Add(new LastFmMembers
                    {
                        Name     = m.Value <string>("name"),
                        YearFrom = m.Value <int>("yearfrom"),
                        YearTo   = m.Value <int>("yearto")
                    });
                }
            }

            art.Url        = artToken.HasProperty("url") ? artToken.Value <string>("url") : "Undefined";
            art.Streamable = artToken.HasProperty("streamable") && artToken.Value <string>("streamable") != "0";
            art.OnTour     = artToken.HasProperty("ontour") && artToken.Value <string>("ontour") != "0";

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

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

            art.Stats = new LastFmStats();
            if (artToken.HasProperty("stats"))
            {
                art.Stats.Listeners = artToken["stats"].HasProperty("listeners")
                    ? artToken["stats"].Value <string>("listeners")
                    : "0";
                art.Stats.PlayCount = artToken["stats"].HasProperty("playcount")
                    ? artToken["stats"].Value <string>("playcount")
                    : "0";
            }
            ;

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

            if (artToken.HasProperty("bio"))
            {
                art.Year = int.Parse(artToken["bio"]["yearformed"].ToString());
            }

            return(art);
        }