-
Notifications
You must be signed in to change notification settings - Fork 1
/
hockey-mchockeyface.py
executable file
·87 lines (65 loc) · 2.02 KB
/
hockey-mchockeyface.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
#!/usr/bin/env python3
import logging
import os
from urllib.parse import urlparse
from flask import (
Flask,
Response,
redirect,
render_template,
request,
send_from_directory,
)
from hockeyface import hockeyface
redirect_to = os.environ.get("REDIRECT_TO")
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)
app = Flask(__name__)
hf = hockeyface()
def ensure_domain(request):
if redirect_to:
requested_domain = urlparse(request.url).netloc
path = urlparse(request.url).path
query = urlparse(request.url).query
if requested_domain != redirect_to:
logger.debug(f"Creating direct from {requested_domain} to {redirect_to}")
return redirect(f"https://{redirect_to}{path}?{query}", code=301)
@app.route("/")
def index_page():
_redirect = ensure_domain(request)
if _redirect:
return _redirect
return render_template("index.html")
@app.route("/favicon.ico")
def favicon():
_redirect = ensure_domain(request)
if _redirect:
return _redirect
return send_from_directory(
os.path.join(app.root_path, "static"),
"images/noun_55243_cc.png",
mimetype="image/png",
)
@app.route("/calendar")
def calendar():
_redirect = ensure_domain(request)
if _redirect:
return _redirect
teams = request.args.getlist("team")
leagues = request.args.getlist("league")
# Default to SHL and HA to keep previous behavior
if not leagues:
leagues = ["SHL", "HA"]
# Teams in SHL might attend in CHL
if teams and "SHL" in leagues:
leagues.append("CHL")
events = hf.get_events(teams, leagues)
ical = hf.build_ical(events, teams, leagues)
response = Response(response=ical, status=200, mimetype="text/calendar")
response.headers[
"Content-disposition"
] = "attachment; filename=hockey-mchockeyface.ics"
return response
@app.route("/feeds")
def feeds_page():
return render_template("feeds.html", team_data = hf.teamdata)