Skip to content
This repository has been archived by the owner on Jul 19, 2024. It is now read-only.

feat: --destination option #24

Merged
merged 6 commits into from
Sep 1, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions youtube_bz/commands/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
from youtube_bz.exceptions import YouTubeBrainzError
from youtube_bz.utils.levenshtein_distance import levenshtein_distance

from typing import Optional


async def get_best_match(release: MusicBrainzAPI.Release, track: MusicBrainzAPI.Track):
"""Get YouTube video corresponding to MusicBrainz track."""
Expand Down Expand Up @@ -50,13 +52,13 @@ async def get_best_match(release: MusicBrainzAPI.Release, track: MusicBrainzAPI.
return None


def download_video_audio(title: str, video_id: str):
def download_video_audio(title: str, video_id: str, destination: Optional[str] = None):
"""Download audio of a YouTube video."""
print("[Downloading] {} : {}".format(title, video_id))
if stream := pytube.YouTube(
f"http://youtube.com/watch?v={video_id}"
).streams.get_audio_only():
stream.download()
stream.download(output_path=destination)
print("[Downloaded] {}".format(title))


Expand All @@ -69,7 +71,7 @@ def generate_search_query(
)


async def download(mbid: str, verbose: bool):
async def download(mbid: str, verbose: bool, destination: Optional[str] = None):
# Get release info from MusicBrainz
musicbrainz_client = await MusicBrainzAPI.Client.new()

Expand All @@ -92,5 +94,5 @@ async def download(mbid: str, verbose: bool):
if result:
loop = asyncio.get_running_loop()
loop.run_in_executor(
None, download_video_audio, result["title"], result["id"]
None, download_video_audio, result["title"], result["id"], destination
)
3 changes: 3 additions & 0 deletions youtube_bz/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ async def run_command(args: argparse.Namespace):
await commands.download(
args.mbid,
verbose,
destination=args.destination
)
else:
print(f"Unknown command {args.command}")
Expand All @@ -43,6 +44,8 @@ def get_command_parser() -> argparse.ArgumentParser:
download_parser.add_argument("mbid", help="music brainz identifer of a release")
download_parser.add_argument("--verbose", action="store_true")

download_parser.add_argument("--destination", help="Path to the output directory")

parser.add_argument("--version", action="store_true", help="Print version and exit")

return parser
Expand Down