mirror of
https://github.com/Crocmagnon/charasheet.git
synced 2024-12-22 13:51:49 +01:00
Import races
This commit is contained in:
parent
9aeaceaab3
commit
f601b71928
1 changed files with 39 additions and 0 deletions
39
src/character/management/commands/import_races.py
Normal file
39
src/character/management/commands/import_races.py
Normal file
|
@ -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)
|
Loading…
Reference in a new issue