Purple exclamation mark.svg Planning the future of Botwiki! - Help us bring Botwiki up to date, contribute to our strategy discussion, add bot scripts, and contribute manuals, guides, and tutorials! Almost anything related to bots, particularly those used to edit mediawiki, is welcome.

Red exclamation mark.svg UNABLE TO EDIT? - We've experienced attacks by spambots lately and now require you to confirm your e-mail before you can edit (go to your preferences, enter an e-mail address, and request a confirmation e-mail, then go to your e-mail and click on the confirmation link). We also require new accounts to make a few edits and wait a few minutes before before you can create a page; however, if this is a problem contact us in #botwiki and we can manually confirm your account. Sorry for the inconvenience.

Python:UpdateVersityTemplate.py

From Botwiki
Jump to: navigation, search
#!/usr/bin/python
# -*- coding: utf-8  -*-
 
"""
Bot for FedeReghe for it.wikiversity.
 
It add some parameters to a template of it.wikiversity.
"""
 
import wikipedia
import re
 
def main():
    args = wikipedia.handleArgs()
    site = wikipedia.getSite()
    template = wikipedia.Page(site, 'Template:Materia')
    for page in template.getReferences(False, True, True):
        wikipedia.output(u'Loading %s...' % page.title())
        try:
            text = page.get()
        except wikipedia.NoPage:
            wikipedia.output(u'Page empty, skip.')
            continue
        except wikipedia.IsRedirectPage:
            wikipedia.output(u'%s is a redirect, skip.' % page.title())
            continue
        takesettings = re.compile(r'\{\{(?:[Tt]emplate:|)[Mm]ateria(.*?)\}\}', re.DOTALL)
        settings = takesettings.findall(text)[0]
        number = settings.lower().count('|corso') + 1
        print number
        puntanoQui = page.getReferences()
        for corso in puntanoQui:
            if 'corso:' in corso.title().lower():
                addon = '|' + corso.title().replace(':', str(number) + '=').replace('Corso', 'corso')
                if '\n' not in settings and corso.title().lower().replace('corso:', '') not in settings.lower():
                    settings += '\n' + addon + '\n'
                    number += 1
                elif corso.title().lower().replace('corso:', '') not in settings.lower():
                    settings += addon + '\n'
                    number += 1                
        settings += '}}'
        print settings
        newtext = takesettings.sub('{{Materia%s' % settings, text)
        if newtext != text:
            page.put(newtext, 'Bot: aggiorno template Materia')
        else:
            wikipedia.output(u'No changes..')
 
if __name__ == "__main__":
    try:
        main()
    finally:
        wikipedia.stopme()
Personal tools
Share