79 lines
2.1 KiB
Python
79 lines
2.1 KiB
Python
|
|
import json
|
||
|
|
import locale
|
||
|
|
import paramiko
|
||
|
|
import os
|
||
|
|
from datetime import datetime
|
||
|
|
from gen_feed import generate_feed
|
||
|
|
from dotenv import load_dotenv
|
||
|
|
|
||
|
|
def get_publisher(link):
|
||
|
|
if "ledauphine.com" in link:
|
||
|
|
return "Le Dauphiné Libéré"
|
||
|
|
if "bienpublic.com" in link:
|
||
|
|
return "Le Bien Public"
|
||
|
|
if "leprogres.fr" in link:
|
||
|
|
return "Le Progrès"
|
||
|
|
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()
|