diff --git a/Attrap_pref25.py b/Attrap_pref25.py index bb5034b6c562bab702c4af65500cfd0915bdf3aa..000a055f9505601cc963e1c9ba76bc66e5f02a52 100644 --- a/Attrap_pref25.py +++ b/Attrap_pref25.py @@ -1,79 +1,15 @@ -import os -import datetime -import logging +from Attrap_prefdpt import Attrap_prefdpt -from bs4 import BeautifulSoup -from urllib.parse import unquote -from Attrap import Attrap +class Attrap_pref25(Attrap_prefdpt): -logger = logging.getLogger(__name__) - - -class Attrap_pref25(Attrap): - - # Config + # Configuration de la préfecture hostname = 'https://www.doubs.gouv.fr' raa_page = f'{hostname}/Publications/Publications-Legales/Recueil-des-Actes-Administratifs-RAA' - user_agent = 'Mozilla/5.0 (Windows NT 10.0; rv:109.0) Gecko/20100101 Firefox/115.0' full_name = 'Préfecture du Doubs' short_code = 'pref25' 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): - sub_pages = self.get_sub_pages_with_pager( - self.raa_page, - 'a.fr-card__link', - 'a.fr-pagination__link.fr-pagination__link--next.fr-pagination__link--lg-label', - None, - self.hostname, - ) - - pages_to_parse = [] - - # TODO : détecter la date de la page à partir du parsing de la page principale et non à partir de son URL - for sub_page in sub_pages: - url = sub_page['url'] - last_word = url.split('-')[-1] - year = 0 - try: - year = int(last_word) - - if self.not_before.year <= year: - pages_to_parse.append(url) - except Exception as e: - logger.warning(f"Impossible de déterminer l'année de l'URL {url}") - - elements = [] - for raa_page in pages_to_parse: - page_content = self.get_page(raa_page, 'get').content - elements.extend(self.get_raa_elements(page_content)) - - self.parse_raa(elements[::-1], keywords) - self.mailer() - - def get_raa_elements(self, page_content): - # On charge le parser - soup = BeautifulSoup(page_content, 'html.parser') - - elements = [] - # On récupère chaque balise a - for a in soup.select('div.fr-downloads-group.fr-downloads-group--bordered ul li a'): - 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').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 + Attrap_prefdpt.grey_card['regex']['year'] = '([0-9]{4})' + Attrap_prefdpt.grey_card['follow_link_on_unrecognised_date'] = False