-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstreamListener.py
51 lines (44 loc) · 1.72 KB
/
streamListener.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
from tweepy.streaming import StreamListener
import time
import json
import pymongo
from pymongo import MongoClient
client = MongoClient()
db = client['tweets']
tweets = db.tweets
class listener(StreamListener):
def __init__(self, time_limit=60):
self.start_time = time.time()
self.limit = time_limit
def on_data(self, data):
if (time.time() - self.start_time) < self.limit:
tweet = json.loads(data)
hashtags = []
country = tweet['place']['country'] if tweet['place'] is not None else ""
country_code = tweet['place']['country_code'] if tweet['place'] is not None else ""
doc = {
"screen_name": tweet['user']['screen_name'],
"user_name": tweet['user']['name'],
"location": tweet['user']['location'],
"source_device": tweet['source'],
"is_retweeted": tweet['retweeted'],
"retweet_count": tweet['retweet_count'],
"country": country,
"country_code": country_code,
"reply_count": tweet['reply_count'],
"favorite_count": tweet['favorite_count'], # likes
"tweet_text": tweet['text'],
"created_at": tweet['created_at'],
"timestamp_ms": tweet['timestamp_ms'],
"lang": tweet['lang'],
"hashtags": hashtags,
"quote_count":tweet['quote_count'],
}
tweet_text = tweets.insert_one(doc).inserted_id
print(tweet_text)
return True
else:
print("Stream Stopped")
return False
def on_error(self, status):
print(status)