Skip to content

Commit

Permalink
Fix m3u on already exists
Browse files Browse the repository at this point in the history
  • Loading branch information
justin025 committed Nov 17, 2024
1 parent 27d5dab commit f1761b6
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 30 deletions.
40 changes: 11 additions & 29 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, fix_mp3_metadata
from .post_download import convert_audio_format, set_music_thumbnail, fix_mp3_metadata, add_to_m3u_file
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 @@ -104,8 +104,16 @@ def run(self):
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)

# 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)
add_to_m3u_file(item, item_metadata)

if self.gui:
if item['item_status'] == "Downloading":
if item['item_status'] in ('Downloading', 'Adding To M3U'):
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 @@ -310,33 +318,7 @@ def run(self):
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.")
add_to_m3u_file(item, item_metadata)

item['item_status'] = 'Downloaded'
logger.info("Item Successfully Downloaded")
Expand Down
32 changes: 31 additions & 1 deletion src/onthespot/post_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
import requests
from PIL import Image
from mutagen.flac import Picture
from mutagen.id3 import ID3, ID3NoHeaderError, WOAS, USLT, TCMP, COMM
from mutagen.oggvorbis import OggVorbis
import music_tag
from .otsconfig import config
from .runtimedata import get_logger
from mutagen.id3 import ID3, ID3NoHeaderError, WOAS, USLT, TCMP, COMM
from .utils import sanitize_data

logger = get_logger("post_download")

Expand Down Expand Up @@ -327,3 +328,32 @@ def fix_mp3_metadata(filename, metadata):
id3['TCMP'] = TCMP(encoding=3, text=id3['TXXX:TCMP'].text[0])
del id3['TXXX:TCMP']
id3.save()

def add_to_m3u_file(item, item_metadata):
logger.info(f"Adding {item['file_path']} to m3u")
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 item['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{item['file_path']}\n")
else:
logger.info(f"{item['file_path']} already exists in the M3U file.")

0 comments on commit f1761b6

Please sign in to comment.