aboutsummaryrefslogtreecommitdiff
path: root/tools/atom.py
blob: aea16d572b8a3a9e9cbef8512aac71d95d1302af (plain)
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/usr/bin/env python3

import re
from datetime import datetime, timezone, date

release = re.compile("^## Release ([0-9]+\\.[0-9]+\\.[0-9]+) - ([0-9]+-[0-9]+-[0-9]+)$")

title = "SpaceBeans releases"
base_url = "https://www.usebox.net/jjm/spacebeans/"
feed_file = "releases.xml"
author = "jjm"
tz = timezone.utc


def get_entries(changes):
    entries = []

    with open(changes, "rt") as fd:
        lines = fd.readlines()

    i = 0
    while i < len(lines):
        line = lines[i]
        m = release.match(line)
        if m:
            version = m.group(1)
            updated_on = datetime.combine(
                datetime.fromisoformat(m.group(2)), datetime.min.time()
            ).astimezone(tz)
            body = ""
            i += 1
            while i < len(lines) and (lines[i] == "" or lines[i][0] != "#"):
                body += lines[i]
                i += 1

            entries.append(dict(version=version, updated_on=updated_on, body=body))
        else:
            i += 1

    entries.sort(key=lambda o: o["updated_on"], reverse=True)
    return entries


def to_atom(post):
    return """\
<entry>
    <title>{title}</title>
    <link rel="alternate" href="{url}"/>
    <id>{url}</id>
    <updated>{updated_on}</updated>
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">
      <pre>{body}</pre>
      <p>Download available at <a href="{download}">SpaceBeans website</a>.</p>
      </div>
    </content>
</entry>\n""".format(
        title="Released SpaceBeans " + post["version"],
        url=base_url + "#" + post["version"],
        download=base_url + "#download",
        updated_on=post["updated_on"].isoformat(),
        body=post["body"].rstrip() + "\n",
    )


def gen_atom(entries):

    updated_on = datetime(1900, 1, 1, tzinfo=tz)
    posts_atom = []
    for post in entries:
        if post["updated_on"] > updated_on:
            updated_on = post["updated_on"]

        posts_atom.append(to_atom(post))

    return "\n".join(
        [
            """<?xml version="1.0" encoding="utf-8"?>""",
            """<feed xmlns="http://www.w3.org/2005/Atom">""",
            """<title>%s</title>""" % title,
            """<link href="%s"/>""" % base_url,
            """<link rel="self" href="%s"/>""" % (base_url + feed_file),
            """<updated>%s</updated>""" % updated_on.isoformat(),
            """<id>%s</id>""" % base_url,
            """<author>""",
            """    <name>%s</name>""" % author,
            """</author>""",
        ]
        + posts_atom
        + ["</feed>"]
    )


def main():
    entries = get_entries("../CHANGES.md")
    with open(feed_file, "wt") as fd:
        fd.write(gen_atom(entries))


if __name__ == "__main__":
    main()