KaraokeVideoDownloader/example_custom_data_directory.py

199 lines
6.9 KiB
Python

#!/usr/bin/env python3
"""
Example: Using Karaoke Downloader with Custom Data Directory
This example demonstrates how to integrate the karaoke downloader into another project
with a different data directory structure.
"""
import os
import tempfile
from pathlib import Path
from karaoke_downloader.data_path_manager import get_data_path_manager
from karaoke_downloader.config_manager import get_config_manager
from karaoke_downloader.downloader import KaraokeDownloader
def example_custom_data_directory():
"""Example of using the karaoke downloader with a custom data directory."""
print("🚀 Example: Custom Data Directory Integration")
print("=" * 50)
# Create a temporary directory to simulate a different project structure
with tempfile.TemporaryDirectory() as temp_dir:
project_root = Path(temp_dir) / "my_karaoke_project"
project_root.mkdir(exist_ok=True)
# Set up custom data directory structure
custom_data_dir = project_root / "karaoke_data"
custom_data_dir.mkdir(exist_ok=True)
print(f"📁 Project root: {project_root}")
print(f"📁 Custom data directory: {custom_data_dir}")
# Create a custom config file (in the custom data directory for this example)
config_file = custom_data_dir / "config.json"
config_data = {
"folder_structure": {
"data_dir": str(custom_data_dir),
"downloads_dir": str(project_root / "downloads"),
"logs_dir": str(project_root / "logs")
},
"download_settings": {
"preferred_resolution": "720p"
}
}
import json
with open(config_file, 'w') as f:
json.dump(config_data, f, indent=2)
print(f"📄 Created config file: {config_file}")
# Example 1: Using data path manager with custom directory
print("\n📋 Example 1: Data Path Manager")
data_path_manager = get_data_path_manager(str(custom_data_dir))
print(f" Data directory: {data_path_manager.data_dir}")
print(f" Songlist path: {data_path_manager.get_songlist_path()}")
print(f" Channels path: {data_path_manager.get_channels_json_path()}")
# Example 2: Using config manager with custom directory
print("\n📋 Example 2: Config Manager")
config_manager = get_config_manager(str(custom_data_dir))
config = config_manager.get_config()
print(f" Config loaded from: {config_manager.config_file}")
print(f" Downloads directory: {config.folder_structure.downloads_dir}")
print(f" Logs directory: {config.folder_structure.logs_dir}")
print(f" Resolution: {config.download_settings.preferred_resolution}")
# Example 3: Using downloader with custom directory
print("\n📋 Example 3: Karaoke Downloader")
try:
downloader = KaraokeDownloader()
print(f" Downloader initialized successfully")
print(f" Downloads directory: {downloader.downloads_dir}")
print(f" Logs directory: {downloader.logs_dir}")
except Exception as e:
print(f" Downloader initialization failed (expected): {e}")
# Example 4: Creating sample data files
print("\n📋 Example 4: Sample Data Files")
# Create a sample channels file
channels_file = data_path_manager.get_channels_json_path()
channels_data = {
"channels": [
{
"name": "SingKingKaraoke",
"url": "https://www.youtube.com/@SingKingKaraoke/videos",
"parsing_rules": {
"format": "artist_title_separator",
"separator": " - ",
"artist_first": True
}
}
]
}
with open(channels_file, 'w') as f:
json.dump(channels_data, f, indent=2)
print(f" Created channels file: {channels_file}")
# Create a sample songlist file
songlist_file = data_path_manager.get_songlist_path()
songlist_data = [
{
"title": "Sample Playlist",
"songs": [
{"artist": "Artist 1", "title": "Song 1", "position": 1},
{"artist": "Artist 2", "title": "Song 2", "position": 2}
]
}
]
with open(songlist_file, 'w') as f:
json.dump(songlist_data, f, indent=2)
print(f" Created songlist file: {songlist_file}")
# List all files in the custom data directory
print(f"\n📋 Files in custom data directory:")
for file_path in custom_data_dir.iterdir():
if file_path.is_file():
print(f" - {file_path.name}")
print(f"\n✅ Example completed successfully!")
print(f"📁 All data files are in: {custom_data_dir}")
def example_integration_pattern():
"""Example of integration pattern for other projects."""
print("\n🔧 Integration Pattern for Other Projects")
print("=" * 50)
print("""
# Integration Pattern:
1. Set up your project structure:
my_project/
├── karaoke_data/ # Custom data directory
│ ├── config.json # Configuration
│ ├── channels.json # Channel definitions
│ ├── songList.json # Song lists
│ └── ...
├── downloads/ # Downloaded videos
├── logs/ # Log files
└── main.py # Your main application
2. Initialize with custom data directory:
```python
from karaoke_downloader.data_path_manager import get_data_path_manager
from karaoke_downloader.downloader import KaraokeDownloader
# Set up custom data directory
custom_data_dir = "path/to/your/karaoke_data"
# Get data path manager
data_path_manager = get_data_path_manager(custom_data_dir)
# Initialize downloader (it will use the custom data directory)
downloader = KaraokeDownloader()
# Use the downloader
downloader.download_songlist_across_channels(
channel_urls=["https://www.youtube.com/@SingKingKaraoke/videos"],
limit=5
)
```
3. Configuration file (config.json in root, or karaoke_data/config.json for custom data directory):
```json
{
"folder_structure": {
"data_dir": "path/to/your/karaoke_data",
"downloads_dir": "path/to/your/downloads",
"logs_dir": "path/to/your/logs"
},
"download_settings": {
"preferred_resolution": "720p"
}
}
```
""")
def main():
"""Run the examples."""
example_custom_data_directory()
example_integration_pattern()
if __name__ == "__main__":
main()