User:JarektUploadBot/commons institution maintenance.py

From Wikimedia Commons, the free media repository
Jump to navigation Jump to search
#!/usr/bin/python
# -*- coding: utf-8  -*-
"""
Script to 

"""

#
# (C) Jarekt, 2011
#
# Distributed under the terms of the MIT license.
#

import wikipedia as pywikibot
import config, catlib, time, re

def main():
    ''' Main loop. Loop over all Institution templates in
    Category:Institution templates and ...

    '''
    inst_strings = [u'{{institution', u'{{museum', u'{{:institution', u'{{:museum']

    time_sleep = 10
    counter = 1
    site = pywikibot.getSite(u'commons', u'commons')
    inst_cat = catlib.Category(site, u'Category:Institution templates')
    for page in inst_cat.articles(startFrom=None): # page from Category:Institution templates category
      if (page.namespace()==106):    # make sure it is of correct namespace
        inst_str  = '{{'+page.title()+'}}'    # institution string 
        inst_str_ = inst_str.replace(' ','_') # institution string with "_" instead of spaces
        pywikibot.output(u'Getting %s :' % inst_str)
        for template in page.templatesWithParams(): # get all the templates
          if (template[0] == 'Institution'):        # make sure Institution template is present
            for param in template[1]:               # go through all the template parameters
              #pywikibot.output(u'"%s"' % param)
              m=re.search("homecat\s*=\s*([^\n]*)", param)  # and look for homecat
              if m!=None:                           # if homecat found
                cat_name = 'Category:'+m.group(1).strip()
                pywikibot.output(u'   homecat = "%s"' % cat_name)
                cat_page =catlib.Category(site, cat_name)
                if cat_page.exists():               # if homecat exists
                  cat_txt = cat_page.get()          # get the homecat text 
                  cat_txt_l = cat_txt.lower()
                  # if institution template is missing than add it
                  if (('{{institution' not in cat_txt_l) and
                      ('{{:institution' not in cat_txt_l) and
                      ('{{museum' not in cat_txt_l) and
                      ('{{:museum' not in cat_txt_l)and
                      ('redirect' not in cat_txt_l)):
                    cat_txt = inst_str+'\n'+cat_txt
                    pywikibot.output('Adding institution template')
                    cat_page.put(cat_txt, "Add Institution template", False, True)
                    pywikibot.output("Waiting for %s seconds," % time_sleep)
                    time.sleep(time_sleep)
                    counter = counter + 1
                    #if (counter>20):
                      #return

if __name__ == "__main__":
    try:
        main()
    finally:
        pywikibot.stopme()