|
#! python3 # noqa: E265 |
|
|
|
# standard library |
|
import json |
|
import logging |
|
from pathlib import Path |
|
|
|
# 3rd party |
|
import feedparser |
|
from jinja2 import Environment, select_autoescape, FileSystemLoader |
|
|
|
# log |
|
logging.basicConfig(level=logging.INFO) |
|
|
|
# variables |
|
configuration_path = Path("configuration.json") |
|
limit_length_by_feed = 5 |
|
template_path = Path(__file__).parent / "template.jinja" |
|
|
|
# read configuration file |
|
with configuration_path.open(mode="r", encoding="UTF-8") as stream_json: |
|
config = json.load(stream_json) |
|
|
|
logging.info( |
|
"{} feeds configured: {}".format( |
|
len(config.get("feeds")), |
|
", ".join([i.get("name") for i in config.get("feeds")]), |
|
) |
|
) |
|
|
|
# parse feeds and store data |
|
data_feeds = {} |
|
|
|
for f in config.get("feeds"): |
|
logging.info(f"Parsing feed: {f.get('name')}") |
|
parsed_feed = feedparser.parse(f.get("url")) |
|
|
|
# check feed health |
|
if parsed_feed.get("bozo") == 0: |
|
logging.info("Feed is healthy") |
|
else: |
|
logging.warning("Feed is not healthy") |
|
continue |
|
|
|
# parse items |
|
for i in parsed_feed.entries[:limit_length_by_feed]: |
|
data_feeds[i.guid] = { |
|
"title": i.title, |
|
"link": i.link, |
|
"description": i.description, |
|
"published": i.published, |
|
"source": f.get("name"), |
|
} |
|
if len(i.enclosures): |
|
data_feeds.get(i.guid)["illustration"] = i.enclosures[0].get("href") |
|
|
|
# Create output file |
|
|
|
env = Environment( |
|
autoescape=select_autoescape(["html", "xml"]), |
|
loader=FileSystemLoader(Path(__file__).parent), |
|
) |
|
template = env.get_template(template_path.name) |
|
|
|
with open("output.html", mode="w", encoding="UTF8") as fifeed_created: |
|
fifeed_created.write( |
|
template.render(feeds_items=data_feeds, title="Coucou les CQP Geom") |
|
) |