-
Notifications
You must be signed in to change notification settings - Fork 0
/
twitter_stream_client.py
72 lines (62 loc) · 1.81 KB
/
twitter_stream_client.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
import zmq
import datetime
import plotly.plotly as ply
import plotly.graph_objs as go
import plotly.tools as pls
import pandas as pd
import os
# get plotly stream ids
stream_ids = pls.get_credentials_file()['stream_ids']
# set up connection to servers
context = zmq.Context()
socket = context.socket(zmq.SUB)
socket.connect('<IP>:<PORT>')
socket.setsockopt_string(zmq.SUBSCRIBE, '')
# import tweet filter words
t = pd.read_csv(r'filter_names.csv')
filter_name = t['names'].tolist()
tweet_count = []
stream_id = []
token = []
trace = []
sent = []
st = []
# open data from plotly stream
for i in range(len(filter_name)):
token.append(stream_ids[i])
stream_id.append(dict(token=token[i]))
sent.append(0)
tweet_count.append(0)
st.append(ply.Stream(stream_ids[i]))
trace.append(go.Scatter(x=[], y=[], stream=stream_id[i], name=filter_name[i]))
# plot graphs
data = trace
layout = go.Layout(
title='test',
yaxis=dict(
title='sentiment'
)
)
fig = go.Figure(data=data, layout=layout)
plot_url = ply.plot(fig, filename='stream_multiple_data')
for streams in st:
streams.open()
while True:
# receive data from server
msg = socket.recv_string()
# get the current time
t = datetime.datetime.now()
# get the filter name from data
name = msg.split()[6]
# split data in two
for i in range(len(filter_name)):
if name == filter_name[i]:
# st[i].open()
sent[i] += float(msg.split()[4])
tweet_count[i] += 1
# print(str(t) + ' | ' + str(sent[i]) + ' | ' + name)
st[i].write({'x': t, 'y': float(sent[i])})
os.system('cls')
for i in range(len(filter_name)):
print('{0}: Tweet Count: {1}. Sentiment: {2}.'
.format(filter_name[i], tweet_count[i], sent[i]))