1- using NLog ;
1+ using FuzzySharp ;
2+ using NLog ;
23using NzbDrone . Common . Http ;
34using NzbDrone . Common . Instrumentation ;
45using NzbDrone . Core . Download ;
89using NzbDrone . Core . Parser . Model ;
910using System . Text . Json ;
1011using Tubifarry . Core . Model ;
12+ using Tubifarry . Core . Utilities ;
1113
1214namespace Tubifarry . Indexers . Soulseek
1315{
@@ -45,7 +47,6 @@ public IList<ReleaseInfo> ParseResponse(IndexerResponse indexerResponse)
4547 }
4648
4749 SlskdSearchData searchTextData = SlskdSearchData . FromJson ( indexerResponse . HttpRequest . ContentSummary ) ;
48-
4950 HashSet < string > ? ignoredUsers = GetIgnoredUsers ( Settings . IgnoreListPath ) ;
5051
5152 foreach ( SlskdFolderData response in searchResponse . Responses )
@@ -55,7 +56,7 @@ public IList<ReleaseInfo> ParseResponse(IndexerResponse indexerResponse)
5556
5657 IEnumerable < SlskdFileData > filteredFiles = SlskdFileData . GetFilteredFiles ( response . Files , Settings . OnlyAudioFiles , Settings . IncludeFileExtensions ) ;
5758
58- foreach ( IGrouping < string , SlskdFileData > directoryGroup in filteredFiles . GroupBy ( f => GetDirectoryFromFilename ( f . Filename ) ) )
59+ foreach ( IGrouping < string , SlskdFileData > directoryGroup in filteredFiles . GroupBy ( f => SlskdTextProcessor . GetDirectoryFromFilename ( f . Filename ) ) )
5960 {
6061 if ( string . IsNullOrEmpty ( directoryGroup . Key ) )
6162 continue ;
@@ -72,9 +73,11 @@ public IList<ReleaseInfo> ParseResponse(IndexerResponse indexerResponse)
7273 FileCount = response . FileCount
7374 } ;
7475
75- AlbumData albumData = SlskdItemsParser . CreateAlbumData ( searchResponse . Id , directoryGroup , searchTextData , folderData , Settings ) ;
76+ if ( searchTextData . ExpandDirectory && ShouldExpandDirectory ( albumDatas , searchResponse , searchTextData , directoryGroup , folderData ) )
77+ continue ;
7678
77- albumDatas . Add ( albumData ) ;
79+ AlbumData originalAlbumData = SlskdItemsParser . CreateAlbumData ( searchResponse . Id , directoryGroup , searchTextData , folderData , Settings ) ;
80+ albumDatas . Add ( originalAlbumData ) ;
7881 }
7982 }
8083
@@ -88,12 +91,39 @@ public IList<ReleaseInfo> ParseResponse(IndexerResponse indexerResponse)
8891 return albumDatas . OrderByDescending ( x => x . Priotity ) . Select ( a => a . ToReleaseInfo ( ) ) . ToList ( ) ;
8992 }
9093
91- private static string GetDirectoryFromFilename ( string ? filename )
94+ private bool ShouldExpandDirectory ( List < AlbumData > albumDatas , SlskdSearchResponse searchResponse , SlskdSearchData searchTextData , IGrouping < string , SlskdFileData > directoryGroup , SlskdFolderData folderData )
9295 {
93- if ( string . IsNullOrEmpty ( filename ) )
94- return "" ;
95- int lastBackslashIndex = filename . LastIndexOf ( '\\ ' ) ;
96- return lastBackslashIndex >= 0 ? filename [ ..lastBackslashIndex ] : "" ;
96+ if ( string . IsNullOrEmpty ( searchTextData . Artist ) || string . IsNullOrEmpty ( searchTextData . Album ) )
97+ return false ;
98+
99+ bool artistMatch = Fuzz . PartialRatio ( folderData . Artist , searchTextData . Artist ) > 85 ;
100+ bool albumMatch = Fuzz . PartialRatio ( folderData . Album , searchTextData . Album ) > 85 ;
101+
102+ if ( ! artistMatch || ! albumMatch )
103+ return false ;
104+
105+ SlskdFileData ? originalTrack = directoryGroup . FirstOrDefault ( x => AudioFormatHelper . GetAudioCodecFromExtension ( x . Extension ? . ToLowerInvariant ( ) ?? Path . GetExtension ( x . Filename ) ?? "" ) != AudioFormat . Unknown ) ;
106+
107+ if ( originalTrack == null )
108+ return false ;
109+
110+ _logger . Trace ( $ "Expanding directory for: { folderData . Username } :{ directoryGroup . Key } ") ;
111+
112+ SlskdRequestGenerator ? requestGenerator = _indexer . GetExtendedRequestGenerator ( ) as SlskdRequestGenerator ;
113+ IGrouping < string , SlskdFileData > ? expandedGroup = requestGenerator ? . ExpandDirectory ( folderData . Username , directoryGroup . Key , originalTrack ) . Result ;
114+
115+ if ( expandedGroup != null )
116+ {
117+ _logger . Debug ( $ "Successfully expanded directory to { expandedGroup . Count ( ) } files") ;
118+ AlbumData albumData = SlskdItemsParser . CreateAlbumData ( searchResponse . Id , expandedGroup , searchTextData , folderData , Settings ) ;
119+ albumDatas . Add ( albumData ) ;
120+ return true ;
121+ }
122+ else
123+ {
124+ _logger . Warn ( $ "Failed to expand directory for { folderData . Username } :{ directoryGroup . Key } ") ;
125+ }
126+ return false ;
97127 }
98128
99129 public void RemoveSearch ( string searchId , bool delay = false )
@@ -172,7 +202,7 @@ private async Task ExecuteRemovalAsync(SlskdSettings settings, string searchId)
172202 _logger . Trace ( $ "Using cached ignore list from: { ignoreListPath } with { cached . IgnoredUsers . Count } users") ;
173203 return cached . IgnoredUsers ;
174204 }
175- HashSet < string > ignoredUsers = ParseIgnoreListContent ( File . ReadAllText ( ignoreListPath ) ) ;
205+ HashSet < string > ignoredUsers = SlskdTextProcessor . ParseListContent ( File . ReadAllText ( ignoreListPath ) ) ;
176206 _ignoreListCache [ ignoreListPath ] = ( ignoredUsers , fileSize ) ;
177207 _logger . Trace ( $ "Loaded ignore list with { ignoredUsers . Count } users from: { ignoreListPath } ") ;
178208 return ignoredUsers ;
@@ -183,18 +213,5 @@ private async Task ExecuteRemovalAsync(SlskdSettings settings, string searchId)
183213 return null ;
184214 }
185215 }
186-
187- private static HashSet < string > ParseIgnoreListContent ( string content )
188- {
189- if ( string . IsNullOrWhiteSpace ( content ) )
190- return new HashSet < string > ( StringComparer . OrdinalIgnoreCase ) ;
191-
192- return content
193- . Split ( new [ ] { '\t ' , '\r ' , '\n ' } , StringSplitOptions . RemoveEmptyEntries )
194- . Where ( username => ! string . IsNullOrWhiteSpace ( username ) )
195- . Select ( username => username . Trim ( ) )
196- . ToHashSet ( StringComparer . OrdinalIgnoreCase ) ;
197- }
198-
199216 }
200217}
0 commit comments