From be946d94033066c431e6980e17eb76f43283771d Mon Sep 17 00:00:00 2001 From: Bastien Le Querrec <blq@laquadrature.net> Date: Tue, 7 Jan 2025 11:52:49 +0100 Subject: [PATCH] pref34: utilise prefdpt --- Attrap_pref34.py | 65 ++++++------------------------------------------ 1 file changed, 7 insertions(+), 58 deletions(-) diff --git a/Attrap_pref34.py b/Attrap_pref34.py index 6f2e6dd..d466568 100644 --- a/Attrap_pref34.py +++ b/Attrap_pref34.py @@ -1,67 +1,16 @@ -import os -import datetime +from Attrap_prefdpt import Attrap_prefdpt -from bs4 import BeautifulSoup -from urllib.parse import unquote -from Attrap import Attrap +class Attrap_pref34(Attrap_prefdpt): - -class Attrap_pref34(Attrap): - - # Config + # Configuration de la préfecture hostname = 'https://www.herault.gouv.fr' raa_page = f'{hostname}/Publications/Recueils-des-actes-administratifs' - user_agent = 'Mozilla/5.0 (Windows NT 10.0; rv:109.0) Gecko/20100101 Firefox/115.0' full_name = 'Préfecture de l\'Hérault' short_code = 'pref34' timezone = 'Europe/Paris' - def __init__(self, data_dir): - super().__init__(data_dir, self.user_agent) - self.set_sleep_time(30) - - def get_raa(self, keywords): - pages_to_parse = [] - - year_pages = self.get_sub_pages_with_pager( - self.raa_page, - 'div.fr-card.fr-card--horizontal.fr-card--sm.fr-enlarge-link.fr-mb-3w div.fr-card__body div.fr-card__content h2.fr-card__title a.fr-card__link', - 'ul.fr-pagination__list li a.fr-pagination__link.fr-pagination__link--next.fr-pagination__link--lg-label', - 'div.fr-card.fr-card--horizontal.fr-card--sm.fr-enlarge-link.fr-mb-3w div.fr-card__body div.fr-card__content div.fr-card__end p.fr-card__detail', - self.hostname - ) - for year_page in year_pages: - year = Attrap.guess_date(year_page['name'], '.*([0-9]{4})').year - if year >= self.not_before.year: - pages_to_parse.append(year_page['url']) - - elements = [] - for raa_page in pages_to_parse: - page_content = self.get_page(raa_page, 'get').content - for element in self.get_raa_elements(page_content): - elements.append(element) - - self.parse_raa(elements, keywords) - self.mailer() - - def get_raa_elements(self, page_content): - elements = [] - # On charge le parser - soup = BeautifulSoup(page_content, 'html.parser') - - # On récupère chaque balise a - for a in soup.select('a.fr-link.fr-link--download'): - if a.get('href') and a['href'].endswith('.pdf'): - if a['href'].startswith('/'): - url = f"{self.hostname}{a['href']}" - else: - url = a['href'] - - url = unquote(url) - name = a.find('span').previous_sibling.replace('Télécharger ', '').strip() - date = datetime.datetime.strptime(a.find('span').get_text().split(' - ')[-1].strip(), '%d/%m/%Y') - - raa = Attrap.RAA(url, date, name, timezone=self.timezone) - elements.append(raa) - return elements + # Configuration des widgets à analyser + year_regex = '(?:(?:Recueil des actes administratifs)|(?:Année))[ -]([0-9]{4})' + Attrap_prefdpt.white_card['regex']['year'] = year_regex + Attrap_prefdpt.grey_card['regex']['year'] = year_regex -- GitLab