-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi_twitter.py
More file actions
39 lines (31 loc) · 845 Bytes
/
api_twitter.py
File metadata and controls
39 lines (31 loc) · 845 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import os
import twitter
def obtain(search_term, category):
consumer_key = os.getenv('TWITTER_CONSUMER_KEY')
consumer_secret = os.getenv('TWITTER_CONSUMER_SECRET')
access_token_key = os.getenv('TWITTER_TOKEN')
access_token_secret = os.getenv('TWITTER_TOKEN_SECRET')
api = twitter.Api(
consumer_key=consumer_key,
consumer_secret=consumer_secret,
access_token_key=access_token_key,
access_token_secret=access_token_secret
)
try:
tweets = api.GetSearch(
term="%s" % search_term
)
except BaseException:
return None
return {
"metadata": {
"type": "twitter-tweet",
"term": search_term,
"category": category
},
"data": {
"author": tweets[0].user.name,
"authorImage": tweets[0].user.profile_image_url,
"tweet": tweets[0].text
}
}