-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstreamListener.py
More file actions
72 lines (53 loc) · 1.97 KB
/
streamListener.py
File metadata and controls
72 lines (53 loc) · 1.97 KB
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
import tweepy
import sqlite3
from Tweet import Tweet
from User import User
# StreamListener is a subclass of the main StreamListener from the Tweepy API
# We create the subclass to override logic for processing tweets
class StreamListener(tweepy.StreamListener):
def __init__(self, db_file):
super(StreamListener, self).__init__()
self.conn = sqlite3.connect(db_file)
# Method override that processes each incoming tweet
def on_status(self, status):
if hasattr(status, 'retweeted_status'):
return
cursor = self.conn.cursor()
# User Fields
user_id = status.user.id
name = status.user.screen_name
description = status.user.description
user_created = status.user.created_at
followers = status.user.followers_count
loc = status.user.location
statusUser = User(user_id, name, description, user_created, followers, loc)
# Tweet Fields
id = status.id
text = status.text
created = status.created_at
retweets = status.retweet_count
favorites = status.favorite_count
coords = status.coordinates
status = Tweet(statusUser, id, text, created, retweets, favorites, coords)
status.analyze_sentiment()
polarity = status.sentiment.polarity
subjectivity = status.sentiment.subjectivity
print(statusUser)
print(status)
try:
cursor.execute('''INSERT INTO users(user_id, name, description, user_created, followers, loc)
VALUES(?, ?, ?, ?, ?, ?)''', (user_id, name, description, user_created, followers, loc))
except:
print("User already in DB")
try:
cursor.execute('''INSERT INTO tweets(user_id, id, text, created_at, retweets, favorites, coords, polarity, subjectivity)
VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)''', (user_id, id, text, created, retweets, favorites, coords, polarity, subjectivity))
except:
print("Tweet could not be added to DB")
self.conn.commit()
def on_error(self, status_code):
if status_code == 420:
return False
def __del__(self):
self.conn.close()
super(StreamListener, self).__del__()