def main(): old_image = None new_image = None options = {} for arg in pywikibot.handleArgs(): if arg == '-always': options['always'] = True elif arg == '-loose': options['loose'] = True elif arg.startswith('-summary'): if len(arg) == len('-summary'): options['summary'] = pywikibot.input(u'Choose an edit summary: ') else: options['summary'] = arg[len('-summary:'):] elif old_image: new_image = arg else: old_image = arg if old_image: site = pywikibot.Site() old_imagepage = pywikibot.FilePage(site, old_image) gen = pagegenerators.FileLinksGenerator(old_imagepage) preloadingGen = pagegenerators.PreloadingGenerator(gen) bot = ImageRobot(preloadingGen, old_image, new_image, **options) bot.run() else: pywikibot.showHelp()
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.Site() 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.title(withNamespace=False) != filename: gen = pagegenerators.FileLinksGenerator(imagepage) preloadingGen = pagegenerators.PreloadingGenerator(gen) moveSummary = i18n.twtranslate( imagepage.site(), 'commons-file-moved', {'localfile': imagepage.title(withNamespace=False), 'commonsfile': filename}) imagebot = image.ImageRobot(generator=preloadingGen, oldImage=imagepage.title(withNamespace=False), newImage=filename, summary=moveSummary, always=True, loose=True) imagebot.run()
def main(*args): """ Process command line arguments and invoke bot. If args is an empty list, sys.argv is used. @param args: command line arguments @type args: unicode """ old_image = None new_image = None options = {} for arg in pywikibot.handle_args(args): if arg == '-always': options['always'] = True elif arg == '-loose': options['loose'] = True elif arg.startswith('-summary'): if len(arg) == len('-summary'): options['summary'] = pywikibot.input( 'Choose an edit summary: ') else: options['summary'] = arg[len('-summary:'):] elif old_image: new_image = arg else: old_image = arg if old_image: site = pywikibot.Site() old_imagepage = pywikibot.FilePage(site, old_image) gen = pagegenerators.FileLinksGenerator(old_imagepage) preloading_gen = pagegenerators.PreloadingGenerator(gen) bot = ImageRobot(preloading_gen, old_image, new_image, site=site, **options) bot.run() return True else: pywikibot.bot.suggest_help(missing_parameters=['old image']) return False
def run(self): """Run the bot.""" tosend = { 'language': self.imagePage.site.lang.encode('utf-8'), 'image': self.imagePage.title(with_ns=False).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' } pywikibot.output(tosend) CH = pageTextPost('https://commonshelper.toolforge.org/index.php', tosend) pywikibot.output('Got CH desc.') tablock = CH.split('<textarea ')[1].split('>')[0] CH = CH.split('<textarea ' + tablock + '>')[1].split('</textarea>')[0] CH = CH.replace('×', '×') 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 = ('\n\n{{BotMoveToCommons|%s.%s|year={{subst:CURRENTYEAR}}' '|month={{subst:CURRENTMONTHNAME}}|day={{subst:CURRENTDAY}}}}' % (self.imagePage.site.lang, self.imagePage.site.family.name) + CH) if self.category: CH = CH.replace( '{{subst:Unc}} <!-- Remove this line once you have ' 'added categories -->', '') CH += '[[Category:' + self.category + ']]' bot = UploadRobot(url=self.imagePage.get_file_url(), description=CH, use_filename=self.newname, keep_filename=True, verify_description=False, ignore_warning=True, target_site=self.image_repo) bot.run() # Should check if the image actually was uploaded if pywikibot.Page(self.image_repo, 'Image:' + self.newname).exists(): # Get a fresh copy, force to get the page so we don't run into edit # conflicts imtxt = self.imagePage.get(force=True) # Remove the move to commons templates if self.imagePage.site.lang in moveToCommonsTemplate: for moveTemplate in moveToCommonsTemplate[ self.imagePage.site.lang]: imtxt = re.sub(r'(?i)\{\{' + moveTemplate + r'[^\}]*\}\}', '', imtxt) # add {{NowCommons}} if self.imagePage.site.lang in nowCommonsTemplate: addTemplate = nowCommonsTemplate[ self.imagePage.site.lang] % self.newname else: addTemplate = nowCommonsTemplate['_default'] % self.newname commentText = i18n.twtranslate( self.imagePage.site, 'commons-file-now-available', { 'localfile': self.imagePage.title(with_ns=False), '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) moveSummary = i18n.twtranslate( self.imagePage.site, 'commons-file-moved', { 'localfile': self.imagePage.title(with_ns=False), 'commonsfile': self.newname }) # If the image is uploaded under a different name, replace all # instances if self.imagePage.title(with_ns=False) != self.newname: imagebot = ImageRobot( generator=self.preloadingGen, oldImage=self.imagePage.title(with_ns=False), newImage=self.newname, summary=moveSummary, always=True, loose=True) imagebot.run() # If the user want to delete the page and # the user has sysops privilege, delete the page, otherwise # it will be marked for deletion. if self.delete_after_done: self.imagePage.delete(moveSummary, False) return
def run(self): """Run the bot.""" commons = self.commons comment = self.summary for page in self.generator: self.current_page = page try: localImagePage = pywikibot.FilePage(self.site, page.title()) if localImagePage.fileIsShared(): pywikibot.output(u'File is already on Commons.') continue sha1 = localImagePage.latest_file_info.sha1 filenameOnCommons = self.findFilenameOnCommons(localImagePage) if not filenameOnCommons: pywikibot.output(u'NowCommons template not found.') continue commonsImagePage = pywikibot.FilePage( commons, 'Image:%s' % filenameOnCommons) if (localImagePage.title(withNamespace=False) != commonsImagePage.title(withNamespace=False)): usingPages = list(localImagePage.usingPages()) if usingPages and usingPages != [localImagePage]: pywikibot.output( color_format( '"{lightred}{0}{default}" is still used in {1} pages.', localImagePage.title(withNamespace=False), len(usingPages))) if self.getOption('replace') is True: pywikibot.output( color_format( 'Replacing "{lightred}{0}{default}" by ' '"{lightgreen}{1}{default}\".', localImagePage.title(withNamespace=False), commonsImagePage.title( withNamespace=False))) bot = ImageBot( pg.FileLinksGenerator(localImagePage), localImagePage.title(withNamespace=False), commonsImagePage.title(withNamespace=False), '', self.getOption('replacealways'), self.getOption('replaceloose')) bot.run() # If the image is used with the urlname the # previous function won't work is_used = bool( list( pywikibot.FilePage( self.site, page.title()).usingPages(total=1))) if is_used and self.getOption('replaceloose'): bot = ImageBot( pg.FileLinksGenerator(localImagePage), localImagePage.title(withNamespace=False, asUrl=True), commonsImagePage.title( withNamespace=False), '', self.getOption('replacealways'), self.getOption('replaceloose')) bot.run() # refresh because we want the updated list usingPages = len( list( pywikibot.FilePage( self.site, page.title()).usingPages())) else: pywikibot.output(u'Please change them manually.') continue else: pywikibot.output( color_format( 'No page is using "{lightgreen}{0}{default}" ' 'anymore.', localImagePage.title(withNamespace=False))) commonsText = commonsImagePage.get() if self.getOption('replaceonly') is False: if sha1 == commonsImagePage.latest_file_info.sha1: pywikibot.output( u'The image is identical to the one on Commons.') if len(localImagePage.getFileVersionHistory()) > 1: pywikibot.output( 'This image has a version history. Please ' 'delete it manually after making sure that the ' 'old versions are not worth keeping.') continue if self.getOption('always') is False: format_str = color_format( '\n\n>>>> Description on {lightpurple}%s' '{default} <<<<\n') pywikibot.output(format_str % page.title()) pywikibot.output(localImagePage.get()) pywikibot.output(format_str % commonsImagePage.title()) pywikibot.output(commonsText) if pywikibot.input_yn( u'Does the description on Commons contain ' 'all required source and license\n' 'information?', default=False, automatic_quit=False): localImagePage.delete( '%s [[:commons:Image:%s]]' % (comment, 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) as e: pywikibot.output(u'%s' % e[0]) continue else: self._treat_counter += 1 if not self._treat_counter: pywikibot.output('No transcluded files found for %s.' % self.ncTemplates()[0]) self.exit()
def run(self): commons = pywikibot.Site('commons', 'commons') comment = i18n.translate(self.site, nowCommonsMessage, fallback=True) for page in self.getPageGenerator(): if self.getOption('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 self.current_page = page try: localImagePage = pywikibot.FilePage(self.site, page.title()) if localImagePage.fileIsShared(): pywikibot.output(u'File is already on Commons.') continue sha1 = localImagePage.latest_file_info.sha1 if self.getOption('use_hash'): filenameOnCommons = images_list[1] else: filenameOnCommons = self.findFilenameOnCommons( localImagePage) if not filenameOnCommons and not self.getOption('use_hash'): pywikibot.output(u'NowCommons template not found.') continue commonsImagePage = pywikibot.FilePage( commons, 'Image:%s' % filenameOnCommons) if localImagePage.title(withNamespace=False) == \ commonsImagePage.title(withNamespace=False) and self.getOption('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 self.getOption('replace') is 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), '', self.getOption('replacealways'), self.getOption('replaceloose')) oImageRobot.run() # If the image is used with the urlname the # previous function won't work if len(list(pywikibot.FilePage(self.site, page.title()).usingPages())) > 0 and \ self.getOption('replaceloose'): oImageRobot = image.ImageRobot( pg.FileLinksGenerator(localImagePage), localImagePage.title(withNamespace=False, asUrl=True), commonsImagePage.title( withNamespace=False), '', self.getOption('replacealways'), self.getOption('replaceloose')) oImageRobot.run() # refresh because we want the updated list usingPages = len( list( pywikibot.FilePage( self.site, page.title()).usingPages())) if usingPages > 0 and self.getOption('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 self.getOption('replaceonly') is False: if sha1 == commonsImagePage.latest_file_info.sha1: pywikibot.output( u'The image is identical to the one on Commons.') if len(localImagePage.getFileVersionHistory() ) > 1 and not self.getOption('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 self.getOption('always') is 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) if pywikibot.input_yn( u'Does the description on Commons contain ' 'all required source and license\n' 'information?', default=False, automatic_quit=False): localImagePage.delete( '%s [[:commons:Image:%s]]' % (comment, 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) as e: pywikibot.output(u'%s' % e[0]) continue
def run(self): tosend = { 'language': self.imagePage.site.language().encode('utf-8'), 'image': self.imagePage.title(withNamespace=False).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 = urlencode(tosend) pywikibot.output(tosend) CH = pageTextPost('http://tools.wmflabs.org/commonshelper/index.php', tosend) pywikibot.output('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 += u'[[Category:' + self.category + u']]' bot = upload.UploadRobot(url=self.imagePage.fileUrl(), description=CH, useFilename=self.newname, keepFilename=True, verifyDescription=False, ignoreWarning=True, targetSite=pywikibot.Site( 'commons', 'commons')) bot.run() # Should check if the image actually was uploaded if pywikibot.Page(pywikibot.Site('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.title(withNamespace=False), '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.title(withNamespace=False) != self.newname: moveSummary = i18n.twtranslate( self.imagePage.site, 'commons-file-moved', { 'localfile': self.imagePage.title(withNamespace=False), 'commonsfile': self.newname }) imagebot = image.ImageRobot( generator=self.preloadingGen, oldImage=self.imagePage.title(withNamespace=False), newImage=self.newname, summary=moveSummary, always=True, loose=True) imagebot.run() return
def run(self): """Run the bot.""" commons = self.commons comment = self.summary for page in self.generator: self.current_page = page try: local_file_page = pywikibot.FilePage(self.site, page.title()) if local_file_page.file_is_shared(): pywikibot.output('File is already on Commons.') continue sha1 = local_file_page.latest_file_info.sha1 file_on_commons = self.find_file_on_commons(local_file_page) if not file_on_commons: pywikibot.output('NowCommons template not found.') continue commons_file_page = pywikibot.FilePage( commons, 'File:' + file_on_commons) if (local_file_page.title(with_ns=False) != commons_file_page.title(with_ns=False)): using_pages = list(local_file_page.using_pages()) if using_pages and using_pages != [local_file_page]: pywikibot.output( color_format( '"{lightred}{0}{default}" ' 'is still used in {1} pages.', local_file_page.title(with_ns=False), len(using_pages))) if self.opt.replace: pywikibot.output( color_format( 'Replacing "{lightred}{0}{default}" by ' '"{lightgreen}{1}{default}\".', local_file_page.title(with_ns=False), commons_file_page.title(with_ns=False))) bot = ImageBot( pg.FileLinksGenerator(local_file_page), local_file_page.title(with_ns=False), commons_file_page.title(with_ns=False), always=self.opt.replacealways, loose=self.opt.replaceloose) bot.run() # If the image is used with the urlname the # previous function won't work is_used = bool( list( pywikibot.FilePage( self.site, page.title()).using_pages(total=1))) if is_used and self.opt.replaceloose: bot = ImageBot( pg.FileLinksGenerator(local_file_page), local_file_page.title(with_ns=False, as_url=True), commons_file_page.title(with_ns=False), always=self.opt.replacealways, loose=self.opt.replaceloose) bot.run() # refresh because we want the updated list using_pages = len( list( pywikibot.FilePage( self.site, page.title()).using_pages())) else: pywikibot.output('Please change them manually.') continue pywikibot.output( color_format( 'No page is using "{lightgreen}{0}{default}" ' 'anymore.', local_file_page.title(with_ns=False))) commons_text = commons_file_page.get() if not self.opt.replaceonly: if sha1 == commons_file_page.latest_file_info.sha1: pywikibot.output( 'The file is identical to the one on Commons.') if len(local_file_page.get_file_history()) > 1: pywikibot.output( 'This file has a version history. Please ' 'delete it manually after making sure that ' 'the old versions are not worth keeping.') continue if self.opt.always is False: format_str = color_format( '\n\n>>>> Description on {lightpurple}%s' '{default} <<<<\n') pywikibot.output(format_str % page.title()) pywikibot.output(local_file_page.get()) pywikibot.output(format_str % commons_file_page.title()) pywikibot.output(commons_text) if pywikibot.input_yn( 'Does the description on Commons contain ' 'all required source and license\n' 'information?', default=False, automatic_quit=False): local_file_page.delete( '{} [[:commons:File:{}]]'.format( comment, file_on_commons), prompt=False) else: local_file_page.delete( comment + ' [[:commons:File:{}]]'.format( file_on_commons), prompt=False) else: pywikibot.output('The file is not identical to ' 'the one on Commons.') except (NoPageError, IsRedirectPageError) as e: pywikibot.output(str(e[0])) continue else: self._treat_counter += 1 if not self._treat_counter: pywikibot.output('No transcluded files found for {}.'.format( self.nc_templates_list()[0])) self.exit()
def replaceUsage(self, imagepage, filename): """Replace all usage if image is uploaded under a different name.""" if imagepage.title(with_ns=False) != filename: gen = pagegenerators.FileLinksGenerator(imagepage)