User:Shizhao/gacode

#!/usr/bin/python
# -*- coding: utf-8  -*-

import os, sys, re
import wikipedia, query, datetime

mysite = wikipedia.getSite()

gatext=u'{{Wikipedia:优良条目/%s%s%s日}}'% (datetime.datetime.utcnow().year, datetime.datetime.utcnow().month, datetime.datetime.utcnow().day)
gatitle=u'Wikipedia:优良条目/%s%s%s日'% (datetime.datetime.utcnow().year, datetime.datetime.utcnow().month, datetime.datetime.utcnow().day)
rsstitle=u'Template:Good/rss'
rsspage=wikipedia.Page(mysite,rsstitle)
gapage=wikipedia.Page(mysite,gatitle)

params = {
    'action'    :'parse',
    'text'      :gatext,
    }    
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]]' % gatitle)

wikipedia.stopme()