-
Notifications
You must be signed in to change notification settings - Fork 2
/
twitter.py
44 lines (39 loc) · 1.28 KB
/
twitter.py
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
40
41
42
43
44
from twython import Twython, TwythonError
import json
import sys
import os
def default_function():
try:
CREDENTIALS_FILENAME = 'creds.json'
jf = open(CREDENTIALS_FILENAME)
creds = json.load(jf)
jf.close()
except Exception: #On Heroku, we use config vars. Hopefully this works
creds = {}
creds['consumer_key'] = os.environ.get('consumer_key')
creds['consumer_secret'] = os.environ.get('consumer_secret')
twitter = Twython(creds['consumer_key'], creds['consumer_secret'])
tweets = []
try:
user_timeline = twitter.get_user_timeline(screen_name='iitbbs',count=30)
except TwythonError as e:
print(e)
# Version check for python 2 compliance
for tweet in user_timeline:
# Add whatever you want from the tweet, here we just add the text
res = {}
if int(sys.version[0]) == 2:
res['text'] = tweet['text'].encode('utf-8')
else:
res['text'] = tweet['text']
#res['text'] = str(tweet['text'])
if len(tweet['entities']['urls']) != 0:
if int(sys.version[0]) == 2:
res['url'] = tweet['entities']['urls'][0]['expanded_url'].encode('utf-8')
else:
res['url'] = tweet['entities']['urls'][0]['expanded_url']
res['text'] = res['text'][0:tweet['entities']['urls'][0]['indices'][0]]
else:
res['url'] = "No link for this tweet"
tweets.append(res)
return tweets