An intelligent Twitter automation system that crawls BBC news, generates engaging tweets using AI, and automatically posts them to Twitter/X. This project combines web scraping, natural language processing, and social media automation into a comprehensive news-to-tweet pipeline.
- π° Smart News Crawling: Automated BBC news article discovery and extraction
- π§ AI-Powered Content Generation: Uses Ollama (Llama 3.2) for tweet creation and Google Gemini for hashtag generation
- π― Intelligent Tweet Optimization: Sentiment analysis, urgency detection, and topic extraction
- π Automated Social Media Posting: Selenium-based Twitter/X posting with multiple fallback methods
- π Analytics & Insights: Comprehensive data collection and analysis
- β‘ Parallel Processing: Multi-threaded crawling and processing for efficiency
- π‘οΈ Robust Error Handling: Multiple retry mechanisms and failsafes
twitter-news-bot/
βββ bbc_crawler.py # BBC news crawler with AI analysis
βββ tweet_generator.py # AI-powered tweet and hashtag generation
βββ twitter_bot.py # Automated Twitter posting bot
βββ requirements.txt # Python dependencies
βββ README.md # This file
βββ output/
βββ bbc_improved_data.json # Crawled news data
βββ generated_tweets.json # Generated tweets with hashtags
βββ twitter_bot.log # Bot execution logs
- Python 3.8+
- Ollama with Llama 3.2 model
- Google Gemini API Key
- Chrome Browser and ChromeDriver
- Twitter/X Account
-
Clone the repository
https://github.com/MONARCH1108/Twitter_Bot.git cd twitter-news-bot -
Install Python dependencies
pip install -r requirements.txt
-
Install and setup Ollama
# Install Ollama (visit https://ollama.ai for installation instructions) ollama pull llama3.2:latest ollama serve -
Install ChromeDriver
- Download from ChromeDriver
- Add to your system PATH or place in project directory
-
Update
tweet_generator.pyGEMINI_API_KEY = "your_gemini_api_key_here" JSON_FILE_PATH = "path/to/bbc_improved_data.json"
-
Update
twitter_bot.pyTWITTER_USERNAME = "your_twitter_username" TWITTER_PASSWORD = "your_twitter_password" JSON_FILE_PATH = "path/to/generated_tweets.json"
python bbc_crawler.pyThis will:
- Discover and crawl BBC news articles
- Extract content and analyze with AI
- Save results to
bbc_improved_data.json
python tweet_generator.pyThis will:
- Load crawled news data
- Generate engaging tweets using Ollama
- Create trending hashtags using Gemini
- Save results to
generated_tweets.json
python twitter_bot.pyThis will:
- Load generated tweets
- Automatically post to Twitter/X
- Handle rate limiting and errors
- Log all activities
# Run the complete pipeline
python bbc_crawler.py && python tweet_generator.py && python twitter_bot.py- Model Selection: Change Ollama model (default:
llama3.2:latest) - Categories: Customize news categories to crawl
- Article Limits: Set maximum articles per category
- Parallel Processing: Adjust thread count for crawling
- API Keys: Configure Gemini API access
- Content Filtering: Set urgency and sentiment filters
- Tweet Length: Customize tweet character limits
- Hashtag Count: Control number of hashtags (3-5 recommended)
- Posting Intervals: Time between tweets (minimum 10 seconds recommended)
- Browser Mode: Headless vs. visible browser
- Retry Logic: Configure retry attempts and timeouts
- Content Limits: Maximum tweets per session
{
"tweet": "Breaking: New climate change report reveals alarming trends in global temperatures. Scientists call for immediate action.",
"hashtags": ["#ClimateChange", "#BreakingNews", "#GlobalWarming", "#Science", "#Environment"],
"article_url": "https://www.bbc.com/news/science-environment-12345678",
"topics": ["climate change", "environment", "science"],
"tweet_with_hashtags": "Breaking: New climate change report reveals alarming trends... #ClimateChange #BreakingNews #GlobalWarming"
}- Total Articles Processed: Real-time counting
- Sentiment Distribution: Positive/Negative/Neutral breakdown
- Top Topics: Most frequent news topics
- Success Rates: Crawling and posting success metrics
- Ollama (Llama 3.2): Local tweet generation
- Google Gemini: Cloud-based hashtag optimization
- BeautifulSoup: Intelligent content extraction
- Network Resilience: Automatic retry with exponential backoff
- Content Validation: Quality checks for articles and tweets
- Fallback Mechanisms: Multiple posting strategies for Twitter
- Parallel Processing: Concurrent article processing
- Smart Caching: Avoid duplicate content
- Rate Limit Compliance: Respect API and platform limits
requests>=2.31.0
beautifulsoup4>=4.12.0
selenium>=4.15.0
google-generativeai>=0.3.0
ollama>=0.1.0
aiohttp>=3.9.0
lxml>=4.9.0- RAM: 4GB minimum (8GB recommended for Ollama)
- Storage: 2GB free space for models and data
- Network: Stable internet connection
- OS: Windows 10+, macOS 10.15+, or Linux
- News Accuracy: Only shares content from reputable BBC sources
- Fact Checking: AI analysis includes sentiment and urgency validation
- Rate Limiting: Respects Twitter's posting limits and guidelines
- No Personal Data: Only processes public news content
- Secure Credentials: Environment variable support for sensitive data
- GDPR Compliance: No user data collection or storage
- Twitter/X ToS: Designed to comply with platform guidelines
- BBC Content: Respects fair use and attribution policies
- API Usage: Follows rate limits and usage policies
1. Ollama Connection Failed
# Check if Ollama is running
ollama list
ollama serve
# Verify model installation
ollama pull llama3.2:latest2. Twitter Login Issues
- Verify credentials are correct
- Check for 2FA requirements
- Ensure account is not restricted
3. Gemini API Errors
- Verify API key is valid
- Check quota and billing status
- Ensure proper API permissions
4. ChromeDriver Issues
- Update Chrome browser
- Download matching ChromeDriver version
- Check PATH configuration
Enable detailed logging:
logging.basicConfig(level=logging.DEBUG)We welcome contributions! Please see our contributing guidelines:
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
# Install development dependencies
pip install -r requirements-dev.txt
# Run tests
python -m pytest tests/
# Format code
black *.py
flake8 *.pyThis project is licensed under the MIT License - see the LICENSE file for details.
This tool is for educational and personal use only. Users are responsible for:
- Complying with Twitter/X Terms of Service
- Respecting content licensing and fair use
- Following local laws and regulations
- Using the tool ethically and responsibly
- Issues: GitHub Issues
- Discussions: GitHub Discussions
- Documentation: Wiki
- Multi-Platform Support: LinkedIn, Facebook, Instagram
- Custom News Sources: Beyond BBC integration
- Advanced AI Models: GPT-4, Claude integration
- Web Dashboard: Real-time monitoring interface
- Scheduled Posting: Cron-like scheduling system
- Analytics Dashboard: Engagement metrics and insights
- Docker Support: Containerized deployment
- Cloud Deployment: AWS/GCP integration
- Webhook Support: Real-time news notifications
- Content Filtering: Advanced topic and sentiment filters
Made with β€οΈ by [Your Name]
Don't forget to β star this repository if you found it helpful!