From 9ac8ec8c1ac4e4f3b7360ddedae2cc6fd7d043df Mon Sep 17 00:00:00 2001 From: Bastien Le Querrec <blq@laquadrature.net> Date: Wed, 20 Mar 2024 20:40:33 +0100 Subject: [PATCH] =?UTF-8?q?pref34:=20ajout=20de=20la=20pr=C3=A9fecture=20d?= =?UTF-8?q?e=20l'H=C3=A9rault?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitlab-ci.yml | 28 +++++++++++++++++ Makefile | 4 ++- RAAspotter_pref34.py | 73 ++++++++++++++++++++++++++++++++++++++++++++ README.md | 1 + cli.py | 1 + 5 files changed, 106 insertions(+), 1 deletion(-) create mode 100644 RAAspotter_pref34.py diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 62dabd9..ab33d04 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -127,6 +127,34 @@ test_pref13: - output.log expire_in: 1 hour +test_pref34: + stage: test + image: registry.git.laquadrature.net/bastien/raaspotter/base:latest + tags: + - unprivileged + needs: [install] + script: + - curl --silent --location --output artifacts.zip "${CI_SERVER_PROTOCOL}://${CI_SERVER_HOST}:${CI_SERVER_PORT}/api/v4/projects/${CI_PROJECT_ID}/jobs/artifacts/${CI_COMMIT_BRANCH}/download?job=${CI_JOB_NAME}&job_token=${CI_JOB_TOKEN}" || true + - unzip -q artifacts.zip data/pref34/* || true + - rm artifacts.zip || true + - source bin/activate + - /etc/init.d/tor start + - python ./cli.py --pref pref34 + retry: 2 + only: + - main + cache: + key: $CI_COMMIT_REF_SLUG + paths: + - bin/ + - lib/ + - pyvenv.cfg + artifacts: + paths: + - data/pref34/*.txt + - output.log + expire_in: 1 hour + test_pref35: stage: test image: registry.git.laquadrature.net/bastien/raaspotter/base:latest diff --git a/Makefile b/Makefile index fa10661..2a37891 100644 --- a/Makefile +++ b/Makefile @@ -1,10 +1,12 @@ -make: ppparis pref06 pref13 pref35 pref38 pref59 pref62 pref69 +make: ppparis pref06 pref13 pref34 pref35 pref38 pref59 pref62 pref69 ppparis: python cli.py --pref ppparis pref06: python cli.py --pref pref06 pref13: python cli.py --pref pref13 +pref34: + python cli.py --pref pref34 pref35: python cli.py --pref pref35 pref38: diff --git a/RAAspotter_pref34.py b/RAAspotter_pref34.py new file mode 100644 index 0000000..7681435 --- /dev/null +++ b/RAAspotter_pref34.py @@ -0,0 +1,73 @@ +import os, sys +import datetime + +from bs4 import BeautifulSoup +from urllib.parse import unquote + +from RAAspotter import RAAspotter + +class RAAspotter_pref34(RAAspotter): + + # Config + __HOST = 'https://www.herault.gouv.fr' + __RAA_PAGE = {'2024': f'{__HOST}/Publications/Recueils-des-actes-administratifs/Recueil-des-actes-administratifs-2024', + '2023': f'{__HOST}/Publications/Recueils-des-actes-administratifs/Recueil-des-actes-administratifs-2023', + '2022': f'{__HOST}/Publications/Recueils-des-actes-administratifs/Recueil-des-actes-administratifs-2022', + '2021': f'{__HOST}/Publications/Recueils-des-actes-administratifs/Recueil-des-actes-administratifs-2021', + '2020': f'{__HOST}/Publications/Recueils-des-actes-administratifs/Recueil-des-actes-administratifs-2020', + '2019': f'{__HOST}/Publications/Recueils-des-actes-administratifs/Archives/Recueil-des-actes-administratifs-2019'} + __USER_AGENT = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36' + full_name = 'Préfecture de l\'Hérault' + short_code = 'pref34' + + def __init__(self, data_dir): + super().__init__(data_dir, self.__USER_AGENT) + self.enable_tor(10) + + def get_raa(self, keywords): + self.print_output('RAAspotter_pref34') + self.print_output(f'Termes recherchés: {keywords}') + self.print_output('') + + pages_to_parse = [] + if self.not_before.year <= 2024: + pages_to_parse.append(self.__RAA_PAGE['2024']) + if self.not_before.year <= 2023: + pages_to_parse.append(self.__RAA_PAGE['2023']) + if self.not_before.year <= 2022: + pages_to_parse.append(self.__RAA_PAGE['2022']) + if self.not_before.year <= 2021: + pages_to_parse.append(self.__RAA_PAGE['2021']) + if self.not_before.year <= 2020: + pages_to_parse.append(self.__RAA_PAGE['2020']) + if self.not_before.year <= 2019: + pages_to_parse.append(self.__RAA_PAGE['2019']) + + for raa_page in pages_to_parse: + page_content = self.get_page(raa_page, 'get').content + raa_elements = self.get_raa_elements(page_content) + self.parse_raa(raa_elements, keywords.split(',')) + + 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.__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').get_text().split(' - ')[-1].strip(), '%d/%m/%Y') + filename = url.split('/')[-1] + + raa = RAAspotter.RAA(url, date, name, filename) + elements.append(raa) + return elements diff --git a/README.md b/README.md index 75a4818..1b58187 100644 --- a/README.md +++ b/README.md @@ -35,6 +35,7 @@ Il est possible de ne lancer l'analyse que pour une seule administration, avec l - Préfecture de police de Paris (identifiant : `ppparis`) - Préfecture des Alpes-Maritimes (identifiant : `pref06`) - Préfecture des Bouches-du-Rhône (identifiant : `pref13`) +- Préfecture de l'Hérault (identifiant : `pref34`) - Préfecture d'Ille-et-Vilaine (identifiant : `pref35`) - Préfecture du Nord (identifiant : `pref59`) - Préfecture du Pas-de-Calais (identifiant : `pref62`) diff --git a/cli.py b/cli.py index 850cbf7..9eef3a8 100755 --- a/cli.py +++ b/cli.py @@ -38,6 +38,7 @@ available_prefs = [ 'ppparis', 'pref06', 'pref13', + 'pref34', 'pref35', 'pref38', 'pref59', -- GitLab