forked from kylelady/omnomnorth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.py
executable file
·82 lines (67 loc) · 2.13 KB
/
run.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
#!/usr/bin/env python
import datetime
import json
import os
import time
from flask import Flask, make_response, request, render_template
from utils.translator import make_translator
from location import LocationManager
# Load language files
lang = {}
langs = os.listdir('lang')
for l in langs:
name,ext = os.path.splitext(l)
if ext == '.json':
with open('lang/' + l, 'r') as f:
lang[name] = json.load(f)
app = Flask(__name__)
lm = LocationManager.LocationManager('places')
lm.startPlaceWatch()
def gen_info(region):
''' Generate static-ish info'''
info = {}
start = datetime.date(2012, 06, 2)
info['days'] = (datetime.date.today() - start).days
info['regions'] = lm.getRegions()
info['filters'] = lm.getFilters()
info['area_order'] = lm.getGroupOrder(region)
return info
@app.route('/', methods=['GET', ])
def home():
return site('north', None)
@app.route('/<region>', methods=['GET', ])
def just_region (region):
return site(region, None)
@app.route('/<region>/<loc_filter>', methods=['GET', ])
def site(region, loc_filter):
if 'lang' in request.args and request.args['lang'] in lang:
selected_lang = request.args['lang']
elif 'lang' in request.cookies and request.cookies['lang'] in lang:
selected_lang = request.cookies['lang']
else:
selected_lang = 'en'
if region == '' or region not in lm.getRegions():
region = 'north'
status = lm.getStatuses(region, loc_filter)
trans = make_translator(lang[selected_lang], lang['en'])
title = 'OmNom{0}!'.format(region.title())
info = gen_info(region)
info['title'] = title
info['region'] = region
info['filter'] = loc_filter
try:
with open('analytics.txt') as f:
analytics = f.read()
except IOError:
analytics = ''
resp = make_response(
render_template(
'main.html',
info=info,
places=status,
translate=trans,
analytics=analytics))
resp.set_cookie('lang', selected_lang)
return resp
if __name__ == "__main__":
app.run(debug=True, port=5001, host='0.0.0.0')