diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 75d242f6a94c458ac2e57a53ed5f3f253ed6988b..7f6a768867bc2aa07793a8064b6d3e8e484efd91 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -134,6 +134,11 @@ test_pref31: PREF: "pref31" extends: .default_pref +test_pref33: + variables: + PREF: "pref33" + extends: .default_pref + test_pref34: variables: PREF: "pref34" diff --git a/Makefile b/Makefile index 5b48bd2d745f9bd79e4b3f71d548a77df4daf9c8..8933ca77db56b829aa1a3c08ad8a3dc42f646a23 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -make: ppparis pref04 pref05 pref06 pref09 pref13 pref31 pref34 pref35 pref38 pref42 pref44 pref59 pref62 pref65 pref66 pref69 pref80 pref81 pref83 pref87 pref976 +make: ppparis pref04 pref05 pref06 pref09 pref13 pref31 pref33 pref34 pref35 pref38 pref42 pref44 pref59 pref62 pref65 pref66 pref69 pref80 pref81 pref83 pref87 pref976 ppparis: python cli.py --pref ppparis pref04: @@ -13,6 +13,8 @@ pref13: python cli.py --pref pref13 pref31: python cli.py --pref pref31 +pref33: + python cli.py --pref pref33 pref34: python cli.py --pref pref34 pref35: diff --git a/RAAspotter_pref33.py b/RAAspotter_pref33.py new file mode 100644 index 0000000000000000000000000000000000000000..e68827c46c5c7ed8b4c4e7483babfe38710617cf --- /dev/null +++ b/RAAspotter_pref33.py @@ -0,0 +1,112 @@ +import os +import re +import datetime +import logging + +from bs4 import BeautifulSoup +from urllib.parse import unquote + +from RAAspotter import RAAspotter + +logger = logging.getLogger(__name__) + + +class RAAspotter_pref33(RAAspotter): + + # Config + __HOST = 'https://www.gironde.gouv.fr' + __RAA_PAGE = f'{__HOST}/Publications/Recueil-des-Actes-Administratifs' + __USER_AGENT = 'Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/115.0' + full_name = 'Préfecture de la Gironde' + short_code = 'pref33' + + def __init__(self, data_dir): + super().__init__(data_dir, self.__USER_AGENT) + self.enable_tor(10) + + def get_raa(self, keywords): + self.print_output('RAAspotter_pref33') + self.print_output(f'Termes recherchés: {keywords}') + self.print_output('') + + pages_to_parse = [] + + # Parfois un RAA est mal catégorisé et se retrouve sur la page racine, donc on la parse + pages_to_parse.append(self.__RAA_PAGE) + + # On détermine quelles pages d'année parser + year_pages_to_parse = [] + page_content = self.get_page(self.__RAA_PAGE, 'get').content + year_pages = self.get_sub_pages( + page_content, + '.fr-card.fr-card--sm.fr-card--grey.fr-enlarge-link div.fr-card__body div.fr-card__content h2.fr-card__title a', + self.__HOST, + False + ) + for year_page in year_pages: + year = 9999 + try: + year = int(re.search('.*([0-9]{4})', year_page['name'].strip(), re.IGNORECASE).group(1)) + if year is None: + year = 9999 + except Exception as exc: + logger.warning(f"Impossible de deviner l\'année de la page {year_page['name']}") + year = 9999 + + if year >= self.not_before.year: + year_pages_to_parse.append(year_page['url']) + + # Pour chaque année, on cherche les sous-pages de mois + month_pages_to_parse = [] + for year_page in year_pages_to_parse: + page_content = self.get_page(year_page, 'get').content + month_pages = self.get_sub_pages( + page_content, + '.fr-card.fr-card--sm.fr-card--grey.fr-enlarge-link div.fr-card__body div.fr-card__content h2.fr-card__title a', + self.__HOST, + False + )[::-1] + + for month_page in month_pages: + guessed_date = RAAspotter.guess_date(month_page['name'], '([a-zéû]* [0-9]{4})') + if guessed_date >= self.not_before.replace(day=1): + pages_to_parse.append(month_page['url']) + + # On parse les pages sélectionnées + elements = self.get_raa_with_pager( + pages_to_parse, + "ul.fr-pagination__list li a.fr-pagination__link.fr-pagination__link--next.fr-pagination__link--lg-label", + self.__HOST + )[::-1] + + self.parse_raa(elements, keywords.split(',')) + self.mailer() + + def get_raa_elements(self, page_content): + elements = [] + + # On récupère chaque carte avec un RAA + for card in BeautifulSoup(page_content, 'html.parser').select('div.fr-card.fr-card--horizontal div.fr-card__body div.fr-card__content'): + # On récupère le lien + links = card.select('h2.fr-card__title a.fr-card__link.menu-item-link') + # On récupère la date + dates_raw = card.select('div.fr-card__end p.fr-card__detail') + + # Si on a toutes les infos, on continue + if links and links[0] and dates_raw and dates_raw[0]: + a = links[0] + date_raw = dates_raw[0] + + if a.get('href') and a['href'].endswith('.pdf'): + if a['href'].startswith('/'): + url = f"{self.__HOST}{a['href']}" + else: + url = a['href'] + + url = unquote(url) + name = a.get_text().strip() + date = datetime.datetime.strptime(date_raw.get_text().replace('Publié le', '').strip(), '%d/%m/%Y') + + raa = RAAspotter.RAA(url, date, name) + elements.append(raa) + return elements diff --git a/README.md b/README.md index 439399f61cca7a158afd2c35f6b95ee3e3f5498e..d72a8f875ffcc7f69fcf787326b1b19a1313408e 100644 --- a/README.md +++ b/README.md @@ -39,6 +39,7 @@ Il est possible de ne lancer l'analyse que pour une seule administration, avec l - Préfecture de l'Ariège (identifiant : `pref09`) - Préfecture des Bouches-du-Rhône (identifiant : `pref13`) - Préfecture de la Haute-Garonne (identifiant : `pref31`) +- Préfecture de la Gironde (identifiant : `pref33`) - Préfecture de l'Hérault (identifiant : `pref34`) - Préfecture d'Ille-et-Vilaine (identifiant : `pref35`) - Préfecture de l'Isère (identifiant : `pref38`) diff --git a/cli.py b/cli.py index 852856960b7244d3b8e5ce3a5f5811719e742877..2f959d37cdaa71b77a89f729bb6752f687f5fafc 100755 --- a/cli.py +++ b/cli.py @@ -45,6 +45,7 @@ available_prefs = [ 'pref09', 'pref13', 'pref31', + 'pref33', 'pref34', 'pref35', 'pref38',