code it
- User
code it
Assistant
Let's start with the basic structure and implement it piece by piece. We'll begin with the project setup, configuration, and core CLI structure.
First, let's create a new directory for the project and set up the basic structure:
youtube-dl-organizer/ ├── pyproject.toml ├── config.yaml ├── youtube_dl_organizer/ │ ├── __init__.py │ ├── cli.py │ ├── config.py │ ├── downloader.py │ ├── categorizer.py │ └── utils.py └── README.mdFirst, let's create
pyproject.tomlfor dependency management:[tool.poetry] name = "youtube-dl-organizer" version = "0.1.0" description = "A CLI tool for downloading and organizing YouTube videos" authors = ["Your Name <your.email@example.com>"] [tool.poetry.dependencies] python = "^3.9" click = "^8.1.7" pyyaml = "^6.0.1" yt-dlp = "^2023.10.13" rich = "^13.6.0" openai = "^1.0.0" python-dotenv = "^1.0.0" [tool.poetry.scripts] ytorg = "youtube_dl_organizer.cli:cli"Let's start with the configuration module. Here's
youtube_dl_organizer/config.py:from dataclasses import dataclass from datetime import datetime from pathlib import Path from typing import List, Optional import yaml from rich.console import Console console = Console() @dataclass class Source: type: str url: str last_synced: datetime category: str @classmethod def from_dict(cls, data: dict) -> 'Source': return cls( type=data['type'], url=data['url'], last_synced=datetime.fromisoformat(data['last_synced']), category=data['category'] ) @dataclass class Config: debug: bool media_root: Path subtitle_languages: List[str] yt_dlp_options: List[str] sources: List[Source] @classmethod def load(cls, config_path: Path) -> 'Config': try: with open(config_path, 'r') as f: data = yaml.safe_load(f) return cls( debug=data.get('debug', False), media_root=Path(data['media_root']).expanduser(), subtitle_languages=data.get('subtitle_languages', ['en']), yt_dlp_options=data.get('yt_dlp_options', []), sources=[Source.from_dict(s) for s in data.get('sources', [])] ) except Exception as e: console.print(f"[red]Error loading config file: {e}[/red]") raise def save(self, config_path: Path) -> None: """Save the current configuration back to the file.""" data = { 'debug': self.debug, 'media_root': str(self.media_root), 'subtitle_languages': self.subtitle_languages, 'yt_dlp_options': self.yt_dlp_options, 'sources': [ { 'type': s.type, 'url': s.url, 'last_synced': s.last_synced.isoformat(), 'category': s.category } for s in self.sources ] } try: with open(config_path, 'w') as f: yaml.safe_dump(data, f, default_flow_style=False) except Exception as e: console.print(f"[red]Error saving config file: {e}[/red]") raiseNow let's create the main CLI module,
youtube_dl_organizer/cli.py:from pathlib import Path from typing import List, Optional import click from rich.console import Console from rich.table import Table from .config import Config from .downloader import VideoDownloader from .categorizer import Categorizer console = Console() @click.group() def cli(): """YouTube Download Organizer - Download and organize YouTube videos.""" pass @cli.command() @click.option( '--config', type=click.Path(exists=True, dir_okay=False, path_type=Path), default='config.yaml', help='Path to configuration file' ) @click.option( '--override-sources', multiple=True, help='Override sources from config with these URLs' ) @click.option( '--no-verify', is_flag=True, help='Skip verification step and start downloading immediately' ) def sync(config: Path, override_sources: List[str], no_verify: bool): """Synchronize videos from configured sources.""" try: # Load configuration config_obj = Config.load(config) # Initialize components downloader = VideoDownloader(config_obj) categorizer = Categorizer(config_obj) # If override_sources is provided, use those instead of config sources if override_sources: console.print("[yellow]Using override sources instead of config sources[/yellow]") # TODO: Implement override sources handling # Get new videos console.print("[green]Fetching new videos from sources...[/green]") videos = downloader.get_new_videos() if not videos: console.print("[yellow]No new videos found.[/yellow]") return # Categorize videos console.print("[green]Categorizing videos...[/green]") categorized_videos = categorizer.categorize_videos(videos) # Display results table = Table(show_header=True, header_style="bold magenta") table.add_column("Title") table.add_column("Category") table.add_column("URL") for video in categorized_videos: table.add_row( video.title, video.category, video.url ) console.print(table) # Verification step if not no_verify: # TODO: Implement interactive verification pass # Download videos console.print("[green]Starting downloads...[/green]") downloader.download_videos(categorized_videos) # Update last_synced dates config_obj.save(config) console.print("[green]Sync completed successfully![/green]") except Exception as e: console.print(f"[red]Error during sync: {e}[/red]") raise @cli.command() @click.argument('video_title') @click.option( '--category', help='Manually specify category path' ) def categorize(video_title: str, category: Optional[str]): """Manually categorize a video title.""" # TODO: Implement manual categorization pass if __name__ == '__main__': cli()Let's create a basic version of the downloader,
youtube_dl_organizer/downloader.py:from dataclasses import dataclass from datetime import datetime from pathlib import Path from typing import List, Optional import yt_dlp from rich.console import Console from rich.progress import Progress from .config import Config console = Console() @dataclass class Video: title: str url: str upload_date: datetime category: Optional[str] = None class VideoDownloader: def __init__(self, config: Config): self.config = config self.ydl_opts = { 'quiet': not config.debug, 'format': 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/mp4', 'writesubtitles': True, 'writeautomaticsub': True, 'subtitleslangs': config.subtitle_languages, 'embedsubs': True, } def get_new_videos(self) -> List[Video]: """Fetch new videos from all sources.""" videos = [] for source in self.config.sources: try: with yt_dlp.YoutubeDL(self.ydl_opts) as ydl: # Get video information info = ydl.extract_info(source.url, download=False) # Handle both single videos and playlists entries = info.get('entries', [info]) for entry in entries: upload_date = datetime.strptime( entry['upload_date'], '%Y%m%d' ) # Skip videos older than last_synced if upload_date <= source.last_synced: continue videos.append(Video( title=entry['title'], url=entry['webpage_url'], upload_date=upload_date )) except Exception as e: console.print(f"[red]Error fetching videos from {source.url}: {e}[/red]") if self.config.debug: raise return videos def download_videos(self, videos: List[Video]): """Download a list of videos.""" with Progress() as progress: task = progress.add_task("[green]Downloading videos...", total=len(videos)) for video in videos: try: if not video.category: console.print(f"[yellow]Warning: No category for {video.title}, skipping[/yellow]") continue # Create category directory if it doesn't exist output_dir = self.config.media_root / video.category output_dir.mkdir(parents=True, exist_ok=True) # Configure output template opts = dict(self.ydl_opts) opts['outtmpl'] = str(output_dir / '%(title)s.%(ext)s') # Download video with yt_dlp.YoutubeDL(opts) as ydl: ydl.download([video.url]) except Exception as e: console.print(f"[red]Error downloading {video.title}: {e}[/red]") if self.config.debug: raise progress.advance(task)This is the basic structure. We still need to implement:
- The
categorizer.pymodule with LLM integration - The interactive verification step
- The manual categorization command
- Better error handling and retry logic
- The override sources functionality
Would you like me to continue with any of these components next? Also, let me know if you want to make any adjustments to what I've implemented so far.
- The
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.