-
Notifications
You must be signed in to change notification settings - Fork 0
/
twitter_stream_server.py
59 lines (51 loc) · 1.93 KB
/
twitter_stream_server.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
from twython import TwythonStreamer
import time
import zmq
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
import pandas as pd
# set up twitter and server parameters
analyser = SentimentIntensityAnalyzer()
context = zmq.Context()
socket = context.socket(zmq.PUB)
socket.bind('<IP>:<PORT>')
CONSUMER_KEY = '<CONSUMERKEY>'
CONSUMER_SECRET = '<CONSUMERSECRET>'
ACCESS_TOKEN = '<ACCESSTOKEN>'
ACCESS_TOKEN_SECRET = '<ACCESSTOKENSECRET>'
# Twitter Streaming class
class MyStreamer(TwythonStreamer):
counter = 0
def on_success(self, data):
try:
if data['lang'] in ['en', 'fr', 'de', 'es', 'it', 'ru']:
MyStreamer.counter += 1
# get sentiment of tweet
sent = analyser.polarity_scores(data['text'])['compound']
for name in filter_name:
# find which filter name is contained within tweet
if name in data['text'].lower():
# send filter name and sentiment to server
print('Tweet: {c} | Sent: {s} | {n} | {lang}'
.format(c=MyStreamer.counter, s=sent, n=name,
lang=data['lang']))
msg = 'Tweet: {c} | Sent: {s} | {n}' \
.format(c=MyStreamer.counter, s=sent, n=name)
socket.send_string(msg)
except:
pass
def on_error(self, status_code, data):
print(status_code, data)
self.disconnect()
stream = MyStreamer(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN,
ACCESS_TOKEN_SECRET)
# import words to filter on
t = pd.read_csv(r'filter_names.csv')
filter_name = t['names'].tolist()
t = ','.join(filter_name)
print(t)
timeout = time.time() + 60
# continuously filter tweets based on names defined
while True:
if time.time() > timeout:
break
stream.statuses.filter(track=t)