Пример #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
        public Scrobble(string trackData)
        {
            List<string> trackDataList = trackData.Split('-').ToList();
            if (trackDataList.Count > 0)
            {
                string artist = trackDataList[0];
                string title = string.Empty;
                for (int i = 1; i < trackDataList.Count; i++)
                {
                    title += trackDataList[i];
                }

                Scrobbler scrobbler = new Scrobbler("0a5674077da2782718075412eab00800", "56668ad9e4293be48def8f5ab1a6c658");
                Track track = new Track {ArtistName = artist, TrackName = title};
                ScrobbleResponse response = scrobbler.Scrobble(track);
                ErrorMessage = response.Exception.Message + " " + response.ErrorCode.ToString();
            }
            else
            {
                ErrorMessage = trackDataList.Count.ToString();
            }
        }
Пример #3
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);
        }