-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlive_cornetometro.py
More file actions
73 lines (57 loc) · 2.12 KB
/
Copy pathlive_cornetometro.py
File metadata and controls
73 lines (57 loc) · 2.12 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
72
73
#Import the necessary methods from tweepy library
from tweepy.streaming import StreamListener
from tweepy import OAuthHandler
from tweepy import Stream
from tokens import *
import json
import time
import sys
from cornetometro import Cornetometro
class CornetometroListener(StreamListener):
def __init__(self, teams, players):
self.cornetometro = Cornetometro(teams, players)
self.cornetometro.load('classifier.obj')
self.time = time.time()
self.print_scores()
def on_data(self, data):
tweet_json = json.loads(data)
if not tweet_json['text'].startswith('RT'):
tweet = tweet_json['text']
self.cornetometro.classify(tweet)
if self.__minute_passed():
self.record_scores()
self.time = time.time()
return True
def record_scores(self):
# Could do more than printing, like saving or putting in a list
self.print_scores()
def print_scores(self):
print time.strftime("%H:%M:%S")
for player in self.cornetometro.players:
stats = self.cornetometro.get_stats(player)
print "{}: {} ({} / {})".format(player, self.cornetometro.get_score(player), stats['positive'], stats['negative'])
print ''
def __minute_passed(self):
return time.time() - self.time >= 60
def on_error(self, status):
print status
def main():
if len(sys.argv) < 2:
print "ERROR: Less arguments than expected"
print "python live_cornetometro.py <input file>"
return
input_file = open(sys.argv[1])
teams = input_file.readline().split(',')
players = input_file.readline().split(',')
input_file.close()
for i in xrange(len(teams)):
teams[i] = teams[i].strip()
for i in xrange(len(players)):
players[i] = players[i].strip()
listener = CornetometroListener(teams, players)
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
stream = Stream(auth, listener)
stream.filter(languages=['en'], track=players)
if __name__ == '__main__':
main()