coach-scraper/app/exporter.py

56 lines
1.5 KiB
Python
Raw Normal View History

2023-12-01 00:12:16 +00:00
from typing import Union
2023-12-01 23:36:53 +00:00
2023-12-01 00:12:16 +00:00
from typing_extensions import TypedDict
2023-12-01 23:36:53 +00:00
from app.repo import AnsiColor, Repo
2023-12-01 00:12:16 +00:00
class Export(TypedDict, total=False):
2023-12-01 14:10:58 +00:00
# The coach's rapid rating relative to the site they were sourced from.
2023-12-01 03:35:20 +00:00
rapid: int
2023-12-01 14:10:58 +00:00
# The coach's blitz rating relative to the site they were sourced from.
blitz: int
# The coach's bullet rating relative to the site they were sourced from.
bullet: int
def _insert(export: Export, key: str, value: any):
if value is None:
return
export[key] = value
2023-12-01 00:12:16 +00:00
class BaseExporter(Repo):
def __init__(self, site: str, username: str):
super().__init__(site)
self.username = username
2023-12-01 03:35:20 +00:00
def export_rapid(self) -> Union[int, None]:
2023-12-01 00:12:16 +00:00
raise NotImplementedError()
2023-12-01 14:10:58 +00:00
def export_blitz(self) -> Union[int, None]:
raise NotImplementedError()
def export_bullet(self) -> Union[int, None]:
raise NotImplementedError()
2023-12-01 00:12:16 +00:00
def export(self) -> Export:
"""Transform coach-specific data into uniform format."""
export: Export = {}
_insert(export, "site", self.site)
_insert(export, "username", self.username)
2023-12-01 14:10:58 +00:00
_insert(export, "rapid", self.export_rapid())
_insert(export, "blitz", self.export_blitz())
_insert(export, "bullet", self.export_bullet())
2023-12-01 00:12:16 +00:00
self.log(
[
(AnsiColor.INFO, "[INFO]"),
(None, ": Exported "),
(AnsiColor.DATA, self.username),
]
)
return export