From cf5f66fa0625c697e59e5a90a81b83ecc7ff566e Mon Sep 17 00:00:00 2001 From: Bastien Le Querrec <blq@laquadrature.net> Date: Mon, 6 Jan 2025 22:19:39 +0100 Subject: [PATCH] pref31: utilise prefdpt --- Attrap_pref31.py | 69 ++++-------------------------------------------- 1 file changed, 5 insertions(+), 64 deletions(-) diff --git a/Attrap_pref31.py b/Attrap_pref31.py index a7d356c..0970d64 100644 --- a/Attrap_pref31.py +++ b/Attrap_pref31.py @@ -1,73 +1,14 @@ -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_pref31(Attrap_prefdpt): - -class Attrap_pref31(Attrap): - - # Config + # Configuration de la préfecture hostname = 'https://www.haute-garonne.gouv.fr' raa_page = f'{hostname}/Publications/Recueil-des-Actes-Administratifs/Recueil-des-Actes-Administratifs-Haute-Garonne' - user_agent = 'Mozilla/5.0 (Windows NT 10.0; rv:109.0) Gecko/20100101 Firefox/115.0' full_name = 'Préfecture de la Haute-Garonne' short_code = 'pref31' 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): - # On cherche les pages de chaque mois - page_content = self.get_page(self.raa_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.hostname, - False - )[::-1] - - pages_to_parse = [] - - # On filtre les pages de mois pour limiter le nombre de requêtes - for month_page in month_pages: - guessed_date = Attrap.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']) - - elements = [] - # On parse les pages des mois qu'on veut analyser - for element in self.get_raa_with_pager( - pages_to_parse, - ".fr-pagination__link.fr-pagination__link--next", - self.hostname - ): - 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('div.fr-card__body div.fr-card__content h2.fr-card__title a.fr-card__link.menu-item-link'): - 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.get_text().strip().capitalize() - date = datetime.datetime.strptime(a['title'].split(' - ')[-1].strip(), '%d/%m/%Y') - - raa = Attrap.RAA(url, date, name, timezone=self.timezone) - elements.append(raa) - return elements + # Configuration des widgets à analyser + Attrap_prefdpt.grey_card['regex']['month'] = '([A-Za-zéû]* [0-9]{4})' -- GitLab