Skip to content

Commit

Permalink
Refactor process_video and process_playlist functions
Browse files Browse the repository at this point in the history
  • Loading branch information
mauriciobellon committed Jul 12, 2024
1 parent e910139 commit 1a91561
Show file tree
Hide file tree
Showing 10 changed files with 745 additions and 169 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
data/
__pycache__/
cookies.txt
cookies.txt
.env
447 changes: 446 additions & 1 deletion poetry.lock

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ pydub = "^0.25.1"
youtube-transcript-api = "^0.6.2"

pytest = "^8.2.2"
python-dotenv = "^1.0.1"
aiohttp = "^3.9.5"
tenacity = "^8.5.0"
[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
Expand Down
31 changes: 10 additions & 21 deletions youtubed/main.py
Original file line number Diff line number Diff line change
@@ -1,38 +1,28 @@
import os
import sys
import click
from typing import Callable
import asyncio
from typing import Callable, Coroutine

from .utils.process_playlist import process_playlist
from .utils.process_video import process_video
from .utils.process import playlist_processor
from .utils.process import video_processor
from .utils.help_box import help_box


def ensure_output_directory(path: str) -> None:
"""Create the output directory if it doesn't exist."""
os.makedirs(path, exist_ok=True)


def get_processor(url: str) -> Callable:
"""Return the appropriate processor function based on the URL."""
def get_processor(url: str) -> Coroutine:
if url.startswith('https://www.youtube.com/playlist'):
return process_playlist
return playlist_processor(url)
elif url.startswith('https://www.youtube.com/watch'):
return process_video
return video_processor(url)
else:
raise ValueError("Invalid URL. Please provide a valid YouTube video or playlist URL.")


@click.command()
@click.argument('url')
def youtubed(url: str) -> None:
"""Process YouTube videos or playlists."""
output_base_path = 'data'
ensure_output_directory(output_base_path)

try:
processor = get_processor(url)
processor(url, output_base_path)
coroutine = get_processor(url)
asyncio.run(coroutine)
except ValueError as e:
click.echo(str(e), err=True)
sys.exit(1)
Expand All @@ -42,5 +32,4 @@ def youtubed(url: str) -> None:


if __name__ == '__main__':
help_box()
youtubed()
help_box()
17 changes: 0 additions & 17 deletions youtubed/utils/audio_extractor.py

This file was deleted.

50 changes: 0 additions & 50 deletions youtubed/utils/downloader.py

This file was deleted.

Loading

0 comments on commit 1a91561

Please sign in to comment.