public bool MoveNext(CancellationToken token)
        {
            if (_songQueue == null)
            {
                using (var session = new EchoNestSession(EchoNestModule.ApiKey))
                {
                    var response = session.Query <Static>().Execute(_argument);

                    if (response == null)
                    {
                        // Try desperatly once more to see if its really not possible to get a proper response
                        response = session.Query <Static>().Execute(_argument);
                    }

                    if (response == null)
                    {
                        _toastService.Show("Trouble getting customized playlist from EchoNest");
                        return(false);
                    }

                    if (response.Status.Code == ResponseCode.Success)
                    {
                        _songQueue = new Queue <SongBucketItem>(response.Songs);
                    }
                    else
                    {
                        _songQueue = new Queue <SongBucketItem>();
                    }
                }
            }

            while (_songQueue.Count > 0)
            {
                if (token.IsCancellationRequested)
                {
                    token.ThrowIfCancellationRequested();
                }

                var song = _songQueue.Dequeue();

                var queryResult = _radio.GetTracksByName(song.ArtistName + " " + song.Title).ToArray();

                if (!queryResult.Any())
                {
                    queryResult = _radio.GetTracksByName(song.ArtistName).ToArray();
                }

                _currentTracks = queryResult.Take(1).ToArray();

                if (_currentTracks.Any())
                {
                    return(true);
                }
            }

            return(false);
        }
        private Task <Track> GetTopTrackForArtistAsync(HotArtistModel artist)
        {
            return(Task.Factory
                   .StartNew(state =>
            {
                HotArtistModel m = (HotArtistModel)state;
                using (_loadingIndicator.EnterLoadingBlock())
                {
                    var result = _radio.GetTracksByName(m.Name + " " + m.HotSongTitle);
                    var track = result.FirstOrDefault(
                        t => t.Artist.Equals(m.Name, StringComparison.InvariantCultureIgnoreCase));

                    if (track == null)
                    {
                        track = result.FirstOrDefault();
                    }

                    return track;
                }
            }, artist)
                   .ContinueWith(task =>
            {
                if (task.Exception != null)
                {
                    _logger.Log(task.Exception.ToString(), Category.Exception, Priority.Medium);
                    _toastService.Show("Unable to play track");
                }

                return task.Result;
            }));
        }
        private Task <IEnumerable <Track> > GetArtistTracksAsync(string artistName)
        {
            return(Task <IEnumerable <Track> > .Factory
                   .StartNew(state =>
            {
                string name = state.ToString();
                using (_loadingIndicator.EnterLoadingBlock())
                {
                    var result = _radio.GetTracksByName(name)
                                 .Where(t => t.Artist.Equals(name, StringComparison.InvariantCultureIgnoreCase))
                                 .ToArray();

                    return result;
                }
            }, artistName));
        }
        public bool MoveNext(CancellationToken token)
        {
            if (_similarArtistsEnumerator.MoveNext())
            {
                _currentTrackList = _radio.GetTracksByName(_similarArtistsEnumerator.Current);

                if (!_currentTrackList.Any())
                {
                    return(MoveNext(token));
                }

                return(true);
            }

            _currentTrackList = new Track[0];
            return(false);
        }
예제 #5
0
        public bool MoveNext(CancellationToken token)
        {
            using (var session = new EchoNestSession(EchoNestModule.ApiKey))
            {
                if (token.IsCancellationRequested)
                {
                    token.ThrowIfCancellationRequested();
                }

                var response = session.Query <TopHottt>().Execute(2, _start);

                if (response != null && response.Status.Code == ResponseCode.Success)
                {
                    _start += response.Artists.Count;

                    List <Track> tracks = new List <Track>();

                    foreach (var artist in response.Artists.OrderBy(k => Guid.NewGuid()))
                    {
                        if (token.IsCancellationRequested)
                        {
                            token.ThrowIfCancellationRequested();
                        }

                        var result = _radio
                                     .GetTracksByName(artist.Name)
                                     .Where(a => a.Artist.Equals(artist.Name, StringComparison.InvariantCultureIgnoreCase))
                                     .Take(2);

                        tracks.AddRange(result);
                    }

                    _currentTracks = tracks;

                    return(true);
                }
            }

            return(false);
        }
        public bool MoveNext(CancellationToken token)
        {
            if (string.IsNullOrEmpty(_sessionId))
            {
                using (var session = new EchoNestSession(EchoNestModule.ApiKey))
                {
                    var argument = new DynamicArgument();
                    argument.Type = "artist-radio";
                    argument.Artist.Add(_initialArtistName);
                    argument.Dmca = true;

                    var response = session.Query <Dynamic>().Execute(argument);

                    if (response.Status.Code == ResponseCode.Success)
                    {
                        _sessionId = response.SessionId;

                        var song = response
                                   .Songs
                                   .FirstOrDefault(s => s.ArtistName.Equals(_initialArtistName, StringComparison.InvariantCultureIgnoreCase));

                        if (song == null)
                        {
                            song = response.Songs.FirstOrDefault();
                        }

                        if (song != null)
                        {
                            if (token.IsCancellationRequested)
                            {
                                token.ThrowIfCancellationRequested();
                            }

                            var queryResult = _radio.GetTracksByName(_initialArtistName + " " + song.Title);

                            if (!queryResult.Any())
                            {
                                queryResult = _radio.GetTracksByName(_initialArtistName);
                            }

                            _currentTracks =
                                queryResult
                                .Where(s => s.Artist.Equals(_initialArtistName, StringComparison.InvariantCultureIgnoreCase))
                                .Take(1)
                                .ToArray();

                            if (!_currentTracks.Any())
                            {
                                _toastService.Show("Unable to find any tracks matching the query");
                                return(false);
                            }

                            return(true);
                        }
                    }
                    else
                    {
                        _toastService.Show(response.Status.Message);
                    }
                }
            }
            else
            {
                using (var session = new EchoNestSession(EchoNestModule.ApiKey))
                {
                    if (token.IsCancellationRequested)
                    {
                        token.ThrowIfCancellationRequested();
                    }

                    var argument = new DynamicArgument
                    {
                        SessionId = _sessionId
                    };

                    if (_likesCurrentTrack.HasValue)
                    {
                        if (!_likesCurrentTrack.Value)
                        {
                            argument.Ban = "artist";
                        }
                    }

                    if (_currentTrackRating.HasValue)
                    {
                        argument.Rating = Convert.ToInt32(_currentTrackRating.GetValueOrDefault(1));
                    }

                    var response = session.Query <Dynamic>().Execute(argument);

                    _likesCurrentTrack  = null;
                    _currentTrackRating = null;

                    if (response.Status.Code == ResponseCode.Success)
                    {
                        var song = response.Songs.FirstOrDefault();

                        if (song != null)
                        {
                            var queryResult = _radio.GetTracksByName(song.ArtistName + " " + song.Title);

                            if (!queryResult.Any())
                            {
                                queryResult = _radio.GetTracksByName(song.ArtistName);
                            }

                            _currentTracks =
                                queryResult
                                .Where(s => s.Artist.Equals(song.ArtistName, StringComparison.InvariantCultureIgnoreCase))
                                .Take(1)
                                .ToArray();

                            if (!_currentTracks.Any())
                            {
                                _toastService.Show("Unable to find any tracks matching the query");

                                if (response.Songs.Any())
                                {
                                    return(MoveNext(token));
                                }

                                return(false);
                            }

                            return(true);
                        }
                    }
                    else
                    {
                        _toastService.Show(response.Status.Message);
                    }
                }
            }

            return(false);
        }