From fda6f6e17f8e95ce372444f988c53afd36cf633c Mon Sep 17 00:00:00 2001
From: pipou <9146-pipou@users.noreply.git.laquadrature.net>
Date: Sun, 9 Jun 2024 12:00:00 +0200
Subject: [PATCH] =?UTF-8?q?pref25:=20Ajout=20de=20la=20pr=C3=A9fecture=20d?=
 =?UTF-8?q?u=20Doubs?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Closes !14

Co-authored-by: pipou <9146-pipou@users.noreply.git.laquadrature.net>
Co-authored-by: Bastien Le Querrec <blq@laquadrature.net>
---
 .gitlab-ci.yml   |  5 +++
 Attrap_pref25.py | 80 ++++++++++++++++++++++++++++++++++++++++++++++++
 Makefile         |  4 ++-
 README.md        |  1 +
 cli.py           |  1 +
 5 files changed, 90 insertions(+), 1 deletion(-)
 create mode 100644 Attrap_pref25.py

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index 3f2c4f9..4c9f791 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -123,6 +123,11 @@ test_pref13:
     PREF: "pref13"
   extends: .default_pref
 
+test_pref25:
+  variables:
+    PREF: "pref25"
+  extends: .default_pref
+
 test_pref31:
   variables:
     PREF: "pref31"
diff --git a/Attrap_pref25.py b/Attrap_pref25.py
new file mode 100644
index 0000000..d675b5d
--- /dev/null
+++ b/Attrap_pref25.py
@@ -0,0 +1,80 @@
+import os
+import datetime
+import logging
+
+from bs4 import BeautifulSoup
+from urllib.parse import unquote
+
+from Attrap import Attrap
+
+logger = logging.getLogger(__name__)
+
+
+class Attrap_pref25(Attrap):
+
+    # Config
+    __HOST = 'https://www.doubs.gouv.fr'
+    __RAA_PAGE = f'{__HOST}/Publications/Publications-Legales/Recueil-des-Actes-Administratifs-RAA'
+
+    __USER_AGENT = 'Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/115.0'
+
+    full_name = 'Préfecture du Doubs'
+    short_code = 'pref25'
+
+    def __init__(self, data_dir):
+        super().__init__(data_dir, self.__USER_AGENT)
+        self.enable_tor(10)
+
+    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.__HOST,
+        )
+
+        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.__HOST}{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)
+                elements.append(raa)
+
+        return elements
diff --git a/Makefile b/Makefile
index 8601dd7..56c154c 100644
--- a/Makefile
+++ b/Makefile
@@ -1,4 +1,4 @@
-make: ppparis pref04 pref05 pref06 pref09 pref13 pref31 pref33 pref34 pref35 pref38 pref42 pref44 pref59 pref62 pref63 pref64 pref65 pref66 pref69 pref73 pref80 pref81 pref83 pref87 pref93 pref94 pref976
+make: ppparis pref04 pref05 pref06 pref09 pref13 pref25 pref31 pref33 pref34 pref35 pref38 pref42 pref44 pref59 pref62 pref63 pref64 pref65 pref66 pref69 pref73 pref80 pref81 pref83 pref87 pref93 pref94 pref976
 ppparis:
 	bin/python3 cli.py ppparis
 pref04:
@@ -11,6 +11,8 @@ pref09:
 	bin/python3 cli.py pref09
 pref13:
 	bin/python3 cli.py pref13
+pref25:
+	bin/python3 cli.py pref25
 pref31:
 	bin/python3 cli.py pref31
 pref33:
diff --git a/README.md b/README.md
index 40af5ec..895623f 100644
--- a/README.md
+++ b/README.md
@@ -68,6 +68,7 @@ Les options suivantes peuvent être précisées, par un paramètre si l'utilitai
 - Préfecture des Alpes-Maritimes (identifiant : `pref06`)
 - Préfecture de l'Ariège (identifiant : `pref09`)
 - Préfecture des Bouches-du-Rhône (identifiant : `pref13`)
+- Préfecture du Doubs (identifiant : `pref25`)
 - Préfecture de la Haute-Garonne (identifiant : `pref31`)
 - Préfecture de la Gironde (identifiant : `pref33`)
 - Préfecture de l'Hérault (identifiant : `pref34`)
diff --git a/cli.py b/cli.py
index e40a01f..8c1a1d8 100755
--- a/cli.py
+++ b/cli.py
@@ -45,6 +45,7 @@ available_administrations = [
     'pref06',
     'pref09',
     'pref13',
+    'pref25',
     'pref31',
     'pref33',
     'pref34',
-- 
GitLab