Utilisateur:Probot/ImageDuJour.py

# -*- coding: utf-8      -*-
"""
This module prepares and fills a page on :fr for the localized picture of the day
using the picture of the day on Commons.

Author : Kévin Drapel (:fr:User:Dake)
License : GPL
"""

import wikipedia, pagegenerators
import sys
import re

class IOTDBot:
        def __init__(self):
                wikipedia.setAction('Bot : préparation automatisée de la page')
                self.commons = wikipedia.Site('commons', 'commons')
        
        def prepareSubPages(self, nbDays):
                articles = []
                
                for i in range(1,nbDays):
                        title = u'Template:Potd/%s-%s-%02d' % (self.year,self.monthNumber,i)
                       	print "Getting one page..."
		       	page = wikipedia.Page(self.commons, title)
                        imageName = page.get()
                       
		      	print "Got page..."
                        comment = u'\'\'commentaire à écrire\'\''
                        
                        commentFr = u'Template:Potd/%s-%s-%d (fr)' % (self.year,self.monthNumber,i)
                        commentEn = u'Template:Potd/%s-%s-%d (en)' % (self.year,self.monthNumber,i)
                        pageFr = wikipedia.Page(self.commons, commentFr)
                        pageEn = wikipedia.Page(self.commons, commentEn)
                        
                        if pageFr.exists():
                                comment = pageFr.get()
                        else:
                                if pageEn.exists():
                                        comment = u'\'\'commentaire à traduire\'\' : ' + pageEn.get()
                                
                        article = u'{{ImageDuJour|%s|%s}}' % (imageName, comment)
                        articles.append(article)        
                        
                return articles
                
        def createSubPages(self, articles):
                for i,x in enumerate(articles):
                        p = wikipedia.Page(wikipedia.getSite(), u'Wikipédia:Image du jour/%d_%s_%s' % (i+1,self.monthName, self.year))
                        p.put(x, comment=u'Bot : création d\'une image du jour (en phase de test)')
                
        def createMainPage(self):
                article = u'__NOTOC__\n__NOEDITSECTION__\n';
                article += u'<table border="1">\n'

                i = 1
                while i <= self.monthDays:
                        article += u'<tr valign="top">\n'
                        article += u'<td width=50% style="padding-left:7px;padding-right:7px;padding-top:5px;padding-bottom:5px">\n'
                        article += u'== %d %s %s ==\n'  % (i,self.monthName,self.year)
                        article += u'{{Wikipédia:Image du jour/%d_%s_%s}}' % (i,self.monthName, self.year)      
                        article += u'<div valign="bottom" align="center" style="font-size:xx-small;">[[Wikipédia:Image du jour/%d_%s_%s|Modifier]]</div>\n' % (i,self.monthName,self.year)
                        article += u'</td>\n'
                        i += 1
                        
                        if i<self.monthDays:
                                article += u'<td width=50% style="padding-left:5px;padding-right:5px;padding-top:5px;padding-bottom:5px">\n'
                                article += u'== %d %s %s==\n'  % (i,self.monthName, self.year)
                                article += u'{{Wikipédia:Image du jour/%d_%s_%s}}' % (i,self.monthName, self.year)      
                                article += u'<div valign="bottom" align="center" style="font-size:xx-small;">[[Wikipédia:Image du jour/%d_%s_%s|Modifier]]</div>\n' % (i,self.monthName,self.year)
                                article += u'</td>\n'
                                i += 1
                
                        article += u'</tr>\n'
                        
                article += u'</table>'
                
                p = wikipedia.Page(wikipedia.getSite(), u'Wikipédia:Image du jour/%s_%s' % (self.monthName, self.year))
                
                print article
                p.put(article, comment=u'Bot : création du calendrier')
        

        def run(self):
                ## A MODIFIER POUR CREER UNE NOUVELLE SERIE D'IMAGES
                self.year = u'2007'
                self.monthName = u'janvier'
                self.monthNumber = u'01'
                self.monthDays = 31
                articles = self.prepareSubPages(self.monthDays+1)
		self.createSubPages(articles)
                self.createMainPage()

                        
def main():
                bot = IOTDBot()
                bot.run()
                
if __name__ == "__main__":
        try:
                main()
        finally:
                wikipedia.stopme()