Skip to content

Commit

Permalink
Add dump json (#341)
Browse files Browse the repository at this point in the history
* Add -j/--dump-json argument

* Output JSON with double quotes instead of single quotes

* clean up rebase, touch-up logging

For the option --dump-json the only thing outputted needs to be the json so that other applications can use it, we therefore need to make sure nothing other than the json is outputtet when using the option

* logging touch-up

* Fix DeepSource issues

---------

Co-authored-by: Mattia <io.mattia98@gmail.com>
  • Loading branch information
jonas-resch and Mattia98 committed May 10, 2023
1 parent f2beda1 commit 14f241d
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 15 deletions.
10 changes: 9 additions & 1 deletion spotify_dl/scaffold.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from rich.logging import RichHandler
from rich.console import Console

__all__ = ["log", "get_tokens", "console"]
__all__ = ["log", "setLogLevel", "get_tokens", "console"]

logging.basicConfig(
level=logging.INFO,
Expand All @@ -19,6 +19,14 @@
)


def setLogLevel(level):
"""
Sets the log level of the global logger to the passed level
:param level: the log level
"""
logging.getLogger().setLevel(level)


def get_tokens():
"""
Checks if the required API keys for Spotify has been set.
Expand Down
47 changes: 33 additions & 14 deletions spotify_dl/spotify_dl.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@
import json
import os
import sys
from logging import DEBUG
from logging import DEBUG, ERROR
from pathlib import Path, PurePath
import spotipy
from spotipy.oauth2 import SpotifyClientCredentials

from spotify_dl.constants import VERSION
from spotify_dl.scaffold import log, console, get_tokens
from spotify_dl.scaffold import log, setLogLevel, console, get_tokens
from spotify_dl.spotify import (
fetch_tracks,
parse_spotify_url,
validate_spotify_urls,
get_item_name,
)

from spotify_dl.youtube import download_songs, default_filename, playlist_num_filename
from spotify_dl.youtube import download_songs, default_filename, playlist_num_filename, dump_json


def spotify_dl():
Expand Down Expand Up @@ -49,6 +49,13 @@ def spotify_dl():
help="Download using youtube-dl",
default=True,
)
parser.add_argument(
"-j",
"--dump-json",
action="store_true",
help="Dump info-json using youtube-dl",
default=False
)
parser.add_argument(
"-f",
"--format_str",
Expand Down Expand Up @@ -122,14 +129,21 @@ def spotify_dl():
args = parser.parse_args()
num_cores = os.cpu_count()
args.multi_core = int(args.multi_core)
console.log(f"Starting spotify_dl [bold green]v{VERSION}[/bold green]")

if args.dump_json:
setLogLevel(ERROR)
if args.verbose:
log.setLevel(DEBUG)
setLogLevel(DEBUG)

log.info("Starting spotify_dl v%s", VERSION)
log.debug("Setting debug mode on spotify_dl")

if args.multi_core > (num_cores - 1):
console.log(
f"Requested cores [bold red]{args.multi_core}[/bold red] exceeds available [bold green]{num_cores}[/bold green], using [bold green]{num_cores - 1}[/bold green] cores."
log.info(
"Requested cores %d exceeds available %d, using %d cores.",
args.multi_core,
num_cores,
num_cores - 1
)
args.multi_core = num_cores - 1
if args.version:
Expand Down Expand Up @@ -163,8 +177,9 @@ def spotify_dl():
)
)
log.debug("Arguments: %s ", args)
console.print(
f"Sponsorblock enabled?: [bold green]{args.use_sponsorblock}[/bold green]"
log.info(
"Sponsorblock enabled?: %s",
args.use_sponsorblock
)
valid_urls = validate_spotify_urls(args.url)
if not valid_urls:
Expand All @@ -180,12 +195,15 @@ def spotify_dl():
PurePath.joinpath(Path(args.output), Path(directory_name))
)
url_dict["save_path"].mkdir(parents=True, exist_ok=True)
console.print(
f"Saving songs to [bold green]{directory_name}[/bold green] directory"
log.info(
"Saving songs to %s directory",
directory_name
)
url_dict["songs"] = fetch_tracks(sp, item_type, url)
url_data["urls"].append(url_dict.copy())
if args.download is True:
if args.dump_json is True:
dump_json(url_dict["songs"])
elif args.download is True:
file_name_f = default_filename
if args.keep_playlist_order:
file_name_f = playlist_num_filename
Expand All @@ -208,6 +226,7 @@ def spotify_dl():
if __name__ == "__main__":
start_time = time.time()
spotify_dl()
console.log(
f"Download completed in [bold green]{time.time() - start_time} seconds.[/bold green]"
log.info(
"Download completed in %f seconds.",
time.time() - start_time
)
23 changes: 23 additions & 0 deletions spotify_dl/youtube.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import os
import multiprocessing

import json
import mutagen
import csv
import yt_dlp
Expand All @@ -26,6 +27,28 @@ def playlist_num_filename(**kwargs):
return f"{kwargs['track_num']} - {default_filename(**kwargs)}"


def dump_json(songs):
"""
Outputs the JSON response of ydl.extract_info to stdout
:param songs: the songs for which the JSON should be output
"""
for song in songs:
query = f"{song.get('artist')} - {song.get('name')} Lyrics".replace(":", "").replace("\"", "")

ydl_opts = {
'quiet': True
}

with yt_dlp.YoutubeDL(ydl_opts) as ydl:
try:
ytJson = ydl.extract_info('ytsearch:' + query, False)
print(json.dumps(ytJson.get('entries')))
except Exception as e: # skipcq: PYL-W0703
log.debug(e)
print(f"Failed to download {song.get('name')}, make sure yt_dlp is up to date")
continue


def write_tracks(tracks_file, song_dict):
"""
Writes the information of all tracks in the playlist[s] to a text file in csv kind of format
Expand Down

0 comments on commit 14f241d

Please sign in to comment.