From d6e720ed723489e46365339fcb0a0cc1a9e9313b Mon Sep 17 00:00:00 2001 From: andy Date: Thu, 24 Feb 2022 23:32:02 +0000 Subject: [PATCH] injecting artist scrobble count removing all now playings from pages --- Selector.CLI/ScrobbleSaver.cs | 6 +++--- Selector.Web/Hubs/NowPlayingHub.cs | 10 +++++++++- Selector.Web/Startup.cs | 1 + .../Collection/Interfaces/IWatcherCollection.cs | 2 +- Selector/Watcher/Collection/WatcherCollection.cs | 2 +- Selector/Watcher/Context/WatcherContext.cs | 6 +++--- 6 files changed, 18 insertions(+), 9 deletions(-) diff --git a/Selector.CLI/ScrobbleSaver.cs b/Selector.CLI/ScrobbleSaver.cs index b0dc211..21cd453 100644 --- a/Selector.CLI/ScrobbleSaver.cs +++ b/Selector.CLI/ScrobbleSaver.cs @@ -97,11 +97,11 @@ namespace Selector scrobbles = page1.Scrobbles; } - IdentifyDuplicates(scrobbles); - logger.LogDebug("Ordering and filtering pulled scrobbles"); - scrobbles = RemoveNowPlaying(scrobbles); + scrobbles = scrobbles.Where(s => !(s.IsNowPlaying is bool playing && playing)); + + IdentifyDuplicates(scrobbles); var nativeScrobbles = scrobbles .DistinctBy(s => new { s.TimePlayed?.UtcDateTime, s.Name, s.ArtistName }) diff --git a/Selector.Web/Hubs/NowPlayingHub.cs b/Selector.Web/Hubs/NowPlayingHub.cs index 4e1b73c..c63b6c3 100644 --- a/Selector.Web/Hubs/NowPlayingHub.cs +++ b/Selector.Web/Hubs/NowPlayingHub.cs @@ -28,11 +28,13 @@ namespace Selector.Web.Hubs private readonly AudioFeaturePuller AudioFeaturePuller; private readonly PlayCountPuller PlayCountPuller; private readonly ApplicationDbContext Db; + private readonly IScrobbleRepository ScrobbleRepository; public NowPlayingHub( IDatabaseAsync cache, AudioFeaturePuller featurePuller, ApplicationDbContext db, + IScrobbleRepository scrobbleRepository, PlayCountPuller playCountPuller = null ) { @@ -40,6 +42,7 @@ namespace Selector.Web.Hubs AudioFeaturePuller = featurePuller; PlayCountPuller = playCountPuller; Db = db; + ScrobbleRepository = scrobbleRepository; } public async Task OnConnected() @@ -89,10 +92,15 @@ namespace Selector.Web.Hubs .SingleOrDefault() ?? throw new SqlNullValueException("No user returned"); - if(user.LastFmConnected()) + if (user.LastFmConnected()) { var playCount = await PlayCountPuller.Get(user.LastFmUsername, track, artist, album, albumArtist); + if (user.ScrobbleSavingEnabled()) + { + playCount.Artist = ScrobbleRepository.GetAll(userId: user.Id, artistName: artist).Count(); + } + if (playCount is not null) { await Clients.Caller.OnNewPlayCount(playCount); diff --git a/Selector.Web/Startup.cs b/Selector.Web/Startup.cs index 2e900d2..af819a2 100644 --- a/Selector.Web/Startup.cs +++ b/Selector.Web/Startup.cs @@ -54,6 +54,7 @@ namespace Selector.Web services.AddDbContext(options => options.UseNpgsql(Configuration.GetConnectionString("Default")) ); + services.AddTransient(); services.AddIdentity() .AddEntityFrameworkStores() diff --git a/Selector/Watcher/Collection/Interfaces/IWatcherCollection.cs b/Selector/Watcher/Collection/Interfaces/IWatcherCollection.cs index bb3d1dc..def5ce8 100644 --- a/Selector/Watcher/Collection/Interfaces/IWatcherCollection.cs +++ b/Selector/Watcher/Collection/Interfaces/IWatcherCollection.cs @@ -17,7 +17,7 @@ namespace Selector /// /// New watcher /// Consumers to subscribe to new watcher - public IWatcherContext Add(IWatcher watcher, List consumers); + public IWatcherContext Add(IWatcher watcher, IEnumerable consumers); /// /// Start watcher collection diff --git a/Selector/Watcher/Collection/WatcherCollection.cs b/Selector/Watcher/Collection/WatcherCollection.cs index aad2eb0..3fd2f01 100644 --- a/Selector/Watcher/Collection/WatcherCollection.cs +++ b/Selector/Watcher/Collection/WatcherCollection.cs @@ -43,7 +43,7 @@ namespace Selector return Add(watcher, default); } - public IWatcherContext Add(IWatcher watcher, List consumers) + public IWatcherContext Add(IWatcher watcher, IEnumerable consumers) { var context = WatcherContext.From(watcher, consumers); if (IsRunning) context.Start(); diff --git a/Selector/Watcher/Context/WatcherContext.cs b/Selector/Watcher/Context/WatcherContext.cs index 43fb4b4..2486189 100644 --- a/Selector/Watcher/Context/WatcherContext.cs +++ b/Selector/Watcher/Context/WatcherContext.cs @@ -24,10 +24,10 @@ namespace Selector Watcher = watcher; } - public WatcherContext(IWatcher watcher, List consumers) + public WatcherContext(IWatcher watcher, IEnumerable consumers) { Watcher = watcher; - Consumers = consumers ?? new(); + Consumers = consumers.ToList() ?? new(); } public static WatcherContext From(IWatcher watcher) @@ -35,7 +35,7 @@ namespace Selector return new(watcher); } - public static WatcherContext From(IWatcher watcher, List consumers) + public static WatcherContext From(IWatcher watcher, IEnumerable consumers) { return new(watcher, consumers); }