Spaces:
Sleeping
Sleeping
| from collections import defaultdict | |
| _STARTLINE = r'<div class="infocard-filter-block">' | |
| _EVO_STARTLINE = '<div class="infocard "><span class="infocard-lg-img">' | |
| def _remove_a(s: str): | |
| while "<a" in s: | |
| start = s.index("<a") | |
| end = s.index(">", start) + 1 | |
| sl = list(s) | |
| sl[start:end] = [] | |
| s = ''.join(sl) | |
| return s.replace("</a>", "") | |
| def _proc_evo_line(evo_line: str): | |
| res = defaultdict(list) | |
| assert evo_line.startswith('<div class="infocard-list-evo">'), evo_line | |
| evo_line = evo_line.replace('div class="infocard-list-evo">', '') | |
| chain = evo_line.split(_EVO_STARTLINE)[1:] | |
| for pokemon in chain: | |
| if '<i class="icon-arrow icon-arrow-e"></i><small>' not in pokemon: | |
| continue | |
| name = pokemon[:pokemon.find("</a><br>")] | |
| name = name.rsplit(">", 1)[-1] | |
| evolve_method = pokemon.split('<i class="icon-arrow icon-arrow-e"></i><small>')[-1] | |
| evolve_method = _remove_a(evolve_method) | |
| evolve_method = evolve_method.split("<", 1)[0].replace("\n", " ") | |
| while " " in evolve_method: | |
| evolve_method = evolve_method.replace(" ", " ") | |
| res[name].append(evolve_method) | |
| return res | |
| def _process_method(method:str): | |
| assert method.startswith("(") and method.endswith(")"), method | |
| method = method[1:-1] | |
| if "Level" in method: | |
| idx = method.index("Level") + len("Level") | |
| level_remainder = ''.join([c for c in method[idx+1:idx+3] if not c.isnumeric()]) | |
| lmethod = list(method) | |
| lmethod[method.index("Level"):idx+3] = f"Evolves with level{level_remainder}" | |
| method = "".join(lmethod) | |
| return method | |
| def get_pokemon_evolutions(): | |
| pokemons = defaultdict(list) | |
| # load html | |
| with open("pokemondb.net evolution.html", "r", encoding='utf-8') as f: | |
| html = f.read() | |
| # all evo line strings | |
| evo_lines = [i.strip() for i in html.split(_STARTLINE)][1:] | |
| for evo_line in evo_lines: | |
| pokemons.update(_proc_evo_line(evo_line)) | |
| proc = {} | |
| name_map = {} | |
| for pokemon, methods in pokemons.items(): | |
| proc[pokemon.lower().strip()] = [_process_method(m) for m in methods] | |
| name_map[pokemon.lower().strip()] = pokemon | |
| return proc, name_map | |