def generator(self): """Generator method.""" gens = (t.getReferences(follow_redirects=True, namespaces=[6], onlyTemplateInclusion=True) for t in self.nc_templates) gen = chain(*gens) gen = pg.DuplicateFilterPageGenerator(gen) gen = pg.PreloadingGenerator(gen) return gen
def getPageGenerator(self): if self.getOption('use_hash'): gen = self.useHashGenerator() else: gens = [ t.getReferences(follow_redirects=True, namespaces=[6], onlyTemplateInclusion=True) for t in self.nc_templates ] gen = pg.CombinedPageGenerator(gens) gen = pg.DuplicateFilterPageGenerator(gen) gen = pg.PreloadingGenerator(gen) return gen
def getPageGenerator(self): if self.getOption('use_hash'): gen = self.useHashGenerator() else: nowCommonsTemplates = [pywikibot.Page(self.site, title, ns=10) for title in self.ncTemplates()] gens = [pg.ReferringPageGenerator(t, followRedirects=True, onlyTemplateInclusion=True) for t in nowCommonsTemplates] gen = pg.CombinedPageGenerator(gens) gen = pg.NamespaceFilterPageGenerator(gen, [6]) gen = pg.DuplicateFilterPageGenerator(gen) gen = pg.PreloadingGenerator(gen) return gen
def main(*args): templateNames = [] templates = {} subst = False remove = False namespaces = [] editSummary = '' addedCat = '' acceptAll = False genFactory = pagegenerators.GeneratorFactory() # If xmlfilename is None, references will be loaded from the live wiki. xmlfilename = None user = None skip = False timestamp = None # read command line parameters for arg in pywikibot.handleArgs(*args): if arg == '-remove': remove = True elif arg == '-subst': subst = True elif arg == '-assubst': subst = remove = True elif arg == ('-always'): acceptAll = True elif arg.startswith('-xml'): if len(arg) == 4: xmlfilename = pywikibot.input( u'Please enter the XML dump\'s filename: ') else: xmlfilename = arg[5:] elif arg.startswith('-category:'): addedCat = arg[len('-category:'):] elif arg.startswith('-summary:'): editSummary = arg[len('-summary:'):] elif arg.startswith('-user:'******'-user:'******'-skipuser:'******'-skipuser:'******'-timestamp:'): timestamp = arg[len('-timestamp:'):] else: if not genFactory.handleArg(arg): templateNames.append( pywikibot.Page(pywikibot.Site(), arg, ns=10).title(withNamespace=False)) if subst ^ remove: for templateName in templateNames: templates[templateName] = None else: try: for i in range(0, len(templateNames), 2): templates[templateNames[i]] = templateNames[i + 1] except IndexError: pywikibot.output( u'Unless using solely -subst or -remove, you must give an even number of template names.' ) return oldTemplates = [] ns = pywikibot.Site().template_namespace() for templateName in templates.keys(): oldTemplate = pywikibot.Page(pywikibot.Site(), templateName, ns=10) oldTemplates.append(oldTemplate) if xmlfilename: gen = XmlDumpTemplatePageGenerator(oldTemplates, xmlfilename) else: gen = genFactory.getCombinedGenerator() if not gen: gens = [] gens = [ pagegenerators.ReferringPageGenerator(t, onlyTemplateInclusion=True) for t in oldTemplates ] gen = pagegenerators.CombinedPageGenerator(gens) gen = pagegenerators.DuplicateFilterPageGenerator(gen) if user: gen = UserEditFilterGenerator(gen, user, timestamp, skip) preloadingGen = pagegenerators.PreloadingGenerator(gen) bot = TemplateRobot(preloadingGen, templates, subst, remove, editSummary, acceptAll, addedCat) bot.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: list of unicode """ templateNames = [] templates = {} options = {} # If xmlfilename is None, references will be loaded from the live wiki. xmlfilename = None user = None skip = False timestamp = None # read command line parameters local_args = pywikibot.handle_args(args) genFactory = pagegenerators.GeneratorFactory() for arg in local_args: if arg == '-remove': options['remove'] = True elif arg == '-subst': options['subst'] = True elif arg == '-assubst': options['subst'] = options['remove'] = True elif arg == '-always': options['always'] = True elif arg.startswith('-xml'): if len(arg) == 4: xmlfilename = pywikibot.input( u'Please enter the XML dump\'s filename: ') else: xmlfilename = arg[5:] elif arg.startswith('-category:'): options['addedCat'] = arg[len('-category:'):] elif arg.startswith('-summary:'): options['summary'] = arg[len('-summary:'):] elif arg.startswith('-user:'******'-user:'******'-skipuser:'******'-skipuser:'******'-timestamp:'): timestamp = arg[len('-timestamp:'):] else: if not genFactory.handleArg(arg): templateNames.append( pywikibot.Page(pywikibot.Site(), arg, ns=10).title(withNamespace=False)) if not templateNames: pywikibot.showHelp() return if options.get('subst', False) ^ options.get('remove', False): for templateName in templateNames: templates[templateName] = None else: try: for i in range(0, len(templateNames), 2): templates[templateNames[i]] = templateNames[i + 1] except IndexError: pywikibot.output( u'Unless using solely -subst or -remove, you must give an even number of template names.' ) return oldTemplates = [] for templateName in templates.keys(): oldTemplate = pywikibot.Page(pywikibot.Site(), templateName, ns=10) oldTemplates.append(oldTemplate) if xmlfilename: gen = XmlDumpTemplatePageGenerator(oldTemplates, xmlfilename) else: gen = genFactory.getCombinedGenerator() if not gen: gens = [ pagegenerators.ReferringPageGenerator(t, onlyTemplateInclusion=True) for t in oldTemplates ] gen = pagegenerators.CombinedPageGenerator(gens) gen = pagegenerators.DuplicateFilterPageGenerator(gen) if user: gen = UserEditFilterGenerator(gen, user, timestamp, skip, max_revision_depth=100) if not genFactory.gens: # make sure that proper namespace filtering etc. is handled gen = genFactory.getCombinedGenerator(gen) preloadingGen = pagegenerators.PreloadingGenerator(gen) bot = TemplateRobot(preloadingGen, templates, **options) bot.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: list of unicode """ templateNames = [] templates = {} options = {} # If xmlfilename is None, references will be loaded from the live wiki. xmlfilename = None user = None skip = False timestamp = None # read command line parameters local_args = pywikibot.handle_args(args) # Avoid conflicts with pagegenerators.py parameters. if any(arg.startswith('-category:') for arg in local_args): warn( '-category (to append a category to each edited page) has been' ' renamed to -addcat; make sure you are using the correct param.', ArgumentDeprecationWarning) site = pywikibot.Site() genFactory = pagegenerators.GeneratorFactory() for arg in local_args: if arg == '-remove': options['remove'] = True elif arg == '-subst': options['subst'] = True elif arg == '-assubst': options['subst'] = options['remove'] = True elif arg == '-always': options['always'] = True elif arg.startswith('-xml'): if len(arg) == 4: xmlfilename = pywikibot.input( u'Please enter the XML dump\'s filename: ') else: xmlfilename = arg[5:] elif arg.startswith('-addcat:'): options['addedCat'] = arg[len('-addcat:'):] elif arg.startswith('-summary:'): options['summary'] = arg[len('-summary:'):] elif arg.startswith('-onlyuser:'******'-onlyuser:'******'-skipuser:'******'-skipuser:'******'-timestamp:'): timestamp = arg[len('-timestamp:'):] else: if not genFactory.handleArg(arg): templateName = pywikibot.Page(site, arg, ns=10) templateNames.append(templateName.title(withNamespace=False)) if not templateNames: pywikibot.bot.suggest_help(missing_parameters=['templates']) return False if options.get('subst', False) ^ options.get('remove', False): for templateName in templateNames: templates[templateName] = None else: try: for i in range(0, len(templateNames), 2): templates[templateNames[i]] = templateNames[i + 1] except IndexError: pywikibot.output('Unless using solely -subst or -remove, ' 'you must give an even number of template names.') return oldTemplates = [] for templateName in templates.keys(): oldTemplate = pywikibot.Page(site, templateName, ns=10) oldTemplates.append(oldTemplate) if xmlfilename: builder = textlib._MultiTemplateMatchBuilder(site) predicate = builder.search_any_predicate(oldTemplates) gen = XmlDumpTemplatePageGenerator(xmlfilename, site=site, text_predicate=predicate) else: gen = genFactory.getCombinedGenerator() if not gen: gens = [ pagegenerators.ReferringPageGenerator(t, onlyTemplateInclusion=True) for t in oldTemplates ] gen = pagegenerators.CombinedPageGenerator(gens) gen = pagegenerators.DuplicateFilterPageGenerator(gen) if user: gen = pagegenerators.UserEditFilterGenerator(gen, user, timestamp, skip, max_revision_depth=100, show_filtered=True) if not genFactory.gens: # make sure that proper namespace filtering etc. is handled gen = genFactory.getCombinedGenerator(gen) preloadingGen = pagegenerators.PreloadingGenerator(gen) bot = TemplateRobot(preloadingGen, templates, **options) bot.run()
def main(): summary_commandline, gen, template = None, None, None namespaces, PageTitles, exceptions = [], [], [] encat, newcatfile = '', '' autoText, autoTitle = False, False recentcat, newcat = False, False genFactory = pagegenerators.GeneratorFactory() for arg in pywikibot.handleArgs(): if arg == '-autotitle': autoTitle = True elif arg == '-autotext': autoText = True elif arg.startswith('-page'): if len(arg) == 5: PageTitles.append( pywikibot.input(u'Which page do you want to chage?')) else: PageTitles.append(arg[6:]) break elif arg.startswith('-except:'): exceptions.append(arg[8:]) elif arg.startswith('-template:'): template = arg[10:] elif arg.startswith('-facat:'): encat = arg[7:].replace(u'Category:', u'').replace(u'category:', u'').replace(u'رده:', u'') encat = englishdictionry(u'رده:' + encat, fa_site, en_site).replace(u'Category:', u'').replace( u'category:', u'') break elif arg.startswith('-encat:'): encat = arg[7:].replace(u'Category:', u'').replace(u'category:', u'').replace(u'رده:', u'') break elif arg.startswith('-newcatfile:'): newcatfile = arg[12:] break elif arg.startswith('-recentcat'): arg = arg.replace(':', '') if len(arg) == 10: genfa = pagegenerators.RecentchangesPageGenerator() else: genfa = pagegenerators.RecentchangesPageGenerator( number=int(arg[10:])) genfa = pagegenerators.DuplicateFilterPageGenerator(genfa) genfa = pagegenerators.NamespaceFilterPageGenerator(genfa, [14]) preloadingGen = pagegenerators.PreloadingGenerator(genfa, 60) recentcat = True break elif arg.startswith('-newcat'): arg = arg.replace(':', '') if len(arg) == 7: genfa = pagegenerators.NewpagesPageGenerator(step=100, namespaces=14) else: genfa = pagegenerators.NewpagesPageGenerator(step=int(arg[7:]), namespaces=14) preloadingGen = pagegenerators.PreloadingGenerator(genfa, 60) newcat = True break elif arg.startswith('-namespace:'): namespaces.append(int(arg[11:])) elif arg.startswith('-summary:'): pywikibot.setAction(arg[9:]) summary_commandline = True else: generator = genFactory.handleArg(arg) if generator: gen = genFactory.getCombinedGenerator(gen) if encat != '': encatfalist, encatlists = encatlist(encat) if encatlists: for encat in encatlists: encat = englishdictionry(encat, en_site, fa_site) if encat: run([encat]) if encatfalist is not False: run(encatfalist) if PageTitles: pages = [ pywikibot.Page(fa_site, PageTitle) for PageTitle in PageTitles ] gen = iter(pages) if recentcat: for workpage in preloadingGen: workpage = workpage.title() cat = pywikibot.Category(fa_site, workpage) gent = pagegenerators.CategorizedPageGenerator(cat) run(gent) pywikibot.stopme() sys.exit() if newcat: for workpage in preloadingGen: workpage = workpage.title() workpage = englishdictionry(workpage, fa_site, en_site) if workpage is not False: encatfalist, encatlists = encatlist(workpage) if encatlists: for encat in encatlists: encat = englishdictionry(encat, en_site, fa_site) if encat: run([encat]) if encatfalist is not False: run(encatfalist) pywikibot.stopme() sys.exit() if newcatfile: text2 = codecs.open(newcatfile, 'r', 'utf8') text = text2.read() linken = re.findall(ur'\[\[.*?\]\]', text, re.S) if linken: for workpage in linken: workpage = workpage.split(u'|')[0].replace(u'[[', u'').replace( u']]', u'').strip() workpage = englishdictionry(workpage, fa_site, en_site) if workpage is not False: encatfalist, encatlists = encatlist(workpage) if encatlists: run(encatlists) if encatfalist is not False: run(encatfalist) pywikibot.stopme() sys.exit() if not gen: pywikibot.stopme() sys.exit() if namespaces != []: gen = pagegenerators.NamespaceFilterPageGenerator(gen, namespaces) preloadingGen = pagegenerators.PreloadingGenerator(gen, pageNumber=60) run(preloadingGen)