diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index bdb1946384828a0e3f803ca9927e72fb629db597..75d242f6a94c458ac2e57a53ed5f3f253ed6988b 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -154,6 +154,11 @@ test_pref42: PREF: "pref42" extends: .default_pref +test_pref44: + variables: + PREF: "pref44" + extends: .default_pref + test_pref59: variables: PREF: "pref59" diff --git a/Makefile b/Makefile index ba5f24aa001da4c2bcc03af71ba3b4555c95affe..5b48bd2d745f9bd79e4b3f71d548a77df4daf9c8 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -make: ppparis pref04 pref05 pref06 pref09 pref13 pref31 pref34 pref35 pref38 pref42 pref59 pref62 pref65 pref66 pref69 pref80 pref81 pref83 pref87 pref976 +make: ppparis pref04 pref05 pref06 pref09 pref13 pref31 pref34 pref35 pref38 pref42 pref44 pref59 pref62 pref65 pref66 pref69 pref80 pref81 pref83 pref87 pref976 ppparis: python cli.py --pref ppparis pref04: @@ -21,6 +21,8 @@ pref38: python cli.py --pref pref38 pref42: python cli.py --pref pref42 +pref44: + python cli.py --pref pref44 pref59: python cli.py --pref pref59 pref62: diff --git a/RAAspotter_pref44.py b/RAAspotter_pref44.py new file mode 100644 index 0000000000000000000000000000000000000000..49ad13d48e3980e316189f8c12fce55a4f2ee1d3 --- /dev/null +++ b/RAAspotter_pref44.py @@ -0,0 +1,112 @@ +import os +import datetime +import logging + +from bs4 import BeautifulSoup +from urllib.parse import unquote + +from RAAspotter import RAAspotter + +logger = logging.getLogger(__name__) + + +class RAAspotter_pref44(RAAspotter): + + # Config + __HOST = 'https://www.loire-atlantique.gouv.fr' + __RAA_PAGE = f'{__HOST}/Publications/Recueil-des-actes-administratifs-RAA-en-Loire-Atlantique' + __USER_AGENT = 'Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/115.0' + full_name = 'Préfecture de la Loire-Atlantique' + short_code = 'pref44' + + 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_pref44') + 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(year_page['name'].strip()) + 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']) + + # Parfois un RAA est mal catégorisé et se retrouve sur la page de l'année, donc on la parse + 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: + 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 6d8a32ead9c44f177aeea8cea92cad2218ca1f82..439399f61cca7a158afd2c35f6b95ee3e3f5498e 100644 --- a/README.md +++ b/README.md @@ -43,6 +43,7 @@ Il est possible de ne lancer l'analyse que pour une seule administration, avec l - Préfecture d'Ille-et-Vilaine (identifiant : `pref35`) - Préfecture de l'Isère (identifiant : `pref38`) - Préfecture de la Loire (identifiant : `pref42`) +- Préfecture de la Loire-Atlantique (identifiant : `pref44`) - Préfecture du Nord (identifiant : `pref59`) - Préfecture du Pas-de-Calais (identifiant : `pref62`) - Préfecture des Pyrénées-Atlantiques (identifiant : `pref64`) diff --git a/cli.py b/cli.py index 31e7f470012891341800b452466670b3774cb644..852856960b7244d3b8e5ce3a5f5811719e742877 100755 --- a/cli.py +++ b/cli.py @@ -49,6 +49,7 @@ available_prefs = [ 'pref35', 'pref38', 'pref42', + 'pref44', 'pref59', 'pref62', 'pref64',