Skip to content

Commit d663114

Browse files
committed
Lossy will be set to Wanted Lossless during processing
1 parent 81f6d9e commit d663114

File tree

2 files changed

+45
-16
lines changed

2 files changed

+45
-16
lines changed

headphones/postprocessor.py

+16-5
Original file line numberDiff line numberDiff line change
@@ -405,12 +405,15 @@ def doPostProcessing(albumid, albumpath, release, tracks, downloaded_track_list,
405405
# Need to update the downloaded track list with the new location.
406406
# Could probably just throw in the "headphones-modified" folder,
407407
# but this is good to make sure we're not counting files that may have failed to move
408+
408409
if new_folder:
409410
downloaded_track_list = []
410411
for r, d, f in os.walk(albumpath):
411412
for files in f:
412413
if any(files.lower().endswith('.' + x.lower()) for x in headphones.MEDIA_FORMATS):
413414
downloaded_track_list.append(os.path.join(r, files))
415+
416+
all_files_lossless = all(file.lower().endswith(tuple('.' + format.lower() for format in headphones.LOSSLESS_MEDIA_FORMATS)) for file in downloaded_track_list)
414417

415418
builder = metadata.AlbumMetadataBuilder()
416419
# Check if files are valid media files and are writable, before the steps
@@ -504,11 +507,19 @@ def doPostProcessing(albumid, albumpath, release, tracks, downloaded_track_list,
504507
if headphones.CONFIG.FILE_PERMISSIONS_ENABLED:
505508
updateFilePermissions(albumpaths)
506509

507-
myDB = db.DBConnection()
508-
myDB.action('UPDATE albums SET status = "Downloaded" WHERE AlbumID=?', [albumid])
509-
myDB.action(
510-
'UPDATE snatched SET status = "Processed" WHERE Status NOT LIKE "Seed%" and AlbumID=?',
511-
[albumid])
510+
# If the downloaded tracks are lossy, but lossless is wanted, set status to wanted lossless
511+
if not all_files_lossless and headphones.CONFIG.PREFERRED_QUALITY == 1:
512+
myDB = db.DBConnection()
513+
myDB.action('UPDATE albums SET status = "Wanted Lossless" WHERE AlbumID=?', [albumid])
514+
myDB.action(
515+
'UPDATE snatched SET status = "Processed" WHERE Status NOT LIKE "Seed%" and AlbumID=?',
516+
[albumid])
517+
else:
518+
myDB = db.DBConnection()
519+
myDB.action('UPDATE albums SET status = "Downloaded" WHERE AlbumID=?', [albumid])
520+
myDB.action(
521+
'UPDATE snatched SET status = "Processed" WHERE Status NOT LIKE "Seed%" and AlbumID=?',
522+
[albumid])
512523

513524
# Check if torrent has finished seeding
514525
if headphones.CONFIG.TORRENT_DOWNLOADER != 0:

headphones/soulseek.py

+29-11
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@
1111
def initialize_soulseek_client():
1212
host = headphones.CONFIG.SOULSEEK_API_URL
1313
api_key = headphones.CONFIG.SOULSEEK_API_KEY
14-
return slskd_api.SlskdClient(host=host, api_key=api_key)
14+
try:
15+
return slskd_api.SlskdClient(host=host, api_key=api_key)
16+
except:
17+
logger.info("Something went wrong while connecting to the soulseek client")
1518

1619
# Search logic, calling search and processing fucntions
1720
def search(artist, album, year, num_tracks, losslessOnly):
@@ -24,14 +27,14 @@ def search(artist, album, year, num_tracks, losslessOnly):
2427
return processed_results
2528

2629
# Stage 2: If Stage 1 fails, search with artist, album, and num_tracks (excluding year)
27-
logger.info("Soulseek search stage 1 did not meet criteria. Retrying without year...")
30+
logger.debug("Soulseek search stage 1 did not meet criteria. Retrying without year...")
2831
results = execute_search(client, artist, album, None, losslessOnly)
2932
processed_results = process_results(results, losslessOnly, num_tracks)
3033
if processed_results:
3134
return processed_results
3235

3336
# Stage 3: Final attempt, search only with artist and album
34-
logger.info("Soulseek search stage 2 did not meet criteria. Final attempt with only artist and album.")
37+
logger.debug("Soulseek search stage 2 did not meet criteria. Final attempt with only artist and album.")
3538
results = execute_search(client, artist, album, None, losslessOnly)
3639
processed_results = process_results(results, losslessOnly, num_tracks, ignore_track_count=True)
3740

@@ -42,17 +45,23 @@ def execute_search(client, artist, album, year, losslessOnly):
4245
if year:
4346
search_text += f" {year}"
4447
if losslessOnly:
45-
search_text += ".flac"
48+
search_text += " .flac"
4649

4750
# Actual search
48-
search_response = client.searches.search_text(searchText=search_text, filterResponses=True)
51+
try:
52+
search_response = client.searches.search_text(searchText=search_text, filterResponses=True)
53+
except:
54+
logger.info('Something went wrong trying to search soulseek')
4955
search_id = search_response.get('id')
5056

5157
# Wait for search completion and return response
5258
while not client.searches.state(id=search_id).get('isComplete'):
5359
time.sleep(2)
5460

55-
return client.searches.search_responses(id=search_id)
61+
try:
62+
return client.searches.search_responses(id=search_id)
63+
except:
64+
logger.info('Something went wrong getting search responsed from soulseek')
5665

5766
# Processing the search result passed
5867
def process_results(results, losslessOnly, num_tracks, ignore_track_count=False):
@@ -110,12 +119,18 @@ def process_results(results, losslessOnly, num_tracks, ignore_track_count=False)
110119

111120
def download(user, filelist):
112121
client = initialize_soulseek_client()
113-
client.transfers.enqueue(username=user, files=filelist)
122+
try:
123+
client.transfers.enqueue(username=user, files=filelist)
124+
except:
125+
logger.info('Something went wrong enqueueing downloads in soulseek')
114126

115127

116128
def download_completed():
117129
client = initialize_soulseek_client()
118-
all_downloads = client.transfers.get_all_downloads(includeRemoved=False)
130+
try:
131+
all_downloads = client.transfers.get_all_downloads(includeRemoved=False)
132+
except:
133+
logger.info('Something went wrong grabbing all downloads from soulseek')
119134
album_completion_tracker = {} # Tracks completion state of each album's songs
120135
album_errored_tracker = {} # Tracks albums with errored downloads
121136

@@ -161,15 +176,18 @@ def download_completed():
161176
# Extract 'id' and 'username' for each file to cancel the download
162177
file_id = file_data.get('id', '')
163178
username = file_data.get('username', '')
164-
success = client.transfers.cancel_download(username, file_id)
179+
try:
180+
success = client.transfers.cancel_download(username, file_id)
181+
except:
182+
logger.info('Something went wrong canceling downloads in soulseek')
165183
if not success:
166184
print(f"Failed to cancel download for file ID: {file_id}")
167185

168186
# Clear completed/canceled/errored stuff from client downloads
169187
try:
170188
client.transfers.remove_completed_downloads()
171-
except Exception as e:
172-
print(f"Failed to remove completed downloads: {e}")
189+
except:
190+
print(f"Failed to remove completed downloads")
173191

174192
# Identify completed albums
175193
completed_albums = {album for album, counts in album_completion_tracker.items() if counts['total'] == counts['completed']}

0 commit comments

Comments
 (0)