/
mak13
/
leagues
Обзор
Документация
Войти
/
mak13
/
leagues
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
main.py
55 строк
2 KB
mak7154
Рабочий вариант
05 апр 2025, 21:45
05 апр 2025, 21:45
e7958fd
Код
Авторство
О чём код?
# -*- encoding: utf-8 -*- from sstats.config import configure from asyncio import run, gather from sstats.api import * from json import loads as jloads, dumps as jdumps from jinja2 import Environment, FileSystemLoader from pathlib import Path def search_tournament(leagues, name, country): for t in leagues: if t['name'] == name and t['country']['name'] == country: return t raise NotImplementedError(F'{name} {country}') async def get_season(cfg, leagues, name, country, season): t_json = search_tournament(leagues, name, country) data = await get_scores(cfg, t_json, season) stem = F'{name.replace(" ", "")}-{country.replace(" ", "")}-{season}' Path(F'{stem}.raw.json').write_text(jdumps(data), encoding='utf-8') env = Environment(loader=FileSystemLoader(".")) tpl = env.get_template("season.txt.j2") Path(F'{stem}.txt').write_text(tpl.render({'games': data}), encoding='utf-8') async def main(): cfg = configure() cfg.headers['apikey'] = cfg.apikey leagues = cfg.cache / 'leagues.json' if not leagues.exists(): leagues_json = await get_leagues(cfg) leagues.write_text(jdumps(leagues_json), encoding='utf-8') else: leagues_json = jloads(leagues.read_text(encoding='utf-8')) env = Environment(loader=FileSystemLoader(".")) tpl = env.get_template("leagues.txt.j2") Path('leagues.txt').write_text(tpl.render({'leagues': leagues_json}), encoding='utf-8') aleksey_wishes = ( (('Bundesliga', 'Germany'), (2023,2024)), (('Bundesliga', 'Austria'), (2024,)), (('Premier League', 'Great Britain'), (2024,)), ) tasks = [] for (name, country), seasons in aleksey_wishes: for season in seasons: tasks.append(get_season(cfg, leagues_json, name, country, season)) await gather(*tasks) if __name__ == '__main__': run(main())