From 634d0c353b6564f62a237d9ae7a51e62fb19a756 Mon Sep 17 00:00:00 2001 From: Bastien Le Querrec <blq@laquadrature.net> Date: Fri, 3 Jan 2025 18:27:44 +0100 Subject: [PATCH] prefdpt: les regex des cartes sont configurables --- Attrap_pref01.py | 2 +- Attrap_pref02.py | 2 +- Attrap_pref11.py | 2 +- Attrap_pref29.py | 2 +- Attrap_pref49.py | 2 +- Attrap_pref50.py | 2 +- Attrap_pref73.py | 2 +- Attrap_pref87.py | 2 +- Attrap_prefdpt.py | 61 +++++++++++++++++++++++++++++++++++------------ 9 files changed, 54 insertions(+), 23 deletions(-) diff --git a/Attrap_pref01.py b/Attrap_pref01.py index 64c194e..2b31a10 100644 --- a/Attrap_pref01.py +++ b/Attrap_pref01.py @@ -9,4 +9,4 @@ class Attrap_pref01(Attrap_prefdpt): full_name = 'Préfecture de l\'Ain' short_code = 'pref01' timezone = 'Europe/Paris' - year_regex = '(?:Recueil|Recueils) (?:des actes administratifs)(?:[ -])*([0-9]{4})' + Attrap_prefdpt.grey_card['year'] = '(?:Recueil|Recueils) (?:des actes administratifs)(?:[ -])*([0-9]{4})' diff --git a/Attrap_pref02.py b/Attrap_pref02.py index e9447b5..f3835d0 100644 --- a/Attrap_pref02.py +++ b/Attrap_pref02.py @@ -9,4 +9,4 @@ class Attrap_pref02(Attrap_prefdpt): full_name = 'Préfecture de l\'Aisne' short_code = 'pref02' timezone = 'Europe/Paris' - year_regex = 'RAA [Aa]nnée ([0-9]{4})' + Attrap_prefdpt.grey_card['year'] = 'RAA [Aa]nnée ([0-9]{4})' diff --git a/Attrap_pref11.py b/Attrap_pref11.py index b0f460e..8d097e5 100644 --- a/Attrap_pref11.py +++ b/Attrap_pref11.py @@ -9,4 +9,4 @@ class Attrap_pref11(Attrap_prefdpt): full_name = 'Préfecture de l\'Aude' short_code = 'pref11' timezone = 'Europe/Paris' - year_regex = 'Année *([0-9]{4})' + Attrap_prefdpt.grey_card['year'] = 'Année *([0-9]{4})' diff --git a/Attrap_pref29.py b/Attrap_pref29.py index 294e09a..c71f9e6 100644 --- a/Attrap_pref29.py +++ b/Attrap_pref29.py @@ -9,4 +9,4 @@ class Attrap_pref29(Attrap_prefdpt): full_name = 'Préfecture du Finistère' short_code = 'pref29' timezone = 'Europe/Paris' - year_regex = '(?:Recueils publiés en ).*([0-9]{4})' + Attrap_prefdpt.grey_card['year'] = '(?:Recueils publiés en ).*([0-9]{4})' diff --git a/Attrap_pref49.py b/Attrap_pref49.py index 80401a2..7c74aa0 100644 --- a/Attrap_pref49.py +++ b/Attrap_pref49.py @@ -9,4 +9,4 @@ class Attrap_pref49(Attrap_prefdpt): full_name = 'Préfecture de Maine-et-Loire' short_code = 'pref49' timezone = 'Europe/Paris' - year_regex = '.*([0-9]{4})' + Attrap_prefdpt.grey_card['year'] = '.*([0-9]{4})' diff --git a/Attrap_pref50.py b/Attrap_pref50.py index e99087e..61032ea 100644 --- a/Attrap_pref50.py +++ b/Attrap_pref50.py @@ -9,4 +9,4 @@ class Attrap_pref50(Attrap_prefdpt): full_name = 'Préfecture de la Manche' short_code = 'pref50' timezone = 'Europe/Paris' - year_regex = '([0-9]{4})' + Attrap_prefdpt.grey_card['year'] = '([0-9]{4})' diff --git a/Attrap_pref73.py b/Attrap_pref73.py index ddaaecf..b85003e 100644 --- a/Attrap_pref73.py +++ b/Attrap_pref73.py @@ -9,4 +9,4 @@ class Attrap_pref73(Attrap_prefdpt): full_name = 'Préfecture de Savoie' short_code = 'pref73' timezone = 'Europe/Paris' - year_regex = '([0-9]{4})' + Attrap_prefdpt.grey_card['year'] = '([0-9]{4})' diff --git a/Attrap_pref87.py b/Attrap_pref87.py index 86e355e..f02d00a 100644 --- a/Attrap_pref87.py +++ b/Attrap_pref87.py @@ -9,4 +9,4 @@ class Attrap_pref87(Attrap_prefdpt): full_name = 'Préfecture de la Haute-Vienne' short_code = 'pref87' timezone = 'Europe/Paris' - year_regex = '([0-9]{4})' + Attrap_prefdpt.grey_card['year'] = '([0-9]{4})' diff --git a/Attrap_prefdpt.py b/Attrap_prefdpt.py index b5b2066..5d7068d 100644 --- a/Attrap_prefdpt.py +++ b/Attrap_prefdpt.py @@ -11,6 +11,21 @@ class Attrap_prefdpt(Attrap): # Config user_agent = 'Mozilla/5.0 (Windows NT 10.0; rv:109.0) Gecko/20100101 Firefox/115.0' year_regex = '([0-9]{4})' + grey_card = { + 'year': None, + 'month': None + } + white_card = { + 'year': None, + 'month': None + } + + grey_card_css_path = 'div.fr-card.fr-card--sm.fr-card--grey.fr-enlarge-link div.fr-card__body div.fr-card__content h2.fr-card__title a' # Chemin CSS des cartes grises + white_card_title_css_path = 'div.fr-card.fr-card--horizontal.fr-card--sm.fr-enlarge-link div.fr-card__body div.fr-card__content h2.fr-card__title a.fr-card__link' # Chemin CSS du titre des cartes blanches + white_card_details_css_path = 'div.fr-card.fr-card--horizontal.fr-card--sm.fr-enlarge-link div.fr-card__body div.fr-card__content div.fr-card__end p.fr-card__detail' # Chemin CSS du détail des cartes blanches + white_card_pager_css_path = 'ul.fr-pagination__list li a.fr-pagination__link.fr-pagination__link--next.fr-pagination__link--lg-label' # Chemin CSS du pager des cartes blanches + + element_css_path = 'div.fr-downloads-group.fr-downloads-group--bordered ul li a,div a.fr-link.fr-link--download' # Chemin CSS d'un RAA def __init__(self, data_dir): super().__init__(data_dir, self.user_agent) @@ -23,36 +38,52 @@ class Attrap_prefdpt(Attrap): while not self.page_urls_to_parse == []: page_url = self.page_urls_to_parse[-1] page_content = self.get_page(page_url, 'get').content # On récupère le HTML de la page - self.get_year_pages(page_content) # On cherche les cartes d'années - self.get_elements_pages(page_url) # On cherche les cartes d'éléments - for element in self.get_raa_elements(page_content): # On cherche les éléments + self.parse_grey_cards(page_content) # On parse les cartes grises + self.parse_white_cards(page_url) # On parse les cartes blanches + for element in self.get_raa_elements(page_content): # On cherche les RAA self.elements.append(element) self.page_urls_to_parse.remove(page_url) # On supprime la page de la liste de celles à parser self.parse_raa(self.elements[::-1], keywords) self.mailer() - def get_year_pages(self, page_content): + def parse_grey_cards(self, page_content): for card in self.get_sub_pages( page_content, - 'div.fr-card.fr-card--sm.fr-card--grey.fr-enlarge-link div.fr-card__body div.fr-card__content h2.fr-card__title a', # Pages d'années + self.grey_card_css_path, self.hostname, False ): - date = Attrap.guess_date(card['name'].strip(), self.year_regex).replace(day=1, month=1) - if date.year >= self.not_before.year: - self.page_urls_to_parse.append(card['url']) + if self.grey_card['year']: + date = Attrap.guess_date(card['name'].strip(), self.grey_card['year']).replace(day=1, month=1) + if date >= self.not_before.replace(day=1, month=1) and card['url'] not in self.page_urls_to_parse: + self.page_urls_to_parse.append(card['url']) + if self.grey_card['month']: + date = Attrap.guess_date(card['name'].strip(), self.grey_card['month']).replace(day=1) + if date >= self.not_before.replace(day=1) and card['url'] not in self.page_urls_to_parse: + self.page_urls_to_parse.append(card['url']) - def get_elements_pages(self, page_url): + def parse_white_cards(self, page_url): for card in self.get_sub_pages_with_pager( page_url, - 'div.fr-card.fr-card--horizontal.fr-card--sm.fr-enlarge-link div.fr-card__body div.fr-card__content h2.fr-card__title a.fr-card__link', # Carte avec un élément (mois ou arrêté seul) - 'ul.fr-pagination__list li a.fr-pagination__link.fr-pagination__link--next.fr-pagination__link--lg-label', # Pager - 'div.fr-card.fr-card--horizontal.fr-card--sm.fr-enlarge-link div.fr-card__body div.fr-card__content div.fr-card__end p.fr-card__detail', # Détails (avec la date de publication) + self.white_card_title_css_path, # Carte avec un élément (mois ou arrêté seul) + self.white_card_pager_css_path, # Pager + self.white_card_details_css_path, # Détails (avec la date de publication) self.hostname ): + # Pour les cartes blanches, on parse le titre si un regex a été spécifié + if self.white_card['year']: + date = Attrap.guess_date(card['name'].strip(), self.white_card['year']).replace(day=1, month=1) + if date >= self.not_before.replace(day=1, month=1) and card['url'] not in self.page_urls_to_parse: + self.page_urls_to_parse.append(card['url']) + if self.white_card['month']: + date = Attrap.guess_date(card['name'].strip(), self.white_card['month']).replace(day=1) + if date >= self.not_before.replace(day=1) and card['url'] not in self.page_urls_to_parse: + self.page_urls_to_parse.append(card['url']) + + # On regarde aussi la date dans les détails de la carte date = datetime.datetime.strptime(card['details'].replace('Publié le ', '').strip(), '%d/%m/%Y') - if date >= self.not_before: + if date >= self.not_before and card['url'] not in self.page_urls_to_parse: self.page_urls_to_parse.append(card['url']) def get_raa_elements(self, page_content): @@ -61,8 +92,8 @@ class Attrap_prefdpt(Attrap): soup = BeautifulSoup(page_content, 'html.parser') # On récupère chaque balise a - for a in soup.select('div.fr-downloads-group.fr-downloads-group--bordered ul li a,div a.fr-link.fr-link--download'): - if a.get('href') and a['href'].endswith('.pdf'): + for a in soup.select(self.element_css_path): + if a.get('href'): if a['href'].startswith('/'): url = f"{self.hostname}{a['href']}" else: -- GitLab