diff --git a/Attrap_pref62.py b/Attrap_pref62.py index 38b88419f912eb210262cf7a4473b2211dceb2bc..3148e184c20c1932b68bd5f5c355caa0b71d40cd 100644 --- a/Attrap_pref62.py +++ b/Attrap_pref62.py @@ -1,69 +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_pref62(Attrap): +class Attrap_pref62(Attrap_prefdpt): # Config hostname = 'https://www.pas-de-calais.gouv.fr' raa_page = f'{hostname}/Publications/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 du Pas-de-Calais' short_code = 'pref62' 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 = [] - - # 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: - if Attrap.guess_date(year_page['name'].strip(), '([0-9]{4}).*').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 le div qui contient la liste des RAA - cards = soup.select('div.fr-downloads-group.fr-downloads-group--bordered')[0] - # On analyse chaque balise a dans ce div - for a in cards.find_all('a', href=True): - if 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[::-1] + # Configuration des widgets à analyser + Attrap_prefdpt.white_card['regex']['year'] = '([0-9]{4})'