diff --git a/Attrap_pref42.py b/Attrap_pref42.py index 4c0733da02a545d2e7abae3e1f808aa80dc5840d..3a2b3130760767ce8a82fa2888363a00cde55988 100644 --- a/Attrap_pref42.py +++ b/Attrap_pref42.py @@ -1,79 +1,14 @@ -import os -import datetime -import re +from Attrap_prefdpt import Attrap_prefdpt -from bs4 import BeautifulSoup -from urllib.parse import unquote -from Attrap import Attrap +class Attrap_pref42(Attrap_prefdpt): - -class Attrap_pref42(Attrap): - - # Config + # Configuration de la préfecture hostname = 'https://www.loire.gouv.fr' raa_page = f'{hostname}/Publications/Publications-legales/Recueil-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 la Loire' short_code = 'pref42' 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): - year_pages_to_parse = [] - - # On détermine quelles pages d'année parser - 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 = 9999 - try: - year = int(re.search('([0-9]{4})', year_page['name'], re.IGNORECASE).group(1)) - 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']) - - elements = [] - # Pour chaque année, on parse les RAA - for year_page in year_pages_to_parse: - page_content = self.get_page(year_page, 'get').content - for element in self.get_raa_elements(page_content)[::-1]: - elements.append(element) - - # On parse les RAA - 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-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').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 + Attrap_prefdpt.white_card['regex']['year'] = '([0-9]{4})'