User:JarektBot/Replace PD-old.py

#!/usr/bin/python
# -*- coding: utf-8  -*-
'''
 
 
'''
import sys, wikipedia as pywikibot, csv, string, re
 
def main(args):
   always  = False
   minorEdit = False
   botflag = True
   
   # Read CSV file
   enc='utf-8'
   fname= []
   deathyear = []

   site = pywikibot.getSite(u'commons', u'commons')
   index_page = pywikibot.Page(site, 'User:Jarekt/c')
   creator2year = {}
   for page in index_page.linkedPages(withImageLinks = True):
      if page.namespace()!=6:
        continue
      try:
        # Load the page
        text = page.get()
      except pywikibot.NoPage:
        pywikibot.output(u"   Page %s does not exist; skipping."
                           % page.title(asLink=True))
        continue
      except pywikibot.IsRedirectPage:
        pywikibot.output(u"   Page %s is a redirect; skipping."
                           % page.title(asLink=True))
        continue
      deathyear = 0
      for template in page.templates(text):         # get all the templates
        if "Creator:" in template:
          #pywikibot.output(u'file="%s" template="%s"' % (page.title(), template))
          if template in creator2year.keys():
             deathyear = creator2year[template]
             break
          creator_page = pywikibot.Page(site, template)
          if creator_page.isRedirectPage():
            creator_page = creator_page.getRedirectTarget()
            pywikibot.output(u"   Redirect -> %s" % creator_page.title(asLink=True))
          creator_txt = creator_page.get()
          m=re.search("[Dd]eath\w\w\w\w\s*=\s*(\d\d\d\d?)", creator_txt)
          creator2year[template] = 0
          if m!=None:
            deathyear = int(m.group(1).strip())
            creator2year[template] = deathyear
            #pywikibot.output(u'deathyear="%i"' % deathyear)
          break    

      if deathyear==0:
        pywikibot.output(u'Deathyear not found -> Skipping')
        continue
      old_text = text


      # common alternative names
      exceptions = ['comment', 'nowiki']
      text = pywikibot.replaceExcept(text, '([\{\|])PD-art([\}\|])','\1PD-Art\2',exceptions, True)
      text = pywikibot.replaceExcept(text, '([\{\|])Pd-art([\}\|])','\1PD-Art\2',exceptions, True)
      text = pywikibot.replaceExcept(text, '([\{\|])PD-Arte([\}\|])','\1PD-Art\2',exceptions, True)
      text = pywikibot.replaceExcept(text, '([\{\|])PD-art-life-70([\}\|])','\1PD-Art\2',exceptions, True)
      text = pywikibot.replaceExcept(text, '([\{\|])Pd-Art([\}\|])','\1PD-Art\2',exceptions, True)
      text = pywikibot.replaceExcept(text, '([\{\|])PD-ART([\}\|])','\1PD-Art\2',exceptions, True)
      text = pywikibot.replaceExcept(text, '([\{\|])PD-OLD([\}\|])','\1PD-Old\2',exceptions, True)
      text = pywikibot.replaceExcept(text, '([\{\|])Pd-old([\}\|])','\1PD-Old\2',exceptions, True)
      text = pywikibot.replaceExcept(text, '([\{\|])PD-oud([\}\|])','\1PD-Old\2',exceptions, True)

      # PD-Art replacements
      if deathyear>=1912:
        license_txt = "{{PD-Art|PD-old-auto|deathyear=%s}}" % deathyear
      else:
        license_txt = "{{PD-Art|PD-old-100}}"  
      comment     = "Replace {{PD-Art}} with %s" % license_txt
      text = pywikibot.replaceExcept(text, '\{\{PD-Art\}\}',license_txt, exceptions, True)
      text = pywikibot.replaceExcept(text, '\{\{PD-Art\|PD-old\}\}',license_txt, exceptions, True)
      text = pywikibot.replaceExcept(text, '\{\{PD-Art\|PD-old-70\}\}',license_txt, exceptions, True)
      text = pywikibot.replaceExcept(text, '\{\{self\|PD-Art\|PD-old\}\}',license_txt, exceptions, True)
      text = pywikibot.replaceExcept(text, '\{\{self\|PD-Art\|PD-old-70\}\}',license_txt, exceptions, True)

      # PD-Old replacements
      if text != old_text:
        if deathyear>=1912:
          license_txt = "{{PD-old-auto|deathyear=%s}}" % deathyear
        else:
          license_txt = "{{PD-old-100}}"
        comment     = "Replace {{PD-Old}} with %s" % license_txt
        text = pywikibot.replaceExcept(text, '\{\{PD-old\}\}',license_txt, exceptions, True)
        text = pywikibot.replaceExcept(text, '\{\{PD-old-70\}\}',license_txt, exceptions, True)
        text = pywikibot.replaceExcept(text, '\{\{self\|PD-old\}\}',license_txt, exceptions, True)
        text = pywikibot.replaceExcept(text, '\{\{self\|PD-old-70\}\}',license_txt, exceptions, True)

      if text != old_text:
        # Show the title of the page we're working on.
        # Highlight the title in purple.
        pywikibot.output(u"\n\n>>> \03{lightpurple}%s\03{default} <<<"
                        % page.title())
        # show what was changed
        pywikibot.showDiff(old_text, text)
        pywikibot.output(u'Comment: %s' %comment)
        if not always:
          choice = pywikibot.inputChoice(
             u'Do you want to accept these changes?',
             ['Yes', 'No', 'Always', 'Quit'],
             ['y', 'N', 'a', 'q'], 'N')
          if choice == 'a':
             always = True
          elif choice == 'q':
             import sys
             sys.exit()
        if always or choice == 'y':
            try:
                # Save the page
                page.put(text, comment=comment,
                         minorEdit=minorEdit, botflag=botflag)
            except pywikibot.LockedPage:
                pywikibot.output(u"Page %s is locked; skipping."
                                 % page.title(asLink=True))
            except pywikibot.EditConflict:
                pywikibot.output(
                    u'Skipping %s because of edit conflict'
                    % (page.title()))
            except pywikibot.SpamfilterError, error:
                pywikibot.output(
u'Cannot change %s because of spam blacklist entry %s'
                    % (page.title(), error.url))


 
if __name__ == "__main__":
   try:
       main(sys.argv[1:])
   finally:
       print "All done!"