def main(): oldImage = None newImage = None summary = '' always = False loose = False # read command line parameters for arg in pywikibot.handleArgs(): if arg == '-always': always = True elif arg == '-loose': loose = True elif arg.startswith('-summary'): if len(arg) == len('-summary'): summary = pywikibot.input(u'Choose an edit summary: ') else: summary = arg[len('-summary:'):] else: if oldImage: newImage = arg else: oldImage = arg if not oldImage: pywikibot.showHelp('image') else: mysite = pywikibot.getSite() ns = mysite.image_namespace() oldImagePage = pywikibot.ImagePage(mysite, ns + ':' + oldImage) gen = pagegenerators.FileLinksGenerator(oldImagePage) preloadingGen = pagegenerators.PreloadingGenerator(gen) bot = ImageRobot(preloadingGen, oldImage, newImage, summary, always, loose) bot.run()
def replaceUsage(self, imagepage, filename): ''' If the image is uploaded under a different name, replace all usage. ''' if imagepage.titleWithoutNamespace() != filename: gen = pagegenerators.FileLinksGenerator(imagepage) preloadingGen = pagegenerators.PreloadingGenerator(gen) moveSummary = i18n.twtranslate(imagepage.site(), 'commons-file-moved', {'localfile' : imagepage.titleWithoutNamespace(), 'commonsfile' : filename}) imagebot = ImageRobot(generator = preloadingGen, oldImage = imagepage.titleWithoutNamespace(), newImage = filename, summary = moveSummary, always = True, loose = True) imagebot.run()
def replaceUsage(self, imagepage, filename): ''' If the image is uploaded under a different name, replace all usage. ''' if imagepage.titleWithoutNamespace() != filename: gen = pagegenerators.FileLinksGenerator(imagepage) preloadingGen = pagegenerators.PreloadingGenerator(gen) if imagepage.site().language() in imageMoveMessage: moveSummary = imageMoveMessage[imagepage.site().language()] % (imagepage.titleWithoutNamespace(), filename) else: moveSummary = imageMoveMessage['_default'] % (imagepage.titleWithoutNamespace(), filename) imagebot = ImageRobot(generator = preloadingGen, oldImage = imagepage.titleWithoutNamespace(), newImage = filename, summary = moveSummary, always = True, loose = True) imagebot.run()
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'×', u'×') CH = self.fixAuthor(CH) pywikibot.output(CH) # I want every picture to be tagged with the bottemplate so i can check my contributions later. CH = u'\n\n{{BotMoveToCommons|' + self.imagePage.site().language( ) + '.' + self.imagePage.site( ).family.name + '|year={{subst:CURRENTYEAR}}|month={{subst:CURRENTMONTHNAME}}|day={{subst:CURRENTDAY}}}}' + CH if self.category: CH = CH.replace( u'{{subst:Unc}} <!-- Remove this line once you have added categories -->', u'') CH = CH + u'[[Category:' + self.category + u']]' bot = UploadRobot(url=self.imagePage.fileUrl(), description=CH, useFilename=self.newname, keepFilename=True, verifyDescription=False, ignoreWarning=True, targetSite=pywikibot.getSite('commons', 'commons')) bot.run() #Should check if the image actually was uploaded if pywikibot.Page(pywikibot.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 self.imagePage.site().language() in moveToCommonsTemplate: for moveTemplate in moveToCommonsTemplate[ self.imagePage.site().language()]: imtxt = re.sub(u'(?i)\{\{' + moveTemplate + u'[^\}]*\}\}', u'', imtxt) #add {{NowCommons}} if self.imagePage.site().language() in nowCommonsTemplate: addTemplate = nowCommonsTemplate[ self.imagePage.site().language()] % self.newname else: addTemplate = nowCommonsTemplate['_default'] % self.newname commentText = i18n.twtranslate( self.imagePage.site(), 'commons-file-now-available', { 'localfile': self.imagePage.titleWithoutNamespace(), 'commonsfile': self.newname }) pywikibot.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: moveSummary = i18n.twtranslate( self.imagePage.site(), 'commons-file-moved', { 'localfile': self.imagePage.titleWithoutNamespace(), 'commonsfile': self.newname }) imagebot = ImageRobot( generator=self.preloadingGen, oldImage=self.imagePage.titleWithoutNamespace(), newImage=self.newname, summary=moveSummary, always=True, loose=True) imagebot.run() return
def run(self): commons = pywikibot.getSite('commons', 'commons') comment = pywikibot.translate(self.site, nowCommonsMessage) for page in self.getPageGenerator(): if use_hash: # Page -> Has the namespace | commons image -> Not images_list = page # 0 -> local image, 1 -> commons image page = pywikibot.Page(self.site, images_list[0]) else: # If use_hash is true, we have already print this before, no need # Show the title of the page we're working on. # Highlight the title in purple. pywikibot.output(u"\n\n>>> \03{lightpurple}%s\03{default} <<<" % page.title()) try: localImagePage = pywikibot.ImagePage(self.site, page.title()) if localImagePage.fileIsOnCommons(): pywikibot.output(u'File is already on Commons.') continue md5 = localImagePage.getFileMd5Sum() if use_hash: filenameOnCommons = images_list[1] else: filenameOnCommons = self.findFilenameOnCommons( localImagePage) if not filenameOnCommons and not use_hash: pywikibot.output(u'NowCommons template not found.') continue commonsImagePage = pywikibot.ImagePage(commons, 'Image:%s' % filenameOnCommons) if localImagePage.title(withNamespace=False) == \ commonsImagePage.title(withNamespace=False) and use_hash: pywikibot.output( u'The local and the commons images have the same name') if localImagePage.title(withNamespace=False) != \ commonsImagePage.title(withNamespace=False): usingPages = list(localImagePage.usingPages()) if usingPages and usingPages != [localImagePage]: pywikibot.output( u'\"\03{lightred}%s\03{default}\" is still used in %i pages.' % (localImagePage.title(withNamespace=False), len(usingPages))) if replace == True: pywikibot.output( u'Replacing \"\03{lightred}%s\03{default}\" by \"\03{lightgreen}%s\03{default}\".' % (localImagePage.title(withNamespace=False), commonsImagePage.title(withNamespace=False))) oImageRobot = image.ImageRobot( pg.FileLinksGenerator(localImagePage), localImagePage.title(withNamespace=False), commonsImagePage.title(withNamespace=False), '', replacealways, replaceloose) oImageRobot.run() # If the image is used with the urlname the # previous function won't work if len(list(pywikibot.ImagePage(self.site, page.title()).usingPages())) > 0 and \ replaceloose: oImageRobot = image.ImageRobot( pg.FileLinksGenerator( localImagePage), self.urlname( localImagePage.title( withNamespace=False)), commonsImagePage.title( withNamespace=False), '', replacealways, replaceloose) oImageRobot.run() # refresh because we want the updated list usingPages = len(list(pywikibot.ImagePage( self.site, page.title()).usingPages())) if usingPages > 0 and use_hash: # just an enter pywikibot.input( u'There are still %s pages with this image, confirm the manual removal from them please.' % usingPages) else: pywikibot.output(u'Please change them manually.') continue else: pywikibot.output( u'No page is using \"\03{lightgreen}%s\03{default}\" anymore.' % localImagePage.title(withNamespace=False)) commonsText = commonsImagePage.get() if replaceonly == False: if md5 == commonsImagePage.getFileMd5Sum(): pywikibot.output( u'The image is identical to the one on Commons.') if len(localImagePage.getFileVersionHistory()) > 1 and not use_hash: pywikibot.output( u"This image has a version history. Please delete it manually after making sure that the old versions are not worth keeping.""") continue if autonomous == False: pywikibot.output( u'\n\n>>>> Description on \03{lightpurple}%s\03{default} <<<<\n' % page.title()) pywikibot.output(localImagePage.get()) pywikibot.output( u'\n\n>>>> Description on \03{lightpurple}%s\03{default} <<<<\n' % commonsImagePage.title()) pywikibot.output(commonsText) choice = pywikibot.inputChoice( u'Does the description on Commons contain all required source and license\n' u'information?', ['yes', 'no'], ['y', 'N'], 'N') if choice.lower() in ['y', 'yes']: localImagePage.delete( comment + ' [[:commons:Image:%s]]' % filenameOnCommons, prompt = False) else: localImagePage.delete( comment + ' [[:commons:Image:%s]]' % filenameOnCommons, prompt = False) else: pywikibot.output( u'The image is not identical to the one on Commons.') except (pywikibot.NoPage, pywikibot.IsRedirectPage), e: pywikibot.output(u'%s' % e[0]) continue