-
Notifications
You must be signed in to change notification settings - Fork 4
/
twitapi.py
73 lines (58 loc) · 1.73 KB
/
twitapi.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import twitter,json,re,nltk
#nltk.download('words')
from langdetect import detect
def connect():
consumer_key = 'S8gMgBFhScZ1PEQJqYmoDYcL9'
consumer_secret = 'SupHG06pkr15zhXyFA9W66fRejODbc7bKNnRILcrQoouG2cgbh'
access_token = '433140581-SbLJxTUuACh87NOJgOcZ3bexAeHELkbLN9Mzsu2K'
access_token_secret = '7r43Hsq38OxWpvbrVSQGzYoVUwLHnIRVSqRvIfcQVO7gD'
auth = twitter.oauth.OAuth(access_token, access_token_secret,
consumer_key, consumer_secret)
twitter_api = twitter.Twitter(auth=auth)
return twitter_api
def get_trends():
twitter_api = connect()
WORLD_WOE_ID = 1
US_WOE_ID = 23424977
Mumbai = 2295411
# world_trends = twitter_api.trends.place(_id=WORLD_WOE_ID)
# us_trends = twitter_api.trends.place(_id=US_WOE_ID)
trends = twitter_api.trends.place(_id=23424977)
#words = set(nltk.corpus.words.words())
exact = []
pro = []
rank = []
c=0
#print (mum_trends)
# print (json.dumps(trends, indent=1))
for trend in trends[0]["trends"]:
r = trend["tweet_volume"]
rank.append(r)
rank
for trend in trends[0]["trends"]:
try:
if(c<=10):
e = trend["name"]
t = e.strip("#")
tt = re.sub(r'(?<=[a-z])(?=[A-Z])', ' ', t).lower()
l = len(tt)
b = tt[l-1]
a = tt[0]
if(ord(a)<=122 and ord(a)>=97):
if(ord(b)<=122 and ord(b)>=97):
pro.append(tt)
exact.append(e)
c = c+1
except Exception as e:
pass
tds = dict( zip(pro,exact))
return tds
#print(tds)
#print(trends[0]["trends"])
# Do not try,this shit works on my twitter handle dont tweet pls
def post_tweet(summary):
#Update your status
twitter_api = connect()
twitter_api.statuses.update(status=summary)
if __name__ == '__main__':
print(get_trends())