Jump to content

User:RonBot/3/Source1

From Wikipedia, the free encyclopedia
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

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

#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/3/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 main():
    go = startAllowed() #Check if task is enabled
    #Set up loop to find pages oversize
    #parameters for API request
    tagged=0
    params = {'action':'query',
             'list':'search',
             'srsearch':'incategory: "All non-free media" fileres:>325 -incategory:"Non-free images tagged for no reduction"',
             'srnamespace':6,
             'srlimit':5000
             }
    print "main.params"
    request = api.APIRequest(site, params) #Set the API request
    print "main.request"
    result = request.query(False)
    print result
    totalhits=result['query']['searchinfo']['totalhits']
    print totalhits
    if totalhits>0:
        for loopvar in range(0, totalhits):
            #print result
            print ""
            if go == "no":
                break
            pagetitle = result['query']['search'][loopvar]['title']
            pagetitletext = pagetitle.encode('utf-8')
            fileext = pagetitletext[-3:]
            fileext2 = pagetitletext[-4:]
            print "fileext", fileext,"fileext2", fileext2, "name", pagetitletext
            pageid = result['query']['search'][loopvar]['pageid']
            pageids = str(pageid)
            print pageids
            print ""
            params = {'action':'query',
                  'prop':'imageinfo',
                  'titles':pagetitle,
                  'iiprop':'size'
                  }
            print "checksize.params"
            req = api.APIRequest(site, params)
            print "checksize.req"
            res = req.query(False)
            print "checksize.res"
            print res
            print "width = ",res['query']['pages'][pageids]['imageinfo'][0]['width']
            print "height = ",res['query']['pages'][pageids]['imageinfo'][0]['height']
            pixel = res['query']['pages'][pageids]['imageinfo'][0]['width'] * res['query']['pages'][pageids]['imageinfo'][0]['height']
            print "pixel=",pixel
            pagepage = page.Page(site, pagetitle)
            print "main.pagepage"
            pagetext = pagepage.getWikiText() 
            #pnt(pagetext)
            #Stop if there's nobots
            stop = allow_bots(pagetext, "RonBot")
            if not stop:
                continue
            print "allow bot to edit image page"
            print "Files tagged so far", tagged
            #if tagged>=30:
            #    sys.exit('done for now') #Code to limit number of items
            if pixel<=105000:
                print "image is <105000 pixels"
                print"============================="
                continue
            if fileext =="ogg":
                print "Video"
                print"============================="
                continue
            if fileext =="ogv":
                print "Video"
                print"============================="
                continue
            if fileext2 =="webm":
                print "Video"
                print"============================="
                continue
            if re.search("{{[Nn]fr}}", pagetext):
                print "Alternative Non-free reduce present"
                print"============================="
                continue
            if re.search("[Nn]on-free reduce", pagetext):
                print "Non-free reduce present"
                print"============================="
                continue
            if re.search("[Nn]on-free manual reduce", pagetext):
                print "Non-free manual reduce present"
                print"============================="
                continue    
            if re.search("[Nn]on-free manual svg reduce", pagetext):
                print "Non-free manual svg reduce present"
                print"============================="
                continue
            if re.search("[Nn]on-free no reduce", pagetext):
                print "Non-free no reduce present"
                print"============================="
                continue
            if re.search("[Oo][Tr][Rr][Ss]", pagetext):
                print "OTRS"
                print"============================="
                continue
            if re.search("[Nn]on-free [Mm]icrosoft screenshot", pagetext):
                print "Fair Use MS shreenshot"
                print"============================="
                continue
            pagetext="{{non-free reduce}}\n"+ pagetext
            pnt(pagetitletext)
            pnt(pagetext)
            pagepage.edit(text=pagetext, bot=True, summary="(Task 3) Tagging image - non free reduce - for image reduction as per non-free guideline [[Wikipedia:Image resolution]]") #(DO NOT UNCOMMENT UNTIL BOT IS APPROVED)
            print "Uploader"
            params = {'action':'query',
                  'prop':'imageinfo',
                  'titles':pagetitle,
                  'iiprop':'user'
                  }
            print "uploader.params"
            req = api.APIRequest(site, params)
            print "uploader.req"
            res = req.query(False)
            print "uploader.res",res
            username = res['query']['pages'][pageids]['imageinfo'][0]['user']
            pnt(username)
            username1="User talk:"
            username=username1.encode('utf-8')+username
            pagepage = page.Page(site, username)
            print "main.pagepage2"
            #test for exiting page
            try:
                pagetext = pagepage.getWikiText()
                newpage=False
            except:
                pagetext="{{subst:Welcomemenu}} ~~~~"
                newpage=True
            #page either exists or has a welcome template
            #pnt(pagetext)
            pnt(pagetitle)
            go = allow_bots(pagetext, 'RonBot')# does user page allow bots
            if go:
                print"bot allowed on talk page"
                pagetext=pagetext+"\n==[[:"
                pagetext=pagetext+pagetitle.encode('utf-8')
                pagetext=pagetext+"]]==\n"
                pagetext=pagetext+"{{subst:Non-free image to be reduced}} ~~~~"
                #pnt(pagetext)
                try:
                    if newpage==True:
                        print "Newpage"
                        pagepage.edit(text=pagetext, createonly=True, bot=True, summary="(Task 3) Adding {{Non free image to be reduced}} ([[User:RonBot/3/Run|disable]])") #(DO NOT UNCOMMENT UNTIL BOT IS APPROVED)
                    else:
                        print "Existing Page"
                        pagepage.edit(text=pagetext, bot=True, summary="(Task 3) Adding {{Non free image to be reduced}} ([[User:RonBot/3/Run|disable]])") #(DO NOT UNCOMMENT UNTIL BOT IS APPROVED)
                    print "Added {{Non free image to be reduced}} to",username
                except:
                    print
                    print "Failed to add"
            else:
                print "no bots on user page"
                print"##################################################################"
            tagged += 1
    else:
        print "no images to do"
        
if __name__ == "__main__":
    with warnings.catch_warnings():
        warnings.simplefilter("ignore", FutureWarning)
        main()