Skip to content

Commit 2315d8f

Browse files
authored
Merge pull request #152 from insanemal/download_filter
Added file filtering to reduce unwated files being downloaded
2 parents 812b949 + 2d74fd7 commit 2315d8f

2 files changed

Lines changed: 45 additions & 2 deletions

File tree

config.ini

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,14 @@ search_source = missing
3737
enable_search_denylist = False
3838
max_search_failures = 3
3939

40+
[Download Settings]
41+
download_filtering = True
42+
use_extension_whitelist = False
43+
extensions_whitelist = lrc,nfo,txt
44+
4045
[Logging]
4146
level = INFO
4247
# https://docs.python.org/3/library/logging.html#logrecord-attributes
4348
format = [%(levelname)s|%(module)s|L%(lineno)d] %(asctime)s: %(message)s
4449
# https://docs.python.org/3/library/time.html#time.strftime
45-
datefmt = %Y-%m-%dT%H:%M:%S%z
50+
datefmt = %Y-%m-%dT%H:%M:%S%z

soularr.py

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import logging
1515
import json
1616
from datetime import datetime
17-
17+
import copy
1818
import music_tag
1919
import slskd_api
2020
from pyarr import LidarrAPI
@@ -254,6 +254,39 @@ def verify_filetype(file,allowed_filetype):
254254
else:
255255
return False
256256

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+
257290

258291
def search_and_download(grab_list, query, tracks, track, artist_name, release):
259292
search = slskd.searches.search_text(searchText = query,
@@ -322,6 +355,7 @@ def search_and_download(grab_list, query, tracks, track, artist_name, release):
322355

323356
if tracks_info['count'] == track_num and tracks_info['filetype'] != "":
324357
if album_match(tracks, directory['files'], username, allowed_filetype):
358+
directory = download_filter(allowed_filetype,directory) #Filter files if requested
325359
for i in range(0,len(directory['files'])):
326360
directory['files'][i]['filename'] = file_dir + "\\" + directory['files'][i]['filename']
327361

@@ -874,6 +908,10 @@ def update_search_denylist(denylist, album_id, success):
874908
ignored_users = config.get('Search Settings', 'ignored_users', fallback='').split(",")
875909
search_type = config.get('Search Settings', 'search_type', fallback='first_page').lower().strip()
876910
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(',')
877915

878916
search_sources = [search_source]
879917
if search_sources[0] == 'all':

0 commit comments

Comments
 (0)