|
14 | 14 | import logging |
15 | 15 | import json |
16 | 16 | from datetime import datetime |
17 | | - |
| 17 | +import copy |
18 | 18 | import music_tag |
19 | 19 | import slskd_api |
20 | 20 | from pyarr import LidarrAPI |
@@ -254,6 +254,39 @@ def verify_filetype(file,allowed_filetype): |
254 | 254 | else: |
255 | 255 | return False |
256 | 256 |
|
| 257 | +def download_filter(allowed_filetype,directory): |
| 258 | + """ |
| 259 | + Filters the directory listing from SLSKD using the filetype whitelist. |
| 260 | + If not using the whitelist it will only return the audio files of the allowed filetype. |
| 261 | + This is to prevent downloading m3u,cue,txt,jpg,etc. files that are sometimes stored in |
| 262 | + the same folders as the music files. |
| 263 | + """ |
| 264 | + |
| 265 | + if download_filtering: |
| 266 | + if use_extension_whitelist: |
| 267 | + whitelist = copy.deepcopy(extensions_whitelist) #Copy the whitelist to allow us to append the allowed_filetype |
| 268 | + else: |
| 269 | + whitelist = [] #Init an empty list to take just the allowed_filetype |
| 270 | + whitelist.append(allowed_filetype.split(" ")[0]) |
| 271 | + unwanted =[] |
| 272 | + logger.debug(f"Accepted extensions: {whitelist}") |
| 273 | + for file in directory['files']: |
| 274 | + for extension in whitelist: |
| 275 | + if file['filename'].split(".")[-1].lower() == extension.lower(): |
| 276 | + break #Jump out and don't add wanted files to the unwanted list |
| 277 | + else: |
| 278 | + unwanted.append(file['filename']) #Add to list of files to remove from the wanted list |
| 279 | + logger.debug(f"Unwanted file: {file['filename']}") |
| 280 | + if len(unwanted) > 0: |
| 281 | + temp = [] |
| 282 | + for file in directory['files']: |
| 283 | + if file['filename'] not in unwanted: |
| 284 | + logger.debug(f"Added file to queue: {file['filenamee']}") |
| 285 | + temp.append(file) #Build the new list of files |
| 286 | + directory['files'] = temp |
| 287 | + return directory #Return the modified list |
| 288 | + return directory #If we didn't find unwanted files or we aren't filtering just return the original list |
| 289 | + |
257 | 290 |
|
258 | 291 | def search_and_download(grab_list, query, tracks, track, artist_name, release): |
259 | 292 | search = slskd.searches.search_text(searchText = query, |
@@ -322,6 +355,7 @@ def search_and_download(grab_list, query, tracks, track, artist_name, release): |
322 | 355 |
|
323 | 356 | if tracks_info['count'] == track_num and tracks_info['filetype'] != "": |
324 | 357 | if album_match(tracks, directory['files'], username, allowed_filetype): |
| 358 | + directory = download_filter(allowed_filetype,directory) #Filter files if requested |
325 | 359 | for i in range(0,len(directory['files'])): |
326 | 360 | directory['files'][i]['filename'] = file_dir + "\\" + directory['files'][i]['filename'] |
327 | 361 |
|
@@ -874,6 +908,10 @@ def update_search_denylist(denylist, album_id, success): |
874 | 908 | ignored_users = config.get('Search Settings', 'ignored_users', fallback='').split(",") |
875 | 909 | search_type = config.get('Search Settings', 'search_type', fallback='first_page').lower().strip() |
876 | 910 | search_source = config.get('Search Settings', 'search_source', fallback='missing').lower().strip() |
| 911 | + |
| 912 | + download_filtering = config.get('Download Settings', 'download_filtering', fallback=False) |
| 913 | + use_extension_whitelist = config.get('Download Settings', 'use_extension_whitelist', fallback=False) |
| 914 | + extensions_whitelist = config.get('Download Settings', 'extensions_whitelist', fallback='txt,nfo,jpg').split(',') |
877 | 915 |
|
878 | 916 | search_sources = [search_source] |
879 | 917 | if search_sources[0] == 'all': |
|
0 commit comments