Skip to content

Instantly share code, notes, and snippets.

@danielrobertson
Created November 17, 2015 16:57
Show Gist options
  • Save danielrobertson/e2682441434cd2dcb825 to your computer and use it in GitHub Desktop.
Save danielrobertson/e2682441434cd2dcb825 to your computer and use it in GitHub Desktop.
Given a search term or hashtag, return relevant and recent Tweets using the Twitter Search API
# prereqs:
# Create a Twitter app and generate oauth credentials at https://apps.twitter.com
# python 2.7
# pip install oauth2
import oauth2 as oauth
import json
import urllib
CONSUMER_KEY = 'your consumer key'
CONSUMER_SECRET = 'your consumer secret'
ACCESS_TOKEN = 'your access token'
ACCESS_TOKEN_SECRET = 'your access token secret'
consumer = oauth.Consumer(key=CONSUMER_KEY, secret=CONSUMER_SECRET)
access_token = oauth.Token(key=ACCESS_TOKEN, secret=ACCESS_TOKEN_SECRET)
client = oauth.Client(consumer, access_token)
searchTerm = '#PaxPrime'
response, data = client.request('https://api.twitter.com/1.1/search/tweets.json?q=' + urllib.quote(searchTerm) + '&result_type=recent')
tweets = json.loads(data) # dictionary
statuses = tweets['statuses']
for status in statuses:
user = status['user']
print user['screen_name'] # twitter handle without @
print status['text'] + '\n' # tweet contents
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment