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:Gvf.py

From Botwiki
Jump to: navigation, search
#!/usr/bin/python
# -*- coding: utf-8  -*-
 
"""
This bot check all the references to the italian Template Bio, looking for a settings.
If this settings doesn't have |ExtImmagine (that define the extension of the image) the
bot will add the default ones. Unfortunately there are a lot of exceptions, and so the
code is become a little bit bigger of what I thought.
 
Bot written for Gvf, it.wiki
"""
 
#
# (C) Filnik, 2008
#
# Distributed under the terms of the MIT license.
#
__version__ = '$Id: $'
#
 
import wikipedia, pagegenerators, re
 
def main():
    arg = wikipedia.handleArgs()
    site = wikipedia.getSite()
    page = wikipedia.Page(site, 'Template:Bio')
    #pagegenerators.ReferringPageGenerator(page, False, True, True)
    pages_to_check = pagegenerators.ReferringPageGenerator(page, False, True, True)#[wikipedia.Page(site, 'Antonio Fogazzaro')]
    preloadingGen = pagegenerators.PreloadingGenerator(pages_to_check, pageNumber=60)
    for page in preloadingGen:
        wikipedia.output('Loading %s...' % page.title())
        try:
            text = page.get()
        except Exception, e:
            print e
            continue
        regex_image = re.compile(r'\{\{(?:[Tt]emplate|[Bb]io[^{]*?)\|[\s\n\t]*?[Ii]mmagine *?= *?(.*?)\s*?([|\n}])[^{]*?\}', re.DOTALL)
        regex_sub_image = re.compile(r'(\{\{(?:[Tt]emplate|)[Bb]io[^{]*?)\|[\s\n\t]*?[Ii]mmagine *?= *?(.*?)\s*?([|\n}])([^{]*?\})', re.DOTALL)
        image_inside_list = regex_image.findall(text)
 
        if image_inside_list == []:
            wikipedia.output(u'Image not found, skip.')
            continue
        else:
            if not re.findall(r'\w', image_inside_list[0][0]):
                wikipedia.output(u'No image, only setting empty, skip')
                continue
            extension = re.findall(r'\.(\w{3,5})[\s\n]*?$', image_inside_list[0][0])
            if extension != []:
                wikipedia.output(u'WARNING: Found %s as extension :S something is gone wrong' % extension)
                wikipedia.input(u'ping') # Wait that the mainteiner takes a look before go on
            extension_regex = re.compile(r'(\{\{(?:[Tt]emplate|)[Bb]io[^{]*?)\|[\s\n\t]*?[Ee]xt[Ii]mmagine *?= *?(.*?)(\s*?[|\n}])([^{]*?\})', re.DOTALL)
            find_ext_regex = re.compile(r'\{\{(?:[Tt]emplate|)[Bb]io[^{]*?\|[\s\n\t]*?[Ee]xt[Ii]mmagine *?= *?(.*?)\s*?[|\n}][^{]*?\}', re.DOTALL)
            extension_inside_list = find_ext_regex.findall(text)
            if extension_inside_list != []:
                if not re.findall(r'\w', extension_inside_list[0]):
                    newtext = extension_regex.sub(r'\1|ExtImmagine = jpg\2\3\4', text)
                else:
                    wikipedia.output(u'%s has the image %s with the extension: %s skip.' % (page.title(), image_inside_list[0][0], extension_inside_list[0]))
                    continue
            else:
                if image_inside_list[0][1] == '\n':
                    newtext = regex_sub_image.sub(r'\1|Immagine = \2\3|ExtImmagine = jpg\n\4', text)
                elif image_inside_list[0][1] == '|' or image_inside_list[0][1] == '}':
                    newtext = regex_sub_image.sub(r'\1|Immagine = \2\n|ExtImmagine = jpg\n\3\4', text)
                else:
                    newtext = regex_sub_image.sub(r'\1|Immagine = \2\4\n|ExtImmagine = jpg\n\3', text)
            if newtext != text:
                wikipedia.showDiff(text, newtext)
                try:
                    page.put(newtext, 'Bot: Aggiungo estensione al template bio')
                except Exception, e:
                    print "WARNING: %s" % e
                    wikipedia.input(u'ping') # Wait that the mainteiner takes a look before go on
                    continue
 
if __name__ == "__main__":
    try:
        main()
    finally:
        wikipedia.stopme()
Personal tools
Share