From f601b7192868e0bb49ddf3988485b75c400cd66e Mon Sep 17 00:00:00 2001 From: Gabriel Augendre Date: Sat, 29 Oct 2022 11:05:58 +0200 Subject: [PATCH] Import races --- .../management/commands/import_races.py | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 src/character/management/commands/import_races.py diff --git a/src/character/management/commands/import_races.py b/src/character/management/commands/import_races.py new file mode 100644 index 0000000..686354f --- /dev/null +++ b/src/character/management/commands/import_races.py @@ -0,0 +1,39 @@ +from django.core.management import BaseCommand +from selenium import webdriver +from selenium.webdriver.common.by import By + +from character.models import Race + + +class Command(BaseCommand): + def handle(self, *args, **options) -> None: + url = "https://www.co-drs.org/fr/jeu/races" + self.setup_selenium() + self.selenium.get(url) + anchors = self.selenium.find_elements(By.CSS_SELECTOR, "h2 a") + urls = [anchor.get_attribute("href") for anchor in anchors] + for url in urls: + try: + self.import_race(url) + except Exception as e: + print(f"{type(e)}: {e}") + self.stdout.write(f"Finished processing {len(urls)} races.") + + def import_race(self, url: str) -> None: + self.selenium.get(url) + name = self.selenium.find_element(By.TAG_NAME, "h1").text.strip() + name = self.fix_name(name) + race, _ = Race.objects.update_or_create(name=name, defaults={}) + self.stdout.write(self.style.SUCCESS(f"Created/updated race {race}")) + + def fix_name(self, name: str) -> str: + if name == "Elfe, haut": + return "Haut-Elfe" + if name == "Elfe, sylvain": + return "Elfe Sylvain" + return name + + def setup_selenium(self) -> None: + options = webdriver.FirefoxOptions() + options.add_argument("-headless") + self.selenium = webdriver.Firefox(options=options)