Skip to content

Commit

Permalink
Sauvegarde des nouvelles des semaines précédentes
Browse files Browse the repository at this point in the history
  • Loading branch information
CelestinHuet committed Dec 23, 2023
1 parent b8cfb7f commit cfa58c2
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 13 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,5 @@ no_delete.csv
replace_facteurs.csv
appariements_osm.json
appariements_topo.json
facteurs_localisation.json
facteurs_localisation.json
save_news.txt
40 changes: 28 additions & 12 deletions orgues/management/commands/get_news.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from django.core.mail import get_connection, EmailMessage
import re
import datetime
import os

class Command(BaseCommand):
"""
Expand Down Expand Up @@ -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():
Expand All @@ -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(
Expand All @@ -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")


0 comments on commit cfa58c2

Please sign in to comment.