User:JarektBot/Replace PD-Art CSV.py

From Wikimedia Commons, the free media repository
Jump to navigation Jump to search
#!/usr/bin/python
# -*- coding: utf-8  -*-
'''
 
 
'''
import sys, wikipedia as pywikibot, csv, string, re
 
def main(args):
   csvFile = "Jarek's/Book2a.csv"
   always  = False
   minorEdit = False
   botflag = True
   
   # Read CSV file
   enc='utf-8'
   fname= []
   deathyear = []

   reader = csv.DictReader(open(csvFile, "rb"), dialect='excel', delimiter=';')
   for row in reader:
     fname.append(unicode(row.get(u'fname'), enc))
     deathyear.append(unicode(row.get(u'deathyear'), enc))

   site = pywikibot.getSite(u'commons', u'commons')
   for i in range(1, len(fname)):
      page = pywikibot.Page(site, fname[i])

      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
      old_text = text
      license_txt = "{{PD-Art|PD-old-auto|deathyear=%s}}" % deathyear[i]
      comment     = "Replace {{PD-Art}} with %s" % license_txt
      text = text.replace('{{PD-art}}','{{PD-Art}}')
      text = text.replace('{{Pd-art}}','{{PD-Art}}')
      text = text.replace('{{PD-Arte}}','{{PD-Art}}')
      text = text.replace('{{PD-art-life-70}}','{{PD-Art}}')
      text = text.replace('{{Pd-Art}}','{{PD-Art}}')
      text = text.replace('{{PD-ART}}','{{PD-Art}}')
      text = text.replace('{{PD-Art}}',license_txt)
      text = text.replace('{{PD-Art|PD-old}}',license_txt)
      text = text.replace('{{PD-Art|PD-old-70}}',license_txt)
      text = text.replace('{{self|PD-Art|PD-old}}',license_txt)
      text = text.replace('{{self|PD-Art|PD-old-70}}',license_txt)
      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!"