An automated Instagram bot that posts aesthetic science quotes with beautiful gradient backgrounds.
- Generates unique science quotes using Google's Gemini 1.5 Pro API
- Creates beautiful gradient backgrounds with grain effect
- Dynamic text color selection for optimal readability
- Automated posting with randomized schedules
- Configurable posting frequency
- Smart token management to prevent API limits
- Install dependencies:
pip install -r requirements.txt
- Create a
.env
file with the following variables:
GEMINI_API_KEY=your_gemini_api_key
INSTAGRAM_ACCESS_TOKEN=your_instagram_graph_api_token
FACEBOOK_PAGE_ID=your_facebook_page_id
INSTAGRAM_ACCOUNT_ID=your_instagram_business_account_id
POSTS_PER_DAY=1 # or any number you prefer
RESEND_API_KEY=your_resend_api_key
MONITORING_EMAIL=[email protected]
- Run the bot:
python src/main.py
- Create and activate virtual environment:
python -m venv venv
source venv/bin/activate # On Windows: .\venv\Scripts\activate
- Install dependencies:
pip install -r requirements.txt
- Set up environment variables in
.env
:
GEMINI_API_KEY=your_gemini_api_key
INSTAGRAM_ACCESS_TOKEN=your_instagram_graph_api_token
FACEBOOK_PAGE_ID=your_facebook_page_id
INSTAGRAM_ACCOUNT_ID=your_instagram_business_account_id
POSTS_PER_DAY=1
RESEND_API_KEY=your_resend_api_key
[email protected]
# Activate virtual environment (if not already activated)
source venv/bin/activate
# Run test script
cd src
python test_generation.py
If you want to clear the Gemini chat history (useful when quotes start repeating):
cd src
python test_generation.py --purge
- First, test the connection:
cd src
python main.py --test
- Run the bot in production mode:
# Run in background with logging
nohup python main.py > ../logs/instagram_bot.log 2>&1 &
# Or run in foreground
python main.py
- Monitor the logs:
tail -f logs/instagram_bot.log
# Find the process
ps aux | grep "python main.py"
# Kill the process
kill <process_id>
- If quotes start repeating: Run
python test_generation.py --purge
to clear chat history - If Instagram login fails: Wait 24 hours before trying again (Instagram rate limiting)
- If image generation fails: Check if font download is working properly
The bot includes email-based monitoring using the Resend API. To enable monitoring:
- Add these variables to your
.env
:
RESEND_API_KEY=your_resend_api_key
MONITORING_EMAIL=[email protected]
The monitoring system will send emails for:
- Service startup
- Error notifications (rate limited to 1 per hour)
- Service recovery notifications
To run this application as a system service that starts on boot and automatically restarts:
- Copy the service file to systemd:
sudo cp quotable-science.service /etc/systemd/system/
- Reload systemd daemon:
sudo systemctl daemon-reload
- Enable the service to start on boot:
sudo systemctl enable quotable-science
- Start the service:
sudo systemctl start quotable-science
You can check the status of the service using:
sudo systemctl status quotable-science
View logs:
# View application logs
sudo tail -f /var/log/quotable-science.log
# View error logs
sudo tail -f /var/log/quotable-science.error.log
src/main.py
: Main script that orchestrates the entire processsrc/quote_generator.py
: Handles quote generation using Gemini APIsrc/image_generator.py
: Creates beautiful gradient images with quotessrc/instagram_poster.py
: Handles Instagram posting
- Image Size: 1080x1080 pixels
- Font: Playfair Display
- Font Size: 55px
- Side Padding: 130px
- Background: Dynamic grainy gradient
- Text Color: Automatically selected for optimal contrast
The ImageGenerator
class uses several parameters that can be tuned to customize the image output:
WIDTH
,HEIGHT
: 1080x1080 pixels (Instagram square format)PADDING
: 130 pixels (space between text and image edge)
FONT_SIZE
: 55 pixelsLINE_SPACING
: 55 pixels (space between quote and author)- Font: Playfair Display (downloaded dynamically)
scale
: 4.0 (controls size of blob patterns - higher = larger blobs)octaves
: 2 (number of noise layers - higher = more detail)persistence
: 0.5 (how much each octave contributes)lacunarity
: 2.0 (how frequency increases with each octave)sigma
: 30 (gaussian blur intensity - higher = smoother transitions)grain
: 0.015 (noise intensity - higher = more grainy texture)
- Uses 3 random colors for rich, varied gradients
- Colors are mixed using normalized Perlin noise bases
- Each color gets its own noise layer offset by 5 units
- Colors are blended smoothly using gaussian blur
- Final image includes subtle grain effect for texture
- Quotes are wrapped in quotation marks
- Authors are prefixed with "~" and end with a period
- Text color adapts to background brightness
- Text is center-aligned both horizontally and vertically
-
For larger, smoother blobs:
- Increase
scale
- Decrease
octaves
- Increase
sigma
- Increase
-
For more detailed, intricate patterns:
- Decrease
scale
- Increase
octaves
- Decrease
sigma
- Decrease
-
For color variation:
- Modify the color generation logic in
generate_blobby_gradient()
- Adjust the offset between noise layers (currently 5)
- Modify the color generation logic in
-
For texture:
- Adjust
grain
value (0.01-0.02 recommended range) - Modify gaussian blur
sigma
(20-50 recommended range)
- Adjust
- google-generativeai: For quote generation
- Pillow: Image processing
- numpy: Numerical operations
- instabot: Instagram API interaction
- python-dotenv: Environment variable management
- noise: Perlin noise for grain effect
- colorthief: Color analysis
- apscheduler: Scheduling posts
This bot uses the Instagram API through instabot. Be aware that Instagram's API policies may change, and excessive automation might lead to account restrictions. Use responsibly and consider Instagram's terms of service.