Jump to content

User:RonBot/12/Source1

From Wikipedia, the free encyclopedia

CM.py

DoneList=list()

Main program

from wikitools import *
import time
import datetime
import urllib
import json
import userpassbot #Bot password
import warnings
import re
import mwparserfromhell
import datetime
import sys
import CM


site = wiki.Wiki() #Tell Python to use the English Wikipedia's API
site.login(userpassbot.username, userpassbot.password) #login

name = 'Category:Articles with missing files'
reTemplate =  re.compile('\n*\{\{.*[Bb]rokenImage.*}}')

#routine to autoswitch some of the output - as filenames have accented chars!
def pnt(s):
    try:
        print(s)
    except UnicodeEncodeError:
        print(s.encode('utf-8'))

def startAllowed():
    textpage = page.Page(site, "User:RonBot/12/Run").getWikiText()
    if textpage == "Run":
        return "run"
    else:
        return "no"
    
def allow_bots(text, user):
    user = user.lower().strip()
    text = mwparserfromhell.parse(text)
    for tl in text.filter_templates():
        if tl.name.matches(['bots', 'nobots']):
            break
    else:
        return True
    print "template found" #Have we found one
    for param in tl.params:
        bots = [x.lower().strip() for x in param.value.split(",")]
	if param.name == 'allow':
            print "We have an ALLOW" # allow found
            if ''.join(bots) == 'none': return False
            for bot in bots:
                if bot in (user, 'all'):
                    return True
        elif param.name == 'deny':
            print "We have a DENY" # deny found
            if ''.join(bots) == 'none':
                print "none - true"
                return True
	    for bot in bots:
                if bot in (user, 'all'):
                    pnt(bot)
                    pnt(user)
                    print "all - false"
                    return False
    if (tl.name.matches('nobots') and len(tl.params) == 0):
        print "match - false"
        return False
    return True

def clearpages():
    lastContinue=''
    while True:
        params = {'action':'query',
                  'prop':'templates',
                  'list':'embeddedin',
                  'eititle':'Template:BrokenImage',
                  'tllimit':'max',
                  'tlnamespace':'0',
                  'tlcontinue':lastContinue
                  }
        req = api.APIRequest(site, params) #Set the API request
        res = req.query(False) #Send the API request and store the result in res
        print res
        touse = pagelist.listFromQuery(site, res['query']['embeddedin'])#Make a list
        print
        print touse
        for articlep in touse: #For page in the list
            pagename=articlep.unprefixedtitle
            pnt(pagename)
            if pagename not in CM.DoneList:
                print"removal"
                pagepage = page.Page(site, pagename)
                pagetext = pagepage.getWikiText()
                pagetext = reTemplate.sub( '', pagetext)
                pagetext=pagetext.lstrip()
                try:
                    print "writing page"
                   #pagepage.edit(text=pagetext, bot=True, summary="(Task 12) Removing unneeded template ([[User:RonBot/12/Run|disable]])") #(DO NOT UNCOMMENT UNTIL BOT IS APPROVED)
                except:
                    print "Cannot remove tag!"
                pnt ("Done for %s" % articlep.unprefixedtitle) #For debugging
            else:
                print "Page not fixed"
        if 'continue' not in res:
            break
        lastContinue = res['continue']['tlcontinue']
        print "continue"
    return

def findpages(cat):
    lastContinue=''
    while True:
        params = {'action':'query',
                  'list':'categorymembers',
                  'cmtitle':cat,
                  'cmlimit':'max',
                  'cmnamespace':'0',
                  'cmcontinue':lastContinue
                  }
        req = api.APIRequest(site, params) #Set the API request
        res = req.query(False) #Send the API request and store the result in res
        touse = pagelist.listFromQuery(site, res['query']['categorymembers'])#Make a list
        #pnt(touse)
        for articlep in touse: #For page in the list
            pagename=articlep.unprefixedtitle
            pnt(pagename)
            pagepage = page.Page(site, pagename)
            pagetext = pagepage.getWikiText()
            CM.DoneList.append(pagename)
            if not reTemplate.match(pagetext):
                pagetext="{{BrokenImage}}\n"+pagetext
                print "TagPage"
                try:
                    print "writing page"
                   #pagepage.edit(text=pagetext, bot=True, summary="(Task 12) Adding {{BrokenImage}} ([[User:RonBot/12/Run|disable]])") #(DO NOT UNCOMMENT UNTIL BOT IS APPROVED)
                except:
                    print "Cannot add tag!"
           else:
                print "Page Already Tagged"
                print "----"
        if 'continue' not in res:
            break
        lastContinue = res['continue']['cmcontinue']
        print "continue"
    return
                                   
def main():
    go = startAllowed() #Check if task is enabled
    if go=="run":
        CM.DoneList=list()
        findpages(name)
        clearpages()

if __name__ == "__main__":
    with warnings.catch_warnings():
        warnings.simplefilter("ignore", FutureWarning)
        main()