This project is just a fun idea experiment to create an AI-powered bot designed to automate engagement with comments on a YouTube channel. The bot simulates human-like behavior by fetching new comments, applying randomized delays, and generating context-aware replies using Google's Gemini API. The system is built to be compliant with YouTube's API policies, avoiding robotic patterns and ensuring a natural interaction with the audience.
- Automated Comment Fetching: Regularly polls for new comments on a specified YouTube channel.
- Human-Like Delays: Implements randomized delays for replying (60-240 seconds) to comments to avoid detection as a bot and simulate natural behavior.
- State Persistence: Uses an SQLite database to track processed comments, ensuring that each comment is processed only once.
- Automatic Database Cleanup: A background task runs periodically to delete records older than 24 hours, keeping the database size manageable.
- AI-Powered Replies: Leverages the Google Gemini API (via
google-genaiSDK) to generate high-quality, context-aware replies using the latest Flash model. - Rate Limit Handling:
- Rotational Keys: Supports multiple Gemini API keys to distribute load.
- Staggered Processing: Automatically staggers the processing of multiple comments (with 10-second delays) to respect Gemini API Free Tier rate limits and prevent 429 errors.
- Safety and Filtering: Includes a filtering system to ignore spam, short, or inappropriate comments.
- YouTube API Compliance: Note that "liking" comments is not supported by the public YouTube Data API v3, so this bot focuses purely on high-quality replies.
- Modular Architecture: The codebase is organized into distinct modules for easy maintenance.
- Configuration Management: Sensitive info managed via
.envandcredentials.json.
.
├── src
│ ├── __init__.py
│ ├── config.py
│ ├── database.py
│ ├── youtube_api.py
│ ├── gemini_api.py
│ ├── comment_filter.py
│ ├── scheduler.py
│ └── main.py
├── .env
├── .gitignore
├── requirements.txt
└── README.md
- Python 3.10 or higher
- A Google Cloud Platform project with the YouTube Data API v3 enabled
- One or more Gemini API keys (Free Tier compatible)
- OAuth 2.0 credentials for the YouTube Data API v3 (
credentials.json)
-
Clone the repository:
git clone <repository-url> cd <repository-directory>
-
Create and activate a virtual environment:
python3 -m venv venv source venv/bin/activate -
Install the dependencies:
pip install -r requirements.txt
-
Create a
.envfile in the root directory of the project and add the following environment variables:YOUTUBE_API_KEY="your_youtube_api_key" CHANNEL_ID="your_youtube_channel_id" # Add one or more Gemini API keys, separated by commas GEMINI_API_KEYS="your_gemini_api_key_1,your_gemini_api_key_2" -
Set up OAuth 2.0 credentials:
- Follow the instructions in the Google Cloud documentation to create OAuth 2.0 credentials.
- Download the
credentials.jsonfile and place it in the root directory of the project.
To start the bot, run the main.py script from the src directory:
python -m src.mainThe bot will start polling for new comments on the configured YouTube channel and will handle all engagement activities automatically.
This project is licensed under the MIT License. See the LICENSE file for more details.