Path: blob/master/april_18/lessons/lesson-14/code/starter-code/twitter.py
1905 views
from TwitterAPI import TwitterAPI12access_token_key = "<ENTER ACCESS TOKEN KEY>"3access_token_secret = "<ENTER ACCESS TOKEN SECRET>"45api_key = "<ENTER CONSUMER KEY>"6api_secret = "<ENTER CONSUMER SECRET>"78_debug = 091011api = TwitterAPI(api_key, api_secret, access_token_key, access_token_secret)1213'''14Construct, sign, and open a twitter request15using the hard-coded credentials above.16'''1718def retrieve_tweets(topic,19url="https://stream.twitter.com/1/statuses/filter.json",20method="GET", ):21"""2223Params:24topic - must be in this format "#topic" or '@handle"25Returns26"""27response = api.request('statuses/filter', {'track': topic})28if response.status_code != 200:29raise ValueError("Unable to retrieve tweets, please check your API credentials")30for x in response:31try:32yield x33except UnicodeError as unicode_error:34continue353637