Skip to content

Commit

Permalink
Add m3u8 format to config file
Browse files Browse the repository at this point in the history
Closes: #32
  • Loading branch information
justin025 committed Oct 24, 2024
1 parent c6b5e50 commit 4de02e3
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 34 deletions.
63 changes: 32 additions & 31 deletions src/onthespot/downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,38 @@ def run(self):

item['file_path'] = file_path

# Skip file if exists under different extension
# M3U
if config.get('create_m3u_playlists') and item.get('is_playlist_item', False):
if self.gui:
self.progress.emit(item, self.tr("Adding To M3U"), 0)

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:-1, {item_metadata['artists']} - {item_metadata['title']}\n{file_path}\n")


# Skip download if file exists under different extension
file_directory = os.path.dirname(file_path)
base_file_path = os.path.splitext(os.path.basename(file_path))[0]

Expand Down Expand Up @@ -170,36 +201,6 @@ def run(self):
self.progress.emit(item, self.tr("Getting Lyrics"), 99)
globals()[f"{item_service}_get_lyrics"](token, item_id, item_type, item_metadata, file_path)

# M3U
if config.get('create_m3u_playlists') and item.get('is_playlist_item', False):
if self.gui:
self.progress.emit(item, self.tr("Adding To M3U"), 99)

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 += ".m3u"

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:-1, {conv_list_format(item_metadata['artists'])} - {item_metadata['title']}\n{file_path}\n")

if self.gui:
self.progress.emit(item, self.tr("Downloaded"), 100)
time.sleep(config.get("download_delay"))
Expand Down
1 change: 1 addition & 0 deletions src/onthespot/otsconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ def __init__(self, cfg_path=None):
"podcast_path_formatter": "Episodes" + os.path.sep + "{album}" + os.path.sep + "{name}", # Episode path format string
"playlist_path_formatter": "Playlists" + os.path.sep + "{playlist_name} by {playlist_owner}" + os.path.sep + "{name}", # Playlist path format string
"m3u_name_formatter": "M3U" + os.path.sep + "{playlist_name} by {playlist_owner}", # M3U name format string
"m3u_format": "m3u8", # M3U file format
"watch_bg_for_spotify": 0, # Detect and download songs playing on spotify client,
"max_retries": 3, # Number of times to retry before giving up on download
"max_search_results": 10, # Number of search results to display of each type
Expand Down
6 changes: 3 additions & 3 deletions src/onthespot/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,12 @@ def format_track_path(item_metadata, item_service, item_type, is_playlist_item,
name = item_metadata.get('title', '')
album = item_metadata.get('album_name', '')

if item_type == 'track':
if is_playlist_item and config.get("use_playlist_path"):
path = config.get("playlist_path_formatter")
elif item_type == 'track':
path = config.get("track_path_formatter")
elif item_type == 'episode':
path = config.get("podcast_path_formatter")
elif is_playlist_item and config.get("use_playlist_path"):
path = config.get("playlist_path_formatter")

item_path = path.format(
artist=sanitize_data(item_metadata.get('artists', '')),
Expand Down

0 comments on commit 4de02e3

Please sign in to comment.