The AMW YouTube Media Library is a comprehensive system that automatically fetches and displays videos from the AMW YouTube channel. It includes features like genre categorization, filtering, and a custom video player, all designed to showcase AMW's video content in an engaging way.
The library uses the YouTube Data API v3 to fetch videos from the AMW channel. The main functions are:
fetchYouTubeVideos: Fetches videos from the YouTube APIextractGenresFromDescription: Extracts genre tags from video descriptionssyncYouTubeVideos: Syncs videos with the local databaseThe system uses a scheduled deployment job to check for new videos every hour. This is configured in the site scheduler config:
{
"crons": [
{
"path": "/api/cron/youtube-sync",
"schedule": "0 * * * *"
}
]
}The cron job calls the /api/cron/youtube-sync endpoint, which is secured with the CRON_SECRET environment variable. This ensures that only authorized requests can trigger the synchronization.
The YouTube Media Library requires two environment variables:
YOUTUBE_API_KEY: Your YouTube Data API keyCRON_SECRET: Secret key for securing cron job endpointsThe library includes a sophisticated algorithm for extracting genre information from video descriptions:
If you want to add or modify the genres that are extracted from video descriptions, you can edit the extractGenresFromDescription function in lib/youtube-api.ts:
// Function to extract genres from video description
export function extractGenresFromDescription(description: string): VideoGenre[] {
const genreMap: Record<string, VideoGenre> = {
house: "House",
"tech house": "Tech House",
techhouse: "Tech House",
// Add or modify genre mappings here
}
// Rest of the function...
}To change how often the system checks for new videos, modify the schedule property in the scheduler config. The schedule uses the standard cron syntax:
{
"crons": [
{
"path": "/api/cron/youtube-sync",
"schedule": "0 */2 * * *" // Every 2 hours
}
]
}The custom video player can be modified in components/video-player.tsx. You can change the appearance, controls, and behavior of the player.
The main UI components for the YouTube Media Library are:
app/media-library/page.tsx: The main media library pageapp/videos/page.tsx: The videos page with genre filteringcomponents/youtube-gallery/youtube-gallery.tsx: The gallery component used on the home pageYou can test the synchronization manually in several ways:
/api/cron/youtube-sync with the Authorization header set to Bearer YOUR_CRON_SECRET.If you encounter issues with the YouTube Media Library, check the following:
YOUTUBE_API_KEY and CRON_SECRET are set correctly in your deployment environment.Here are some common syntax errors and their solutions:
These often occur when there's a syntax issue in your JSX or JavaScript code:
// Error: Unexpected token '<'
// Problem: Missing import for a component
<SomeComponent /> // SomeComponent is not imported
// Solution:
import { SomeComponent } from '@/components/some-component'
<SomeComponent />
// Error: Unexpected token ')'
// Problem: Mismatched parentheses or brackets
function Component({ prop1, prop2) { // Missing closing curly brace
// Solution:
function Component({ prop1, prop2 }) { // Fixed closing curly braceThese occur when JSX expressions aren't properly formatted:
// Error: Unexpected token, expected "}"
// Problem: Unclosed JSX expression
<div>
{videos.map(video => (
<VideoCard key={video.id} video={video}
))}
</div>
// Solution:
<div>
{videos.map(video => (
<VideoCard key={video.id} video={video} />
))}
</div>If you're still experiencing issues after checking these common problems, try the following:
Required for YouTube API access
Secures the cron job endpoint
These environment variables must be set in your deployment environment settings.