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 all 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
9 changes: 5 additions & 4 deletions youtube_bz/commands/download.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import asyncio
from typing import Optional

import aiohttp
import pytube
Expand Down Expand Up @@ -50,13 +51,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 +70,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 +93,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
)
9 changes: 5 additions & 4 deletions youtube_bz/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@ def print_version() -> None:
async def run_command(args: argparse.Namespace):
verbose = args.verbose if "verbose" in args else False
if args.command == "download":
await commands.download(
args.mbid,
verbose,
)
await commands.download(args.mbid, verbose, args.destination)
else:
print(f"Unknown command {args.command}")

Expand All @@ -43,6 +40,10 @@ 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(
"-d", "--destination", help="Path to the output directory"
)

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

return parser
Expand Down