forked from Noctem/Monocle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gyms.py
141 lines (126 loc) · 4.09 KB
/
gyms.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
139
140
141
from datetime import datetime, timedelta
from pkg_resources import resource_filename
import time
import argparse
from flask import Flask, render_template
from pokeminer.names import POKEMON_NAMES
from pokeminer import config
from pokeminer import db
from pokeminer import utils
app = Flask(__name__, template_folder=resource_filename('pokeminer', 'templates'))
def get_args():
parser = argparse.ArgumentParser()
parser.add_argument(
'-H',
'--host',
help='Set web server listening host',
default='127.0.0.1'
)
parser.add_argument(
'-P',
'--port',
type=int,
help='Set web server listening port',
default=5001
)
parser.add_argument(
'-d', '--debug', help='Debug Mode', action='store_true'
)
parser.set_defaults(debug=False)
return parser.parse_args()
CACHE = {
'data': None,
'generated_at': None,
}
def get_stats():
cache_valid = (
CACHE['data'] and
CACHE['generated_at'] > datetime.now() - timedelta(minutes=15)
)
if cache_valid:
return CACHE['data']
session = db.Session()
forts = db.get_forts(session)
session.close()
count = {t.value: 0 for t in db.Team}
strongest = {t.value: None for t in db.Team}
guardians = {t.value: {} for t in db.Team}
top_guardians = {t.value: None for t in db.Team}
prestige = {t.value: 0 for t in db.Team}
percentages = {}
prestige_percent = {}
total_prestige = 0
last_date = 0
for fort in forts:
if fort['last_modified'] > last_date:
last_date = fort['last_modified']
team = fort['team']
count[team] = count[team] + 1
if team != 0:
# Strongest gym
existing = strongest[team]
should_replace = (
existing is not None and
fort['prestige'] > existing[0] or
existing is None
)
pokemon_id = fort['guard_pokemon_id']
if should_replace:
strongest[team] = (
fort['prestige'],
pokemon_id,
POKEMON_NAMES[pokemon_id],
)
# Guardians
guardian_value = guardians[team].get(pokemon_id, 0)
guardians[team][pokemon_id] = guardian_value + 1
# Prestige
prestige[team] += fort['prestige']
total_prestige = sum(prestige.values())
for team in db.Team:
# TODO: remove float(...) as soon as we move to Python 3
percentages[team.value] = (
count.get(team.value) / float(len(forts)) * 100
)
prestige_percent[team.value] = (
prestige.get(team.value) / float(total_prestige) * 100
)
if guardians[team.value]:
pokemon_id = sorted(
guardians[team.value],
key=guardians[team.value].__getitem__,
reverse=True
)[0]
top_guardians[team.value] = POKEMON_NAMES[pokemon_id]
CACHE['generated_at'] = datetime.now()
CACHE['data'] = {
'order': sorted(count, key=count.__getitem__, reverse=True),
'count': count,
'total_count': len(forts),
'strongest': strongest,
'prestige': prestige,
'prestige_percent': prestige_percent,
'percentages': percentages,
'last_date': last_date,
'top_guardians': top_guardians,
'generated_at': CACHE['generated_at'],
}
return CACHE['data']
@app.route('/')
def index():
stats = get_stats()
team_names = {k.value: k.name.title() for k in db.Team}
styles = {1: 'primary', 2: 'danger', 3: 'warning'}
return render_template(
'gyms.html',
area_name=config.AREA_NAME,
area_size=utils.get_scan_area(),
minutes_ago=int((datetime.now() - stats['generated_at']).seconds / 60),
last_date_minutes_ago=int((time.time() - stats['last_date']) / 60),
team_names=team_names,
styles=styles,
**stats
)
if __name__ == '__main__':
args = get_args()
app.run(debug=True, host=args.host, port=args.port)