~hristoast/hristoast

ref: a31dcabaddae86aeb72cc49980b22881e10d9f7d hristoast/scripts/json2feed.py -rwxr-xr-x 1.5 KiB
a31dcabaHristos N. Triantafillou Dehardcode that (sorta) 1 year, 2 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/env python3

# Adapted from:
# https://github.com/dmbaturin/baturin.org/blob/be8feeb8cf369ce73f601a3e4fc9f43f22b9d758/scripts/json2feed.py

import sys
import json
import dateutil.tz
import dateutil.parser

from datetime import datetime
from feedgen.feed import FeedGenerator


base_url = "https://hristos.co"

default_date = datetime(1970, 1, 1, tzinfo=dateutil.tz.gettz("Etc/UTC"))

feed_id = base_url + "/feed.xml"
feed_author = "Hristos N Triantafillou"
feed_title = feed_author + "'s blog"
feed_subtitle = "On programming and things related and not"
feed_logo = base_url + "/img/slime.png"
feed_language = "en"
feed_author_email = "me@hristos.co"


def get_date(ds):
    try:
        return dateutil.parser.parse(ds, default=default_date).isoformat()
    except Exception:
        return default_date


fg = FeedGenerator()
fg.id(feed_id)
fg.title(feed_title)
fg.subtitle(feed_subtitle)
fg.author({"name": feed_author, "email": feed_author_email})
# fg.link( href=, rel='alternate' )
fg.logo(feed_logo)
fg.language(feed_language)

index_file = sys.argv[1]

with open(index_file, "r") as f:
    entries = json.load(f)

for entry in entries:
    entry["author"] = feed_author

    if not entry["title"] or not entry["date"]:
        continue

    fe = fg.add_entry()

    url = base_url + entry["url"]

    fe.id(url)
    fe.link(href=url, rel="alternate")
    fe.title(entry["title"])
    fe.content(entry["excerpt"], type="html")
    fe.updated(get_date(entry["date"]))


atomfeed = fg.atom_str(pretty=True).decode()

print(atomfeed)