-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsep2rss.py
executable file
·69 lines (62 loc) · 2.27 KB
/
sep2rss.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
#!/usr/bin/env python
import os, glob, time, datetime, stat, re, sys
import codecs
import PyRSS2Gen as rssgen
RSS_PATH = os.path.join(sys.argv[1], 'seps.rss')
def firstline_startingwith(full_path, text):
for line in codecs.open(full_path, encoding="utf-8"):
if line.startswith(text):
return line[len(text):].strip()
return None
# get list of seps with creation time (from "Created:" string in sep .txt)
seps = glob.glob('sep-*.txt')
def sep_creation_dt(full_path):
created_str = firstline_startingwith(full_path, 'Created:')
# bleh, I was hoping to avoid re but some Reps editorialize
# on the Created line
m = re.search(r'''(\d+-\w+-\d{4})''', created_str)
if not m:
# some older ones have an empty line, that's okay, if it's old
# we ipso facto don't care about it.
# "return None" would make the most sense but datetime objects
# refuse to compare with that. :-|
return datetime.datetime(*time.localtime(0)[:6])
created_str = m.group(1)
try:
t = time.strptime(created_str, '%d-%b-%Y')
except ValueError:
t = time.strptime(created_str, '%d-%B-%Y')
return datetime.datetime(*t[:6])
seps_with_dt = [(sep_creation_dt(full_path), full_path) for full_path in seps]
# sort seps by date, newest first
seps_with_dt.sort(reverse=True)
# generate rss items for 10 most recent seps
items = []
for dt, full_path in seps_with_dt[:10]:
try:
n = int(full_path.split('-')[-1].split('.')[0])
except ValueError:
pass
title = firstline_startingwith(full_path, 'Title:')
author = firstline_startingwith(full_path, 'Author:')
url = 'http://ros.org/seps/sep-%0.4d' % n
item = rssgen.RSSItem(
title = 'SEP %d: %s' % (n, title),
link = url,
description = 'Author: %s' % author,
guid = rssgen.Guid(url),
pubDate = dt)
items.append(item)
# the rss envelope
desc = """
Newest ROS Enhancement Proposals (SEPs) - Information on new
language features, and some meta-information like release
procedure and schedules
""".strip()
rss = rssgen.RSS2(
title = 'Newest ROS Reps',
link = 'http://ros.org/seps',
description = desc,
lastBuildDate = datetime.datetime.now(),
items = items)
file(RSS_PATH, 'w').write(rss.to_xml())