Formatting

This commit is contained in:
Cyril NOVEL 2026-07-28 17:33:28 +02:00
commit 8a059a722d

View file

@ -1,16 +1,13 @@
import requests
import re
import datetime import datetime
import xml.dom.minidom import xml.dom.minidom
from bs4 import BeautifulSoup
from urllib.parse import urlparse from urllib.parse import urlparse
from zoneinfo import ZoneInfo from zoneinfo import ZoneInfo
import requests
from bs4 import BeautifulSoup
from rfeed import * from rfeed import *
regex_date = r"/(\d{4})/(\d{2})/(\d{2})/"
regex_time = r"à (\d+):(\d{2})"
class Article: class Article:
def __init__(self, title: str, link: str, is_paid: bool): def __init__(self, title: str, link: str, is_paid: bool):
self.title = title self.title = title
@ -26,11 +23,13 @@ class Article:
print(f" Response is <{response.status_code}>") print(f" Response is <{response.status_code}>")
if response.status_code != 200: if response.status_code != 200:
return return
soup = BeautifulSoup(response.text, 'html.parser') soup = BeautifulSoup(response.text, "html.parser")
# Handle time # Handle time
publish_date_time = soup.find("meta", property="article:published_time")["content"] publish_date_time = soup.find("meta", property="article:published_time")[
self.date = datetime.datetime.strptime(publish_date_time, '%Y-%m-%dT%H:%M:%SZ') "content"
]
self.date = datetime.datetime.strptime(publish_date_time, "%Y-%m-%dT%H:%M:%SZ")
self.date = self.date.replace(tzinfo=ZoneInfo("UTC")) self.date = self.date.replace(tzinfo=ZoneInfo("UTC"))
# Handle text # Handle text
@ -45,20 +44,22 @@ class Article:
# Handle image if any # Handle image if any
img_url = soup.find("meta", property="og:image") img_url = soup.find("meta", property="og:image")
if img_url is not None: if img_url is not None:
self.text = f"<img alt=\"Image illustrant l'article\" src=\"{img_url['content']}\" />\n" + self.text self.text = (
f'<img alt="Image illustrant l\'article" src="{img_url["content"]}" />\n'
+ self.text
)
def full_title(self): def full_title(self):
paid = "" paid = ""
if self.is_paid: if self.is_paid:
paid = "[€] " paid = "[€] "
return f'{paid}{self.title}' return f"{paid}{self.title}"
def __str__(self): def __str__(self):
paid = "" paid = ""
if self.is_paid: if self.is_paid:
paid = "[€] " paid = "[€] "
return f'{paid}{self.title} [{self.date}]' return f"{paid}{self.title} [{self.date}]"
def __repr__(self): def __repr__(self):
return self.__str__() return self.__str__()
@ -73,10 +74,12 @@ def generate_feed(town_url, feed_url, feed_path):
print(f"Failed to get url: {response.status_code}") print(f"Failed to get url: {response.status_code}")
return False return False
soup = BeautifulSoup(response.text, 'html.parser') soup = BeautifulSoup(response.text, "html.parser")
articles = [] articles = []
small_articles = soup.find(id="ListUneMain").find_all("div", class_="wrapperClickArticle") + soup.find(id="ListUneSecondary").find_all("div", class_="wrapperClickArticle") small_articles = soup.find(id="ListUneMain").find_all(
"div", class_="wrapperClickArticle"
) + soup.find(id="ListUneSecondary").find_all("div", class_="wrapperClickArticle")
print(f"Found {len(small_articles)} articles...") print(f"Found {len(small_articles)} articles...")
for small_article in small_articles: for small_article in small_articles:
t = small_article.find_all("span")[1].text t = small_article.find_all("span")[1].text
@ -93,24 +96,32 @@ def generate_feed(town_url, feed_url, feed_path):
print("Generating feed...") print("Generating feed...")
items = [] items = []
for a in articles: for a in articles:
item = Item(title = a.full_title(), link= a.link, guid = Guid(a.link), pubDate = a.date, description = a.text) item = Item(
title=a.full_title(),
link=a.link,
guid=Guid(a.link),
pubDate=a.date,
description=a.text,
)
items.append(item) items.append(item)
feed = Feed(title = soup.title.string, feed = Feed(
title=soup.title.string,
link=feed_url, link=feed_url,
description=soup.title.string + " - Derniers articles", description=soup.title.string + " - Derniers articles",
language="fr-FR", language="fr-FR",
lastBuildDate=datetime.datetime.now(datetime.UTC), lastBuildDate=datetime.datetime.now(datetime.UTC),
items = items) items=items,
)
print("Writing feed...") print("Writing feed...")
with open(feed_path, 'w') as out_rss: with open(feed_path, "w") as out_rss:
dom = xml.dom.minidom.parseString(feed.rss()) dom = xml.dom.minidom.parseString(feed.rss())
out_rss.write(dom.toprettyxml()) out_rss.write(dom.toprettyxml())
print("All done!") print("All done!")
return True return True
if __name__ == '__main__': if __name__ == "__main__":
town_url = "https://www.leprogres.fr/c/rhone/69163-quincieux" town_url = "https://www.leprogres.fr/c/rhone/69163-quincieux"
town_url = "https://www.ledauphine.com/c/isere/38425-saint-maurice-l-exil" town_url = "https://www.ledauphine.com/c/isere/38425-saint-maurice-l-exil"
town_url = "https://www.ledauphine.com/c/isere/38124-corbelin" town_url = "https://www.ledauphine.com/c/isere/38124-corbelin"