From ce535d9c06f326361874c23bbb451498ec51de89 Mon Sep 17 00:00:00 2001
From: Bastien Le Querrec <blq@laquadrature.net>
Date: Sat, 18 Jan 2025 20:38:45 +0100
Subject: [PATCH] pref59: utilise prefdpt

---
 Attrap_pref59.py | 75 +++++-------------------------------------------
 1 file changed, 7 insertions(+), 68 deletions(-)

diff --git a/Attrap_pref59.py b/Attrap_pref59.py
index 7ea0f8c..966214d 100644
--- a/Attrap_pref59.py
+++ b/Attrap_pref59.py
@@ -1,77 +1,16 @@
-import os
-import datetime
-import dateparser
-import logging
+from Attrap_prefdpt import Attrap_prefdpt
 
-from bs4 import BeautifulSoup
-from urllib.parse import unquote
 
-from Attrap import Attrap
+class Attrap_pref59(Attrap_prefdpt):
 
-logger = logging.getLogger(__name__)
-
-
-class Attrap_pref59(Attrap):
-
-    # Config
+    # Configuration de la préfecture
     hostname = 'https://www.nord.gouv.fr'
     raa_page = f'{hostname}/Publications/Recueils-des-actes-administratifs/RAA-du-departement-du-Nord'
-    user_agent = 'Mozilla/5.0 (Windows NT 10.0; rv:109.0) Gecko/20100101 Firefox/115.0'
     full_name = 'Préfecture du Nord'
     short_code = 'pref59'
     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 récupère les pages d'années
-        year_pages = []
-        page_content = self.get_page(self.raa_page, 'get').content
-        for card in self.get_sub_pages(
-            page_content,
-            'div.fr-card__body div.fr-card__content h2.fr-card__title a',
-            self.hostname,
-            False
-        ):
-            if Attrap.guess_date(card['name'], '([0-9]{4})').year >= self.not_before.year:
-                year_pages.append(card['url'])
-
-        elements = []
-        for raa_page in year_pages:
-            page_content = self.get_page(raa_page, 'get').content
-            sub_pages = self.get_sub_pages(
-                page_content,
-                'div.fr-card__body div.fr-card__content h2.fr-card__title a',
-                self.hostname,
-                True
-            )
-            for sub_page in sub_pages[::-1]:
-                sub_page_content = self.get_page(sub_page['url'], 'get').content
-                for element in self.get_raa_elements(sub_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
+    Attrap_prefdpt.grey_card['regex']['year'] = '([0-9]{4})'
+    Attrap_prefdpt.grey_card['regex']['month'] = '([A-Za-zéû]* [0-9]{4})'
+    Attrap_prefdpt.grey_card['add_year_to_months'] = True
-- 
GitLab