-
Notifications
You must be signed in to change notification settings - Fork 1
/
daily-rss-commons.py
executable file
·230 lines (177 loc) · 6.54 KB
/
daily-rss-commons.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
#!/usr/bin/python
#
# http://en.wikipedia.org/wiki/User:Skagedal/Fafafa
#
# This program generates RSS feed of Commons' Picture of the Day.
import sys
import os
import string
import datetime
import time
import urllib
import re
import cPickle
import xml.sax.saxutils
import types # FIXME see too_old
#
# Settings
#
# ...General
settings = {
'rss_webmaster': '[email protected]',
'program_name': 'Fafafa',
'version': '0.8'
}
# ...for Picture of the Day
settings_potd = {
'language': 'en',
'entries': 20,
'output_filename': '/home/t/tgergo/public_html/commons-potd.xml',
'cache_filename': '/home/t/tgergo/temp/commons-potd_cache.pickle',
#'image_url' : 'http://commons.wikimedia.org/wiki/Template:Potd/%(year)d-%(month)02d-%(day)d',
'name_url' : 'http://commons.wikimedia.org/wiki/Template:Potd/%(year)d-%(month)02d-%(day)02d_(%(lang)s)',
'url': 'http://commons.wikimedia.org/wiki/Template:Potd/%(year)d-%(month)02d#%(day)02d',
'rss_title': 'Commons Picture of the Day', # http://commons.wikimedia.org/wiki/Template:Potd/name/%(lang)
'rss_link': 'http://commons.wikimedia.org/wiki/Commons:Picture_of_the_day',
'rss_description': 'RSS feed of the Commons Picture of the Day, generated from HTML by ' \
'(slightly modified) Fafafa: http://en.wikipedia/wiki/User:Skagedal/Fafafa'
}
# Insert time- and language-related variables into URL
months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
def calculate_url(url, date, lang='en'):
return url % { \
'days_ago': (datetime.date.today() - date).days +1, 'monthname': months[date.month - 1], \
'day': date.day, 'month': date.month, 'year': date.year, 'lang': lang }
def get_content_url(date):
return calculate_url(settings['url'], date)
def get_name_url(date, lang):
return calculate_url(settings['name_url'], date, lang)
# Subclassing of URLopener - sets "User-agent: ", which Wikipedia requires to be set
# to something else than the default "Python-urllib"
class MyURLopener(urllib.URLopener):
version = settings['program_name'] + "/" + settings['version']
def too_old(date):
# FIXME ugly hack to mix dates and date-language tuples in the cache
if type(date) is types.TupleType:
date = date[0]
return (datetime.date.today() - date).days > settings['entries']
# Caching of HTML from Wikipedia
class CacheItem:
def __init__(self, html, fetchtime):
self.html = html
self.fetchtime = fetchtime
class WPCache:
def __init__(self, cachefilename):
self.url_opener = MyURLopener()
self.filename = cachefilename
if (os.path.exists(cachefilename)):
file = open(cachefilename)
self.cache = cPickle.load(file)
file.close()
else:
self.cache = {}
def get_html(self, date):
if date in self.cache:
return self.cache[date].html
else:
html = self.url_opener.open(get_content_url(date)).read()
cacheitem = CacheItem(html, time.gmtime())
self.cache[date] = cacheitem
return html
def get_name(self, date, lang):
if (date, lang) in self.cache:
return self.cache[(date, lang)].html
else:
html = self.url_opener.open(get_name_url(date, lang)).read()
cacheitem = CacheItem(html, time.gmtime())
self.cache[(date, lang)] = cacheitem
return html
# Weed out old entries, so cache doesn't get big
def weed_out_old(self):
self.cache = dict([x for x in self.cache.items() if not too_old(x[0])])
def save(self):
self.weed_out_old()
file = open(self.filename, "w")
p = cPickle.Pickler(file)
p.dump(self.cache)
# Get useful part of the article
def get_section(s, n):
re_section = re.compile('<h2><span class="mw-headline">%02d</span></h2>(.*?</table>)' % (n), re.DOTALL)
m = re_section.search(s)
s = m.group(1)
return s.strip()
# Get title of article
def get_title(s):
# get content
re_content = re.compile('<!--\s*start\s+content\s*-->(.*)<div class="printfooter">', re.DOTALL)
m = re_content.search(s)
s = m.group(1)
# strip HTML tags
re_tag = re.compile('<[^>]*>', re.DOTALL)
s = re.sub(re_tag, '', s)
# strip superfluous whitespace
s = re.sub(r'\s+', ' ', s)
return s.strip()
# Create RSS item - expects html filtered by get_content
def rss_item(date, name, content):
if 'no_title' in settings and settings['no_title']:
title = "%s %d" % (months[date.month - 1], date.day)
else:
title = "%s %d: %s" % (months[date.month - 1], date.day, name)
return """<item>
<title>%(title)s</title>
<link>%(url)s</link>
<description>%(escaped_content)s</description>
</item>
""" % {
'title': title,
'url': get_content_url(date),
'escaped_content': xml.sax.saxutils.escape(content)}
# Puts the final RSS together
def rss(items):
return """<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:blogChannel="http://backend.userland.com/blogChannelModule">
<channel>
<title>%(rss_title)s</title>
<link>%(rss_link)s</link>
<description>%(rss_description)s</description>
<language>en-us</language>
<copyright>Various free licenses</copyright>
<lastBuildDate>%(build_date)s</lastBuildDate>
<docs>http://blogs.law.harvard.edu/tech/rss</docs>
<webMaster>%(webmaster)s</webMaster>
<generator>%(generator)s</generator>
%(items)s
</channel>
</rss>
""" % {
'rss_title': settings['rss_title'],
'rss_link': settings['rss_link'],
'rss_description': settings['rss_description'],
'webmaster': settings['rss_webmaster'],
'build_date': time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.gmtime()),
'items': items,
'generator': settings['program_name'] + " " + settings['version'] }
# Main
def main():
settings.update(settings_potd)
today = datetime.date.today()
one_day = datetime.timedelta(days = 1)
cache = WPCache(settings['cache_filename'])
dates = [today - one_day*x for x in range(settings['entries'])]
def item(date, lang):
#TODO fall back to english if language not found
name = get_title(cache.get_name(date, lang))
content = get_section(cache.get_html(date), date.day)
return rss_item(date, name, content)
# Iterate over the items
items = string.join([item(date, settings['language']) for date in dates], "")
the_rss = rss(items)
# Write to file
file = open(settings['output_filename'], "w")
file.write(the_rss)
file.close()
cache.save()
# Don't run if we're imported
if __name__ == '__main__':
main()