-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert2rss.py
executable file
·217 lines (154 loc) · 6.48 KB
/
convert2rss.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#!/usr/bin/env python
import json
import sys
from datetime import datetime
from typing import Dict, Any, List, Callable
from serlo_api_client import fetch_publisher
from utils import has_description
GERMAN_LANGUAGE_CODE = "de"
VIDEO_RESOURCE_TYPE = "VideoObject"
QUIZ_RESOURCE_TYPE = "Quiz"
MATHEMATICS_SUBJECT_ID = "http://w3id.org/kim/schulfaecher/s1017"
def main(input_filename: str, output_filename: str):
with open(input_filename, "r", encoding="utf-8") as input_file:
metadata = json.load(input_file)
with open("keywords.json", "r", encoding="utf-8") as input_file:
keywords = json.load(input_file)
with open(output_filename, "w", encoding="utf-8") as output_file:
publisher = get_publisher()
rss_export = generate_rss(metadata, publisher, datetime.utcnow, keywords)
print(rss_export, file=output_file)
def generate_rss(
metadata: List[Dict[str, Any]],
publisher: Dict[str, Any],
get_current_time: Callable[[], datetime],
keywords,
) -> str:
published_date = get_current_time()
serlo_url = escape(publisher["url"])
serlo_description = escape(publisher["description"])
serlo_name = escape(publisher["name"])
rss = """<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" xmlns:sdx="sodix.dtd" version="2.0">
<channel>
"""
rss += f" <title>{serlo_name}</title>\n"
rss += f" <link>{serlo_url}</link>\n"
rss += f" <description>{serlo_description}</description>\n"
rss += " <language>de-DE</language>\n"
rss += f" <copyright>{serlo_name}</copyright>\n"
rss += f" <pubDate>{format_date(published_date)}</pubDate>\n"
for resource in filtered_data(metadata):
rss += converted_resource(
resource,
publisher,
keywords,
)
rss += """</channel>
</rss>
"""
return rss
def filtered_data(metadata: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
def is_in_german(res):
return escape(res["inLanguage"][0]) == GERMAN_LANGUAGE_CODE
def is_a_video_resource(res):
return VIDEO_RESOURCE_TYPE in res["type"]
def is_a_quiz_resource(res):
return QUIZ_RESOURCE_TYPE in res["type"]
def is_the_subject_math(res):
return (
"about" in res
and len(res["about"]) == 1
and res["about"][0]["id"] == MATHEMATICS_SUBJECT_ID
)
return [
res
for res in metadata
if is_in_german(res)
and has_description(res)
and not is_a_video_resource(res)
and not "Course" in res["type"]
and not is_a_quiz_resource(res)
and is_the_subject_math(res)
and get_license_name(res["license"]["id"]) is not None
]
def converted_resource(
resource: Dict[str, Any],
publisher: Dict[str, Any],
keywords,
) -> str:
rss = "<item>\n"
rss += f' <title>{escape(resource["name"])}</title>\n'
rss += f" <sdx:language>{GERMAN_LANGUAGE_CODE}</sdx:language>\n"
if has_description(resource):
description = resource["description"]
rss += f" <description>{escape(description)}</description>\n"
resource_keywords = keywords.get(resource["id"], [])
if len(resource_keywords) > 0:
rss += f' <itunes:keywords>{", ".join(resource_keywords)}</itunes:keywords>\n'
rss += f' <link>{escape(resource["id"])}</link>\n'
authors = ", ".join(escape(author["name"]) for author in resource["creator"])
rss += f" <author>{authors}</author>\n"
rss += f' <sdx:authorsWebsite>{escape(publisher["url"])}</sdx:authorsWebsite>\n'
rss += f' <sdx:producer>{escape(publisher["name"])}</sdx:producer>\n'
rss += f' <guid isPermaLink="true">{escape(resource["id"])}</guid>\n'
date_created = format_date(datetime.fromisoformat(resource["dateCreated"]))
rss += f" <pubDate>{date_created}</pubDate>\n"
rss += ' <itunes:image href="https://de.serlo.org/_assets/img/meta/mathe.png"></itunes:image>\n'
rss += """ <sdx:userGroups>learner, teacher</sdx:userGroups>
<sdx:educationalLevel>Sekundarstufe I, Sekundarstufe II</sdx:educationalLevel>
<sdx:classLevel>5-13</sdx:classLevel>
"""
rss += f" <sdx:learnResourceType>{escape(get_resource_type(resource))}</sdx:learnResourceType>\n"
rss += " <sdx:schoolType>Realschule, Gymnasium, Mittel- / Hauptschule, Fachoberschule</sdx:schoolType>\n"
# This need to be updated when we add additional subjects
rss += " <sdx:subject>380</sdx:subject>\n"
resource_license = resource["license"]["id"]
license_name = get_license_name(resource_license)
if license_name:
rss += f" <sdx:licenseName>{escape(license_name)}</sdx:licenseName>\n"
rss += " <sdx:licenseCountry>de</sdx:licenseCountry>\n"
version = get_license_version(resource_license)
if version:
rss += f" <sdx:licenseVersion>{escape(version)}</sdx:licenseVersion>\n"
rss += """ <sdx:costs>FREE</sdx:costs>
</item>
"""
return rss
def get_license_version(resource_license):
for version in ["4.0", "3.0", "2.5", "2.0"]:
if version in resource_license:
return version
return None
def get_license_name(resource_license):
if "/by/" in resource_license:
return "CC BY"
if "/zero/" in resource_license:
return "CC0"
if "/by-sa/" in resource_license:
return "CC BY-SA"
return None
def get_resource_type(resource):
resource_types = resource["type"]
if "Article" in resource_types:
return "Arbeitsblatt, Text, Unterrichtsbaustein, Veranschaulichung, Webseite"
if "Course" in resource_types:
return "Kurs, Entdeckendes Lernen, Text, Unterrichtsbaustein, Veranschaulichung, Webseite"
if "Quiz" in resource_types:
return "Übung, Test/Prüfung, Lernkontrolle, Webseite"
return "App, Interaktion, Entdeckendes Lernen, Webtool"
def escape(input_string):
escape_dict = {"&": "&", "<": "<", ">": ">", '"': """, "'": "'"}
for char, escaped_char in escape_dict.items():
input_string = input_string.replace(char, escaped_char)
return input_string
def format_date(date):
return escape(date.strftime("%a, %d %b %Y %H:%M:%S +0000"))
def get_publisher() -> Dict[str, Any]:
response = fetch_publisher()
return response["metadata"]["publisher"] # pylint: disable=E1136
if __name__ == "__main__":
if len(sys.argv) < 3:
print("Usage: convert2rss.py INPUT_FILENAME OUTPUT_FILENAME")
sys.exit(1)
main(sys.argv[1], sys.argv[2])