From b3ae8e65ac270daebf4cb567e8cd7172f02089df Mon Sep 17 00:00:00 2001 From: Andy Pack <andy@sarsoo.xyz> Date: Wed, 2 Apr 2025 08:28:39 +0100 Subject: [PATCH] adding consumer, changing default poll period --- Selector.AppleMusic/Watcher/WatcherFactory.cs | 4 ++-- Selector.CLI/Services/DbWatcherService.cs | 4 +++- Selector.Event/Consumers/UserEventFirerFactory.cs | 14 ++++++++++++-- Selector.LastFm/Extensions/ServiceExtensions.cs | 6 ++++-- 4 files changed, 21 insertions(+), 7 deletions(-) diff --git a/Selector.AppleMusic/Watcher/WatcherFactory.cs b/Selector.AppleMusic/Watcher/WatcherFactory.cs index 22beb71..9648049 100644 --- a/Selector.AppleMusic/Watcher/WatcherFactory.cs +++ b/Selector.AppleMusic/Watcher/WatcherFactory.cs @@ -6,7 +6,7 @@ namespace Selector.AppleMusic.Watcher public interface IAppleMusicWatcherFactory { Task<IWatcher> Get<T>(AppleMusicApiProvider appleMusicProvider, string developerToken, string teamId, - string keyId, string userToken, string id = null, int pollPeriod = 3000) + string keyId, string userToken, string id = null, int pollPeriod = 10000) where T : class, IWatcher; } @@ -22,7 +22,7 @@ namespace Selector.AppleMusic.Watcher } public async Task<IWatcher> Get<T>(AppleMusicApiProvider appleMusicProvider, string developerToken, - string teamId, string keyId, string userToken, string id = null, int pollPeriod = 3000) + string teamId, string keyId, string userToken, string id = null, int pollPeriod = 10000) where T : class, IWatcher { if (typeof(T).IsAssignableFrom(typeof(AppleMusicPlayerWatcher))) diff --git a/Selector.CLI/Services/DbWatcherService.cs b/Selector.CLI/Services/DbWatcherService.cs index ca084c1..cf0c203 100644 --- a/Selector.CLI/Services/DbWatcherService.cs +++ b/Selector.CLI/Services/DbWatcherService.cs @@ -161,7 +161,7 @@ namespace Selector.CLI if (MappingPersisterFactory is not null && !Magic.Dummy) consumers.Add(await MappingPersisterFactory.Get()); - if (UserEventFirerFactory is not null) consumers.Add(await UserEventFirerFactory.Get()); + if (UserEventFirerFactory is not null) consumers.Add(await UserEventFirerFactory.GetSpotify()); if (dbWatcher.User.LastFmConnected()) { @@ -187,6 +187,8 @@ namespace Selector.CLI if (CacheWriterFactory is not null) consumers.Add(await CacheWriterFactory.GetApple()); if (PublisherFactory is not null) consumers.Add(await PublisherFactory.GetApple()); + if (UserEventFirerFactory is not null) consumers.Add(await UserEventFirerFactory.GetApple()); + break; } diff --git a/Selector.Event/Consumers/UserEventFirerFactory.cs b/Selector.Event/Consumers/UserEventFirerFactory.cs index 6c8fc91..8dc215b 100644 --- a/Selector.Event/Consumers/UserEventFirerFactory.cs +++ b/Selector.Event/Consumers/UserEventFirerFactory.cs @@ -5,7 +5,8 @@ namespace Selector.Events { public interface IUserEventFirerFactory { - public Task<SpotifyUserEventFirer> Get(ISpotifyPlayerWatcher watcher = null); + public Task<SpotifyUserEventFirer> GetSpotify(ISpotifyPlayerWatcher watcher = null); + public Task<AppleUserEventFirer> GetApple(IAppleMusicPlayerWatcher watcher = null); } public class UserEventFirerFactory : IUserEventFirerFactory @@ -19,7 +20,7 @@ namespace Selector.Events UserEvent = userEvent; } - public Task<SpotifyUserEventFirer> Get(ISpotifyPlayerWatcher watcher = null) + public Task<SpotifyUserEventFirer> GetSpotify(ISpotifyPlayerWatcher watcher = null) { return Task.FromResult(new SpotifyUserEventFirer( watcher, @@ -27,5 +28,14 @@ namespace Selector.Events LoggerFactory.CreateLogger<SpotifyUserEventFirer>() )); } + + public Task<AppleUserEventFirer> GetApple(IAppleMusicPlayerWatcher watcher = null) + { + return Task.FromResult(new AppleUserEventFirer( + watcher, + UserEvent, + LoggerFactory.CreateLogger<AppleUserEventFirer>() + )); + } } } \ No newline at end of file diff --git a/Selector.LastFm/Extensions/ServiceExtensions.cs b/Selector.LastFm/Extensions/ServiceExtensions.cs index 0358b4b..4ea4e0a 100644 --- a/Selector.LastFm/Extensions/ServiceExtensions.cs +++ b/Selector.LastFm/Extensions/ServiceExtensions.cs @@ -1,4 +1,5 @@ using IF.Lastfm.Core.Api; +using IF.Lastfm.Core.Scrobblers; using Microsoft.Extensions.DependencyInjection; namespace Selector.Extensions; @@ -7,8 +8,7 @@ public static class ServiceExtensions { public static IServiceCollection AddLastFm(this IServiceCollection services, string client, string secret) { - var lastAuth = new LastAuth(client, secret); - services.AddSingleton(lastAuth); + services.AddTransient(sp => new LastAuth(client, secret)); services.AddTransient(sp => new LastfmClient(sp.GetService<LastAuth>())); services.AddTransient<ITrackApi>(sp => sp.GetService<LastfmClient>().Track); @@ -21,6 +21,8 @@ public static class ServiceExtensions services.AddTransient<ILibraryApi>(sp => sp.GetService<LastfmClient>().Library); services.AddTransient<ITagApi>(sp => sp.GetService<LastfmClient>().Tag); + services.AddTransient<IScrobbler, MemoryScrobbler>(); + return services; } } \ No newline at end of file