Utilisateur:Xavier Combelle/annee de naissance.py

Un article de Wikipédia, l'encyclopédie libre.

 #!/usr/bin/python
 # -*- coding: utf-8  -*-
 
 """
 This bot goes over multiple pages of the home wiki, and edits them without
 changing. This is for example used to get category links in templates
 working.
 
 This script understands various command-line arguments:
 
     -start:        used as -start:page_name, specifies that the robot should
                    go alphabetically through all pages on the home wiki,
                    starting at the named page.
 
     -file:         used as -file:file_name, read a list of pages to treat
                    from the named textfile. Page titles should be enclosed
                    in [[double-squared brackets]].
 
     -ref:          used as -start:page_name, specifies that the robot should
                    touch all pages referring to the named page.
 
     -cat:          used as -cat:category_name, specifies that the robot should
                    touch all pages in the named category.
 
 All other parameters will be regarded as a page title; in this case, the bot
 will only touch a single page.
 """
 import wikipedia, pagegenerators, catlib
 import sys
 import re
 site = wikipedia.getSite()
 #todo reutiliser le contenu de wikipedia
 
 
 class TouchBot:
     def __init__(self, generator):
         self.generator = generator
 
     def run(self):
         for page in self.generator:
             try:
                    if True:
                        next = False
                        for p in page.get().split("\n"):
                                if p[:3] == "*[[":
                                        annee = p[3:7]
                                        reste = p[11:]
                                        #print annee,repr(reste)
                                        import re
                                        nb = len(re.findall("[Uu]tilisateur",reste))
                                        if nb>0:
                                                print annee, nb
                                if next:
                                        next = False
                                        nb = p[2:].strip()
                                        if nb !="":
                                                print annee,nb
                                if p[:4] == "| [[":
                                        next = True
                                        annee = p[4:8]
                                                
                                                
                
                
                #utiliser page.title()
             except wikipedia.NoPage:
                 print "Page %s does not exist?!" % page.aslink()
             except wikipedia.IsRedirectPage:
                 print "Page %s is a redirect?!" % page.aslink()
             except wikipedia.LockedPage:
                 print "Page %s is locked?!" % page.aslink()
 
 def main():
     #page generator
     gen = None
     pageTitle = []
     for arg in sys.argv[1:]:
         arg = wikipedia.argHandler(arg, 'touch')
         if arg:
             pageTitle.append(arg)
 
     if pageTitle:
         page = wikipedia.Page(wikipedia.getSite(), ' '.join(pageTitle))
         gen = iter([page])
     if not gen:
         wikipedia.showHelp('touch')
     else:
         preloadingGen = pagegenerators.PreloadingGenerator(gen)
         bot = TouchBot(preloadingGen)
         bot.run()
 
 if __name__ == "__main__":
     try:
         main()
     finally:
         wikipedia.stopme()