coach-scraper/app/__main__.py

60 lines
1.4 KiB
Python
Raw Normal View History

import aiohttp
import argparse
import asyncio
2023-12-01 00:12:16 +00:00
import json
2023-12-01 00:12:16 +00:00
from app.chesscom import (
Exporter as ChesscomExporter,
Scraper as ChesscomScraper,
)
from app.lichess import (
Exporter as LichessExporter,
Scraper as LichessScraper,
)
from app.repo import Site
async def run():
parser = argparse.ArgumentParser(
prog="coach-scraper",
2023-12-01 00:12:16 +00:00
description="Scraping/exporting of chess coaches.",
)
parser.add_argument("-u", "--user-agent", required=True)
parser.add_argument(
"-s",
"--site",
required=True,
choices=[
Site.CHESSCOM.value,
2023-11-30 22:36:44 +00:00
Site.LICHESS.value,
],
)
args = parser.parse_args()
async with aiohttp.ClientSession(
headers={"User-Agent": f"BoardWise coach-scraper ({args.user_agent})"}
) as session:
if args.site == Site.CHESSCOM.value:
scraper = ChesscomScraper(session)
2023-12-01 00:12:16 +00:00
exporter_cls = ChesscomExporter
2023-11-30 22:36:44 +00:00
elif args.site == Site.LICHESS.value:
scraper = LichessScraper(session)
2023-12-01 00:12:16 +00:00
exporter_cls = LichessExporter
2023-12-01 00:12:16 +00:00
dump = {}
usernames = await scraper.scrape()
for username in usernames:
dump[username] = exporter_cls(username).export()
with open(scraper.path_site_file("export.json"), "w") as f:
json.dump(dump, f, indent=2)
def main():
asyncio.run(run())
2023-11-28 12:28:21 +00:00
if __name__ == "__main__":
main()