-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
94 lines (76 loc) · 2.37 KB
/
app.py
File metadata and controls
94 lines (76 loc) · 2.37 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import datetime
import json
import sys
import threading
import time
import traceback
import pusher
from flask import Flask, render_template
from flask_assets import Bundle, Environment
from flask_bootstrap import Bootstrap
from sheet import Sheets
from Announcement import Announcement
sheet: Sheets = Sheets('1wpSA1YsQguMT4tqulLR-1niqKHO8oM5qbGne3SWcOzE', 'A1:F')
sheet.credentials()
sheet.build_service()
posts: [Announcement] = sheet.get_past()
print('Preloaded posts:', posts, file=sys.stderr)
# run code in background
def background():
global posts
global sheet
while True:
print('loading announcements... ', end='')
try:
announcements = sheet.get_current_active(datetime.timedelta(minutes=1))
if len(announcements) > 0:
announcement = announcements.pop(0)
text = str(json.dumps(announcement.__dict__, default=str))
send_announcement(text)
sheet.set_active(announcement, False)
posts = sheet.get_past()
print()
print('Updated posts:', posts, file=sys.stderr)
else:
print("no announcements")
time.sleep(3)
except:
traceback.print_last(file=sys.stderr)
time.sleep(5)
thread = threading.Thread(target=background)
thread.setDaemon(True)
thread.start()
# create app
app = Flask(__name__)
# setup app bootstrap
Bootstrap(app)
assets = Environment(app)
# setup app assets
assets.url = app.static_url_path
scss = Bundle('scss/main.scss', filters='pyscss', output='build/all.css')
assets.register('scss_all', scss)
@app.route('/')
def index():
global posts
return render_template('index.html', posts=posts)
# @app.route('/announce')
def announce():
global sheet
data = sheet.get_first()
text = str(json.dumps(data.__dict__, default=str))
send_announcement(text)
return text
def send_announcement(message):
print('Announcing: ' + str(message))
channels_client = pusher.Pusher(
app_id='885408',
key='e26d2d5c77e1ace54c55',
secret='1e6aa8be2c8dcd2c52c0',
cluster='eu',
ssl=True
)
channels_client.trigger('announcements', 'new', message)
if __name__ == "__main__":
app.run(app, host='0.0.0.0', port=5000)
# setup()
# announce(str(json.dumps(s.get_all()[10].__dict__, default=str)))