示例#1
0
        public async Task <List <LastFmAlbum> > Search(string album, int limit = 0)
        {
            var parameters = new Dictionary <string, string>();

            parameters.Add("album", album);
            if (limit > 0)
            {
                parameters.Add("limit", limit.ToString());
            }
            parameters.Add("api_key", _lastFm.ApiKey);

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

            LastFmErrorProcessor.ProcessError(response);


            if (response.SelectToken("results.albummatches.album") != null)
            {
                var albumJson = response.SelectToken("results.albummatches.album");
                if (albumJson is JArray)
                {
                    return
                        ((from a in response.SelectToken("results.albummatches.album") select LastFmAlbum.FromJson(a)).ToList());
                }
                else
                {
                    return new List <LastFmAlbum>()
                           {
                               LastFmAlbum.FromJson(albumJson)
                           }
                };
            }

            return(null);
        }
示例#2
0
        public async Task <LastFmAlbum> GetInfo(string mbid, string album, string artist, bool autoCorrect = true)
        {
            var parameters = new Dictionary <string, string>();

            if (!string.IsNullOrEmpty(mbid))
            {
                parameters.Add("mbid", mbid);
            }
            else
            {
                parameters.Add("album", album);
                parameters.Add("artist", artist);
            }

            if (autoCorrect)
            {
                parameters.Add("autocorrect", "1");
            }

            parameters.Add("api_key", _lastFm.ApiKey);

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

            LastFmErrorProcessor.ProcessError(response);


            if (response["album"] != null)
            {
                return(LastFmAlbum.FromJson(response["album"]));
            }

            return(null);
        }
示例#3
0
        public static LastFmAlbum FromJson(JToken json)
        {
            if (json == null)
                throw new ArgumentException("Json can not be null.");

            var result = new LastFmAlbum();
            if (json["mbid"] != null)
                result.Mbid = json["mbid"].Value<string>();
            result.Name = json["name"].Value<string>();
            if (json["artist"] != null)
            {
                if (!json["artist"].HasValues)
                    result.Artist = json["artist"].Value<string>();
                else
                {
                    result.Artist = json["artist"]["name"].Value<string>();
                }
            }

            var imageToken = json["image"];
            if (imageToken != null)
            {
                foreach (var image in imageToken.Children())
                {
                    switch (image["size"].Value<string>())
                    {
                        case "small":
                            result.ImageSmall = image["#text"].Value<string>();
                            break;
                        case "medium":
                            result.ImageMedium = image["#text"].Value<string>();
                            break;
                        case "large":
                            result.ImageLarge = image["#text"].Value<string>();
                            break;
                        case "extralarge":
                            result.ImageExtraLarge = image["#text"].Value<string>();
                            break;
                    }
                }
            }

            if (json.SelectToken("tracks.track") != null)
            {
                result.Tracks = new List<LastFmTrack>();
                foreach (var trackToken in json.SelectToken("tracks.track"))
                {
                    var track = LastFmTrack.FromJson(trackToken);
                    result.Tracks.Add(track);
                }
            }

            if (json.SelectToken("toptags.tag") != null)
            {
                result.TopTags = new List<string>();
                foreach (var tagToken in json.SelectToken("toptags.tag"))
                {
                    if (tagToken is JProperty)
                        result.TopTags.Add(((JProperty)tagToken).Value.Value<string>());
                    else
                        result.TopTags.Add(tagToken["name"].Value<string>());
                }
            }

            return result;
        }
示例#4
0
        public static LastFmAlbum FromJson(JToken json)
        {
            if (json == null)
            {
                throw new ArgumentException("Json can not be null.");
            }

            var result = new LastFmAlbum();

            if (json["id"] != null)
            {
                result.Id = json["id"].Value <string>();
            }
            if (json["mbid"] != null)
            {
                result.Mbid = json["mbid"].Value <string>();
            }
            result.Name = json["name"].Value <string>();
            if (json["artist"] != null)
            {
                if (!json["artist"].HasValues)
                {
                    result.Artist = json["artist"].Value <string>();
                }
                else
                {
                    result.Artist = json["artist"]["name"].Value <string>();
                }
            }

            var imageToken = json["image"];

            if (imageToken != null)
            {
                foreach (var image in imageToken.Children())
                {
                    switch (image["size"].Value <string>())
                    {
                    case "small":
                        result.ImageSmall = image["#text"].Value <string>();
                        break;

                    case "medium":
                        result.ImageMedium = image["#text"].Value <string>();
                        break;

                    case "large":
                        result.ImageLarge = image["#text"].Value <string>();
                        break;

                    case "extralarge":
                        result.ImageExtraLarge = image["#text"].Value <string>();
                        break;
                    }
                }
            }

            if (json.SelectToken("tracks.track") != null)
            {
                result.Tracks = new List <LastFmTrack>();

                var trackJson = json.SelectToken("tracks.track");
                if (trackJson is JArray)
                {
                    result.Tracks.AddRange((from a in trackJson select LastFmTrack.FromJson(a)).ToList());
                }
                else
                {
                    result.Tracks.Add(LastFmTrack.FromJson(trackJson));
                }
            }

            if (json.SelectToken("toptags.tag") != null)
            {
                result.TopTags = new List <string>();
                foreach (var tagToken in json.SelectToken("toptags.tag"))
                {
                    if (tagToken is JProperty)
                    {
                        result.TopTags.Add(((JProperty)tagToken).Value.Value <string>());
                    }
                    else
                    {
                        result.TopTags.Add(tagToken["name"].Value <string>());
                    }
                }
            }

            return(result);
        }