From cfa58c28b39372caa7667ad31f1de0cf0288f5a6 Mon Sep 17 00:00:00 2001 From: CelestinHuet Date: Sat, 23 Dec 2023 09:28:01 +0100 Subject: [PATCH] =?UTF-8?q?Sauvegarde=20des=20nouvelles=20des=20semaines?= =?UTF-8?q?=20pr=C3=A9c=C3=A9dentes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 3 +- orgues/management/commands/get_news.py | 40 ++++++++++++++++++-------- 2 files changed, 30 insertions(+), 13 deletions(-) diff --git a/.gitignore b/.gitignore index cd09ffe..1276769 100644 --- a/.gitignore +++ b/.gitignore @@ -128,4 +128,5 @@ no_delete.csv replace_facteurs.csv appariements_osm.json appariements_topo.json -facteurs_localisation.json \ No newline at end of file +facteurs_localisation.json +save_news.txt \ No newline at end of file diff --git a/orgues/management/commands/get_news.py b/orgues/management/commands/get_news.py index bf7e0a1..504eba9 100644 --- a/orgues/management/commands/get_news.py +++ b/orgues/management/commands/get_news.py @@ -5,6 +5,7 @@ from django.core.mail import get_connection, EmailMessage import re import datetime +import os class Command(BaseCommand): """ @@ -71,6 +72,13 @@ def handle(self, *args, **options): today = datetime.datetime.now() + titres = [] + if os.path.exists("save_news.txt"): + with open("save_news.txt", "r") as f: + for line in f: + titres.append(line.strip()) + + date_expression_reguliere = "((19\d\d|20\d\d)[-/](0[1-9]|1[0-2])[-/](0[1-9]|[12]\d|3[01]))" for journal in list_journaux.keys(): @@ -87,21 +95,24 @@ def handle(self, *args, **options): if "items" in resultats.keys(): for resultat in resultats["items"]: - if not "concert" in resultat["title"].lower() and not "bort-les-orgues" in resultat["title"].lower() and not "bort-les-orgues" in resultat["link"].lower(): + if not "bort-les-orgues" in resultat["title"].lower() and not "bort-les-orgues" in resultat["link"].lower(): if "orgue" in resultat["title"].lower() or "orgue" in resultat["link"].lower(): - date_article = re.findall(date_expression_reguliere, resultat["link"]) - - if len(date_article) >= 1: - if "/" in date_article[0][0]: - date_splitted = date_article[0][0].split("/") + if resultat["title"] not in titres: + date_article = re.findall(date_expression_reguliere, resultat["link"]) + + if len(date_article) >= 1: + if "/" in date_article[0][0]: + date_splitted = date_article[0][0].split("/") + else: + date_splitted = date_article[0][0].split("-") + date_article = datetime.datetime(int(date_splitted[0]), int(date_splitted[1]), int(date_splitted[2])) + difference = today - date_article + if difference.days < 8: + string += "{}, {}\n".format(resultat["title"], resultat["link"]) + self.add_title(resultat["title"]) else: - date_splitted = date_article[0][0].split("-") - date_article = datetime.datetime(int(date_splitted[0]), int(date_splitted[1]), int(date_splitted[2])) - difference = today - date_article - if difference.days < 8: string += "{}, {}\n".format(resultat["title"], resultat["link"]) - else: - string += "{}, {}\n".format(resultat["title"], resultat["link"]) + self.add_title(resultat["title"]) with get_connection( @@ -116,5 +127,10 @@ def handle(self, *args, **options): recipient_list = settings.NEWS_EMAILS message = string EmailMessage(subject, message, email_from, recipient_list, connection=connection).send() + + + def add_title(self, title): + with open("save_news.txt", "a") as f: + f.write(title + "\n") \ No newline at end of file