-
Notifications
You must be signed in to change notification settings - Fork 13
/
sbds.py
109 lines (99 loc) · 3.24 KB
/
sbds.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
import dataclasses
import itertools
import re
import typing
import urllib.parse
import requests
if typing.TYPE_CHECKING:
import bot
@dataclasses.dataclass
class SBDS:
translations: dict = None
spells: dict = None
buffs: dict = None
def matches_key(self, query: str, key: str):
translations: dict[str, dict[str, str]] = self.translations['translations']
try:
translated = translations[key]
except KeyError:
if query == key.casefold():
return 'en'
else:
for lang, translation in translated.items():
if query == translation.casefold():
return lang
def translate(self, key: str, lang: str):
try:
translated: dict[str, str] = self.translations['translations'][key]
except KeyError:
return key
else:
translation = translated[lang]
return translation.replace('[b]', '**').replace('[/b]', '**')
def translate_all(self, s: str, lang: str):
def ttl(m: re.Match):
return str(self.translate(m.group(0), lang))
return re.sub(r'\b[A-Z_]+\b', ttl, s)
def sbds(cmd: 'bot.CommandEvent'):
if not cmd.args:
return
name = cmd.args
embed = get_embed(cmd.args)
if embed is None:
cmd.reply(f'couldn\'t find "{name}"')
else:
cmd.reply('', embed)
def get_embed(name: str) -> dict:
data = _get_data()
name = name.casefold()
for spell_id, spell in itertools.chain(data.spells['SPELL'].items(), data.spells['EVOLVED'].items()):
if spell['spellName'] is None:
continue
if lang := data.matches_key(name, spell['spellName']):
translatedName = data.translate(spell['spellName'], lang)
return {
'title': translatedName,
'description': data.translate(spell['levelUpDescriptions'][0], lang),
'thumbnail': {'url': f'https://sbds.fly.dev/static/data/spells/{spell_id}.png'},
'url': 'https://sbds.fly.dev/spells?' + urllib.parse.urlencode(
{'q': translatedName, 'lang': lang}),
}
for aura_pair in data.spells['AURA']:
for aura in aura_pair:
if lang := data.matches_key(name, aura['titleText']):
translatedName = data.translate(aura['titleText'], lang)
return {
'title': translatedName,
'description': data.translate_all(aura['description'], lang),
'thumbnail': {'url': f'https://sbds.fly.dev/static/data/spells/{aura["titleText"]}.png'},
'url': 'https://sbds.fly.dev/spells?' + urllib.parse.urlencode(
{'q': translatedName, 'lang': lang}),
}
for player_buff, enemy_buff in data.buffs:
for buff in (player_buff, enemy_buff):
if not buff:
continue
if lang := data.matches_key(name, buff['shrineText']):
translated = data.translate(buff['shrineText'], lang)
embed = {
'title': translated,
'thumbnail': {'url': f'https://sbds.fly.dev/static/data/buffs/{player_buff["shrineText"]}.png'},
'url': 'https://sbds.fly.dev/buffs?' + urllib.parse.urlencode(
{'q': translated, 'lang': lang}),
}
if 'notificationText' in buff:
embed['description'] = data.translate(buff['notificationText'], lang)
return embed
_cache = None
def _get_data():
global _cache
if _cache is not None:
return _cache
data = SBDS()
rs = requests.Session()
for filename in SBDS.__dataclass_fields__:
r = rs.get(f'https://sbds.fly.dev/static/data/{filename}.json', timeout=5)
r.raise_for_status()
setattr(data, filename, r.json())
_cache = data
return data