-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
41 lines (35 loc) · 1.17 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
from flask import Flask, render_template, g
from spyn_pro_api import SpynProAPI
import threading
import sched, time
import pickle
app = Flask(__name__)
def periodic(scheduler, interval, action, actionargs=()):
scheduler.enter(interval, 1, periodic, (scheduler, interval, action, actionargs))
action(*actionargs)
def save_attendance_data():
spyn_pro_api = SpynProAPI('[email protected]', 'Sagar09@')
spyn_pro_api.fetch_active_classes()
spyn_pro_api.fetch_users()
data = spyn_pro_api.filtered_data
# Saving data to a temporary file
with open('tmp_data.pkl', 'wb') as f:
pickle.dump(data, f)
@app.before_first_request
def activate_job():
scheduler = sched.scheduler(time.time, time.sleep)
scheduler.enter(0, 1, periodic, (scheduler, 900, save_attendance_data)) # 900 seconds = 15 min
t = threading.Thread(target=scheduler.run)
t.start()
@app.route('/get_attendance')
@app.route('/')
def get_attendance():
data = []
try:
with open('tmp_data.pkl', 'rb') as f:
data = pickle.load(f)
except:
pass
return render_template('index.html', data=data)
if __name__ == '__main__':
app.run(debug=True)