예제 #1
0
        public void Scrobble(Song song, bool playing)
        {
            if (song.track == null || !song.track.Loaded)
            {
                return;
            }

            if (!_scrobbler.HasSession)
            {
                Auth();
                try
                {
                    if (_settings.LastfmSession == null)
                    {
                        _settings.LastfmSession = _scrobbler.GetSession();
                    }
                }
                catch (Exception)
                { }
                return;
            }

            try
            {
                lock (trackLock)
                    if (thisTrack != null && thisTrack.TrackName != song.track.Name &&
                        thisTrack.ArtistName != song.track.Artists[0] &&
                        DateTime.Now - thisTrack.WhenStartedPlaying > TimeSpan.FromSeconds(30))
                    {
                        _scrobbler.Scrobble(thisTrack);
                        thisTrack = null;
                    }

                if (playing)
                {
                    thisTrack = new Track
                    {
                        TrackName          = song.track.Name,
                        AlbumName          = song.track.Album.Name,
                        ArtistName         = song.track.Artists[0],
                        Duration           = TimeSpan.FromSeconds(decimal.ToInt32(song.track.Seconds)),
                        WhenStartedPlaying = DateTime.Now
                    };
                    _scrobbler.NowPlaying(thisTrack);
                }
            }
            catch (Exception e)
            {
                LogTo.Error("Scrobble error: {0}", e.Message);
            }
        }
예제 #2
0
        /// <summary>
        /// Synchronously processes all scrobbles and now playing notifications that are in the Queues, and returns the results
        /// </summary>
        /// <param name="throwExceptionDuringProcess">When true, will throw the first Exception encountered during Scrobbling (and cease to process).
        /// When false, any exceptions raised will be attached to the corresponding <see cref="ScrobbleResponse"/>, but will not be thrown. Default is false.</param>
        /// <returns><see cref="ScrobbleResponses"/>, a list of <see cref="ScrobbleResponse"/> </returns>
        /// <remarks>This method will complete synchronously and may take some time. This should be invoked by a single timer. This
        /// method may not be thread safe</remarks>
        public List <Response> Process(bool throwExceptionDuringProcess = false)
        {
            if (string.IsNullOrEmpty(SessionKey))
            {
                return(null);
            }
            var results = new List <Response>();

            Track track;

            while (NowPlayingQueue.TryDequeue(out track))
            {
                try
                {
                    results.Add(_scrobbler.NowPlaying(track));
                }
                catch (Exception exception)
                {
                    if (throwExceptionDuringProcess)
                    {
                        throw;
                    }
                    results.Add(new NowPlayingResponse {
                        Track = track, Exception = exception
                    });
                }
            }

            while (ScrobbleQueue.TryDequeue(out track))
            {
                //TODO: Implement bulk scrobble
                try
                {
                    results.Add(_scrobbler.Scrobble(track));
                }
                catch (Exception exception)
                {
                    if (throwExceptionDuringProcess)
                    {
                        throw;
                    }
                    results.Add(new ScrobbleResponse {
                        Track = track, Exception = exception
                    });
                }
            }

            RatingObject rating;

            while (RatingQueue.TryDequeue(out rating))
            {
                try
                {
                    switch (rating.RatingType)
                    {
                    case Rating.love:
                        results.Add(_scrobbler.Love(rating.Track));
                        break;

                    case Rating.ban:
                        results.Add(_scrobbler.Ban(rating.Track));
                        break;

                    case Rating.unlove:
                        results.Add(_scrobbler.UnLove(rating.Track));
                        break;

                    case Rating.unban:
                        results.Add(_scrobbler.UnBan(rating.Track));
                        break;
                    }
                }
                catch (Exception exception)
                {
                    if (throwExceptionDuringProcess)
                    {
                        throw;
                    }
                    results.Add(new RatingResponse {
                        ErrorCode = -1, Exception = exception, Track = rating.Track
                    });
                }
            }

            return(results);
        }