예제 #1
0
        public async Task <IEnumerable <IRevTrack> > GetRecentTracks(string username, int page = 1, int pageSize = 50)
        {
            var caller = new RestCaller
            {
                Host   = "http://ws.audioscrobbler.com/",
                Action = "2.0/",
                Method = "GET"
            };

            caller.AddParam("method", "user.getRecentTracks")
            .AddParam("user", username)
            .AddParam("api_key", "e2e16b5513251519bdce400fcd094332")
            .AddParam("page", page.ToString())
            .AddParam("limit", pageSize.ToString())
            .AddParam("format", "json");

            dynamic result = await caller.ExecuteAsync();

            //return new RTrack { PlayCount = result.Data.track.userplaycount };

            var list = new List <IRevTrack>();

            DateTime epoch = new DateTime(1970, 1, 1);

            IEnumerable <JToken> tracks = result.Data != null ? result.Data.recenttracks.track : null;

            if (tracks == null)
            {
                return(list);
            }

            foreach (dynamic item in tracks)
            {
                Console.WriteLine(item.name.Value);

                if (item["@attr"]?["nowplaying"] == "true")
                {
                    continue;
                }

                string utsString = item.date.uts.Value;
                var    uts       = double.Parse(utsString);
                if (uts > 0)
                {
                    var rtrack = new RTrack
                    {
                        Album    = item.album["#text"].Value,
                        Artist   = item.artist["#text"].Value,
                        Song     = item.name.Value,
                        MBId     = item.mbid.Value,
                        PlayDate = epoch.AddSeconds(uts).ToLocalTime()
                    };

                    list.Add(rtrack);
                }
            }

            return(list);
        }
예제 #2
0
        public async Task <IRevTrack> GetTrackAsync(string artist, string song)
        {
            var caller = new RestCaller
            {
                Host   = "http://ws.audioscrobbler.com/",
                Action = "2.0/",
                Method = "GET"
            };

            caller.AddParam("method", "track.getInfo")
            .AddParam("username", Settings.Default.LastFMUser)
            .AddParam("api_key", "e2e16b5513251519bdce400fcd094332")
            .AddParam("format", "json");

            caller.AddParam("artist", HttpUtility.UrlEncode(artist));
            caller.AddParam("track", HttpUtility.UrlEncode(song));

            dynamic result = await caller.ExecuteAsync();

            if (result.Error != null && result.Error.message == "Track not found")
            {
                return(null);
            }

            var rtrack = new RTrack();
            //try
            {
                dynamic track = result.Data != null ? result.Data.track : null;
                if (track == null)
                {
                    return(null);
                }

                rtrack.PlayCount = track.userplaycount;
                rtrack.MBId      = track.mbid;
                rtrack.Album     = track.album?.title;

                return(rtrack);
            }
            // catch (RuntimeBinderException)
            {
                //if (!Debugger.IsAttached)
                //{
                //    Debugger.Launch();
                //}

                //throw;
            }
        }