# YouTube-Directus Automation Service Automatically extract YouTube video thumbnails and upload them to Directus when YouTube videos are created or updated in your Directus CMS. ## ๐ŸŽฏ MVP Features - **Webhook Integration**: Receives Directus webhooks on item create/update events - **YouTube Processing**: Extracts video IDs from various YouTube URL formats - **Thumbnail Extraction**: Downloads best quality thumbnail (maxres โ†’ high โ†’ medium โ†’ default) - **Directus Upload**: Uploads thumbnails to Directus files collection - **Item Association**: Updates YouTube video items with thumbnail file reference - **Error Handling**: Robust error handling with logging and fallback mechanisms - **Security**: Optional webhook signature verification ## ๐Ÿ—๏ธ Architecture ``` [Directus CMS] โ†’ [Webhook] โ†’ [FastAPI Service] โ†’ [YouTube API] โ†’ [Thumbnail Download] โ†“ [Directus Item Update] โ† [File Upload] โ† [Image Processing] โ† [Quality Selection] ``` ## ๐Ÿš€ Quick Start ### 1. Setup Environment ```bash # Copy environment template cp .env.example .env # Edit .env with your configuration nano .env ``` Required configuration: ```bash DIRECTUS_URL="https://your-directus-instance.com/" DIRECTUS_TOKEN="your_directus_api_token" ``` Optional but recommended: ```bash YOUTUBE_API_KEY="your_youtube_api_key" # For better reliability DIRECTUS_WEBHOOK_SECRET="your_webhook_secret" # For security ``` ### 2. Start the Service ```bash # Install dependencies and start service ./start.sh ``` The service will start on `http://localhost:8000` ### 3. Test the Service ```bash # Run basic service tests python3 test_service.py ``` ### 4. Configure Directus Webhook In your Directus admin panel: 1. Go to **Settings** โ†’ **Webhooks** 2. Create a new webhook: - **Name**: YouTube Thumbnail Automation - **Method**: POST - **URL**: `http://localhost:8000/webhook/directus` (or your deployed service URL) - **Status**: Active - **Collections**: Select your media_items collection - **Actions**: Create, Update ## ๐Ÿ”ง Configuration ### Environment Variables | Variable | Required | Description | |----------|----------|-------------| | `DIRECTUS_URL` | โœ… | Your Directus instance URL | | `DIRECTUS_TOKEN` | โœ… | Directus API token with file upload permissions | | `YOUTUBE_API_KEY` | โš ๏ธ | YouTube Data API v3 key (recommended) | | `DIRECTUS_WEBHOOK_SECRET` | โš ๏ธ | Secret for webhook signature verification | | `PORT` | โŒ | Service port (default: 8000) | ### YouTube API Setup (Optional but Recommended) 1. Go to [Google Cloud Console](https://console.cloud.google.com/) 2. Create a new project or select existing 3. Enable **YouTube Data API v3** 4. Create API credentials (API Key) 5. Add the API key to your `.env` file **Benefits of YouTube API:** - Better error handling for private/deleted videos - Access to video metadata - More reliable thumbnail URLs - Rate limit information **Without YouTube API:** - Service uses fallback thumbnail URLs - Works for most public videos - Less error information available ## ๐Ÿ“‹ Collection Schema Requirements Your Directus collection should have: **Required Fields:** - A field containing YouTube URLs (detected automatically from common field names) - A file field for thumbnail association (assumes `thumbnail` field name) **Supported YouTube URL Field Names:** - `youtube_url` - `url` - `link` - `video_url` - `youtube_link` **Example Collection Schema:** ```sql -- media_items collection id (Primary Key) title (String) youtube_url (String) -- YouTube URL field thumbnail (File) -- Thumbnail association description (Text) created_at (DateTime) updated_at (DateTime) ``` ## ๐Ÿงช Testing ### Manual Testing 1. Start the service: `./start.sh` 2. Run tests: `python3 test_service.py` 3. Check service health: `curl http://localhost:8000/health` ### Test with Real Directus 1. Create a new item in your media_items collection 2. Add a YouTube URL (e.g., `https://www.youtube.com/watch?v=dQw4w9WgXcQ`) 3. Check service logs for processing status 4. Verify thumbnail appears in Directus ### Webhook Testing ```bash # Test webhook endpoint directly curl -X POST http://localhost:8000/webhook/directus \ -H "Content-Type: application/json" \ -d '{ "event": "items.create", "payload": { "id": "test-123", "youtube_url": "https://www.youtube.com/watch?v=dQw4w9WgXcQ" }, "keys": ["test-123"] }' ``` ## ๐Ÿ“Š Monitoring ### Service Endpoints - `GET /` - Service information - `GET /health` - Health check with configuration status - `POST /webhook/directus` - Webhook endpoint for Directus ### Health Check Response ```json { "status": "healthy", "service": "youtube-directus-automation", "timestamp": "2025-01-11T10:30:00Z", "config": { "directus_url": "https://your-instance.com/", "has_directus_token": true, "has_youtube_api_key": false, "has_webhook_secret": false } } ``` ### Logging Service logs include: - Webhook event processing - YouTube video ID extraction - Thumbnail download status - Directus upload results - Error details and stack traces ## ๐Ÿ”’ Security ### Webhook Signature Verification Enable webhook security by setting `DIRECTUS_WEBHOOK_SECRET`: 1. Generate a secure random secret 2. Add to `.env`: `DIRECTUS_WEBHOOK_SECRET="your-secret-here"` 3. Configure the same secret in Directus webhook settings 4. Service will verify HMAC-SHA256 signatures automatically ### API Token Permissions Your Directus token needs: - Read access to your YouTube videos collection - Create/Upload access to files collection - Update access to your YouTube videos collection ## ๐Ÿšจ Troubleshooting ### Common Issues **โŒ "DIRECTUS_TOKEN not set" error** - Add your Directus API token to `.env` - Verify token has correct permissions **โŒ "Could not extract video ID" error** - Check YouTube URL format - Ensure URL field contains valid YouTube URLs **โŒ "Failed to download thumbnail" error** - Video might be private/deleted - Check internet connectivity - Try adding YouTube API key for better error handling **โŒ "Directus upload failed" error** - Check Directus token permissions - Verify Directus instance is accessible - Check Directus storage configuration **โŒ Webhook not triggering** - Verify webhook URL is correct - Check Directus webhook configuration - Ensure service is running and accessible - Check webhook secret configuration ### Debug Mode For detailed debugging, check service logs when running `./start.sh`. ## ๐Ÿ› ๏ธ Development ### Project Structure ``` youtube-automation/ โ”œโ”€โ”€ src/ โ”‚ โ””โ”€โ”€ main.py # Main FastAPI application โ”œโ”€โ”€ requirements.txt # Python dependencies โ”œโ”€โ”€ .env.example # Environment template โ”œโ”€โ”€ .env # Your configuration (not in git) โ”œโ”€โ”€ start.sh # Service startup script โ”œโ”€โ”€ test_service.py # Test suite โ”œโ”€โ”€ README.md # This file โ””โ”€โ”€ .taskmaster/ # Task Master project management ``` ### Extending the Service The service is built with extensibility in mind: - Add new webhook endpoints in `main.py` - Extend `YouTubeProcessor` class for additional features - Add new thumbnail processing logic - Implement additional file format support ### Task Master Integration This project uses Task Master for project management: ```bash # View project tasks task-master list # Get next task task-master next # Update task status task-master set-status --id=1 --status=done ``` ## ๐Ÿค Contributing 1. Check existing tasks: `task-master list` 2. Pick a task: `task-master next` 3. Make changes and test: `python3 test_service.py` 4. Update task status: `task-master set-status --id=X --status=done` ## ๐Ÿ“„ License This project is part of the Personal AI Assistant ecosystem. See main project license. --- **๐ŸŽ‰ Happy automating!** If you run into issues, check the troubleshooting section or create a task in Task Master.