Jump to content

User:John Bot II/Source

From Wikipedia, the free encyclopedia
# -*- coding: utf-8 -*-
# MoveIMG.py
# Moves images from wikipedia to the commons that have been approved
# by users.
# 
# By CWii, 2008
# 
# Uploading portion based off imagecopy.py

import os, sys, re, codecs
import urllib, httplib, urllib2
import catlib, thread, webbrowser
import time, threading
import wikipedia, config, socket
import pagegenerators, add_text
from upload import *
from image import *

moveToCommonsTemplate = {
    'en': [u'Commons ok', u'Copy to Wikimedia Commons', u'Move to commons', u'Movetocommons', u'To commons', u'Copy to Wikimedia Commons by BotMultichill'],
}

def pageTextPost(url,parameters):
    gotInfo = False;    
    while(not gotInfo):
        try:
            commonsHelperPage = urllib.urlopen("http://toolserver.org/~magnus/commonshelper.php", parameters)
            data = commonsHelperPage.read().decode('utf-8')
            gotInfo = True;
        except IOError:
            wikipedia.output(u'Got an IOError, let\'s try again')
        except socket.timeout:
            wikipedia.output(u'Got a timeout, let\'s try again')
    return data
    
class imageTransfer (threading.Thread):

    def __init__ ( self, imagePage, newname, comcat1, comcat2, comcat3, comcat4, comcat5, comcat6, hascat, user ):
        self.imagePage = imagePage
        self.newname = newname
        self.comcat1 = comcat1
        self.comcat2 = comcat2
        self.comcat3 = comcat3
        self.comcat4 = comcat4
        self.comcat5 = comcat5
        self.comcat6 = comcat6
        self.hascat = hascat
        self.user = user
        threading.Thread.__init__ ( self )

    def run(self):
        tosend={'language':self.imagePage.site().language().encode('utf-8'),
                'image':self.imagePage.titleWithoutNamespace().encode('utf-8'),
                'newname':self.newname.encode('utf-8'),
                'project':self.imagePage.site().family.name.encode('utf-8'),
                'username':'',
                'commonsense':'1',
                'remove_categories':'1',
                'ignorewarnings':'1',
                'doit':'Uitvoeren'
                }
      
        tosend=urllib.urlencode(tosend)
        print tosend
        CH=pageTextPost('http://www.toolserver.org/~magnus/commonshelper.php', tosend)
        print 'Got CH desc.'
        
        tablock=CH.split('<textarea ')[1].split('>')[0]
        CH=CH.split('<textarea '+tablock+'>')[1].split('</textarea>')[0]
        CH=CH.replace(u'&times;', u'×')
        wikipedia.output(CH);
        
        if self.hascat:
            if not self.comcat1 == '':
                CH=CH+'\n[[Category:%s]]'%self.comcat1
            if not self.comcat2 == '':
                CH=CH+'\n[[Category:%s]]'%self.comcat2
            if not self.comcat3 == '':
                CH=CH+'\n[[Category:%s]]'%self.comcat3
            if not self.comcat4 == '':
                CH=CH+'\n[[Category:%s]]'%self.comcat4
            if not self.comcat5 == '':
                CH=CH+'\n[[Category:%s]]'%self.comcat5
            if not self.comcat6 == '':
                CH=CH+'\n[[Category:%s]]'%self.comcat6
        
        CH=u'{{JB2-Moved|'+ self.imagePage.site().language() + '.' + self.imagePage.site().family.name +'|time=~~~~~|user=%s}}' % self.user + CH
        CH=CH.decode('utf-8')
        if '[[category:' in CH.lower():
        # I want every picture to be tagged with the bottemplate so i can check my contributions later.
            re.sub(r'\{\{subst:Unc\}\} <!-- Remove this line once you have added categories -->', '', CH)
        #urlEncoding='utf-8'
        bot = UploadRobot(url=self.imagePage.fileUrl(), description=CH, useFilename=self.newname, keepFilename=True, verifyDescription=False, ignoreWarning = True, targetSite = wikipedia.getSite('commons', 'commons'))
        bot.run()

        #Should check if the image actually was uploaded
        if wikipedia.Page(wikipedia.getSite('commons', 'commons'), u'Image:' + self.newname).exists():
            #Get a fresh copy, force to get the page so we dont run into edit conflicts
            imtxt=self.imagePage.get(force=True)

            #Remove the move to commons templates
            ##if moveToCommonsTemplate.has_key(self.imagePage.site().language()):
            for moveTemplate in moveToCommonsTemplate[self.imagePage.site().language()]:
                imtxt = re.sub(u'(?i)\{\{' + moveTemplate + u'\}\}', u'', imtxt)

            #add {{NowCommons}}
            #if nowCommonsTemplate.has_key(self.imagePage.site().language()):
            addTemplate = u'{{subst:ncd|%s}}' % self.newname
            #else:
            #    addTemplate = nowCommonsTemplate['_default'] % self.newname

            #if nowCommonsMessage.has_key(self.imagePage.site().language()):
            commentText = u'File is now available on Wikimedia Commons.'
            #else:
            #    commentText = nowCommonsMessage['_default']
            
            re.sub('\|canMove=yes\|', '|canMove=botMoved|', imtxt)

            wikipedia.showDiff(self.imagePage.get(), imtxt+addTemplate)
            self.imagePage.put(imtxt + addTemplate, comment = commentText)

            self.gen = pagegenerators.FileLinksGenerator(self.imagePage)
            self.preloadingGen = pagegenerators.PreloadingGenerator(self.gen)

            #If the image is uploaded under a different name, replace all instances
            if self.imagePage.titleWithoutNamespace() != self.newname:
                #if imageMoveMessage.has_key(self.imagePage.site().language()):
                moveSummary = u'[[:Image:%s|Image]] moved to [[:commons:Image:%s|commons]].'  % (self.imagePage.titleWithoutNamespace(), self.newname)
                #else:
                #    moveSummary = imageMoveMessage['_default'] % (self.imagePage.titleWithoutNamespace(), self.newname)
                imagebot = ImageRobot(generator = self.preloadingGen, oldImage = self.imagePage.titleWithoutNamespace(), newImage = self.newname, summary = moveSummary, always = True, loose = True)
                imagebot.run()
        return


def log(event, site):
    logPage = wikipedia.Page(site,'User:John_Bot_II/Report')
    pageText = logPage.get()
    newText = '\n*' + event + '-- ~~~~'
    logPage.put(pageText+newText,'[[WP:BOT|BOT]]: Error Report')
    return

def main():
    site = wikipedia.getSite()
    
    cat = catlib.Category(site,'Images_ready_to_be_moved_by_John_Bot_II')
    #gen = pagegenerators.CategorizedPageGenerator('Category:Images ready to be moved by John Bot II')
    gen = pagegenerators.CategorizedPageGenerator(cat)
    
    pgen = pagegenerators.PreloadingGenerator(gen)
    
    wikipedia.handleArgs()
    
    checkPage = wikipedia.Page(site,'User:John Bot II/CheckPage')
    checkPageText = checkPage.get()
    aUsers = checkPageText.lower().split('\n')
    
    for page in pgen:
        #page = wikipedia.Page(site,'Image:CWii2.jpg')
        if page.exists() and (page.namespace() == 6) and (not page.isRedirectPage()) :
            imagepage = wikipedia.ImagePage(page.site(), page.title())
            #Let's process.
            ptext = imagepage.get()
            if not re.search('\{\{Jb2move',ptext,re.I):
                #page not found
                wikipedia.output('Template not found')
                continue
            #test = re.findall('\|canMove="(.*?)"\|', ptext, re.I)
            canmoveRe = re.compile(r"\|canMove=(.*?)\|")
            cmMatch = canmoveRe.search(ptext)
            if cmMatch:
                canMove = cmMatch.group()
                canMove = canMove.strip('|').split('=')
                canMove = canMove[1]
            else:
                canMove = ""
                wikipedia.output("ERROR: Couldn't get canMove status, skiping.")
                log('Could not get canMove status on [[:%s]]' % imagepage.title(), site)
                #TODO: Log and error mark here
                continue
            
            if not canMove == 'yes':
                wikipedia.output("Image isn't movable")
                log('WTF? Image isnt movable on [[:%s]]' % imagepage.title(), site)
                continue
            
            userRe = re.compile(r"\|user=(.*?)\|")
            uMatch = userRe.search(ptext)
            if uMatch:
                user = uMatch.group()
                user = user.strip('|').split('=')
                user = user[1]
            else:
                user = ""
                wikipedia.output("ERROR: User is not given")
                log('User not given. [[:%s]]' % imagepage.title(), site)
                continue
            
            if not user.lower() in aUsers:
                wikipedia.output("ERROR: %s isn't on CheckPage!" % user)
                log('User %s isnt given on [[:%s]]' % (user,imagepage.title()) , site)
                continue
            
            newnameRe = re.compile(r"\|newName=.*?\}\}")
            newnameMatch = newnameRe.search(ptext)
            if newnameMatch:
                newname = newnameMatch.group()
                newname = newname.strip('|}').split('=')
                newname = newname[1]
            else:
                newname = ''
            if not newname == '':    
                commonsImage = wikipedia.Page(wikipedia.getSite('commons','commons'),'Image:'+newname)
            else:
                commonsImage = wikipedia.Page(wikipedia.getSite('commons','commons'),imagepage.title())
            if commonsImage.exists():
                #Yarr! They be taking our names!
                wikipedia.output('ERROR: The image already exists on the commons')
                continue
            
            hascatRe = re.compile(r"\|comCat\d=.*?\|")
            hascatMatch = hascatRe.search(ptext)
            if hascatMatch:
                hascat = True
            else:
                hascat = False
                wikipedia.output("NOTICE: No cats defined!")

            if hascat:
                comcat1Match = re.search(r"\|comCat1=.*?\|", ptext, re.I)
                comcat2Match = re.search(r"\|comCat2=.*?\|", ptext, re.I)
                comcat3Match = re.search(r"\|comCat3=.*?\|", ptext, re.I)
                comcat4Match = re.search(r"\|comCat4=.*?\|", ptext, re.I)
                comcat5Match = re.search(r"\|comCat5=.*?\|", ptext, re.I)
                comcat6Match = re.search(r"\|comCat6=.*?\|", ptext, re.I)
                
                if comcat1Match:
                    comcat1 = comcat1Match.group()
                    comcat1 = comcat1.strip('|').split('=')
                    comcat1 = comcat1[1]
                else:
                    comcat1 = ''
                
                if comcat2Match:
                    comcat2 = comcat2Match.group()
                    comcat2 = comcat2.strip('|').split('=')
                    comcat2 = comcat2[1]
                else:
                    comcat2 = ''
                
                if comcat3Match:
                    comcat3 = comcat3Match.group()
                    comcat3 = comcat3.strip('|').split('=')
                    comcat3 = comcat3[1]
                else:
                    comcat3 = ''
                
                if comcat4Match:
                    comcat4 = comcat4Match.group()
                    comcat4 = comcat4.strip('|').split('=')
                    comcat4 = comcat4[1]
                else:
                    comcat4 = ''
                
                if comcat5Match:
                    comcat5 = comcat5Match.group()
                    comcat5 = comcat5.strip('|').split('=')
                    comcat5 = comcat5[1]
                else:
                    comcat5 = ''
                    
                if comcat6Match:
                    comcat6 = comcat6Match.group()
                    comcat6 = comcat6.strip('|').split('=')
                    comcat6 = comcat6[1]
                else:
                    comcat6 = ''
            else:
                comcat1 = ''
                comcat2 = ''
                comcat3 = ''
                comcat4 = ''
                comcat5 = ''
                comcat6 = ''
                
            if newname == '': newname = imagepage.titleWithoutNamespace()
            
            imageTransfer(imagepage, newname, comcat1, comcat2, comcat3, comcat4, comcat5, comcat6, hascat, user).start()
                    

if __name__ == "__main__":
    try:
        main()
    finally:
        wikipedia.stopme()