Example #1
0
        public async Task Scrobble_WhenTrackIsTooShort_DoesNotCallLastFMApi()
        {
            // Arrange

            var trackScrobble = new TrackScrobble
            {
                Track = new Track
                {
                    Title    = "Some Title",
                    Artist   = "Some Artist",
                    Duration = TimeSpan.FromSeconds(15),
                    Album    = new Album("Some Artist", "Some Album"),
                },

                PlayStartTimestamp = new DateTime(2017, 09, 18),
            };

            var mocker = new AutoMocker();
            var target = mocker.CreateInstance <LastFMScrobbler>();

            // Act

            await target.Scrobble(trackScrobble, CancellationToken.None);

            // Assert

            var apiClientMock = mocker.GetMock <ILastFMApiClient>();

            apiClientMock.Verify(x => x.Scrobble(It.IsAny <TrackScrobble>(), It.IsAny <CancellationToken>()), Times.Never);
        }
Example #2
0
        public async Task Scrobble_ForValidTrack_CallsLastFMApiCorrectly()
        {
            // Arrange

            var trackScrobble = new TrackScrobble
            {
                Track = new Track
                {
                    Title    = "Some Title",
                    Artist   = "Some Artist",
                    Duration = TimeSpan.FromMinutes(5),
                    Album    = new Album("Some Artist", "Some Album"),
                },

                PlayStartTimestamp = new DateTime(2017, 09, 18),
            };

            var mocker = new AutoMocker();
            var target = mocker.CreateInstance <LastFMScrobbler>();

            // Act

            await target.Scrobble(trackScrobble, CancellationToken.None);

            // Assert

            var apiClientMock = mocker.GetMock <ILastFMApiClient>();

            apiClientMock.Verify(x => x.Scrobble(trackScrobble, It.IsAny <CancellationToken>()), Times.Once);
        }
Example #3
0
        public async Task ProcessScrobble(TrackScrobble scrobble, IScrobbler scrobbler, CancellationToken cancellationToken)
        {
            await EnsureLoaded(cancellationToken);

            scrobblesQueue.Enqueue(scrobble);
            await SaveScrobblesQueue(cancellationToken);

            while (scrobblesQueue.Any())
            {
                var currentScrobble = scrobblesQueue.Peek();

                try
                {
                    await scrobbler.Scrobble(currentScrobble, cancellationToken);
                }
#pragma warning disable CA1031 // Do not catch general exception types
                catch (Exception e)
#pragma warning restore CA1031 // Do not catch general exception types
                {
                    logger.LogWarning($"Scrobble failed: {currentScrobble}. Error: {e.Message}. Scrobbles queue size: {scrobblesQueue.Count}");
                    break;
                }

                scrobblesQueue.Dequeue();
            }

            await SaveScrobblesQueue(cancellationToken);
        }
Example #4
0
        public async Task Scrobble(TrackScrobble trackScrobble, CancellationToken cancellationToken)
        {
            if (!TrackCouldBeScrobbled(trackScrobble.Track))
            {
                return;
            }

            await lastFMApiClient.Scrobble(trackScrobble, cancellationToken);
        }
        public async Task RegisterPlaybackFinish(SongModel song, CancellationToken cancellationToken)
        {
            var playbackDateTime = clock.Now;
            await songsService.AddSongPlayback(song, playbackDateTime, cancellationToken);

            var scrobble = new TrackScrobble
            {
                Track = GetTrackFromSong(song),
                PlayStartTimestamp = playbackDateTime - song.Duration,
                ChosenByUser       = true,
            };

            await scrobbler.Scrobble(scrobble, cancellationToken);
        }
        public async Task Scrobble(TrackScrobble trackScrobble, CancellationToken cancellationToken)
        {
            if (!CheckSession())
            {
                return;
            }

            ValidateScrobbledTrack(trackScrobble.Track);

            logger.LogInformation($"Scrobbling track: [Title: {trackScrobble.Track.Title}][Artist: {trackScrobble.Track.Artist}][Album: {trackScrobble.Track.Album.Title}]");

            var requestParams = new NameValueCollection
            {
                { "method", "track.scrobble" },
                { "artist", trackScrobble.Track.Artist },
                { "track", trackScrobble.Track.Title },
                { "timestamp", trackScrobble.PlayStartTimestamp.ToUnixTimeSeconds().ToString(CultureInfo.InvariantCulture) },
                { "choosenByUser", trackScrobble.ChosenByUser ? "1" : "0" },
                { "duration", trackScrobble.Track.Duration.TotalSeconds.ToString(CultureInfo.InvariantCulture) },
            };

            if (!String.IsNullOrEmpty(trackScrobble.Track.Album.Title))
            {
                requestParams.Add("album", trackScrobble.Track.Album.Title);
            }

            if (trackScrobble.Track.Number.HasValue)
            {
                requestParams.Add("trackNumber", trackScrobble.Track.Number.Value.ToString(CultureInfo.InvariantCulture));
            }

            var response = await PerformPostRequest <ScrobbleTrackResponse>(requestParams, true, cancellationToken);

            if (response.Scrobbles.Statistics.Ignored > 0)
            {
                logger.LogWarning($"{response.Scrobbles.Statistics.Ignored} tracks ignored");
            }

            LogCorrections(trackScrobble.Track, response.Scrobbles.Scrobble);
        }
 public Task Scrobble(TrackScrobble trackScrobble, CancellationToken cancellationToken)
 {
     return(scrobblesProcessor.ProcessScrobble(trackScrobble, scrobbler, cancellationToken));
 }