-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
83 lines (64 loc) · 2.15 KB
/
app.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
# -*- coding: utf8 -*-
from gevent import monkey;
monkey.patch_all()
import gevent
import os
from flask import Flask, render_template, request
from flask.ext.socketio import SocketIO, emit
from twython import TwythonStreamer
app = Flask(__name__)
app.debug = True
app.config['SECRET_KEY'] = 'secret!'
socketio = SocketIO(app)
port = int(os.getenv('VCAP_APP_PORT', 5000))
from config import CONF
class TwitterStreamer(TwythonStreamer):
def __init__(self, *args, **kwargs):
TwythonStreamer.__init__(self, *args, **kwargs)
print("Initialized TwitterStreamer.")
self.queue = gevent.queue.Queue()
def on_success(self, data):
self.queue.put_nowait(data)
if self.queue.qsize() > 10000:
self.queue.get()
def on_error(self, status_code, data):
print status_code, data, "TwitterStreamer stopped because of an error!"
self.disconnect()
class TwitterWatchDog:
def __init__(self):
self.streamer = TwitterStreamer(CONF['APP_KEY'], CONF['APP_SECRET'], CONF['OAUTH_TOKEN'], CONF['OAUTH_TOKEN_SECRET'])
self.green = gevent.spawn(self.streamer.statuses.filter, track=CONF['KEYWORD'])
def check_alive(self):
if self.green.dead:
# stop everything
self.streamer.disconnect()
self.green.kill()
# then reload
self.__init__()
dog = TwitterWatchDog()
@app.route('/')
def index():
dog.check_alive()
return render_template('index.html')
@socketio.on('connect', namespace='/tweets')
def tweets_connect():
dog.check_alive()
uid = request.namespace.socket.sessid
print('Client %s connected' % uid)
while True:
try:
tweet = dog.streamer.queue.get(timeout=5)
except gevent.queue.Empty:
dog.check_alive()
else:
emit('tweet', tweet, broadcast=True)
@socketio.on('disconnect', namespace='/tweets')
def tweets_disconnect():
dog.check_alive()
uid = request.namespace.socket.sessid
print('Client %s disconnected' % uid)
if __name__ == '__main__':
try:
socketio.run(app, port=port, host="0.0.0.0")
except KeyboardInterrupt:
pass