Created
November 17, 2015 16:57
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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