-
Notifications
You must be signed in to change notification settings - Fork 0
/
RSSGenerator.py
528 lines (462 loc) · 13.8 KB
/
RSSGenerator.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
# RSS2.0 Specification: https://cyber.harvard.edu/rss/rss.html
# Atom Specification: https://tools.ietf.org/html/rfc4287
from urllib.parse import urlparse, urlunparse, ParseResult
from constants import GMT_FORMAT, ISO8601_FORMAT
from time import strftime, gmtime
from typing import Union
from functools import wraps
import xml.etree.ElementTree as ET
RSS2_TYPE = 1
ATOM_TYPE = 2
ET._original_serialize_xml = ET._serialize_xml
def serialize_xml_with_CDATA(write, elem, qnames, namespaces,
short_empty_elements, **kwargs):
if elem.tag == 'CDATA':
write("<![CDATA[{}]]>".format(elem.text))
return
return ET._original_serialize_xml(write, elem, qnames, namespaces,
short_empty_elements, **kwargs)
ET._serialize_xml = ET._serialize['xml'] = serialize_xml_with_CDATA
def CDATA(text):
element = ET.Element("CDATA")
element.text = text
return element
class RSSTypeError(Exception):
def __init__(self, typ: int = None):
if typ is None or typ < RSS2_TYPE or typ > ATOM_TYPE:
Exception.__init__(self, "Type Error: Unknown Type.")
else:
d = {1: 'RSS2', 2: 'ATOM'}
Exception.__init__(self, f'Type Error: Type must be {d[typ]}.')
class RSSAttrRequired(Exception):
def __init__(self, text: str):
Exception.__init__(self, f'"{text}" is needed.')
def rss2Required(func):
@wraps(func)
def o(*args, **kwargs):
if args[0]._type != RSS2_TYPE:
raise RSSTypeError(RSS2_TYPE)
return func(*args, **kwargs)
return o
class RSSBasic:
def __init__(self, typ):
self._data = {}
self.category = []
if typ >= RSS2_TYPE and typ <= ATOM_TYPE:
self._type = typ
else:
raise RSSTypeError()
def _getTime(self, key: str) -> Union[int, float]:
if key in self._data:
v = self._data[key]
if v is not None and isinstance(v, (int, float)):
return v
return None
def _setTime(self, key: str, v):
if isinstance(v, (int, float)):
self._data[key] = v
elif isinstance(v, str):
if v.isnumeric():
self._data[key] = int(v)
def _toGMT(self, t: int):
if t is None:
return None
return strftime(GMT_FORMAT, gmtime(t))
def _toISO(self, t: int):
if t is None:
return None
return strftime(ISO8601_FORMAT, gmtime(t))
@property
def title(self):
if 'title' in self._data:
v = self._data['title']
if v is not None and isinstance(v, str) and v != '':
return v
return None
@title.setter
def title(self, v):
if isinstance(v, str):
if v != '':
self._data['title'] = v
else:
try:
self._data['title'] = str(v)
except:
pass
@property
def link(self):
if 'link' in self._data:
v = self._data['link']
if v is not None and isinstance(v, str) and v != '':
return v
return None
@link.setter
def link(self, v):
if isinstance(v, str):
s = v
else:
try:
s = str(v)
except:
return
p = urlparse(s)
n = ParseResult(p.scheme if p.scheme != '' else 'http', p.netloc,
p.path, p.params, p.query, p.fragment)
self._data['link'] = urlunparse(n)
@property
@rss2Required
def description(self):
if 'description' in self._data:
v = self._data['description']
if v is not None and isinstance(v, str) and v != '':
return v
return None
@description.setter
@rss2Required
def description(self, v):
if isinstance(v, str):
if v != '':
self._data['description'] = v
else:
try:
self._data['description'] = str(v)
except:
pass
@property
def pubDate(self):
return self._getTime('pubDate')
@property
def pubDateAsGMT(self):
return self._toGMT(self.pubDate)
@property
def pubDateAsISO(self):
return self._toISO(self.pubDate)
@pubDate.setter
def pubDate(self, v):
self._setTime('pubDate', v)
class RSSItem(RSSBasic):
def __init__(self, typ):
RSSBasic.__init__(self, typ)
@property
def author(self):
if 'author' in self._data:
v = self._data['author']
if v is not None and isinstance(v, str) and v != '':
return v
return None
@author.setter
def author(self, v):
if isinstance(v, str):
if v != '':
self._data['author'] = v
else:
try:
self._data['author'] = str(v)
except:
pass
@property
def comments(self):
if 'comments' in self._data:
v = self._data['comments']
if v is not None and isinstance(v, str) and v != '':
return v
return None
@comments.setter
def comments(self, v):
if isinstance(v, str):
s = v
else:
try:
s = str(v)
except:
return
p = urlparse(s)
n = ParseResult(p.scheme if p.scheme != '' else 'http', p.netloc,
p.path, p.params, p.query, p.fragment)
self._data['comments'] = urlunparse(n)
@property
def guid(self):
if 'guid' in self._data:
v = self._data['guid']
if v is not None and isinstance(v, str) and v != '':
return v
return None
@guid.setter
def guid(self, v):
if isinstance(v, str):
if v != '':
self._data['guid'] = v
else:
try:
self._data['guid'] = str(v)
except:
pass
def generate(self):
if self._type == RSS2_TYPE:
return self.genRSS2()
def genRSS2(self):
r = ET.Element("item")
t = self.title
has_title = t is not None
if has_title:
e = ET.Element("title")
e.append(CDATA(t))
r.append(e)
t = self.description
has_desc = t is not None
if has_desc:
e = ET.Element("description")
e.append(CDATA(t))
r.append(e)
if not has_title and not has_desc:
raise RSSAttrRequired("title or description")
t = self.comments
if t is not None:
e = ET.Element("comments")
e.text = t
r.append(e)
t = self.guid
if t is not None:
e = ET.Element("guid")
e.text = t
r.append(e)
t = self.link
if t is not None:
e = ET.Element("link")
e.text = t
r.append(e)
t = self.pubDateAsGMT
if t is not None:
e = ET.Element("pubDate")
e.text = t
r.append(e)
return r
class RSSMetadata(RSSBasic):
def __init__(self, typ):
RSSBasic.__init__(self, typ)
@property
@rss2Required
def language(self):
if 'language' in self._data:
v = self._data['language']
if v is not None and isinstance(v, str) and v != '':
return v
return None
@language.setter
@rss2Required
def language(self, v):
if isinstance(v, str):
if v != '':
self._data['language'] = v
else:
try:
self._data['language'] = str(v)
except:
pass
@property
def copyright(self):
if 'copyright' in self._data:
v = self._data['copyright']
if v is not None and isinstance(v, str) and v != '':
return v
return None
@copyright.setter
def copyright(self, v):
if isinstance(v, str):
if v != '':
self._data['copyright'] = v
else:
try:
self._data['copyright'] = str(v)
except:
pass
@property
def managingEditor(self):
if 'managingEditor' in self._data:
v = self._data['managingEditor']
if v is not None and isinstance(v, str) and v != '':
return v
return None
@managingEditor.setter
def managingEditor(self, v):
if isinstance(v, str):
if v != '':
self._data['managingEditor'] = v
else:
try:
self._data['managingEditor'] = str(v)
except:
pass
@property
def webMaster(self):
if 'webMaster' in self._data:
v = self._data['webMaster']
if v is not None and isinstance(v, str) and v != '':
return v
return None
@webMaster.setter
def webMaster(self, v):
if isinstance(v, str):
if v != '':
self._data['webMaster'] = v
else:
try:
self._data['webMaster'] = str(v)
except:
pass
@property
def lastBuildDate(self):
return self._getTime('lastBuildDate')
@property
def lastBuildDateAsGMT(self):
return self._toGMT(self.lastBuildDate)
@property
def lastBuildDateAsISO(self):
return self._toISO(self.lastBuildDate)
@lastBuildDate.setter
def lastBuildDate(self, v):
self._setTime('lastBuildDate', v)
@property
@rss2Required
def generator(self):
return 'csweb (https://github.com/lifegpc/csweb)'
@property
@rss2Required
def docs(self):
return 'https://cyber.harvard.edu/rss/rss.html'
@property
def ttl(self):
if 'ttl' in self._data:
v = self._data['ttl']
if v is not None:
if isinstance(v, int) and v > 0:
return v
elif isinstance(v, float) and round(v) > 0:
return round(v)
elif isinstance(v, str) and v.isnumeric():
i = int(v)
if i > 0:
return i
return None
@ttl.setter
def ttl(self, v):
if isinstance(v, int):
if v > 0:
self._data['ttl'] = v
elif isinstance(v, float):
if round(v) > 0:
self._data['ttl'] = v
elif isinstance(v, str) and v.isnumeric():
i = int(v)
if i > 0:
self._data['ttl'] = i
@property
def image(self):
if 'image' in self._data:
v = self._data['image']
if v is not None and isinstance(v, str) and v != '':
return v
return None
@image.setter
def image(self, v):
if isinstance(v, str):
s = v
else:
try:
s = str(v)
except:
return
p = urlparse(s)
n = ParseResult(p.scheme if p.scheme != '' else 'http', p.netloc,
p.path, p.params, p.query, p.fragment)
self._data['image'] = urlunparse(n)
def generate(self, r: ET.Element):
if self._type == RSS2_TYPE:
self.genRSS2(r)
def genRSS2(self, r: ET.Element):
t = self.title
if t is None:
raise RSSAttrRequired("title")
e = ET.Element("title")
e.append(CDATA(t))
r.append(e)
t = self.link
if t is None:
raise RSSAttrRequired("link")
e = ET.Element("link")
e.text = t
r.append(e)
t = self.description
if t is None:
raise RSSAttrRequired("description")
e = ET.Element("description")
e.append(CDATA(t))
r.append(e)
t = self.copyright
if t is not None:
e = ET.Element("copyright")
e.text = t
r.append(e)
t = self.docs
if t is not None:
e = ET.Element("docs")
e.text = t
r.append(e)
t = self.generator
if t is not None:
e = ET.Element("generator")
e.text = t
r.append(e)
t = self.image
if t is not None:
e = ET.Element("image")
e.text = t
r.append(e)
t = self.language
if t is not None:
e = ET.Element("language")
e.text = t
r.append(e)
t = self.lastBuildDateAsGMT
if t is not None:
e = ET.Element("lastBuildDate")
e.text = t
r.append(e)
t = self.managingEditor
if t is not None:
e = ET.Element("managingEditor")
e.text = t
r.append(e)
t = self.pubDateAsGMT
if t is not None:
e = ET.Element("pubDate")
e.text = t
r.append(e)
t = self.ttl
if t is not None:
e = ET.Element("ttl")
e.text = str(t)
r.append(e)
t = self.webMaster
if t is not None:
e = ET.Element("webMaster")
e.text = str(t)
r.append(e)
class RSSGen:
def __init__(self, typ: int):
self.meta = RSSMetadata(typ)
self._typ = typ
self.list = []
def generate(self):
if self._typ == RSS2_TYPE:
return self.genRSS2()
def genRSS2(self):
r = ET.Element("rss")
r.attrib['version'] = '2.0'
c = ET.Element("channel")
r.append(c)
self.meta.genRSS2(c)
for i in self.list:
if isinstance(i, RSSItem):
c.append(i.genRSS2())
return ET.tostring(r, encoding='UTF8').decode()