This Go application fetches the latest video from a specified YouTube channel and downloads it using yt-dlp
. It tracks the last downloaded video date to avoid duplicate downloads. The application can be dockerized and scheduled to run on a daily basis using a cron job.
Ensure you have the following installed on your local machine:
- Go (latest version)
- Docker
- ffmpeg
- curl
To run this application, you need to set the following environment variable:
YOUTUBE_API_KEY
: Your YouTube API key.
- Clone this repository to your local machine.
- Navigate to the project directory.
- Run
go mod download
to install the necessary Go packages. - Set up the YouTube API key as an environment variable.
- Build the Go application using the command
go build -o main
. - Build the Docker image using the command
docker build -t youtube-video-downloader .
. - Run the application with
./main
or use Docker to run the application (see instructions below).
- Set the channel ID in the
main()
function to your desired YouTube channel. - Run the application.
- The application will fetch and download the latest video from the specified channel to the
videos
directory in the project.
- First, ensure that the Docker daemon is running on your system.
- Build the Docker image using the following command in the project directory:
docker build -t youtube-video-downloader .
- To run the container daily, you can set up a cron job. Open the crontab file with the command:
crontab -e
- Add the following line to run the container daily at a specific time (e.g., at 2 am):
0 0,12 * * * docker run -e YOUTUBE_API_KEY='your-youtube-api-key' -d my-golang-app
- Save and exit the crontab file. The docker container will now run daily at the specified time.
- You can run it once now:
0 0,12 * * * docker run -e YOUTUBE_API_KEY='your-youtube-api-key' -d my-golang-app
. It will appear in Docker's desktop app where you can run it - If you want to mount storage to the Docker automation:
docker run -e YOUTUBE_API_KEY='your-youtube-api-key' -v /yourpathyouwantToSave:/app/videos -d falcony
- Checks and creates a
videos
directory if not exists. - Retrieves the channel ID and API key.
- Initializes a YouTube service.
- Fetches the latest video from the specified channel.
- Compares the latest video's publish date with the last download date to prevent duplicate downloads.
- Downloads the video using
yt-dlp
if a new video is found. - Updates the last download date.
The application contains robust error handling to manage issues like:
- Failure to read the YouTube API key from environment variables.
- Errors during YouTube service initialization.
- Errors during API calls to fetch video details.
- Issues during video download.
- Failures to read or write the last download date.
Feel free to contribute to this project by opening issues or submitting pull requests.
This project is open-source and available under the MIT License.
Feel free to contribute to this project by opening issues or submitting pull requests.
This project is open-source and available under the MIT License.