charasheet/src/character/management/commands/import_profiles.py
2022-10-29 11:02:44 +02:00

80 lines
3 KiB
Python

from django.core.management import BaseCommand
from selenium import webdriver
from selenium.webdriver.common.by import By
from character.models import Profile
from character.models.dice import Dice
class Command(BaseCommand):
def handle(self, *args, **options) -> None:
url = "https://www.co-drs.org/fr/jeu/profils"
self.setup_selenium()
self.selenium.get(url)
anchors = self.selenium.find_elements(By.CSS_SELECTOR, ".card-img-top a")
urls = [anchor.get_attribute("href") for anchor in anchors]
for url in urls:
try:
self.import_profile(url)
except Exception as e:
print(f"{type(e)}: {e}")
def import_profile(self, url: str) -> None:
self.selenium.get(url)
name = self.selenium.find_element(By.TAG_NAME, "h1").text
dice = self.get_dice(name)
magical_strength = self.get_magical_strength()
notes = self.get_notes(name)
profile, _ = Profile.objects.update_or_create(
name=name,
defaults={
"life_dice": dice,
"magical_strength": magical_strength,
"notes": notes,
},
)
self.stdout.write(self.style.SUCCESS(f"Created/updated profile {profile}"))
def get_dice(self, name: str) -> Dice:
dice = self.selenium.find_element(By.CSS_SELECTOR, ".dice + div").text.split(
"D"
)
number_of_dice, dice_value = int(dice[0]), int(dice[1])
if number_of_dice != 1:
self.stdout.write(
self.style.WARNING(f"Multiple dice for {name}: {number_of_dice}")
)
dice = Dice(dice_value)
return dice
def get_magical_strength(self) -> Profile.MagicalStrength:
try:
magical_strength = self.selenium.find_element(
By.CSS_SELECTOR, ".field--name-magic-attack-modifier .field__item"
).text
magical_strength = Profile.MagicalStrength(magical_strength)
except Exception:
magical_strength = Profile.MagicalStrength.NONE
return magical_strength
def get_notes(self, name: str) -> str:
notes = ""
fields = ["weapons-and-armors", "starting-equipment"]
for field_name in fields:
try:
field_class = f"field--name-{field_name}"
field = self.selenium.find_element(By.CLASS_NAME, field_class)
title = field.find_element(By.CLASS_NAME, "field__label").text
text = field.find_element(By.CLASS_NAME, "field__item").text
notes += f"\n\n# {title}\n{text}"
except Exception:
self.stdout.write(f"No {field_name} found for {name}")
notes = notes.strip()
return notes
def setup_selenium(self) -> None:
options = webdriver.FirefoxOptions()
options.add_argument("-headless")
self.selenium = webdriver.Firefox(options=options)