-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
72 lines (60 loc) · 1.59 KB
/
server.py
File metadata and controls
72 lines (60 loc) · 1.59 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
from datetime import date, datetime
from flask import Flask, jsonify, request, Response, send_file, send_from_directory
from flask_socketio import SocketIO
from firehose import Firehose
from scrapers.search_requests import Requests as Scraper
from time import sleep
import json
import os
'''
Configure Flask app.
'''
app = Flask(__name__, static_url_path='')
firehose = Firehose()
firehose.start()
server_queue = []
socketio = SocketIO(app)
print('CLASSIFIER TRAINING')
MAX_TWEETS_ON_SCREEN = 10
'''
Confirm socket connection to the client.
'''
@socketio.on('connect', namespace='/stream')
def client_connect():
print('Connected to /stream')
'''
Listen to and process inputs.
'''
@socketio.on('inputs', namespace='/stream')
def handle_inputs(data):
firehose.set_options(data)
global server_queue
del server_queue[:]
socketio.emit('data', {'data': []}, namespace='/stream')
'''
Stream outputs to the client.
'''
@socketio.on('more', namespace='/stream')
def more_data():
global server_queue
tweets = firehose.get_tweets()
server_queue = tweets + server_queue
socketio.emit('data', {'data': server_queue}, namespace='/stream')
if len(server_queue) > MAX_TWEETS_ON_SCREEN:
del server_queue[-1:]
'''
Serve homepage and related assets.
'''
@app.route('/')
def root():
print('FETCH ROOT')
return send_file('index.html')
@app.route('/js/<path:path>')
def js(path):
return send_from_directory('js', path)
@app.route('/css/<path:path>')
def css(path):
return send_from_directory('css', path)
if __name__ == '__main__':
print('APP STARTED')
socketio.run(app)