-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetTweets.py
120 lines (73 loc) · 2.46 KB
/
getTweets.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import json
import tweepy
# import twitter
from tweepy import Stream
from tweepy import OAuthHandler
from tweepy.streaming import StreamListener
# from twitter import Twitter
ckey ='EgLx9a2h748H0r5zH1zSshdRY'
csecret ='C06Xesjs1S23X8ClP1Q9BiCNIAXCRBAEia139uSXyymCmvjZiA'
atoken ='136394046-GmTHewzpiWVh42IkUebTQ7rOSrQt1V4P8aJxsyaH'
asecret ='MSsI3f2mLmepyK1qMZ7utzP0nFGBOnUQ9JaGbcEjGWg7d'
auth=OAuthHandler(ckey,csecret)
auth.set_access_token(atoken,asecret)
api = tweepy.API(auth)
def analyseTweet(tweet):
length = len(tweet.entities['hashtags']);
size = False
url = False
for hashtag in tweet.entities['hashtags']:
if hashtag['text'][0] == 'T' or hashtag['text'][0] == 't':
size = hashtag['text'].split('_')
size = size[1]
print size
if size:
api.update_status('Votre achat a ete enregistre, taille %s'%(size),tweet.id)
else:
api.update_status('Votre achat a ete enregistre',tweet.id)
print 'ok'
parent_id = tweet.in_reply_to_status_id
parent_status = api.get_status(str(parent_id))
print parent_status
product_url = False
for url in parent_status.entities['urls']:
link = url['expanded_url']
if 'modizy.com' in link:
product_url = link
class listener(StreamListener):
def on_data(self, data):
jsonData = json.loads(data)
#print jsonData
hashtags = jsonData['entities']['hashtags']
has_hashtag = False
for hashtag in hashtags:
if looking_for in hashtag['text']:
has_hashtag = True
if has_hashtag:
print "GOOOOD"
print jsonData['text']
analyseTweet(jsonData)
else:
print "BADDD"
return True
def on_error(self,status):
print status
#status = twitter.showStatus(id="112652479837110273")
#print status['text']
def getTweet(search_term, periods = 60*60*24):
results = api.search(q=search_term, rpp=periods)
#json_results = json.loads(results)
for tweet in results:
# print tweet.entities['hashtags']
# print tweet.text
print '1 tweet'
analyseTweet(tweet)
return results
def listenToTweets(search_term):
twitterStream = Stream(auth, listener())
twitterStream.filter(track=[search_term])
return True
if __name__ == '__main__':
looking_for = '#Modizy_bot'
listenToTweets(looking_for)
#getTweet(looking_for)