RSSEbra/gen_website.py
Cyril NOVEL 8f76a16e5a Support all EBRA websites
Better formatting
2026-07-28 15:47:32 +02:00

93 lines
2.4 KiB
Python

import locale
import os
from datetime import datetime
import paramiko
from dotenv import load_dotenv
from gen_feed import generate_feed
def get_publisher(link):
publishers = {
"ledauphine.com": "Le Dauphiné Libéré",
"bienpublic.com": "Le Bien Public",
"leprogres.fr": "Le Progrès",
"lalsace.fr": "L'Alsace",
"dna.fr": "Dernières Nouvelles d'Alsace",
"estrepublicain.fr": "L'Est Républicain",
"lejsl.com": "Journal de Saône-et-Loire",
"republicain-lorrain.fr": "Le Républicain Lorrain",
"vosgesmatin.fr": "Vosges Matin",
}
for k,v in publishers.items():
if k in link:
return v
return ""
def upload_to_sftp(towns):
host = os.environ["FTP_HOST"]
port = int(os.environ["FTP_PORT"])
username = os.environ["FTP_USER"]
password = os.environ["FTP_PASSWORD"]
transport = paramiko.Transport((host, port))
transport.connect(username=username, password=password)
sftp = paramiko.SFTPClient.from_transport(transport)
sftp.put("index.html", "www/index.html")
for t in towns:
sftp.put(t[3], f"www/{t[3]}")
sftp.close()
transport.close()
def generate_web():
# Load from env
urls = os.environ["URLS"].split(",")
website = os.environ["WEBSITE"]
towns = []
print("Getting articles for each link...")
for link in urls:
town_id = link.rsplit("/", 1)[-1]
town_xml = town_id + ".xml"
town_rss = website + "/" + town_xml
if not generate_feed(link, town_rss, town_xml):
print(f"Failed to generate feed for {town_id}")
return False
towns.append((town_id, town_rss, get_publisher(link), town_xml))
print("Updating HTML template...")
with open("template.html", "r", encoding="utf-8") as template:
data = template.read()
li = ""
for town in towns:
li += f'<li><a href="{town[1]}">{town[0]}</a> ({town[2]})</li>'
data = data.replace("$$$TOWN_LIST$$$", li)
locale.setlocale(locale.LC_ALL, "fr_FR.UTF-8")
now = datetime.now()
data = data.replace(
"$$$GENERATED_DATETIME$$$", now.strftime("%H:%M le %A %d %B %Y")
)
with open("index.html", "w", encoding="utf-8") as out:
out.write(data)
print("Uploading to FTP...")
upload_to_sftp(towns)
print("All done!")
return True
if __name__ == "__main__":
load_dotenv()
generate_web()