-
Notifications
You must be signed in to change notification settings - Fork 1
/
twitter_collector.py
89 lines (76 loc) · 2.74 KB
/
twitter_collector.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
import datetime
import json
import logging
from textblob import TextBlob
from tweepy.streaming import StreamListener
from unidecode import unidecode
from models.sentiment import save_sentiment
class TwitterStreamer(StreamListener):
def __init__(self, search_query, end_time, translate):
super(TwitterStreamer, self).__init__()
self._end_time = end_time
self._search_query = search_query
self._translate = translate
def on_data(self, data):
"""Tweet generic info."""
try:
data = json.loads(data)
tweet_original = str(unidecode(data["text"]))
lang = data["lang"]
if self._translate:
if lang != "en":
tweet_translated = str(TextBlob(tweet_original).translate(to="en"))
else:
tweet_translated = str(tweet_original)
else:
tweet_translated = str(tweet_original)
created_at = datetime.datetime.utcfromtimestamp(
int(data["timestamp_ms"]) / 1000
).strftime("%Y-%m-%dT%H:%M:%SZ")
twitter_id = str(data["id"])
user = data["user"]
geo = data["geo"]
reply_count = data["reply_count"]
retweet_count = data["retweet_count"]
likes_count = data["favorite_count"]
# User info
user_id = str(user["id"])
user_name = user["name"]
user_location = user["location"]
user_description = ["description"]
user_verified = user["verified"]
user_followers_count = user["followers_count"]
user_friends_count = user["friends_count"]
# Sentiment info
sentiment_model = TextBlob(tweet_translated)
polarity = sentiment_model.sentiment.polarity
subjectivity = sentiment_model.sentiment.subjectivity
if datetime.datetime.utcnow() > self._end_time:
return False
save_sentiment(
self._search_query,
self._end_time,
twitter_id,
tweet_original,
tweet_translated,
lang,
created_at,
geo,
reply_count,
retweet_count,
likes_count,
user_id,
user_name,
user_location,
user_description,
user_verified,
user_followers_count,
user_friends_count,
polarity,
subjectivity,
)
except KeyError as e:
logger.error(e)
return True
def on_error(self, status):
logging.error(status)