33using MiniMediaScanner . Callbacks ;
44using MiniMediaScanner . Callbacks . Status ;
55using MiniMediaScanner . Enums ;
6+ using MiniMediaScanner . Helpers ;
67using MiniMediaScanner . Models . Tidal ;
78using MiniMediaScanner . Repositories ;
9+ using Spectre . Console ;
810
911namespace MiniMediaScanner . Services ;
1012
1113public class TidalService
1214{
1315 public readonly int PreventUpdateWithinDays ;
1416 private readonly TidalAPICacheLayerService _tidalAPIService ;
15- private readonly UpdateTidalRepository _updateTidalRepository ;
17+ private readonly string _connectionString ;
18+ private readonly int _ignoreArtistAlbumAmount ;
1619
1720 public TidalService ( string connectionString ,
1821 List < TidalTokenClientSecret > secretTokens ,
1922 string countryCode ,
2023 string proxyFile ,
2124 string singleProxy ,
2225 string proxyMode ,
23- int preventUpdateWithinDays )
26+ int preventUpdateWithinDays ,
27+ int ignoreArtistAlbumAmount )
2428 {
29+ _ignoreArtistAlbumAmount = ignoreArtistAlbumAmount ;
30+ _connectionString = connectionString ;
2531 this . PreventUpdateWithinDays = preventUpdateWithinDays ;
26- _updateTidalRepository = new UpdateTidalRepository ( connectionString ) ;
2732 _tidalAPIService = new TidalAPICacheLayerService ( secretTokens , countryCode , proxyFile , singleProxy , proxyMode ) ;
2833 }
2934
@@ -65,6 +70,7 @@ public async Task UpdateArtistByNameAsync(string artistName,
6570 public async Task UpdateArtistByIdAsync ( int artistId ,
6671 Action < UpdateTidalCallback > ? callback = null )
6772 {
73+ UpdateTidalRepository _updateTidalRepository = new UpdateTidalRepository ( _connectionString ) ;
6874 await _updateTidalRepository . SetConnectionAsync ( ) ;
6975
7076 try
@@ -115,6 +121,12 @@ public async Task UpdateArtistByIdAsync(int artistId,
115121 . Where ( x => x . Type == "albums" )
116122 . ToList ( ) ;
117123
124+ if ( albums . Count >= _ignoreArtistAlbumAmount )
125+ {
126+ await _updateTidalRepository . CommitAsync ( ) ;
127+ return ;
128+ }
129+
118130 int progress = 1 ;
119131 foreach ( var album in albums )
120132 {
@@ -371,11 +383,14 @@ await _updateTidalRepository.UpsertTrackAsync(int.Parse(track.Id),
371383
372384 private async Task < TidalSearchResponse ? > InsertArtistInfoAsync ( int artistId , bool ignorePeventCheck = false )
373385 {
386+ UpdateTidalRepository _updateTidalRepository = new UpdateTidalRepository ( _connectionString ) ;
387+ await _updateTidalRepository . SetConnectionAsync ( ) ;
374388 if ( ! ignorePeventCheck )
375389 {
376390 DateTime ? lastSyncTime = await _updateTidalRepository . GetArtistLastSyncTimeAsync ( artistId ) ;
377391 if ( lastSyncTime ? . Year > 2000 && DateTime . Now . Subtract ( lastSyncTime . Value ) . TotalDays < PreventUpdateWithinDays )
378392 {
393+ await _updateTidalRepository . CommitAsync ( ) ;
379394 return null ;
380395 }
381396 }
@@ -386,6 +401,7 @@ await _updateTidalRepository.UpsertTrackAsync(int.Parse(track.Id),
386401 artistInfo ? . Data == null ||
387402 artistInfo ? . Included == null )
388403 {
404+ await _updateTidalRepository . CommitAsync ( ) ;
389405 return null ;
390406 }
391407
@@ -406,6 +422,7 @@ await _updateTidalRepository.UpsertArtistImageLinkAsync(artistId,
406422 imageLink . Meta . Height ) ;
407423 }
408424
425+ await _updateTidalRepository . CommitAsync ( ) ;
409426 return artistInfo ;
410427 }
411428}
0 commit comments