Skip to content

Commit

Permalink
added ability to print to stdout when using save
Browse files Browse the repository at this point in the history
  • Loading branch information
xnetcat committed Sep 1, 2023
1 parent 8a8891e commit ce2bce6
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 14 deletions.
7 changes: 4 additions & 3 deletions spotdl/console/entry_point.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,10 @@ def console_entry_point():
return None

# Check if save file is present and if it's valid
if isinstance(downloader_settings["save_file"], str) and not downloader_settings[
"save_file"
].endswith(".spotdl"):
if isinstance(downloader_settings["save_file"], str) and (
not downloader_settings["save_file"].endswith(".spotdl")
and not downloader_settings["save_file"] == "-"
):
raise DownloaderError("Save file has to end with .spotdl")

# Check if the user is logged in
Expand Down
27 changes: 17 additions & 10 deletions spotdl/console/save.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ def save(
save_path = downloader.settings["save_file"]
m3u_file = downloader.settings["m3u"]

if save_path is None:
to_stdout = save_path == "-"

if save_path is None and not to_stdout:
raise DownloaderError("Save file is not specified")

# Parse the query
Expand Down Expand Up @@ -77,9 +79,13 @@ async def pool_worker(song: Song):
# call all task asynchronously, and wait until all are finished
save_data = list(downloader.loop.run_until_complete(asyncio.gather(*tasks)))

# Save the songs to a file
with open(save_path, "w", encoding="utf-8") as save_file:
json.dump(save_data, save_file, indent=4, ensure_ascii=False)
if to_stdout:
# Print the songs to stdout
print(json.dumps(save_data, indent=4, ensure_ascii=False))
else:
# Save the songs to a file
with open(save_path, "w", encoding="utf-8") as save_file:
json.dump(save_data, save_file, indent=4, ensure_ascii=False)

if m3u_file:
gen_m3u_files(
Expand All @@ -91,9 +97,10 @@ async def pool_worker(song: Song):
False,
)

logger.info(
"Saved %s song%s to %s",
len(save_data),
"s" if len(save_data) > 1 else "",
save_path,
)
if not to_stdout:
logger.info(
"Saved %s song%s to %s",
len(save_data),
"s" if len(save_data) > 1 else "",
save_path,
)
2 changes: 1 addition & 1 deletion spotdl/utils/arguments.py
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ def parse_output_options(parser: _ArgumentGroup):
"The file to save/load the songs data from/to. "
"It has to end with .spotdl. "
"If combined with the download operation, it will save the songs data to the file. "
"Required for save/preload/sync"
"Required for save/sync (use - to print to stdout when using save). "
),
required=len(sys.argv) > 1 and sys.argv[1] in ["save"],
)
Expand Down

0 comments on commit ce2bce6

Please sign in to comment.