forked from raylu/poecalc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpoecalc.py
executable file
·105 lines (82 loc) · 3.09 KB
/
poecalc.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
#!/usr/bin/env python3
# type: ignore
import sys
if len(sys.argv) == 3:
import eventlet
import eventlet.wsgi
eventlet.monkey_patch()
# pylint: disable=wrong-import-position,wrong-import-order
import mimetypes
import warnings
from httpx import HTTPStatusError
from pigwig import PigWig, Response
from pigwig.exceptions import HTTPException
import auras
import gems
import stats
def root(request):
return Response.render(request, 'index.jinja2', {})
def analyze_auras(request, account: str, character: str):
# eventlet encodes PATH_INFO as latin1
# https://github.com/eventlet/eventlet/blob/890f320b/eventlet/wsgi.py#L690
# because PEP-0333 says so https://github.com/eventlet/eventlet/pull/497
with warnings.catch_warnings(record=True) as warning_list:
account = account.encode('latin1').decode('utf-8')
character = character.encode('latin1').decode('utf-8')
try:
char_stats, char, skills = stats.fetch_stats(account, character)
except HTTPStatusError:
return Response.render(request, 'auras.jinja2', {
'warnings': 'Could not fetch character. Make sure the spelling is correct.',
'account': account,
'character': character,
})
if 'aura_effect' in request.query and request.query['aura_effect'] != '':
char_stats.aura_effect = int(request.query['aura_effect'])
active_skills = []
for item in char['items']:
active_skills += gems.parse_skills_in_item(item, char_stats)
aura_results, vaal_aura_results = analyzer.analyze_auras(char_stats, char, active_skills, skills)
curse_results = analyzer.analyze_curses(char_stats, active_skills)
mine_results = analyzer.analyze_mines(char_stats, active_skills)
link_results = analyzer.analyze_links(char_stats, active_skills)
return Response.render(request, 'auras.jinja2', {
'results': result_to_str(aura_results),
'vaal_results': result_to_str(vaal_aura_results),
'curse_results': result_to_str(curse_results),
'mine_results': result_to_str(mine_results),
'link_results': result_to_str(link_results),
'aura_effect': request.query['aura_effect'],
'warnings': prepare_warnings(warning_list),
'account': account,
'character': character,
})
def prepare_warnings(warning_list: list) -> str:
if not warning_list:
return ''
return 'Warnings:\n - ' + '\n - '.join(str(warning.message) for warning in warning_list)
def result_to_str(results: list[list[str]]) -> str:
return '\n\n'.join('\n'.join(result) for result in results)
def static(request, path: str):
content_type, _ = mimetypes.guess_type(path)
try:
with open('static/' + path, 'rb') as f:
return Response(f.read(), content_type=content_type)
except FileNotFoundError:
raise HTTPException(404, '%r not found\n' % path) # pylint: disable=raise-missing-from
routes = [
('GET', '/', root),
('GET', '/auras/<account>/<character>', analyze_auras),
('GET', '/static/<path:path>', static),
]
app = PigWig(routes, template_dir='templates')
analyzer = auras.Auras()
def main() -> None:
if len(sys.argv) == 3:
addr = sys.argv[1]
port = int(sys.argv[2])
eventlet.wsgi.server(eventlet.listen((addr, port)), app)
else:
app.main()
if __name__ == '__main__':
main()