From 35613677472450a066967795cad63f8f70c2bcc4 Mon Sep 17 00:00:00 2001 From: Alban Gruin Date: Sat, 9 Sep 2017 19:38:02 +0200 Subject: Création des objets Year lorsqu’ils sont rencontrés --- management/commands/scraptimetables.py | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/management/commands/scraptimetables.py b/management/commands/scraptimetables.py index cb5753d..4a07b3b 100644 --- a/management/commands/scraptimetables.py +++ b/management/commands/scraptimetables.py @@ -14,7 +14,8 @@ # along with celcatsanitizer. If not, see . from django.core.management.base import BaseCommand -from ._private import get_xml +from edt.models import Timetable, Year +from ._private import get_from_db_or_create, get_xml import re @@ -26,7 +27,14 @@ class Command(BaseCommand): parser.add_argument("--url", type=str, required=True) def handle(self, *args, **options): - url = options["url"] + timetables = {} + for year, name, finder in self.__get_finders(options["url"]): + if year in timetables: + timetables[year].append((name, finder,)) + else: + timetables[year] = [(name, finder,)] + + def __get_finders(self, url): soup = get_xml(url) choose_regex = re.compile("^- Choisissez votre ([\w ]+) -$") @@ -37,7 +45,12 @@ class Command(BaseCommand): search = choose_regex.search(option.text) if search is not None: - print() - print("Groupe :", search.groups(0)[0]) + current_year = get_from_db_or_create(Year, name=search.groups(0)[0]) else: - print(option.text, option["value"]) + finder = option["value"].replace("finder", "finder2") + if option.text.startswith(current_year.name): + name = option.text[len(current_year.name):].strip() + else: + name = option.text + + yield current_year, name, finder -- cgit v1.2.1