User:Shizhao/fpcode

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

import os, sys
import wikipedia, query, datetime

mysite = wikipedia.getSite()

fptext=u'{{Wikipedia:每日图片/%s%s%s日}}'% (datetime.datetime.utcnow().year, datetime.datetime.utcnow().month, datetime.datetime.utcnow().day)
fptitle=u'Wikipedia:每日图片/%s%s%s日'% (datetime.datetime.utcnow().year, datetime.datetime.utcnow().month, datetime.datetime.utcnow().day)
rsstitle=u'Template:Featurepic/rss'
rsspage=wikipedia.Page(mysite,rsstitle)
fppage=wikipedia.Page(mysite,fptitle)
params = {
    'action'    :'query',
    'prop'      :'images',
    'titles'    :fptitle
    }  

imagepages=query.GetData(params, encodeTitle = False)[u'query'][u'pages'].values()[0][u'images']

params = {
    'action'    :'parse',
    'text'      :fptext,
    }    
text = query.GetData(params, encodeTitle = False)[u'parse'][u'text'][u'*']

for limagepage in imagepages:

    imagetitle = limagepage[u'title']

    params = {
        'action'    :'query',
        'prop'      :'imageinfo',
        'titles'    :imagetitle,
        'iiprop'    :'user|url|size|mime',
        'iilimit'   :'1'
        }      
    image = query.GetData(params, encodeTitle = False)[u'query'][u'pages'].values()[0][u'imageinfo'][0]

    href=u'{{subst:fullurl:%s}}' % imagetitle

wikitext=u'<title>%s</title>\n<link>%s</link>\n<guid>%s</guid>\n<description>%s</description>\n<enclosure url=\"%s\" length=\"%s\" type=\"%s\"/>\n<pubDate>{{subst:#time:r}}</pubDate>\n<dc:creator>%s提供图片</dc:creator></item>\n' %(fptitle, href, href, text, image[u'url'], image[u'size'], image[u'mime'], image[u'user'])

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]]' % fptitle)

wikipedia.stopme()