1
Fork 0
mirror of https://github.com/RealOrangeOne/RealOrangeOne.git synced 2024-12-22 13:55:59 +00:00
.profile/build.py

47 lines
1.2 KiB
Python
Raw Normal View History

2024-02-05 20:13:45 +00:00
#!/usr/bin/env python3
import jinja2
from pathlib import Path
import requests
2024-02-18 21:09:47 +00:00
import feedparser
from datetime import date
import time
from operator import itemgetter
2024-02-05 20:13:45 +00:00
PROJECT_DIR = Path(__file__).resolve().parent
TEMPLATE_FILE = PROJECT_DIR / "README.md.j2"
OUTPUT_FILE = PROJECT_DIR / "README.md"
def get_posts() -> list[dict]:
response = requests.get("https://theorangeone.net/api/latest-posts/?page_size=5&format=json")
response.raise_for_status()
return response.json()["results"]
2024-02-18 21:09:47 +00:00
def get_notes() -> list[dict]:
notes = []
for note in feedparser.parse("https://notes.theorangeone.net/feed_rss_created.xml")["entries"]:
notes.append({
"title": note.title,
"link": note.link,
"published": date.fromtimestamp(time.mktime(note.published_parsed)),
"tags": sorted([tag.term for tag in getattr(note, "tags", [])])
})
return sorted(notes, key=itemgetter("published"), reverse=True)[:5]
2024-02-05 20:13:45 +00:00
def main():
template = jinja2.Template(TEMPLATE_FILE.read_text())
2024-02-07 19:46:42 +00:00
new_readme = template.render(
2024-02-18 21:09:47 +00:00
latest_posts=get_posts(),
recent_notes=get_notes()
2024-02-07 19:46:42 +00:00
)
OUTPUT_FILE.write_text(new_readme)
2024-02-05 20:13:45 +00:00
if __name__ == "__main__":
main()