Using Tweepy, Firebase, and Pythonanywhere to automate Twitter

The goal of this tutorial is to teach you how to piece together an automated Twitter account. You will be introduced to Tweepy, a package that helps us talk to the Twitter API, a real-time database service called Firebase, and the deployment process on the Pythonanywhere platform.

This tutorial uses python 3.4 that comes with Pip — the widely recommended python package installer — by default.

We’ll need to install Tweepy to talk to Twitter and the Requests package to simplify http requests.

In your terminal:

pip install tweepy requests

With that done, we can now create our project file; feel free to call it whatever you want, I’m going the simplest route and calling it project.py.

In our newly created project file we’ll need to import the packages we just installed along with any others that may help us later. In the top of your project file, include the following:

import tweepy
import requests
import json
import sys
import time
import random

You might have noticed that there are a few extra packages we are importing here, we’ll talk about these later when we start using them.

Twitter

The next thing we need to do is set up authorisation with our twitter account.

Log in to your Twitter account and head to Twitter’s dev site to create a new app. Enter the name and description of your app, and under the website option just put down something like http://myrandomapp.com/.

Once the app is created, head to the Keys and Access Tokens tab. This is where we will get our keys to authorise our automation app.

Back in our project file, add the code below and swap in the corresponding keys from your app’s Keys and Access Tokens tab.

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)

Firebase

Firebase is a fantastic real-time database that offers a limited free option when using their services.

Head to Firebase and setup an account, then create a new app.

Find your Firebase app name, we will use this to build a url to make http calls to later. Add the following to your project file and replace {yourfirebaseappname} with your own app name.

fb_url = https://{yourfirebaseappname}.firebaseio.com/tweets.json

We are going to use Firebase to store the IDs of tweets we have already retweeted. First though, we will build a function to check if a tweet is already in our database.

def check_tweet(tweet):
try:
r = requests.get(fb_url)
r = r.json()
for i in r:
if tweet.id == r[i]["id"]:
return False
finally:
return True

Here we make a call to our Firebase and get all the tweets in our database. Next we iterate over the results and compare them to the tweet we are checking.

There are no tweets in our database just yet so this function won’t work — let’s change that!

def search(tag):
try:
c = tweepy.Cursor(api.search, q=tag)
print("Searching "+tag)
i = 0
for tweet in c.items():
if i < 1 and tweet.retweet_count > 20:
if check_tweet(tweet):
api.retweet(tweet.id)
db_tweet = {"id": tweet.id}
requests.post(fb_url, json.dumps(db_tweet))
i += 1
break

The search function will search with a hashtag, return results, and if a tweet in that result is popular enough (based on number of retweets) and isn’t in our database yet - we will retweet it.

We are also retweeting only one tweet each time the search function is called to avoid making too many requests and getting limit errors back from Twitter.

Now how will the app know what hashtags search for? We’ll tell it.

while True:
tags = ["#danceparty", "#ilovehats", "#firebase", "#tweepy"]
search(random.choice(tags))
time.sleep(60)

There a few different things going on in this while loop. First we specify a list of hashtags we would like to use to search Twitter, then we use random.choice(tags) to choose a random tag to pass to the search function. Once the search function is completed, the app will sleep for 60 seconds and then perform the whole process all over again.

Keeping the lights on

It is pretty much guaranteed that at some point you will hit some sort of resource limit with Twitter’s API and get smacked by an error that terminates your processes, so we’ll have to modify our functions to handle errors.

Back in your search function:

def search(tag):
try:
c = tweepy.Cursor(api.search, q=tag)
print("Searching "+tag)
i = 0
for tweet in c.items():
if i < 1 and tweet.retweet_count > 20:
if check_tweet(tweet):
api.retweet(tweet.id)
db_tweet = {"id": tweet.id}
requests.post(fb_url, json.dumps(db_tweet))
i += 1
break
# Additions below
except KeyboardInterrupt:
sys.exit("KeyboardInterrupt")
except Exception as e:
print(e)
pass

And in your check_tweet function:

def check_tweet(tweet):
try:
r = requests.get(fb_url)
r = r.json()
for i in r:
if tweet.id == r[i]["id"]:
return False
# Additions below
except KeyboardInterrupt:
sys.exit("KeyboardInterrupt")
except Exception as e:
print(e)
pass
finally:
return True

This is a really quick and dirty way to handle errors and keep your app from shutting down.

Pythonanywhere

Head over to pythonanywhere and setup an account.

Go to your files tab and in the right hand corner you will see a link that says “Open Bash console here”, hit that and a console will open up.

Enter the following and DO NOT change user to your username.

pip3.4 install --user tweepy

Go back to your files tab and enter a new file name, e.g. project.py

Copy and paste your code into the text area and hit save & run in the top right hand corner.

We’re done!

There is plenty more you can do with tweepy, firebase, and pythonanywhere. Be sure to play around a bit and figure out even better functionality. Links to documentation for each are just below to help you get started.

Links:
Tweepy
Firebase
Pythonanywhere

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.