Skip to content

Commit

Permalink
Add '~' to the beginning of a file before it finishes downloading and…
Browse files Browse the repository at this point in the history
… embedding, fix episode path
  • Loading branch information
justin025 committed Oct 27, 2024
1 parent 388980c commit 712de00
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
16 changes: 11 additions & 5 deletions src/onthespot/downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ def run(self):

dl_root = config.get("download_root")
file_path = os.path.join(dl_root, item_path)
directory, file_name = os.path.split(file_path)
temp_file_path = os.path.join(directory, '~' + file_name)


os.makedirs(os.path.dirname(file_path), exist_ok=True)

Expand Down Expand Up @@ -178,7 +181,7 @@ def run(self):
downloaded = 0
_CHUNK_SIZE = config.get("chunk_size")

with open(file_path, 'wb') as file:
with open(temp_file_path, 'wb') as file:
while downloaded < total_size:
data = stream.input_stream.stream().read(_CHUNK_SIZE)
downloaded += len(data)
Expand All @@ -192,7 +195,7 @@ def run(self):
bitrate = "320k" if quality == AudioQuality.VERY_HIGH else "160k"

elif item_service == "soundcloud":
command = [config.get('_ffmpeg_bin_path'), "-loglevel", "error", "-i", f"{item_metadata['file_url']}", "-c", "copy", file_path]
command = [config.get('_ffmpeg_bin_path'), "-loglevel", "error", "-i", f"{item_metadata['file_url']}", "-c", "copy", temp_file_path]
if os.name == 'nt':
subprocess.check_call(command, shell=False, creationflags=subprocess.CREATE_NO_WINDOW)
else:
Expand All @@ -215,23 +218,26 @@ def run(self):
item['item_status'] = 'Converting'
if self.gui:
self.progress.emit(item, self.tr("Converting"), 99)
convert_audio_format(file_path, bitrate, default_format)
convert_audio_format(temp_file_path, bitrate, default_format)

# Set Audio Tags
item['item_status'] = 'Embedding Metadata'
if self.gui:
self.progress.emit(item, self.tr("Embedding Metadata"), 99)
set_audio_tags(file_path, item_metadata, item_id)
set_audio_tags(temp_file_path, item_metadata, item_id)

# Thumbnail
item['item_status'] = 'Setting Thumbnail'
if self.gui:
self.progress.emit(item, self.tr("Setting Thumbnail"), 99)
try:
set_music_thumbnail(file_path, item_metadata['image_url'])
set_music_thumbnail(temp_file_path, item_metadata['image_url'])
except MissingSchema:
self.progress.emit(item, self.tr("Failed To Set Thumbnail"), 99)

# Temp file finished, convert to regular format
os.rename(temp_file_path, file_path)

# Lyrics
item['item_status'] = 'Getting Lyrics'
if item_service == "spotify":
Expand Down
2 changes: 1 addition & 1 deletion src/onthespot/post_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def convert_audio_format(filename, bitrate, default_format):
if os.path.isfile(os.path.abspath(filename)):
target_path = Path(filename)
temp_name = os.path.join(
target_path.parent, ".~"+target_path.stem
target_path.parent, "."+target_path.stem
)
if os.path.isfile(temp_name):
os.remove(temp_name)
Expand Down
6 changes: 4 additions & 2 deletions src/onthespot/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,11 @@ def format_track_path(item_metadata, item_service, item_type, is_playlist_item,

if item_service == 'soundcloud' and config.get("force_raw"):
item_path += ".mp3"
if item_service == 'spotify' and config.get("force_raw"):
elif item_service == 'spotify' and config.get("force_raw"):
item_path += ".ogg"
else:
elif item_type == 'track':
item_path += "." + config.get("media_format")
elif item_type == 'episode':
item_path += "." + config.get("podcast_media_format")

return item_path

0 comments on commit 712de00

Please sign in to comment.