fetchYouTubeVideos.js utility provides functions for fetching video data from the Lake Ozark Christian Church YouTube channel, parsing dates from video titles, and managing custom thumbnails.
Overview
This utility:- Fetches videos from the church’s YouTube uploads playlist
- Parses dates from video titles in multiple formats
- Filters and sorts videos by date
- Manages custom thumbnail URLs for specific videos
- Provides fallback data on API failures
~/workspace/source/src/utils/fetchYouTubeVideos.js
Exported Functions
fetchLatestVideos()
Fetches the latest videos from the church YouTube channel, filters for service videos, and returns them sorted by date.Custom Thumbnail Mapping
The utility includes a custom thumbnail map for videos that should use specific thumbnail images instead of YouTube’s default:- Upload thumbnail to CDN:
https://cdn.lakeozarkdisciples.org/images/ - Add video ID and thumbnail URL to
customThumbnailMap - Video will automatically use custom thumbnail when fetched
Date Parsing
The utility includes a sophisticated date parser that handles multiple title formats:Supported Date Formats
| Format | Example | Pattern |
|---|---|---|
| Compact MMDDYY | ”103022” | 6-digit number |
| Compact MDDYY | ”10322” | 5-digit number |
| Slash format | ”10/30/22” | MM/DD/YY or MM/DD/YYYY |
| Dash format | ”10-30-22” | MM-DD-YY or MM-DD-YYYY |
| Full month | ”October 30, 2022” | Month DD, YYYY |
| Full month (no comma) | “October 30 2022” | Month DD YYYY |
| Ordinal suffix | ”October 30th, 2022” | Month DDth, YYYY |
| Abbreviated month | ”Oct 30, 2022” | Mon DD, YYYY |
parseDateFromTitle() (Internal)
While not exported, this internal function is crucial for video sorting:Video Filtering
Videos are filtered to include only church service videos:Video Sorting
Videos are sorted by parsed date (most recent first):YouTube API Integration
The utility uses theyoutube-sr package to fetch videos:
UUUcLKfZo5Su6-ypmt1dkPKA (Church uploads)
Error Handling & Fallback
The utility includes comprehensive error handling:- Logs error to console
- Returns empty array (graceful degradation)
- Allows page to render without videos
Processing Pipeline
Here’s the complete video processing flow:API Endpoint Implementation
The utility is typically used in an API endpoint with caching:- 5-minute cache (max-age)
- 10-minute stale-while-revalidate
- Reduces YouTube API calls
- Improves performance
Client-Side Usage
Fetch videos from the API endpoint:Return Value Structure
Example return value:Testing Notes
The code includes a commented-out dummy video for testing:Dependencies
Required packages:Performance Considerations
- API Caching: Use the API endpoint pattern with cache headers
- Fallback Data: Always return empty array on error (graceful degradation)
- Pagination Fallback: Manual fetch() if fetchAll fails
- Date Parsing: Efficient regex patterns, stops at first match
Maintenance
Regular tasks:- Update custom thumbnails: Add new video IDs to
customThumbnailMap - Exclude videos: Add IDs to
excludedVideoIdsarray - Monitor errors: Check console logs for API failures
- Update playlist ID: If channel uploads playlist changes
- Channel ID starts with
UC - Uploads playlist: Replace
UCwithUU - Example:
UCUcLKfZo5Su6-ypmt1dkPKA→UUUcLKfZo5Su6-ypmt1dkPKA