Use Python and the Twitter API to Find Tweets

In this beginner’s tutorial I’ll walk through the steps to making your first Python program that works with the Twitter API. This will allow you to perform actions with Twitter’s code without being on the website, and also open up other options that are not readily available to normal users.

For this tutorial we’re first going to need a Twitter dev account. Don’t worry, this is a very simple step.

Sign-Up as a Twitter Developer

To get started, first sign up as a developer at https://dev.twitter.com/ using your twitter account. Once you have logged in you’ll be able to create your first application.

Registering an Application with Twitter

Clicking on the “Create a new application” button.

On the next page fill out the Name, Description and Website fields. For our little app we won’t need to use a Callback URL, so ignore that field.

On the next screen you will get your OAuth information. It should look something like this:

Followed by our access token:

Now that we have our own OAuth, we can use the Twitter API to some data mining with a small python app that we’ll be able to access from the terminal.

Prepare Python

Next, we have to get out system ready with for Python. In terminal type “python” to see if you have it installed. If you do, great! If you don’t, you’ll need to install it, but most Mac builds should have it preinstalled.

Ok, so now let’s install the python wrapper for the Twitter API. This is what will let us easily communicate with the Twitter servers. In our terminal type “sudo easy_install python-twitter” to get the installation started. Alternatively you can go to its Google Code website and install it from there.

The Python Code

Twitter API

Great, we’re on a roll. We’ve got our authorization and all the base code to communicate with the API. Now starts the coding.

The skeletal start will look like this:

#!/usr/bin/env python
import twitter
#Setting up Twitter API
api = twitter.Api(
 consumer_key='XXXXXXXXXXXXXXXXXXXXXX',
 consumer_secret='XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
 access_token_key='XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
 access_token_secret='XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
 )

This first part imports the API wrapper we downloaded earlier. The next part gives all the required authorization data to Twitter so they know who we are and we know who they are.

Basic Twitter Find

We can now do a very simple twitter search by using the following code:

search = api.GetSearch(term='adventure', lang='en', result_type='recent', count=100, max_id='')
for t in search:
 print t.user.screen_name + ' (' + t.created_at + ')'
 #Add the .encode to force encoding
 print t.text.encode('utf-8')
 print ''

This will find 100 tweets with the word ‘adventure’ somewhere in the tweet and print the results.

Improving The Code

Argparse

Since we want to make this an app accessible from Terminal, we’ll want to make it look and act like a terminal command. This will allow us to send variables to our program in the form of arguments, and these can be used to customize our search. It is much easer than trying to rewrite our code each time we want to try to search for something else. To make this happen we’ll need to add argparse to our code:

#!/usr/bin/env python
import twitter
import argparse

Great. So far so good. Next, we need to define some arguments.

Argparse Arguments

Our arguments will allow us to do some complex searches using simple commands.

For this, let’s call our program “stwitter”, which stands for Search-Twitter

# Parsing the commands

#Top Level
parser = argparse.ArgumentParser(prog='stwitter', description='Custom search of Twitter')
parser.add_argument('-c', '--count', metavar='num', default=100, type=int, help='The total number of tweets to return (before filters)')
parser.add_argument('-f', '--removefilters', action='store_true', help='Removes the tweet filters')
parser.add_argument('-d', '--removedirect', action='store_true', help='Filters direct tweets to our account')
sp = parser.add_subparsers(dest='command')
#Adventure Sub
sp_adventure = sp.add_parser('adventure', help='%(prog)s searches for adventure')
#MyHob Sub
sp_myhob = sp.add_parser('myhob', help='%(prog)s searches for my hobbies')
#Keyword Sub
sp_keyword = sp.add_parser('search', help='%(prog)s searches using a custom query')
sp_keyword.add_argument('term', type=str, help='A query for searching Twitter; must be in quotes')

We’ve split our arguments into two ‘layers’, a top layer and a sub layer.

The top layer of arguments will be for us to add options to our program, like changing filters or increasing the number of search results we want.

The sub arguments allow us to perform customized searches without needing to type the whole search each time. In this case, we have two: adventure and myhob.

Check the final code to see how args.command == ‘adventure’ and args.command == ‘myhob’ are used to for customized search terms.

Final Python Code

Here’s the final code:

#!/usr/bin/env python
import twitter
import argparse

#Setting up Twitter API
api = twitter.Api(
 consumer_key='XXXXXXXXXXXXXXXXXXXXXX',
 consumer_secret='XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
 access_token_key='XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
 access_token_secret='XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
 )

#Parsing the commands
#Top Level
parser = argparse.ArgumentParser(prog='stwitter', description='Custom search of Twitter')
parser.add_argument('-c', '--count', metavar='num', default=100, type=int, help='The total number of tweets to return (before filters)')
parser.add_argument('-f', '--removefilters', action='store_true', help='Removes the tweet filters')
parser.add_argument('-d', '--removedirect', action='store_true', help='Filters direct tweets to our account')
sp = parser.add_subparsers(dest='command')
#Adventure Sub
sp_adventure = sp.add_parser('adventure', help='%(prog)s searches for adventure')
#MyHob Sub
sp_myhob = sp.add_parser('myhob', help='%(prog)s searches for my hobbies')
#Keyword Sub
sp_keyword = sp.add_parser('search', help='%(prog)s searches using a custom query')
sp_keyword.add_argument('term', type=str, help='A query for searching Twitter; must be in quotes')

#Parse the Args
args = parser.parse_args()
if args.command != '':
#Custom looping so we can search more than 100 tweets
 tweetID = ''
 i = int(round(args.count -51, -2)) / 100 + 1
 for x in range (0, i):

 #Perform Find
 if args.command == 'adventure':
 print '------Searching Tweets about adventure ' + str(x * 100) + '/' + str(args.count) + '------'
 search = api.GetSearch(term='"adventure" OR "space travel" OR "deep sea diving" OR "exploring"', lang='en', result_type='recent', count=(args.count - 100 * x), max_id=tweetID)

 elif args.command == 'myhob':
 print '------Searching Tweets about my hobbies ' + str(x * 100) + '/' + str(args.count) + '------'
 search = api.GetSearch(term='"computers" OR "python" OR "Japanese" OR "Bento"', lang='en', result_type='recent', count=(args.count - 100 * x), max_id=tweetID)

 elif args.command == 'search':
 print '------Searching Tweets using "' + args.term + '"' + str(x * 100) + '/' + str(args.count) + '------'
 search = api.GetSearch(term=args.term, lang='en', result_type='recent', count=(args.count - 100 * x), max_id=tweetID)

 #Filter Results
 for t in search:
 #Filter the results by default
 if args.removefilters == False:
 if (
 #Filters Twitter Account
 t.user.screen_name != 'jerkface' and
 t.user.screen_name != 'notniceguy' and
 t.user.screen_name != 'spambot' and
 t.user.screen_name != 'junkbot' and
 #Filter Retweets
 'RT @' not in t.text and
 #Filter Direct Tweets
 (args.removedirect == False or '@mytwittername' not in t.text) and
 #Filter out words
 'sex' not in t.text):
 print ''
 print t.user.screen_name + ' (' + t.created_at + ')'
 #Add the .encode to force encoding
 print t.text.encode('utf-8')
 print ''

 else:
 print ''
 print t.user.screen_name
 print t.created_at
 print t.text.encode('utf-8')
 print ''
#Save the this tweet ID
 tweetID = t.id

Using the Code in Terminal

That’s it. We can test our code by saving the file as .py (python) code, then running it from terminal. For this, type “python ” with the space, then drag-drop the saved .py file into terminal. You will notice that it gives an error. This is because we didn’t set any arguments. Press the up arrow key in your Terminal window to show the last command, then add in one of our options, like “myhob” or “adventure”.

Alternatively, we can make this code available to us anytime just like a normal terminal command.

Make a copy of your code file, stwitter.py, and remove the extension “.py” from it. Next, in terminal type “sudo chmod +x “, including the space, and drag-drop the file into terminal.

sudo chmod +x /Users/pp/Documents/stwitter

Finally type “sudo cp ” then drag-drop the file and finish with adding ” /usr/local/bin/stwitter” to the end.

sudo cp /Users/pp/Documents/stwitter /usr/local/bin/switter

Now when you type stwitter in Terminal you will be able to access your new Twitter search command.

Leave a Reply

Your email address will not be published. Required fields are marked *