How to tweet new posts of a website, specially useful for websites built with static generators like Jekyll.

Overview

Based in the RSS feed generated by websites, feed a Twitter account with daily new blog posts.

This method is specially useful for websites that has been built with static site generators, like Jekyll for example, where they automatically publish a RSS feed with new content in each build.

Based in this information, you can set up a script in another server that will check the above feed, and if it detects new posts it will publish a link to it directly in Twitter.

Process

Set up a virtual environment

If you already have a virtual environment installed, you can skip this step. In Ubuntu you can install the package python-virtualenv.


$ apt install python-virtualenv

Or refer to this virtualenv installation guide.

Activate the virtual environment to install the required packages


$ virtualenv -p python2.7 ~/.virtualenvs/twitter_bot
Running virtualenv with interpreter /usr/bin/python2.7
New python executable in /home/user/.virtualenvs/twitter_bot/bin/python2.7
Also creating executable in /home/user/.virtualenvs/twitter_bot/bin/python

$ source ~/.virtualenvs/twitter_bot/bin/activate
(twitter_bot)$

Twitter credentials

Log into Twitter Apps and select “Create New App”.

Create the Application with the following content:

Name
Website RSS Twitter Feeder
Description
Automatic new blog posts publisher
Website
Example.com
Callback URL

And then proceed to “Create your Twitter Application” button.

Now go to “Keys and Access Tokens” tab, and make sure your app has rights to publish tweets, the Access Level Read and write option should be enabled.

Then go to “Token Actions” and “Create my access token”.

You will need the following items from this page:

  • Consumer Key (API Key)
  • Consumer Secret (API Secret)
  • Access Token
  • Access Token Secret

Set up application

We will use the feedr script to publish tweets, so we have to clone the repo:


(twitter_bot)$ git clone https://github.com/housed/feedr.git
Cloning into 'feedr'...
remote: Counting objects: 65, done.
remote: Total 65 (delta 0), reused 0 (delta 0), pack-reused 65
Unpacking objects: 100% (65/65), done.
Checking connectivity... done.
(twitter_bot)$ cd feedr
(twitter_bot) feedr$

Edit ` src/main.py` and add the previous generated Twitter consumer keys and replace the feed with your website.

# Initialize the list of desired feeds
# Feed(Name, XML, Media, Hashtags)
FEEDS = [ Feed('SimpleIt.Rocks', 'http://simpleit.rocks/feed', '', '')]

# Twitter Account Keys 
CONSUMER_KEY = 'consumer_key'
CONSUMER_SECRET = 'consumer_secret'
ACCESS_KEY = 'access_key'
ACCESS_SECRET = 'access_secret'

Install dependencies

Feedr relies on BeautifulSoup4, FeedParser and Tweepy.


(twitter_bot) feedr$ pip install bs4
Downloading/unpacking bs4
  Downloading bs4-0.0.1.tar.gz
  Running setup.py (path:/home/user/.virtualenvs/twitter_bot/build/bs4/setup.py) egg_info for package bs4
    
Downloading/unpacking beautifulsoup4 (from bs4)
  Downloading beautifulsoup4-4.5.1-py2-none-any.whl (83kB): 83kB downloaded
Installing collected packages: bs4, beautifulsoup4
  Running setup.py install for bs4
    
Successfully installed bs4 beautifulsoup4
Cleaning up...


(twitter_bot) feedr$ pip install feedparser
Downloading/unpacking feedparser
  Downloading feedparser-5.2.1.zip (1.2MB): 1.2MB downloaded
  Running setup.py (path:/home/user/.virtualenvs/twitter_bot/build/feedparser/setup.py) egg_info for package feedparser
    
Installing collected packages: feedparser
  Running setup.py install for feedparser
    
Successfully installed feedparser
Cleaning up...


(twitter_bot) feedr$ pip install tweepy
Downloading/unpacking tweepy
  Downloading tweepy-3.5.0-py2.py3-none-any.whl
Downloading/unpacking requests>=2.4.3 (from tweepy)
  Downloading requests-2.11.1-py2.py3-none-any.whl (514kB): 514kB downloaded
Downloading/unpacking six>=1.7.3 (from tweepy)
  Downloading six-1.10.0-py2.py3-none-any.whl
Downloading/unpacking requests-oauthlib>=0.4.1 (from tweepy)
  Downloading requests_oauthlib-0.7.0-py2.py3-none-any.whl
Downloading/unpacking oauthlib>=0.6.2 (from requests-oauthlib>=0.4.1->tweepy)
  Downloading oauthlib-2.0.0.tar.gz (122kB): 122kB downloaded
  Running setup.py (path:/home/user/.virtualenvs/twitter_bot/build/oauthlib/setup.py) egg_info for package oauthlib
    
Installing collected packages: tweepy, requests, six, requests-oauthlib, oauthlib
  Running setup.py install for oauthlib
    
Successfully installed tweepy requests six requests-oauthlib oauthlib
Cleaning up...


Execute Twitter feeder

After setting up credentials you can execute feedr/src/main.py


(twitter_bot) feedr$ cd src
(twitter_bot) feedr/src$ python2.7 main.py


Schedule posts

We set up a cron job to check for new posts every a certain amount of time, in this case every hour. We enter the crontab editor:


$ crontab -e

And then we add the following line (adjust the path of your installation, in this case I have used /opt/feedr)

0 * * * * cd /opt/feedr/src; flock -n /tmp/twbot.lock ~/.virtualenvs/twitter_bot/bin/python2.7 main.py

We use the flock command to prevent duplicate cron job executions.

Reference

Uruguay
Marcelo Canina
I'm Marcelo Canina, a developer from Uruguay. I build websites and web-based applications from the ground up and share what I learn here.