#!/usr/bin/python
# -*- coding: utf-8 -*-
import os, sys, re
import wikipedia, query, datetime
mysite = wikipedia.getSite()
fatext=u'{{Wikipedia:特色条目/%s年第%s周}}'% (datetime.datetime.utcnow().year, datetime.datetime.utcnow().strftime("%W"))
fatitle=u'Wikipedia:特色条目/%s年第%s周'% (datetime.datetime.utcnow().year, datetime.datetime.utcnow().strftime("%W"))
rsstitle=u'Template:Feature/rss'
rsspage=wikipedia.Page(mysite,rsstitle)
fapage=wikipedia.Page(mysite,fatitle)
params = {
'action' :'parse',
'text' :fatext,
}
text = query.GetData(params, encodeTitle = False)[u'parse'][u'text'][u'*']
r=ur'<b>(?P<d>.*?)<a (?P<url>.*?)title="(?P<title>.*?)"(?P<x>.*?)>(?P<title1>.*?)</a>(?P<dd>.*?)</b>'
for m in re.finditer(r,text,re.I):
mm = m.groupdict()
href=u'{{subst:fullurl:%s}}' % mm[u'title']
wikitext=u'<title>特色条目:%s</title>\n<link>%s</link>\n<guid>%s</guid>\n<description>%s</description>\n<pubDate>{{subst:#time:r}}</pubDate>\n<dc:creator>中文维基百科编者</dc:creator></item>\n' %(mm[u'title'], href, href, text)
rsstext=rsspage.get()
r=rsstext.split(u'<item>')
if len(r)<6:
r.insert(1, wikitext)
s = u'<item>'.join(r)
else:
r.insert(1, wikitext)
r.pop()
s = u'<item>'.join(r)
s=s+u'\n</channel>\n</rss>'
rsspage.put(s, u'Bot:特色条目rss更新: %s' %s fatitle)
wikipedia.stopme()