-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
112 lines (78 loc) · 2.18 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import threading
import flask
from flask import request, render_template
# imports global variables
import globals
# imports functions from video file
from video import send_completed_video
from video import get_tweets
from video import clean_all, clean_old
configured = False
app = flask.Flask(__name__)
app.config["DEBUG"] = True
def configure_app():
globals.init()
clean_all()
globals.q.join()
# creates and starts threads
threads = []
for i in range(globals.max_threads):
worker = threading.Thread(target=get_tweets)
worker.setDaemon(True)
threads.append(worker)
for t in threads:
t.start()
configured = True
@app.route('/', methods=['GET'])
def home():
if not configured:
configure_app()
return """
<h1>Twitter Video Project</h1>
<p>by Laura Joy Erb</p>
<p>for EC500: Building Software</p>
"""
@app.route('/progress', methods=['GET'])
def progress():
if not configured:
configure_app()
return render_template('progress.html', calls=globals.processes)
@app.route('/tweets/', methods=['GET'])
def twitter_username():
if not configured:
configure_app()
# used to periodically clean out unnecessary files
# cleaning will increase in frequency as calls become more frequent
clean_old()
# default user name if none provided
name = "NatGeo"
if 'username' in request.args:
name = request.args['username']
call = {
"user_name": name,
"id": globals.id,
"status": "queued"
}
ident = str(globals.id)
globals.id += 1
# adds to dict of all process requests
globals.processes[ident] = call
# adds to worker queue to be completed
globals.q.put(call)
globals.q.join()
return send_completed_video(ident)
if __name__ == '__main__':
# resets
globals.init()
clean_all()
globals.q.join()
# creates and starts threads
threads = []
for i in range(globals.max_threads):
worker = threading.Thread(target=get_tweets)
worker.setDaemon(True)
threads.append(worker)
for t in threads:
t.start()
# begins app
app.run()