Skip to content

An intelligent Twitter automation system that crawls BBC news, generates engaging tweets using AI, and automatically posts them to Twitter/X.

License

Notifications You must be signed in to change notification settings

MONARCH1108/Twitter_Bot

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

6 Commits
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ€– Twitter News Bot

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.

🌟 Features

  • πŸ“° 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

πŸ—οΈ Project Structure

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

πŸš€ Quick Start

Prerequisites

  1. Python 3.8+
  2. Ollama with Llama 3.2 model
  3. Google Gemini API Key
  4. Chrome Browser and ChromeDriver
  5. Twitter/X Account

Installation

  1. Clone the repository

    https://github.com/MONARCH1108/Twitter_Bot.git
    cd twitter-news-bot
  2. Install Python dependencies

    pip install -r requirements.txt
  3. Install and setup Ollama

    # Install Ollama (visit https://ollama.ai for installation instructions)
    ollama pull llama3.2:latest
    ollama serve
  4. Install ChromeDriver

    • Download from ChromeDriver
    • Add to your system PATH or place in project directory

Configuration

  1. Update tweet_generator.py

    GEMINI_API_KEY = "your_gemini_api_key_here"
    JSON_FILE_PATH = "path/to/bbc_improved_data.json"
  2. Update twitter_bot.py

    TWITTER_USERNAME = "your_twitter_username"
    TWITTER_PASSWORD = "your_twitter_password"
    JSON_FILE_PATH = "path/to/generated_tweets.json"

πŸ“– Usage

Step 1: Crawl BBC News

python bbc_crawler.py

This will:

  • Discover and crawl BBC news articles
  • Extract content and analyze with AI
  • Save results to bbc_improved_data.json

Step 2: Generate Tweets

python tweet_generator.py

This will:

  • Load crawled news data
  • Generate engaging tweets using Ollama
  • Create trending hashtags using Gemini
  • Save results to generated_tweets.json

Step 3: Post to Twitter

python twitter_bot.py

This will:

  • Load generated tweets
  • Automatically post to Twitter/X
  • Handle rate limiting and errors
  • Log all activities

All-in-One Execution

# Run the complete pipeline
python bbc_crawler.py && python tweet_generator.py && python twitter_bot.py

βš™οΈ Configuration Options

BBC Crawler (bbc_crawler.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

Tweet Generator (tweet_generator.py)

  • 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)

Twitter Bot (twitter_bot.py)

  • 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

πŸ“Š Output Examples

Generated Tweet Structure

{
  "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"
}

Analytics Dashboard

  • 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

πŸ”§ Advanced Features

Multi-Model AI Integration

  • Ollama (Llama 3.2): Local tweet generation
  • Google Gemini: Cloud-based hashtag optimization
  • BeautifulSoup: Intelligent content extraction

Robust Error Handling

  • Network Resilience: Automatic retry with exponential backoff
  • Content Validation: Quality checks for articles and tweets
  • Fallback Mechanisms: Multiple posting strategies for Twitter

Performance Optimization

  • Parallel Processing: Concurrent article processing
  • Smart Caching: Avoid duplicate content
  • Rate Limit Compliance: Respect API and platform limits

πŸ“‹ Requirements

Python Dependencies

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

System Requirements

  • 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

πŸ›‘οΈ Safety & Ethics

Content Guidelines

  • 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

Privacy Protection

  • No Personal Data: Only processes public news content
  • Secure Credentials: Environment variable support for sensitive data
  • GDPR Compliance: No user data collection or storage

Terms of Service

  • 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

πŸ”§ Troubleshooting

Common Issues

1. Ollama Connection Failed

# Check if Ollama is running
ollama list
ollama serve

# Verify model installation
ollama pull llama3.2:latest

2. 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

Debug Mode

Enable detailed logging:

logging.basicConfig(level=logging.DEBUG)

🀝 Contributing

We welcome contributions! Please see our contributing guidelines:

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

Development Setup

# Install development dependencies
pip install -r requirements-dev.txt

# Run tests
python -m pytest tests/

# Format code
black *.py
flake8 *.py

πŸ“œ License

This project is licensed under the MIT License - see the LICENSE file for details.

⚠️ Disclaimer

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

πŸ†˜ Support

πŸš€ Roadmap

Version 2.0 (Planned)

  • 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

Version 1.5 (In Progress)

  • 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!

About

An intelligent Twitter automation system that crawls BBC news, generates engaging tweets using AI, and automatically posts them to Twitter/X.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages