This document outlines the steps to add functionality for uploading a video and its generated description directly to YouTube.
- Modify
src/cli.rsto accept YouTube-related arguments. - We need arguments for:
--video-file: Path to the video file to upload.--playlist-id: The ID of the YouTube playlist to add the video to.--no-upload: A flag to prevent uploading, allowing the script to only generate local files. This will be the default behavior to maintain existing functionality.
- Create a new file
src/youtube.rs. - This module will encapsulate all logic for interacting with the YouTube API.
- It will handle authentication, token management, video upload, and adding to a playlist.
- Add
oauth2andurlcrates toCargo.toml. - In
youtube.rs, implement a function to handle the Google OAuth 2.0 flow for a desktop application. - The flow should:
- Read
client_secrets.jsonfrom the configuration directory. - Generate an authorization URL and prompt the user to visit it.
- Listen on a local port for the redirect to get the authorization code.
- Exchange the code for an access token and a refresh token.
- Store the token (e.g., in
~/.config/anypod/token.json) for future use. - Implement logic to automatically use the refresh token if the access token has expired.
- Read
- In
youtube.rs, create a functionupload_video. - This function will take the access token, video file path, title, and description as arguments.
- It will perform a
multipart/relatedPOST request to the YouTube Data API v3videos.insertendpoint. - It should return the ID of the newly uploaded video upon success.
- In
youtube.rs, create a functionadd_video_to_playlist. - This function will take the access token, video ID, and playlist ID.
- It will perform a POST request to the
playlistItems.insertendpoint.
- Modify the
mainfunction to incorporate the new upload logic. - After generating the description, check if the
--video-fileand--playlist-idarguments were provided. - If they are, initiate the YouTube upload flow:
- Call the authentication function from
youtube.rsto get a valid token. - Call
upload_videowith the necessary details. - Call
add_video_to_playlistwith the returned video ID. - Print progress and success messages to the console.
- Call the authentication function from
- Decide on a standard location for configuration files (
client_secrets.json,token.json). A good choice would be~/.config/anypod/. - Ensure the application creates this directory if it doesn't exist.
- Add robust error handling for all API interactions.
- Provide clear, user-friendly error messages for common issues like:
client_secrets.jsonnot found.- Invalid token.
- API quota errors.
- File not found for video.
- Add a new section called "YouTube Upload".
- Detail the one-time setup process:
- How to create a Google Cloud project.
- How to enable the YouTube Data API v3.
- How to create OAuth 2.0 credentials for a "Desktop app".
- Where to save the downloaded
client_secrets.json.
- Document the new CLI flags (
--video-file,--playlist-id). - Provide a complete example command for uploading a video.