From 92c5a2641fed9c1d2f02b01ee97143c4e23b8753 Mon Sep 17 00:00:00 2001
From: Bastien Le Querrec <blq@laquadrature.net>
Date: Fri, 22 Mar 2024 18:27:44 +0100
Subject: [PATCH] =?UTF-8?q?RAAspotter:=20ajoute=20une=20fonction=20pour=20?=
 =?UTF-8?q?estimer=20la=20date=20dans=20une=20chaine=20de=20caract=C3=A8re?=
 =?UTF-8?q?s?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 RAAspotter.py        | 13 +++++++++++++
 RAAspotter_pref38.py | 11 ++---------
 2 files changed, 15 insertions(+), 9 deletions(-)

diff --git a/RAAspotter.py b/RAAspotter.py
index 289c00e..23b1b20 100644
--- a/RAAspotter.py
+++ b/RAAspotter.py
@@ -11,6 +11,8 @@ from selenium.webdriver.common.by import By
 from selenium.webdriver.support.wait import WebDriverWait
 from selenium.webdriver.support import expected_conditions
 
+import dateparser
+
 from bs4 import BeautifulSoup
 from pyvirtualdisplay import Display
 from pdfminer.high_level import extract_text
@@ -406,3 +408,14 @@ class RAAspotter:
             smtp.quit()
       except Exception as exc:
         logger.warning(f'Impossible d\'envoyer le courrier électronique : {exc}')
+
+  # Fonction qui essaie de deviner la date d'un RAA à partir de son nom.
+  # Utile pour limiter les requêtes lors de l'obtention des RAA à scanner.
+  def guess_date(string, regex):
+    guessed_date = datetime.datetime(9999, 1, 1)
+    try:
+      search = re.search(regex, string, re.IGNORECASE)
+      guessed_date = dateparser.parse(search.group(1))
+    except Exception as exc:
+      logger.warning(f"Impossible de deviner la date du terme {string} : {exc}")
+    return guessed_date
diff --git a/RAAspotter_pref38.py b/RAAspotter_pref38.py
index a7fed1a..8b5123a 100644
--- a/RAAspotter_pref38.py
+++ b/RAAspotter_pref38.py
@@ -1,6 +1,5 @@
 import os, sys, re
 import datetime
-import dateparser
 import logging
 
 from bs4 import BeautifulSoup
@@ -63,14 +62,8 @@ class RAAspotter_pref38(RAAspotter):
     # On analyse chaque résultat
     for option in select_list.find_all('option'):
       if not option['value'] == "":
-        guessed_date = datetime.datetime(9999, 1, 1)
-        try:
-          # Pour chaque RAA listé, on essaie de deviner sa date à partir de son nom, et si elle correspond à la plage
-          # demandée, on poursuit l'analyse
-          search = re.search('.* n°[ 0-9]* du ([0-9]*(?:er)? [a-zéû]* [0-9]*)', option['title'], re.IGNORECASE)
-          guessed_date = dateparser.parse(search.group(1))
-        except Exception as exc:
-          logger.warning(f"Impossible de deviner la date du RAA {option['title']} : {exc}")
+        # On estime la date à partir du nom de fichier
+        guessed_date = RAAspotter.guess_date(option['title'], '.* n°[ 0-9]* du ([0-9]*(?:er)? [a-zéû]* [0-9]*)')
         
         # Si la date estimée correspond à la plage d'analyse, on demande au serveur les détails du RAA
         if guessed_date >= self.not_before:
-- 
GitLab