Пример #1
0
        /// <summary>
        /// Parses the given <paramref name="token"/> to a
        /// <see cref="LastUser"/>.
        /// </summary>
        /// <param name="token">JToken to parse.</param>
        /// <returns>Parsed LastUser.</returns>
        internal static LastUser ParseJToken(JToken token)
        {
            var u = new LastUser
            {
                Name           = token.Value <string>("name"),
                FullName       = token.Value <string>("realname"),
                Country        = token.Value <string>("country"),
                Id             = token.Value <string>("id"),
                Playcount      = token.Value <int>("playcount"),
                Playlists      = token.Value <int>("playlists"),
                Gender         = ParseGender(token.Value <string>("gender")),
                IsSubscriber   = Convert.ToBoolean(token.Value <int>("subscriber")),
                TimeRegistered = token.Value <double>("registered.unixtime").FromUnixTime().DateTime,
                Bootstrap      = token.Value <int>("bootstrap"),
                Type           = token.Value <string>("type")
            };

            var images = token.SelectToken("image");

            if (images != null)
            {
                u.Avatar = LastImageSet.ParseJToken(images);
            }

            return(u);
        }
Пример #2
0
        /// <summary>
        /// TODO
        ///     "gender": "m",
        //"playcount": "79972",
        //"playlists": "4",
        //"bootstrap": "0",
        //"registered": {
        //  "#text": "2002-11-20 11:50",
        //  "unixtime": "1037793040"
        //},
        //"type": "alumni"
        /// </summary>
        /// <param name="token"></param>
        /// <returns></returns>
        internal static LastUser ParseJToken(JToken token)
        {
            var u = new LastUser();

            u.Name     = token.Value <string>("name");
            u.FullName = token.Value <string>("realname");
            u.Country  = token.Value <string>("country");
            u.Id       = token.Value <string>("id");

            var subscribed = token.SelectToken("subscriber");

            if (subscribed != null)
            {
                u.IsSubscriber = Convert.ToBoolean(subscribed.Value <int>());
            }

            var images = token.SelectToken("image");

            if (images != null)
            {
                u.Avatar = LastImageSet.ParseJToken(images);
            }

            return(u);
        }
Пример #3
0
        internal static LastImageSet GetImagesFromJToken(JToken albumToken)
        {
            var images = albumToken.SelectToken("image");

            if (images != null)
            {
                return(LastImageSet.ParseJToken(images));
            }

            return(null);
        }
Пример #4
0
        internal static LastArtist ParseJToken(JToken token)
        {
            var a = new LastArtist();

            a.Id   = token.Value <string>("id");
            a.Name = token.Value <string>("name");
            a.Mbid = token.Value <string>("mbid");
            var url = token.Value <string>("url");

            // for some stupid reason the api returns the url without http in the get similar method, WHY?
            if (!url.StartsWith("http"))
            {
                url = "http://" + url;
            }

            a.Url = new Uri(url, UriKind.Absolute);

            a.OnTour = Convert.ToBoolean(token.Value <int>("ontour"));

            var tagsToken = token.SelectToken("tags");

            if (tagsToken != null)
            {
                var tagToken = tagsToken.SelectToken("tag");
                if (tagToken != null)
                {
                    a.Tags =
                        tagToken.Type == JTokenType.Array
                        ? tagToken.Children().Select(LastTag.ParseJToken)
                        : new List <LastTag> {
                        LastTag.ParseJToken(tagToken)
                    };
                }
            }

            var images = token.SelectToken("image");

            if (images != null && images.HasValues)
            {
                var imageCollection = LastImageSet.ParseJToken(images);
                a.MainImage = imageCollection;
            }

            return(a);
        }
Пример #5
0
        /// <summary>
        /// Parses the given JToken into a track
        /// </summary>
        /// <param name="token">A valid JToken</param>
        /// <returns>track equivalent to the JToken</returns>
        /// <remarks>If this method is used directly then the duration attribute will be parsed as MILLIseconds</remarks>
        internal static LastTrack ParseJToken(JToken token)
        {
            var t = new LastTrack();

            t.Id   = token.Value <string>("id");
            t.Name = token.Value <string>("name");
            t.Mbid = token.Value <string>("mbid");

            //some tracks do not contain the playcount prop, it will throw a FormatException
            var playCountStr = token.Value <string>("playcount");
            int playCount;

            if (int.TryParse(playCountStr, out playCount))
            {
                t.PlayCount = playCount;
            }

            var listenerCountStr = token.Value <string>("listeners");
            int listenerCount;

            if (int.TryParse(listenerCountStr, out listenerCount))
            {
                t.ListenerCount = listenerCount;
            }

            var userPlayCountStr = token.Value <string>("userplaycount");
            int userPlayCount;

            if (int.TryParse(userPlayCountStr, out userPlayCount))
            {
                t.UserPlayCount = userPlayCount;
            }

            t.Url = new Uri(token.Value <string>("url"), UriKind.Absolute);

            var artistToken = token.SelectToken("artist");

            if (artistToken.Type != JTokenType.String)
            {
                t.ArtistName = LastArtist.GetNameFromJToken(artistToken);
                t.ArtistMbid = artistToken.Value <string>("mbid");
            }
            else
            {
                t.ArtistName = artistToken.ToObject <string>();
            }

            var albumToken = token.SelectToken("album");

            if (albumToken != null)
            {
                t.AlbumName = LastAlbum.GetNameFromJToken(albumToken);
            }

            var tagsToken = token.SelectToken("toptags");

            if (tagsToken != null)
            {
                var tagToken = tagsToken.SelectToken("tag");
                if (tagToken != null)
                {
                    t.TopTags =
                        tagToken.Type == JTokenType.Array
                        ? tagToken.Children().Select(token1 => LastTag.ParseJToken(token1))
                        : new List <LastTag> {
                        LastTag.ParseJToken(tagToken)
                    };
                }
            }

            var date = token.SelectToken("date");

            if (date != null)
            {
                var stamp = date.Value <double>("uts");
                t.TimePlayed = stamp.FromUnixTime();
            }

            var images = token.SelectToken("image");

            if (images != null)
            {
                var imageCollection = LastImageSet.ParseJToken(images);
                t.Images = imageCollection;
            }

            var lovedToken = token.SelectToken("userloved");

            if (lovedToken != null)
            {
                t.IsLoved = Convert.ToBoolean(lovedToken.Value <int>());
            }
            var attrToken = token.SelectToken("@attr");

            if (attrToken != null && attrToken.HasValues)
            {
                t.IsNowPlaying = attrToken.Value <bool>("nowplaying");
                t.Rank         = attrToken.Value <int?>("rank");
            }

            // api returns milliseconds when track.getInfo is called directly
            var    secsStr = token.Value <string>("duration");
            double secs;

            if (double.TryParse(secsStr, out secs))
            {
                if (Math.Abs(secs - default(double)) > double.Epsilon)
                {
                    t.Duration = TimeSpan.FromMilliseconds(secs);
                }
            }

            return(t);
        }
Пример #6
0
        internal static LastAlbum ParseJToken(JToken token)
        {
            var a = new LastAlbum();

            a.Id = token.Value <string>("id");
            var artistToken = token["artist"];

            switch (artistToken.Type)
            {
            case JTokenType.String:
                a.ArtistName = token.Value <string>("artist");
                break;

            case JTokenType.Object:
                a.ArtistName = artistToken.Value <string>("name");
                break;
            }

            var tracksToken = token.SelectToken("tracks");

            if (tracksToken != null)
            {
                var trackToken = tracksToken.SelectToken("track");
                if (trackToken != null)
                {
                    a.Tracks = trackToken.Type == JTokenType.Array
                        ? trackToken.Children().Select(t => LastTrack.ParseJToken(t, a.Name))
                        : new List <LastTrack>()
                    {
                        LastTrack.ParseJToken(trackToken, a.Name)
                    }
                }
                ;
            }
            else
            {
                a.Tracks = Enumerable.Empty <LastTrack>();
            }

            var tagsToken = token.SelectToken("toptags");

            if (tagsToken != null)
            {
                var tagToken = tagsToken.SelectToken("tag");
                if (tagToken != null)
                {
                    a.TopTags =
                        tagToken.Type == JTokenType.Array
                        ? tagToken.Children().Select(token1 => LastTag.ParseJToken(token1))
                        : new List <LastTag> {
                        LastTag.ParseJToken(tagToken)
                    };
                }
            }
            else
            {
                a.TopTags = Enumerable.Empty <LastTag>();
            }

            a.ListenerCount = token.Value <int?>("listeners");
            a.Mbid          = token.Value <string>("mbid");
            a.Name          = token.Value <string>("name");

            var playCountStr = token.Value <string>("playcount");
            int playCount;

            if (int.TryParse(playCountStr, out playCount))
            {
                a.PlayCount = playCount;
            }

            var images = token.SelectToken("image");

            if (images != null)
            {
                var imageCollection = LastImageSet.ParseJToken(images);
                a.Images = imageCollection;
            }

            a.Url = new Uri(token.Value <string>("url"), UriKind.Absolute);

            var            dateString = token.Value <string>("releasedate");
            DateTimeOffset releaseDate;

            if (DateTimeOffset.TryParse(dateString, out releaseDate))
            {
                a.ReleaseDateUtc = releaseDate;
            }

            var wikiToken = token["wiki"];

            if (wikiToken != null)
            {
                a.Wiki = LastWiki.ParseJToken(wikiToken);
            }

            return(a);
        }
Пример #7
0
        internal static LastArtist ParseJToken(JToken token)
        {
            var a = new LastArtist();

            a.Id   = token.Value <string>("id");
            a.Name = token.Value <string>("name");
            a.Mbid = token.Value <string>("mbid");
            var url = token.Value <string>("url");

            var playCountStr = token.Value <string>("playcount");
            int playCount;

            if (int.TryParse(playCountStr, out playCount))
            {
                a.PlayCount = playCount;
            }

            var    matchStr = token.Value <string>("match");
            double match;

            if (double.TryParse(matchStr, out match))
            {
                a.MatchSimilar = match;
            }

            // for some stupid reason the api returns the url without http in the get similar method, WHY?
            if (!url.StartsWith("http"))
            {
                url = "http://" + url;
            }

            a.Url = new Uri(url, UriKind.Absolute);

            a.OnTour = Convert.ToBoolean(token.Value <int>("ontour"));

            var statsToken = token.SelectToken("stats");

            if (statsToken != null)
            {
                a.Stats = LastStats.ParseJToken(statsToken);
            }

            var bioToken = token.SelectToken("bio");

            if (bioToken != null)
            {
                a.Bio = LastWiki.ParseJToken(bioToken);
            }

            var tagsToken = token.SelectToken("tags");

            if (tagsToken != null)
            {
                var tagToken = tagsToken.SelectToken("tag");
                if (tagToken != null)
                {
                    a.Tags =
                        tagToken.Type == JTokenType.Array
                        ? tagToken.Children().Select(token1 => LastTag.ParseJToken(token1))
                        : new List <LastTag> {
                        LastTag.ParseJToken(tagToken)
                    };
                }
            }

            var images = token.SelectToken("image");

            if (images != null && images.HasValues)
            {
                var imageCollection = LastImageSet.ParseJToken(images);
                a.MainImage = imageCollection;
            }

            var similarToken = token.SelectToken("similar");

            if (similarToken != null)
            {
                a.Similar = new List <LastArtist>();
                var similarArtists = similarToken.SelectToken("artist");
                if (similarArtists != null && similarArtists.Children().Any())
                {
                    // array notation isn't used on the api when only one object is available
                    if (similarArtists.Type != JTokenType.Array)
                    {
                        var item = ParseJToken(similarArtists);
                        a.Similar.Add(item);
                    }
                    else
                    {
                        var items = similarArtists.Children().Select(ParseJToken);
                        a.Similar.AddRange(items);
                    }
                }
            }

            return(a);
        }
Пример #8
0
        internal static LastImageSet GetImagesFromJToken(JToken albumToken)
        {
            var images = albumToken.SelectToken("image");

            return(images == null ? null : LastImageSet.ParseJToken(images));
        }