mirror of
https://github.com/Crocmagnon/charasheet.git
synced 2024-11-22 06:28:03 +01:00
Import capabilities
This commit is contained in:
parent
bc0c6108e4
commit
821ff50996
4 changed files with 115 additions and 3 deletions
89
src/character/management/commands/import_capabilities.py
Normal file
89
src/character/management/commands/import_capabilities.py
Normal file
|
@ -0,0 +1,89 @@
|
|||
from django.core.management import BaseCommand
|
||||
from selenium import webdriver
|
||||
from selenium.webdriver import Keys
|
||||
from selenium.webdriver.common.by import By
|
||||
from selenium.webdriver.remote.webelement import WebElement
|
||||
|
||||
from character.models import Capability, Path
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
def handle(self, *args, **options):
|
||||
url = "https://www.co-drs.org/fr/jeu/capacites"
|
||||
self.setup_selenium()
|
||||
self.selenium.get(url)
|
||||
cards = []
|
||||
expected_capability_count = 430
|
||||
while len(cards) < expected_capability_count:
|
||||
self.selenium.find_element(By.TAG_NAME, "body").send_keys(Keys.END)
|
||||
cards = self.selenium.find_elements(
|
||||
By.CSS_SELECTOR, ".col-md-4.col-sm-6.col-12.mb-4.views-row"
|
||||
)
|
||||
for card in cards:
|
||||
try:
|
||||
self.import_capability(card)
|
||||
except Exception as e:
|
||||
print(f"{type(e)}: {e}")
|
||||
self.stdout.write(f"Finished processing {len(cards)} caps.")
|
||||
|
||||
def import_capability(self, card: WebElement):
|
||||
title = (
|
||||
card.find_element(By.CSS_SELECTOR, ".card-front .card-title .fw-bold")
|
||||
.text.strip()
|
||||
.split(" | ")
|
||||
)
|
||||
name = title[0].replace("’", "'").strip()
|
||||
rank = int(title[1].replace("rang ", ""))
|
||||
path = self.get_path(card, name)
|
||||
limited = False
|
||||
if "(L)" in name:
|
||||
limited = True
|
||||
name = name.replace("(L)", "")
|
||||
spell = False
|
||||
if "*" in name:
|
||||
spell = True
|
||||
name = name.replace("*", "")
|
||||
name = name.strip()
|
||||
try:
|
||||
capability, _ = Capability.objects.update_or_create(
|
||||
name=name,
|
||||
defaults={
|
||||
"rank": rank,
|
||||
"path": path,
|
||||
"limited": limited,
|
||||
"spell": spell,
|
||||
},
|
||||
)
|
||||
self.stdout.write(self.style.SUCCESS(f"Created/updated cap {capability}"))
|
||||
except Exception as e:
|
||||
self.stdout.write(
|
||||
self.style.ERROR(f"Couldn't create/update cap {name}: {e}")
|
||||
)
|
||||
|
||||
def get_path(self, card: WebElement, name: str) -> Path | None:
|
||||
try:
|
||||
path_name = (
|
||||
card.find_element(By.CSS_SELECTOR, ".card-back .paths a")
|
||||
.text.replace("’", "'")
|
||||
.strip()
|
||||
)
|
||||
except Exception:
|
||||
self.stdout.write(
|
||||
self.style.WARNING(f"Couldn't find path in card for cap '{name}'.")
|
||||
)
|
||||
return None
|
||||
try:
|
||||
path = Path.objects.get(name__iexact=path_name)
|
||||
return path
|
||||
except Exception:
|
||||
self.stdout.write(
|
||||
self.style.WARNING(
|
||||
f"Couldn't find path name '{path_name}' for cap '{name}'."
|
||||
)
|
||||
)
|
||||
return None
|
||||
|
||||
def setup_selenium(self):
|
||||
options = webdriver.FirefoxOptions()
|
||||
options.add_argument("-headless")
|
||||
self.selenium = webdriver.Firefox(options=options)
|
|
@ -0,0 +1,23 @@
|
|||
# Generated by Django 4.1.2 on 2022-10-29 09:39
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
("character", "0006_alter_path_category"),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterField(
|
||||
model_name="capability",
|
||||
name="limited",
|
||||
field=models.BooleanField(blank=True, default=False),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name="capability",
|
||||
name="spell",
|
||||
field=models.BooleanField(blank=True, default=False),
|
||||
),
|
||||
]
|
|
@ -1 +1 @@
|
|||
0006_alter_path_category
|
||||
0007_alter_capability_limited_alter_capability_spell
|
||||
|
|
|
@ -36,8 +36,8 @@ class Capability(UniquelyNamedModel, TimeStampedModel, models.Model):
|
|||
rank = models.PositiveSmallIntegerField(
|
||||
validators=[MinValueValidator(1), MaxValueValidator(5)]
|
||||
)
|
||||
limited = models.BooleanField(blank=True, null=False)
|
||||
spell = models.BooleanField(blank=True, null=False)
|
||||
limited = models.BooleanField(blank=True, null=False, default=False)
|
||||
spell = models.BooleanField(blank=True, null=False, default=False)
|
||||
description = models.TextField()
|
||||
|
||||
class Meta:
|
||||
|
|
Loading…
Reference in a new issue