This repository has been archived by the owner on Feb 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 36
/
atom_feeder.py
113 lines (88 loc) · 4.53 KB
/
atom_feeder.py
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
102
103
104
105
106
107
108
109
110
111
112
113
#!/bin/python
"""
Does rudimentary JSON diffing between two git versions of the manifest.json file,
and on new mod versions updates the feed.xml Atom feed in the github pages repo.
"""
# pylint: disable=redefined-outer-name,broad-except
import json
import datetime
from os import environ
from typing import Any
from xml.dom import minidom
import util
REF_BASE = util.exec_shell(f"git rev-parse {environ.get('REF_BASE') or 'HEAD^1'}")
REF_NEW = util.exec_shell(f"git rev-parse {environ.get('REF_NEW') or 'HEAD'}")
# The JSON manifests.
OLD_MANIFEST: dict[str, Any] = json.loads(util.exec_shell(f"git show {REF_BASE}:manifest.json"))
NEW_MANIFEST: dict[str, Any] = json.loads(util.exec_shell(f"git show {REF_NEW}:manifest.json"))
NEW_MODS = []
# Iterate over all the (new) mods
for mod_guid in NEW_MANIFEST["mods"]:
mod: dict[str, Any] = NEW_MANIFEST["mods"][mod_guid]
# Maps the versions into filtered & validated ones
mod["versions"] = util.map_mod_versions(mod["versions"], mod_guid)
# Transfer only mods that should be shown to grouped_mods
if util.should_show_mod(mod):
# Sort the mod's versions
mod["versions"].sort(reverse=True, key=lambda version: version["id"])
IS_NEW_MOD = True
if mod_guid in OLD_MANIFEST["mods"]:
old_versions = OLD_MANIFEST["mods"][mod_guid]['versions']
old_versions = util.map_mod_versions(old_versions, mod_guid)
for version in old_versions:
if version['id'] == mod["versions"][0]['id']:
IS_NEW_MOD = False
break
if IS_NEW_MOD:
mod["guid"] = mod_guid
NEW_MODS.append(mod)
if len(NEW_MODS) > 0:
atomNow = datetime.datetime.now(datetime.timezone.utc).isoformat()
with minidom.parse("gh-pages/feed.xml") as atomFeed:
atomFeed.getElementsByTagName("updated").item(0).firstChild.data = atomNow
for mod in NEW_MODS:
entry = atomFeed.createElement("entry")
id = atomFeed.createElement("id")
id.appendChild(atomFeed.createTextNode(mod["versions"][0]["releaseUrl"]))
entry.appendChild(id)
title = atomFeed.createElement("title")
title.appendChild(atomFeed.createTextNode("Released Version " + str(mod["versions"][0]["id"]) + " for '" + mod["name"] + "'"))
entry.appendChild(title)
content = atomFeed.createElement("content")
content.appendChild(atomFeed.createTextNode(mod["description"]))
entry.appendChild(content)
for modAuthor in mod["authors"]:
author = atomFeed.createElement("author")
authorName = atomFeed.createElement("name")
authorName.appendChild(atomFeed.createTextNode(modAuthor))
author.appendChild(authorName)
authorUri = atomFeed.createElement("uri")
authorUri.appendChild(atomFeed.createTextNode(mod["authors"][modAuthor]["url"]))
author.appendChild(authorUri)
entry.appendChild(author)
if environ.get("GITHUB_ACTOR"):
contributor = atomFeed.createElement("contributor")
contributorName = atomFeed.createElement("name")
contributorName.appendChild(atomFeed.createTextNode(environ.get("GITHUB_ACTOR")))
contributor.appendChild(contributorName)
entry.appendChild(contributor)
category = atomFeed.createElement("category")
category.setAttribute("term", "Games/NeosVR/Mods/" + mod["category"])
category.setAttribute("label", "NeosVR Mods")
entry.appendChild(category)
link = atomFeed.createElement("link")
link.setAttribute("rel", "alternate")
link.setAttribute("href", mod["versions"][0]["releaseUrl"])
entry.appendChild(link)
published = atomFeed.createElement("published")
published.appendChild(atomFeed.createTextNode(atomNow))
entry.appendChild(published)
updated = atomFeed.createElement("updated")
updated.appendChild(atomFeed.createTextNode(atomNow))
entry.appendChild(updated)
atomFeed.getElementsByTagName("feed").item(0).appendChild(entry)
atomWriter = open("gh-pages/feed.xml", "w")
xmlString = atomFeed.toprettyxml(encoding="utf-8", standalone=True).decode("utf-8")
xmlString = "\n".join([line for line in xmlString.splitlines() if line.strip()])
atomWriter.write(xmlString)
atomWriter.close()