From fcaa1df5a741b68db25aa2890416e365d4b83051 Mon Sep 17 00:00:00 2001
From: Bastien Le Querrec <blq@laquadrature.net>
Date: Sun, 10 Mar 2024 14:52:28 +0100
Subject: [PATCH] ppparis: conserve la version texte des PDF

---
 .gitlab-ci.yml |  2 +-
 ppparis.py     | 14 +++++++++-----
 2 files changed, 10 insertions(+), 6 deletions(-)

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index 27f0409..7d5b4c4 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -45,6 +45,6 @@ test_ppparis:
     - pyvenv.cfg
   artifacts:
     paths:
-    - data/ppparis/
+    - data/ppparis/*.txt
     - output.log
     expire_in: 1 year
diff --git a/ppparis.py b/ppparis.py
index 7bd1b2b..acaa4b8 100755
--- a/ppparis.py
+++ b/ppparis.py
@@ -56,6 +56,9 @@ def download_file(url, dest):
   except Exception as exc:
     logging.warning(f'ATTENTION: Impossible de télécharger le fichier {url}: {exc}')
 
+def get_txt_file(filename):
+  return re.sub('(\.pdf)$', '.txt', filename)
+
 def parse_pdf(filename, name, date):
   if not os.path.isfile(__DATA_DIR+filename):
     logging.warning(f'ATTENTION: le fichier {filename} n\'existe pas')
@@ -69,10 +72,11 @@ def parse_pdf(filename, name, date):
           found = True
         print_output(f'    Le terme \033[1m{keyword}\033[0m a été trouvé.')
 
-    # Remplace le PDF par un fichier vide, afin de savoir la prochaine fois qu'il a déjà été analysé
-    f = open(__DATA_DIR+filename,'w')
-    f.write('')
+    # Écrit le texte du PDF dans un fichier texte pour une analyse future, puis supprime le PDF
+    f = open(get_txt_file(__DATA_DIR+filename),'w')
+    f.write(text)
     f.close()
+    os.remove(__DATA_DIR+filename)
     if found:
       print_output('')
 
@@ -166,8 +170,8 @@ for a in soup.find_all('a', href=True):
 
     filename = unquote(url.split('/')[-1])
 
-    # Si le fichier n'a pas été téléchargé, on le télécharge et on le parse
-    if not os.path.isfile(__DATA_DIR+filename):
+    # Si le fichier n'a pas déjà été parsé, on le télécharge et on le parse
+    if not os.path.isfile(get_txt_file(__DATA_DIR+filename)):
       logging.info(f'Nouveau fichier : {name} ({date}). URL : {url}')
       download_file(url, __DATA_DIR+filename)
 
-- 
GitLab