#!/usr/bin/python # -*- coding: utf-8 -*- """ -----// Stub Adder //------------------------------------------------------ File: jmh_addstubs.py Version: 1.0 Author: John Hobbs Contact: john@velvetcache.org This bot will iterate through all pages of the wiki and append a generic stub ('{{Stub}}') to them if they do not have one already and have under a given number of "words" in them. Words, here, are counted as _any_ series of characters seperated by a space. The default maximum number of words that the bot will work on is 5, so it is recommended that you pass it a more realistic value. Call python wordcount.py to have your change be done on all pages of the wiki. If that takes too long to work in one stroke, run: python wordcount.py Pagename to do all pages starting at pagename. There are two command line options: -dryrun This will check and notify you but will not actually change anything. -words=XX This is the word threshold. Replace XX with the biggest wordcount that you want the bot to append stubs to. """ import wikipedia import pagegenerators import sys def workon(page): try: text = page.get() except wikipedia.IsRedirectPage: return jmh_tokens = text.split(' ') if len(jmh_tokens) <= jmh_count and -1 == text.find('Stub}}'): text += '{{Stub}}' if jmh_dryrun: print '--// MATCH: [['+page.title()+']] -> Dry Run, No Change //--' else: print '--// MATCH: [['+page.title()+']] -> Stub Added //--' page.put(text) try: start = [] test = False jmh_dryrun = False jmh_count = 5 for arg in wikipedia.handleArgs(): if arg.startswith("-words="): temp = arg.split('=') jmh_count = int(temp[1]) elif arg.startswith("-dryrun"): jmh_dryrun = True else: start.append(arg) if start: start = " ".join(start) else: start = "!" mysite = wikipedia.getSite() basicgenerator = pagegenerators.AllpagesPageGenerator(start=start) generator = pagegenerators.PreloadingGenerator(basicgenerator) for page in generator: workon(page) finally: wikipedia.stopme()