RSS to Twitter Automation Script

This script automates the process of posting items from an RSS feed to Twitter. It uses the Tweepy library to interact with the Twitter API and the feedparser library to parse the RSS feed. The script ensures that only new items are posted to avoid duplicates and handles rate limits by introducing delays between tweets.

Requirements

  • Python 3.x
  • Tweepy library
  • Feedparser library

Installation

  1. Install Python: Ensure Python is installed on your system. You can download it from the official Python website.
  2. Install Required Libraries: Open your terminal or command prompt and run the following commands to install the required libraries:
    sh
    pip install feedparser tweepy
    

Setup

  1. Twitter Developer Account: Create a Twitter Developer account and set up an app to obtain your API keys and access tokens. You can find more information . You will need the following credentials:
    • Consumer Key (API Key)
    • Consumer Secret (API Secret)
    • Access Token
    • Access Token Secret
  2. Script Configuration: Replace the placeholder values in the script with your actual Twitter API credentials and RSS feed URL.

Script

Here is the complete script:

python
import feedparser
import tweepy
import time

# Twitter API credentials
consumer_key = 'YOUR_CONSUMER_KEY'
consumer_secret = 'YOUR_CONSUMER_SECRET'
access_token = 'YOUR_ACCESS_TOKEN'
access_token_secret = 'YOUR_ACCESS_TOKEN_SECRET'

# Authenticate to Twitter
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)

# RSS feed URL
rss_url = 'YOUR_RSS_FEED_URL'

# Parse the RSS feed
feed = feedparser.parse(rss_url)

# Track last posted item
last_posted = None

# Load last posted item from a file
try:
    with open('last_posted.txt', 'r') as file:
        last_posted = file.read().strip()
except FileNotFoundError:
    last_posted = None

# Post each item to Twitter
for entry in reversed(feed.entries):
    tweet = f"{entry.title} {entry.link}"
    if tweet != last_posted:
        try:
            api.update_status(tweet)
            last_posted = tweet
            print(f"Posted: {tweet}")
            time.sleep(300)  # Delay of 300 seconds (5 minutes) between tweets
        except tweepy.Forbidden as e:
            print("Error: Forbidden - You might be hitting rate limits or posting duplicate content.")
        except tweepy.TweepyException as e:
            print(f"Error: {e}")
    else:
        print(f"No new items to post.")
        break

# Save the last posted item to a file
with open('last_posted.txt', 'w') as file:
    file.write(last_posted)

Adding the Script to a Server

  1. Upload the Script: Transfer the script to your server using a method like SCP, SFTP, or a file manager.
  2. Install Python and Required Libraries: Ensure Python and the required libraries are installed on your server. You can use the same installation commands as mentioned above.

Creating a Cron Job

  1. Open Crontab: Open the crontab file for editing by running the following command:
    sh
    crontab -e
    
  2. Add Cron Job: Add the following line to schedule the script to run every hour (adjust the schedule as needed):
    sh
    0 * * * * /usr/bin/python3 /path/to/your/script.py
    

    Replace /path/to/your/script.py with the actual path to your script.

  3. Save and Exit: Save the crontab file and exit the editor. The script will now run automatically according to the schedule you set.