"--write-to -" for writing to to STDOUT

This commit is contained in:
Ritiek Malhotra
2020-05-12 03:49:37 +05:30
parent a365746e45
commit 635c18723b
4 changed files with 53 additions and 33 deletions

View File

@@ -283,13 +283,13 @@ class Arguments:
)
if self.parsed.write_m3u and not self.parsed.list:
self.parser.error("--write-m3u can only be used with --list")
self.parser.error("--write-m3u can only be used with --list.")
if self.parsed.write_to and not (
self.parsed.playlist or self.parsed.album or self.parsed.all_albums or self.parsed.username
self.parsed.playlist or self.parsed.album or self.parsed.all_albums or self.parsed.username or self.parsed.write_m3u
):
self.parser.error(
"--write-to can only be used with --playlist, --album, --all-albums, or --username"
"--write-to can only be used with --playlist, --album, --all-albums, --username, or --write-m3u."
)
ffmpeg_exists = shutil.which("ffmpeg")

View File

@@ -118,8 +118,13 @@ class Spotdl:
total_tracks = len(tracks)
logger.info("Generating {0} from {1} YouTube URLs.".format(target_file, total_tracks))
with open(target_file, "w") as output_file:
output_file.write("#EXTM3U\n\n")
write_to_stdout = target_file == "-"
m3u_headers = "#EXTM3U\n\n"
if write_to_stdout:
sys.stdout.write(m3u_headers)
else:
with open(target_file, "w") as output_file:
output_file.write(m3u_headers)
videos = []
for n, track in enumerate(tracks, 1):
@@ -147,8 +152,11 @@ class Spotdl:
youtube_url=video["url"],
)
logger.debug(m3u_key.strip())
with open(target_file, "a") as output_file:
output_file.write(m3u_key)
if write_to_stdout:
sys.stdout.write(m3u_key)
else:
with open(target_file, "a") as output_file:
output_file.write(m3u_key)
def download_track(self, track):
logger.info('Downloading "{}"'.format(track))