-
Notifications
You must be signed in to change notification settings - Fork 0
/
show_status.py
138 lines (105 loc) · 3.74 KB
/
show_status.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
from flask import Flask, render_template, jsonify, g, request, redirect, url_for
from urf import URFClient, IcecastClient, get_connection, BartenderJSONEncoder, Slot
from urf.wsgi_util import ReverseProxiedApp
from flask_dance.contrib.google import make_google_blueprint, google
from configparser import ConfigParser
config = ConfigParser()
config.read("config/config.ini")
app = Flask(__name__, template_folder="views")
app.wsgi_app = ReverseProxiedApp(app.wsgi_app, config["oauth"]["scheme"])
blueprint = make_google_blueprint(
client_id=config["oauth"]["client_id"],
client_secret=config["oauth"]["client_secret"],
scope=[
"openid",
"https://www.googleapis.com/auth/userinfo.email",
"https://www.googleapis.com/auth/userinfo.profile"
],
redirect_to="admin"
)
app.secret_key = config["app"]["secret_key"]
app.register_blueprint(blueprint, url_prefix="/login")
urf = URFClient()
icecast = IcecastClient()
allowed_ips = ["139.184.150.23", "127.0.0.1"]
app.json_encoder = BartenderJSONEncoder
@app.route("/")
def index():
if request.access_route[0] not in allowed_ips:
return "Not Studio PC", 404
return render_template("show_confirm.html")
@app.route("/admin")
def admin():
if not google.authorized:
return redirect(url_for("google.login"))
try:
r = google.get("https://people.googleapis.com/v1/people/me?personFields=names")
r.raise_for_status()
except Exception:
return redirect(url_for("google.login"))
user_data = r.json()
db = get_connection()
slots = urf.get_all_shows()
attendance = db.get_show_data()
slots.sort(key=attendance.key_for(), reverse=True)
return render_template("admin_home.html", slots=slots, register=attendance, user=user_data["names"][0])
@app.route("/admin/actions", methods=["POST"])
def admin_force_attend():
if not google.authorized:
return redirect(url_for("google.login"))
if request.form["action"] == "force_attend":
db = get_connection()
slot = urf.get_current_show()
if slot is None:
raise Exception("No slot was found?")
if db.get_logged_attendance(slot) is None:
db.register_attendance(slot)
return redirect(url_for("admin"))
else:
raise Exception("Slot already registered!")
@app.route("/logout")
def logout():
token = blueprint.token["access_token"]
resp = google.post(
"https://accounts.google.com/o/oauth2/revoke",
params={"token": token},
headers={"Content-Type": "application/x-www-form-urlencoded"}
)
if resp.ok:
return "Logged out.", 200
else:
return "There was a problem logging out :(", 500
@app.route("/api/attend", methods=["POST"])
def register():
if request.access_route[0] not in allowed_ips:
return jsonify({"error": "Invalid remote IP address"}), 400
db = get_connection()
slot = urf.get_current_show()
#status = icecast.fetch_status()
#if status.primary_source is None:
# return jsonify({"error": "Failed to contact streaming server."}), 500
if slot is None:
return jsonify({"error": "No active slot?"}), 500
# temporary
if True:
if db.get_logged_attendance(slot) is None:
db.register_attendance(slot)
return jsonify({"show_name": slot.show.name}), 200
else:
return jsonify({"error": "It looks like you're not currently broadcasting. Please start broadcasting before signing in."}), 400
@app.route("/api/status")
def get_status():
db = get_connection()
slot = urf.get_current_show()
if slot is None:
return jsonify({"attended": False, "slot": None, "show": None})
row = db.get_logged_attendance(slot)
if row is None:
return jsonify({"attended": False, "slot": slot, "show": slot.show}), 400
else:
return jsonify({"attended": row.attended, "slot": slot, "show": slot.show, "signed_in_time": row.signed_in_time})
@app.teardown_appcontext
def close_connection(exc):
db = getattr(g, "_database", None)
if db is not None:
db.close()