Skip to content

Commit

Permalink
Fix mp3 metadata, fix m3u files, fix mirror playback bug on playlists
Browse files Browse the repository at this point in the history
  • Loading branch information
justin025 committed Nov 11, 2024
1 parent 53a99e3 commit 27d5dab
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 41 deletions.
7 changes: 6 additions & 1 deletion src/onthespot/api/spotify.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,13 @@ def run(self):
playlist_name = ''
playlist_by = ''
if data['context']['type'] == 'playlist':
match = re.search(r'spotify:playlist:(\w+)', data['context']['uri'])
if match:
playlist_id = match.group(1)
else:
continue
token = account_pool[parsing_index]['login']['session'].tokens().get("user-read-email")
playlist_name, playlist_by = spotify_get_playlist_data(token, data['item']['id'])
playlist_name, playlist_by = spotify_get_playlist_data(token, playlist_id)
parent_category = 'playlist'
elif data['context']['type'] == 'collection':
playlist_name = 'Liked Songs'
Expand Down
78 changes: 39 additions & 39 deletions src/onthespot/downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from librespot.metadata import TrackId, EpisodeId
from .runtimedata import get_logger, download_queue, download_queue_lock, account_pool
from .otsconfig import config
from .post_download import convert_audio_format, set_music_thumbnail
from .post_download import convert_audio_format, set_music_thumbnail, fix_mp3_metadata
from .api.spotify import spotify_get_token, spotify_get_track_metadata, spotify_get_episode_metadata, spotify_get_lyrics
from .api.soundcloud import soundcloud_get_token, soundcloud_get_track_metadata
from .api.deezer import deezer_get_track_metadata, get_song_info_from_deezer_website, genurlkey, calcbfkey, decryptfile
Expand Down Expand Up @@ -93,39 +93,6 @@ def run(self):

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

# M3U
if config.get('create_m3u_playlists') and item.get('parent_category') == 'playlist':
item['item_status'] = 'Adding To M3U'
if self.gui:
self.progress.emit(item, self.tr("Adding To M3U"), 1)

path = config.get("m3u_name_formatter")
m3u_file = path.format(
playlist_name=sanitize_data(item['playlist_name']),
playlist_owner=sanitize_data(item['playlist_by']),
)

m3u_file += "." + config.get("m3u_format")

dl_root = config.get("download_root")
m3u_path = os.path.join(dl_root, m3u_file)

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

if not os.path.exists(m3u_path):
with open(m3u_path, 'w') as m3u_file:
m3u_file.write("#EXTM3U\n")

# Check if the item_path is already in the M3U file
with open(m3u_path, 'r') as m3u_file:
m3u_contents = m3u_file.readlines()

if file_path not in [line.strip() for line in m3u_contents]:
with open(m3u_path, 'a') as m3u_file:
m3u_file.write(f"#EXTINF:{round(int(item_metadata['length'])/1000)}, {item_metadata['artists']} - {item_metadata['title']}\n{file_path}\n")
else:
logger.info(f"{file_path} already exists in the M3U file.") # Log or handle the existing entry case

# Skip download if file exists under different extension
file_directory = os.path.dirname(file_path)
base_filename = os.path.basename(file_path)
Expand All @@ -134,14 +101,11 @@ def run(self):
full_path = os.path.join(file_directory, entry) # Construct the full file path

# Check if the entry is a file and if its name matches the base filename
if os.path.isfile(full_path) and os.path.splitext(entry)[0] == base_filename:
if os.path.isfile(full_path) and os.path.splitext(entry)[0] == base_filename and os.path.splitext(entry)[1] != '.lrc':

item['file_path'] = os.path.join(file_directory, entry)
if self.gui:
if item['item_status'] in (
"Downloading",
"Adding To M3U"
):
if item['item_status'] == "Downloading":
self.progress.emit(item, self.tr("Already Exists"), 100)
item['item_status'] = 'Already Exists'
logger.info(f"File already exists, Skipping download for track by id '{item_id}'")
Expand Down Expand Up @@ -338,6 +302,42 @@ def run(self):
self.progress.emit(item, self.tr("Setting Thumbnail"), 99)
set_music_thumbnail(file_path, item_metadata)

if os.path.splitext(file_path)[1] == '.mp3':
fix_mp3_metadata(file_path, item_metadata)

# M3U
if config.get('create_m3u_playlists') and item.get('parent_category') == 'playlist':
item['item_status'] = 'Adding To M3U'
if self.gui:
self.progress.emit(item, self.tr("Adding To M3U"), 1)

path = config.get("m3u_name_formatter")
m3u_file = path.format(
playlist_name=sanitize_data(item['playlist_name']),
playlist_owner=sanitize_data(item['playlist_by']),
)

m3u_file += "." + config.get("m3u_format")

dl_root = config.get("download_root")
m3u_path = os.path.join(dl_root, m3u_file)

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

if not os.path.exists(m3u_path):
with open(m3u_path, 'w') as m3u_file:
m3u_file.write("#EXTM3U\n")

# Check if the item_path is already in the M3U file
with open(m3u_path, 'r') as m3u_file:
m3u_contents = m3u_file.readlines()

if file_path not in [line.strip() for line in m3u_contents]:
with open(m3u_path, 'a') as m3u_file:
m3u_file.write(f"#EXTINF:{round(int(item_metadata['length'])/1000)}, {item_metadata['artists']} - {item_metadata['title']}\n{file_path}\n")
else:
logger.info(f"{file_path} already exists in the M3U file.")

item['item_status'] = 'Downloaded'
logger.info("Item Successfully Downloaded")
if self.gui:
Expand Down
3 changes: 2 additions & 1 deletion src/onthespot/gui/mainui.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ def run(self):
item = pending.pop(item_id)
token = get_account_token()
item_metadata = globals()[f"{item['item_service']}_get_{item['item_type']}_metadata"](token, item['item_id'])
self.add_item_to_download_list.emit(item, item_metadata)
if item_id not in download_queue:
self.add_item_to_download_list.emit(item, item_metadata)
continue
except Exception as e:
logger.error(f"Unknown Exception for {item}: {str(e)}")
Expand Down
16 changes: 16 additions & 0 deletions src/onthespot/post_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,3 +311,19 @@ def set_music_thumbnail(filename, metadata):

if not config.get('save_album_cover'):
os.remove(image_path)

def fix_mp3_metadata(filename, metadata):
id3 = ID3(filename)
if 'TXXX:WOAS' in id3:
id3['WOAS'] = WOAS(url=id3['TXXX:WOAS'].text[0])
del id3['TXXX:WOAS']
if 'TXXX:USLT' in id3:
id3.add(USLT(encoding=3, lang=u'und', desc=u'desc', text=id3['TXXX:USLT'].text[0]))
del id3['TXXX:USLT']
if 'TXXX:COMM' in id3:
id3['COMM'] = COMM(encoding=3, lang='und', text=id3['TXXX:COMM'].text[0])
del id3['TXXX:COMM']
if 'TXXX:TCMP' in id3:
id3['TCMP'] = TCMP(encoding=3, text=id3['TXXX:TCMP'].text[0])
del id3['TXXX:TCMP']
id3.save()

0 comments on commit 27d5dab

Please sign in to comment.