mirror of
https://github.com/Crocmagnon/charasheet.git
synced 2024-11-05 06:13:55 +01:00
Add import weapons
This commit is contained in:
parent
369449442e
commit
627bda3529
6 changed files with 72 additions and 5 deletions
|
@ -14,12 +14,12 @@ class Command(BaseCommand):
|
||||||
states = self.selenium.find_elements(By.CSS_SELECTOR, "tbody tr")
|
states = self.selenium.find_elements(By.CSS_SELECTOR, "tbody tr")
|
||||||
for state in states:
|
for state in states:
|
||||||
try:
|
try:
|
||||||
self.import_race(url, state)
|
self.import_row(url, state)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"{type(e)}: {e}")
|
print(f"{type(e)}: {e}")
|
||||||
self.stdout.write(f"Finished processing {len(states)} states.")
|
self.stdout.write(f"Finished processing {len(states)} states.")
|
||||||
|
|
||||||
def import_race(self, url: str, state_row: WebElement) -> None:
|
def import_row(self, url: str, state_row: WebElement) -> None:
|
||||||
name = state_row.find_element(By.CLASS_NAME, "views-field-name").text.strip()
|
name = state_row.find_element(By.CLASS_NAME, "views-field-name").text.strip()
|
||||||
description = state_row.find_element(
|
description = state_row.find_element(
|
||||||
By.CLASS_NAME, "views-field-description__value"
|
By.CLASS_NAME, "views-field-description__value"
|
||||||
|
|
48
src/character/management/commands/import_weapons.py
Normal file
48
src/character/management/commands/import_weapons.py
Normal file
|
@ -0,0 +1,48 @@
|
||||||
|
from django.core.management import BaseCommand
|
||||||
|
from selenium import webdriver
|
||||||
|
from selenium.webdriver.common.by import By
|
||||||
|
from selenium.webdriver.remote.webelement import WebElement
|
||||||
|
|
||||||
|
from character.models import Weapon
|
||||||
|
|
||||||
|
|
||||||
|
class Command(BaseCommand):
|
||||||
|
def handle(self, *args, **options) -> None:
|
||||||
|
url = "https://www.co-drs.org/fr/ressources/equipements/armes"
|
||||||
|
self.setup_selenium()
|
||||||
|
self.selenium.get(url)
|
||||||
|
states = self.selenium.find_elements(By.CSS_SELECTOR, "tbody tr")
|
||||||
|
for state in states:
|
||||||
|
try:
|
||||||
|
self.import_row(url, state)
|
||||||
|
except Exception as e:
|
||||||
|
print(f"{type(e)}: {e}")
|
||||||
|
self.stdout.write(f"Finished processing {len(states)} weapons.")
|
||||||
|
|
||||||
|
def import_row(self, url: str, state_row: WebElement) -> None:
|
||||||
|
name = state_row.find_element(By.CLASS_NAME, "views-field-name").text.strip()
|
||||||
|
category = (
|
||||||
|
state_row.find_element(By.CLASS_NAME, "views-field-type")
|
||||||
|
.text.strip()
|
||||||
|
.lower()
|
||||||
|
)
|
||||||
|
if "distance" in category:
|
||||||
|
category = Weapon.Category.RANGE
|
||||||
|
else:
|
||||||
|
category = Weapon.Category.MELEE
|
||||||
|
damage = state_row.find_element(By.CLASS_NAME, "views-field-dmg").text.strip()
|
||||||
|
weapon, _ = Weapon.objects.update_or_create(
|
||||||
|
name=name,
|
||||||
|
defaults={
|
||||||
|
"damage": damage,
|
||||||
|
"special": "",
|
||||||
|
"category": category,
|
||||||
|
"url": url,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
self.stdout.write(self.style.SUCCESS(f"Created/updated weapon {weapon}"))
|
||||||
|
|
||||||
|
def setup_selenium(self) -> None:
|
||||||
|
options = webdriver.FirefoxOptions()
|
||||||
|
options.add_argument("-headless")
|
||||||
|
self.selenium = webdriver.Firefox(options=options)
|
18
src/character/migrations/0032_weapon_url.py
Normal file
18
src/character/migrations/0032_weapon_url.py
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
# Generated by Django 4.1.2 on 2022-11-02 21:08
|
||||||
|
|
||||||
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
("character", "0031_alter_harmfulstate_options"),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.AddField(
|
||||||
|
model_name="weapon",
|
||||||
|
name="url",
|
||||||
|
field=models.URLField(blank=True, verbose_name="url"),
|
||||||
|
),
|
||||||
|
]
|
|
@ -1 +1 @@
|
||||||
0031_alter_harmfulstate_options
|
0032_weapon_url
|
||||||
|
|
|
@ -1,10 +1,10 @@
|
||||||
from django.db import models
|
from django.db import models
|
||||||
from django_extensions.db.models import TimeStampedModel
|
from django_extensions.db.models import TimeStampedModel
|
||||||
|
|
||||||
from common.models import UniquelyNamedModel
|
from common.models import DocumentedModel, UniquelyNamedModel
|
||||||
|
|
||||||
|
|
||||||
class Weapon(UniquelyNamedModel, TimeStampedModel, models.Model):
|
class Weapon(UniquelyNamedModel, DocumentedModel, TimeStampedModel, models.Model):
|
||||||
class Category(models.TextChoices):
|
class Category(models.TextChoices):
|
||||||
MELEE = "MEL", "corps à corps"
|
MELEE = "MEL", "corps à corps"
|
||||||
RANGE = "RAN", "à distance"
|
RANGE = "RAN", "à distance"
|
||||||
|
|
1
tasks.py
1
tasks.py
|
@ -128,6 +128,7 @@ def import_from_co_drs(ctx):
|
||||||
ctx.run("./manage.py import_paths", pty=True, echo=True)
|
ctx.run("./manage.py import_paths", pty=True, echo=True)
|
||||||
ctx.run("./manage.py import_capabilities", pty=True, echo=True)
|
ctx.run("./manage.py import_capabilities", pty=True, echo=True)
|
||||||
ctx.run("./manage.py import_harmful_states", pty=True, echo=True)
|
ctx.run("./manage.py import_harmful_states", pty=True, echo=True)
|
||||||
|
ctx.run("./manage.py import_weapons", pty=True, echo=True)
|
||||||
|
|
||||||
|
|
||||||
@task(pre=[import_from_co_drs, dump_initial])
|
@task(pre=[import_from_co_drs, dump_initial])
|
||||||
|
|
Loading…
Reference in a new issue