Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
YStrano
GitHub Repository: YStrano/DataScience_GA
Path: blob/master/april_18/lessons/lesson-14/code/starter-code/twitter.py
1905 views
1
from TwitterAPI import TwitterAPI
2
3
access_token_key = "<ENTER ACCESS TOKEN KEY>"
4
access_token_secret = "<ENTER ACCESS TOKEN SECRET>"
5
6
api_key = "<ENTER CONSUMER KEY>"
7
api_secret = "<ENTER CONSUMER SECRET>"
8
9
_debug = 0
10
11
12
api = TwitterAPI(api_key, api_secret, access_token_key, access_token_secret)
13
14
'''
15
Construct, sign, and open a twitter request
16
using the hard-coded credentials above.
17
'''
18
19
def retrieve_tweets(topic,
20
url="https://stream.twitter.com/1/statuses/filter.json",
21
method="GET", ):
22
"""
23
24
Params:
25
topic - must be in this format "#topic" or '@handle"
26
Returns
27
"""
28
response = api.request('statuses/filter', {'track': topic})
29
if response.status_code != 200:
30
raise ValueError("Unable to retrieve tweets, please check your API credentials")
31
for x in response:
32
try:
33
yield x
34
except UnicodeError as unicode_error:
35
continue
36
37