예제 #1
0
        public static LastFmTrack Parse(JObject json)
        {
            LastFmTrack track = new LastFmTrack();
            JToken      token = json["track"];

            track.Id    = token.GetStringValue("id");
            track.Title = token.GetStringValue("name");
            track.Mbid  = token.GetStringValue("mbid");
            track.Stats = new LastFmStats
            {
                Listeners = token.GetStringValue("listeners"),
                PlayCount = token.GetStringValue("playcount")
            };

            track.ArtistName = token["artist"].GetStringValue("name");
            track.ArtistMbid = token["artist"].GetStringValue("mbid");

            track.AlbumArtist = token["album"].GetStringValue("artist");
            track.AlbumTitle  = token["album"].GetStringValue("title");
            track.AlbumMbid   = token["album"].GetStringValue("mbid");

            int r;

            if (!int.TryParse(token["album"]["@attr"].GetStringValue("position"), out r))
            {
                r = -1;
            }

            track.Rank = r;

            track.Duration = token.GetStringValue("duration");
            track.Url      = token.GetStringValue("url");

            track.Tags = new List <string>();
            if (!token.HasProperty("toptags"))
            {
                return(track);
            }
            foreach (var t in token["toptags"]["tag"])
            {
                track.Tags.Add(t.GetStringValue("name"));
            }

            return(track);
        }
예제 #2
0
        /// <summary>
        /// Get Track by MusicBrainz ID (Avoid using this)
        /// </summary>
        /// <param name="mbid">MusicBrainz ID</param>
        /// <returns></returns>
        public async Task <LastFmTrack> GetTrackByMbid(string mbid)
        {
            string args = string.Format("?method=track.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);
            }

            LastFmTrack album = LastFmTrack.Parse(json);

            return(album);
        }