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 """ # Page generator gen = None # Process global args and prepare generator args parser local_args = pywikibot.handle_args(args) genFactory = GeneratorFactory() botArgs = {} for arg in local_args: if arg == '-always': botArgs['always'] = True else: genFactory.handleArg(arg) gen = genFactory.getCombinedGenerator(preload=True) if not gen: pywikibot.bot.suggest_help(missing_generator=True) return False bot = SelflinkBot(gen, **botArgs) bot.run() return True
def main(*args): gen = None notitle = False page_get = False # Process global args and prepare generator args parser local_args = pywikibot.handleArgs(*args) genFactory = GeneratorFactory() for arg in local_args: if arg == '-notitle': notitle = True elif arg == '-get': page_get = True else: genFactory.handleArg(arg) gen = genFactory.getCombinedGenerator() if gen: for i, page in enumerate(gen, start=1): if not notitle: pywikibot.stdout("%4d: %s" % (i, page.title())) if page_get: # TODO: catch exceptions pywikibot.output(page.text, toStdout=True) else: pywikibot.showHelp()
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 """ # Page generator gen = None # Process global args and prepare generator args parser local_args = pywikibot.handle_args(args) genFactory = GeneratorFactory() botArgs = {} for arg in local_args: if arg == '-always': botArgs['always'] = True else: genFactory.handleArg(arg) gen = genFactory.getCombinedGenerator() if not gen: pywikibot.showHelp() return preloadingGen = PreloadingGenerator(gen) bot = SelflinkBot(preloadingGen, **botArgs) bot.run()
def main(*args): gen = None notitle = False fmt = '1' outputlang = None page_get = False # Process global args and prepare generator args parser local_args = pywikibot.handleArgs(*args) genFactory = GeneratorFactory() for arg in local_args: if arg == '-notitle': notitle = True elif arg.startswith("-format:"): fmt = arg[len("-format:"):] fmt = fmt.replace(u'\\03{{', u'\03{{') elif arg.startswith("-outputlang:"): outputlang = arg[len("-outputlang:"):] elif arg == '-get': page_get = True else: genFactory.handleArg(arg) gen = genFactory.getCombinedGenerator() if gen: for i, page in enumerate(gen, start=1): if not notitle: page_fmt = Formatter(page, outputlang) pywikibot.stdout(page_fmt.output(num=i, fmt=fmt)) if page_get: # TODO: catch exceptions pywikibot.output(page.text, toStdout=True) else: pywikibot.showHelp()
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 """ gen = None notitle = False fmt = '1' outputlang = None page_get = False # Process global args and prepare generator args parser local_args = pywikibot.handle_args(args) genFactory = GeneratorFactory() local_args.append("-titleregex .*?Dungeons & Dragons.*?") for arg in local_args: if arg == '-notitle': notitle = True elif arg.startswith("-format:"): fmt = arg[len("-format:"):] fmt = fmt.replace(u'\\03{{', u'\03{{') elif arg.startswith("-outputlang:"): outputlang = arg[len("-outputlang:"):] elif arg == '-get': page_get = True else: genFactory.handleArg(arg) gen = genFactory.getCombinedGenerator() this_site = pywikibot.Site() page_text = "#REDIRECT [[%s]]\n\n{{Redr|unprintworthy}}" if gen: for i, page in enumerate(gen, start=1): replace_title = page.title().replace('Dungeons & Dragons', 'Dungeons and Dragons') dest_page = pywikibot.Page(this_site,replace_title) if not dest_page.exists(): target_redirect = page.title() if page.isRedirectPage(): target_redirect = page.getRedirectTarget().title() dest_text = page_text % target_redirect pdb.set_trace() dest_page.text = dest_text #dest_page.save( # comment=u'Hasteurbot task N: Dungeons and Dragons', # watch = False, # botflag = True, # force = True #) else: pywikibot.showHelp()
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 """ gen = None notitle = False fmt = '1' outputlang = None page_get = False # Process global args and prepare generator args parser local_args = pywikibot.handle_args(args) genFactory = GeneratorFactory() local_args.append("-titleregex .*?Dungeons & Dragons.*?") for arg in local_args: if arg == '-notitle': notitle = True elif arg.startswith("-format:"): fmt = arg[len("-format:"):] fmt = fmt.replace(u'\\03{{', u'\03{{') elif arg.startswith("-outputlang:"): outputlang = arg[len("-outputlang:"):] elif arg == '-get': page_get = True else: genFactory.handleArg(arg) gen = genFactory.getCombinedGenerator() this_site = pywikibot.Site() page_text = "#REDIRECT [[%s]]\n\n{{Redr|unprintworthy}}" if gen: for i, page in enumerate(gen, start=1): replace_title = page.title().replace('Dungeons & Dragons', 'Dungeons and Dragons') dest_page = pywikibot.Page(this_site, replace_title) if not dest_page.exists(): target_redirect = page.title() if page.isRedirectPage(): target_redirect = page.getRedirectTarget().title() dest_text = page_text % target_redirect dest_page.text = dest_text dest_page.save( comment=u'Hasteurbot task 13: Dungeons and Dragons', watch=False, botflag=True, force=True) else: pywikibot.showHelp()
def main(*args): gen = None fmt = "{num:4d} {page.title}" genFactory = GeneratorFactory() for arg in pywikibot.handleArgs(*args): if arg.startswith("-format:"): fmt = arg[len("-format:"):] genFactory.handleArg(arg) gen = genFactory.getCombinedGenerator() if gen: for i, page in enumerate(gen): pywikibot.stdout(fmt.format(num=i, page=page)) else: pywikibot.showHelp()
def main(*args): """ Process command line arguments and invoke bot. @param args: command line arguments @type args: list of unicode """ options = {} local_args = pywikibot.handle_args(args) site = pywikibot.Site() site.login() gen_factory = GeneratorFactory(site) for arg in local_args: if gen_factory.handleArg(arg): continue arg, _, value = arg.partition(':') arg = arg[1:] if arg == 'summary': if not value: value = pywikibot.input( 'Please enter a value for {}'.format(arg), default=None) options[arg] = value else: options[arg] = True gen = gen_factory.getCombinedGenerator(preload=True) CategoryDoubleRedirectFixerBot(gen, site=site, **options).run()
def main(*args): options = {} local_args = pywikibot.handle_args(args) site = pywikibot.Site() genFactory = GeneratorFactory(site=site) for arg in local_args: if genFactory.handleArg(arg): continue if arg.startswith('-'): arg, sep, value = arg.partition(':') if value != '': options[arg[1:]] = int(value) if value.isdigit() else value else: options[arg[1:]] = True else: options['wiki'] = arg db = pymysql.connect( database='s53728__data', host='tools.db.svc.eqiad.wmflabs', read_default_file=os.path.expanduser('~/replica.my.cnf'), charset='utf8mb4', ) generator = genFactory.getCombinedGenerator() if generator: cls = GeneratorBot elif options.get('wiki'): cls = SingleWikiUpdatingBot else: cls = DatabaseUpdatingBot bot = cls(db, generator=generator, **options) bot.run()
def main(*args): options = {} local_args = pywikibot.handle_args(args) site = pywikibot.Site() genFactory = GeneratorFactory(site=site) for arg in local_args: if genFactory.handleArg(arg): continue if arg.startswith('-'): arg, sep, value = arg.partition(':') if value != '': options[arg[1:]] = value if not value.isdigit() else int(value) else: options[arg[1:]] = True generator = genFactory.getCombinedGenerator() if not generator: generator = page_with_property_generator('displaytitle', site=site) if genFactory.namespaces: generator = NamespaceFilterPageGenerator(generator, genFactory.namespaces, site=site) bot = LabelSettingBot(generator=generator, site=site, **options) bot.run()
def main(*args): try: gen = None genFactory = GeneratorFactory() for arg in pywikibot.handleArgs(*args): genFactory.handleArg(arg) gen = genFactory.getCombinedGenerator() if gen: i = 0 for page in gen: i += 1 pywikibot.stdout("%4d: %s" % (i, page.title())) else: pywikibot.showHelp() except Exception: pywikibot.error("Fatal error", exc_info=True) finally: pywikibot.stopme()
def main(*args: str) -> None: """ Process command line arguments and invoke bot. @param args: command line arguments """ local_args = pywikibot.handle_args(args) site = pywikibot.Site() site.login() gen_factory = GeneratorFactory(site) for arg in local_args: gen_factory.handleArg(arg) for key, value in TPL.items(): TPL[key] = get_template_pages( [pywikibot.Page(site, tpl, ns=10) for tpl in value]) for page in gen_factory.getCombinedGenerator(): page = CFDWPage(page) if page.protection().get('edit', ('', ''))[0] == 'sysop': page.parse()
def main(): # Page generator gen = None # Process global args and prepare generator args parser local_args = pywikibot.handleArgs() genFactory = GeneratorFactory() botArgs = {} for arg in local_args: if arg == '-always': botArgs['always'] = True else: genFactory.handleArg(arg) gen = genFactory.getCombinedGenerator() if not gen: pywikibot.showHelp() return preloadingGen = PreloadingGenerator(gen) bot = SelflinkBot(preloadingGen, **botArgs) bot.run()
def main(*args: str) -> None: """Process command line arguments and invoke bot.""" options = {} local_args = pywikibot.handle_args(args) site = pywikibot.Site() site.login() gen_factory = GeneratorFactory(site) for arg in local_args: if gen_factory.handleArg(arg): continue arg, _, _ = arg.partition(':') arg = arg[1:] options[arg] = True gen = gen_factory.getCombinedGenerator(preload=True) SVGValidatorBot(generator=gen, site=site, **options).run()
def main(*args): options = {} local_args = pywikibot.handle_args(args) site = pywikibot.Site() genFactory = GeneratorFactory(site=site) for arg in local_args: if genFactory.handleArg(arg): continue if arg.startswith('-'): arg, sep, value = arg.partition(':') if value != '': options[arg[1:]] = value if not value.isdigit() else int(value) else: options[arg[1:]] = True generator = genFactory.getCombinedGenerator() bot = WikidataRedirectsFixingBot(generator=generator, site=site, **options) bot.run()
def main(*args): options = {} local_args = pywikibot.handle_args(args) site = pywikibot.Site() genFactory = GeneratorFactory(site=site) for arg in local_args: if genFactory.handleArg(arg): continue if arg.startswith('-'): arg, sep, value = arg.partition(':') if value != '': options[arg[1:]] = value if not value.isdigit() else int(value) else: options[arg[1:]] = True generator = genFactory.getCombinedGenerator() bot = DupesMergingBot(generator=generator, site=site, **options) bot.run()
def main(*args: str) -> None: """Process command line arguments and invoke bot.""" local_args = pywikibot.handle_args(args, do_help=False) site = pywikibot.Site() # site.login() gen_factory = GeneratorFactory(site) script_args = [arg for arg in local_args if not gen_factory.handleArg(arg)] parser = argparse.ArgumentParser( description='Tag draftified articles', epilog=re.sub( r'\n\n?-help +.+?(\n\n-|\s*$)', r'\1', _GLOBAL_HELP + parameterHelp, flags=re.S, ), formatter_class=argparse.RawDescriptionHelpFormatter, allow_abbrev=False, ) parser.add_argument( '--always', '-a', action='store_true', help='Do not prompt to save changes', ) parser.add_argument( '--start', type=pywikibot.Timestamp.fromISOformat, help='Timestampt to start from', metavar='%Y-%m-%dT%H:%M:%SZ', ) parser.add_argument('--summary', help='Edit aummary for the bot', default=argparse.SUPPRESS) parser.add_argument('--template', help='Template to add', default=argparse.SUPPRESS) parsed_args = vars(parser.parse_args(args=script_args)) start = parsed_args.pop('start') gen = None if gen_factory.gens else draftified_page_generator(site, start) gen = gen_factory.getCombinedGenerator(gen=gen, preload=True) DfyTaggerBot(generator=gen, site=site, **parsed_args).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 """ options = {} # Process global arguments local_args = pywikibot.handle_args(args) site = pywikibot.Site() site.login() # Parse command line arguments gen_factory = GeneratorFactory(site) for arg in local_args: if gen_factory.handleArg(arg): continue if arg == '-always': options['always'] = True gen = gen_factory.getCombinedGenerator() CommonsPotdImporter(gen, site=site, **options).run()
def main(*args): options = {} local_args = pywikibot.handle_args(args) site = pywikibot.Site() genFactory = GeneratorFactory(site=site) for arg in local_args: if genFactory.handleArg(arg): continue if arg.startswith('-'): arg, sep, value = arg.partition(':') if value != '': options[arg[1:]] = int(value) if value.isdigit() else value else: options[arg[1:]] = True generator = genFactory.getCombinedGenerator(preload=True) if generator: bot = MappingDescriptionBot(generator=generator, site=site, **options) else: bot = MissingDescriptionBot(site=site, **options) bot.run()
def main(*args): options = {} local_args = pywikibot.handle_args(args) site = pywikibot.Site() genFactory = GeneratorFactory(site=site) for arg in local_args: if genFactory.handleArg(arg): continue if arg.startswith('-'): arg, sep, value = arg.partition(':') if arg == '-prop': options.setdefault('props', []).append( value or pywikibot.input('Which property should be treated?')) elif value: options[arg[1:]] = int(value) if value.isdigit() else value else: options[arg[1:]] = True generator = genFactory.getCombinedGenerator() bot = DuplicateDatesBot(generator=generator, site=site, **options) bot.run()
def main(*args): options = {} local_args = pywikibot.handle_args(args) site = pywikibot.Site() genFactory = GeneratorFactory(site=site) for arg in local_args: if genFactory.handleArg(arg): continue if arg.startswith('-'): arg, sep, value = arg.partition(':') if value != '': options[arg[1:]] = value if not value.isdigit() else int(value) else: options[arg[1:]] = True generator = genFactory.getCombinedGenerator() if not generator: generator = page_with_property_generator('displaytitle', site=site) if genFactory.namespaces: generator = NamespaceFilterPageGenerator( generator, genFactory.namespaces, site=site) bot = LabelSettingBot(generator=generator, site=site, **options) bot.run()
def main(*args): """ Process command line arguments and invoke bot. @param args: command line arguments @type args: list of unicode """ options = {} local_args = pywikibot.handle_args(args) site = pywikibot.Site() site.login() gen_factory = GeneratorFactory(site) for arg in local_args: if gen_factory.handleArg(arg): continue arg, _, value = arg.partition(':') arg = arg[1:] if arg == 'config': if not value: value = pywikibot.input( 'Please enter a value for {}'.format(arg), default=None) options[arg] = value else: options[arg] = True gen = gen_factory.getCombinedGenerator(preload=True) if 'config' not in options: pywikibot.bot.suggest_help(missing_parameters=['config']) return False config = get_json_from_page(pywikibot.Page(site, options.pop('config'))) if validate_config(config): options.update(config) else: pywikibot.error('Invalid config.') return False MagicLinksReplacer(gen, site=site, **options).run() return True
def generator(self, pywikibot=True): docsCategory = DoxygenHTMLPage.globalCategory docsImgCategory = ImagePage.globalCategory transCategory = TransclusionPage.globalCategory navCategory = DoxygenHTMLPage.globalNavCategory botUserPage = BotUserPage(self.site) stylesPage = StylesPage() #Generator for categories fac = GeneratorFactory(site=self.site) fac.handleArg("-page:" + docsCategory.mwtitle) fac.handleArg("-subcatsr:" + docsCategory.normtitle.title) fac.handleArg("-page:" + transCategory.mwtitle) fac.handleArg("-page:" + navCategory.mwtitle) fac.handleArg("-subcatsr:" + navCategory.normtitle.title) fac.handleArg("-page:" + docsImgCategory.mwtitle) genCat = fac.getCombinedGenerator() #Generator for DoxyHTMLPages fac = GeneratorFactory(site=self.site) fac.handleArg("-cat:" + docsCategory.normtitle.title) genDoxy = fac.getCombinedGenerator() #Generator for Images fac = GeneratorFactory(site=self.site) fac.handleArg("-cat:" + docsImgCategory.normtitle.title) genImg = fac.getCombinedGenerator() #Generator for TransclusionPages fac = GeneratorFactory(site=self.site) fac.handleArg("-cat:" + transCategory.normtitle.title) genTrans = fac.getCombinedGenerator() if pywikibot: #Select only redirect transclusion pages debugFiltered = doxymwglobal.option["printLevel"].value <= doxymwglobal.msgType.debug.value genTrans = pagegenerators.RedirectFilterPageGenerator(genTrans, no_redirects=False, show_filtered=debugFiltered) #Generators for other pages fac = GeneratorFactory(site=self.site) fac.handleArg("-page:" + botUserPage.mwtitle) genUser = fac.getCombinedGenerator() fac = GeneratorFactory(site=self.site) fac.handleArg("-page:" + stylesPage.mwtitle) genStyles = fac.getCombinedGenerator() #Combined generator if pywikibot: return pagegenerators.CombinedPageGenerator([genCat, genDoxy, genImg, genTrans, genUser]) #No genStyles because we don't fully own it else: ret = [] ret.append((genCat,CategoryPage.getStrategy())) ret.append((genDoxy,DoxygenHTMLPage.getStrategy())) ret.append((genImg,ImagePage.getStrategy())) ret.append((genTrans,TransclusionPage.getStrategy())) ret.append((genUser,BotUserPage.getStrategy())) ret.append((genStyles,StylesPage.getStrategy())) return ret
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: str """ gen = None notitle = False fmt = '1' outputlang = None page_get = False base_dir = None encoding = config.textfile_encoding page_target = None overwrite = False summary = 'listpages-save-list' # Process global args and prepare generator args parser local_args = pywikibot.handle_args(args) gen_factory = GeneratorFactory() for arg in local_args: option, sep, value = arg.partition(':') if option == '-notitle': notitle = True elif option == '-format': fmt = value.replace('\\03{{', '\03{{') if not fmt.strip(): notitle = True elif option == '-outputlang:': outputlang = value elif option == '-get': page_get = True elif option == '-save': base_dir = value or '.' elif option == '-encode': encoding = value elif option == '-put': page_target = value elif option == '-overwrite': overwrite = True elif option == '-summary': summary = value else: gen_factory.handleArg(arg) if base_dir: base_dir = os.path.expanduser(base_dir) if not os.path.isabs(base_dir): base_dir = os.path.normpath(os.path.join(os.getcwd(), base_dir)) if not os.path.exists(base_dir): pywikibot.output( 'Directory "{0}" does not exist.'.format(base_dir)) choice = pywikibot.input_yn( 'Do you want to create it ("No" to continue without saving)?') if choice: os.makedirs(base_dir, mode=0o744) else: base_dir = None elif not os.path.isdir(base_dir): # base_dir is a file. pywikibot.warning('Not a directory: "{0}"\n' 'Skipping saving ...'.format(base_dir)) base_dir = None if page_target: site = pywikibot.Site() page_target = pywikibot.Page(site, page_target) if not overwrite and page_target.exists(): pywikibot.bot.suggest_help( additional_text='Page {0} already exists.\n' 'You can use the -overwrite argument to ' 'replace the content of this page.'.format( page_target.title(as_link=True))) return False if re.match('^[a-z_-]+$', summary): summary = i18n.twtranslate(site, summary) gen = gen_factory.getCombinedGenerator() if gen: i = 0 output_list = [] for i, page in enumerate(gen, start=1): if not notitle: page_fmt = Formatter(page, outputlang) output_list += [page_fmt.output(num=i, fmt=fmt)] pywikibot.stdout(output_list[-1]) if page_get: try: pywikibot.stdout(page.text) except pywikibot.Error as err: pywikibot.output(err) if base_dir: filename = os.path.join(base_dir, page.title(as_filename=True)) pywikibot.output('Saving {0} to {1}'.format( page.title(), filename)) with open(filename, mode='wb') as f: f.write(page.text.encode(encoding)) pywikibot.output('{0} page(s) found'.format(i)) if page_target: page_target.text = '\n'.join(output_list) page_target.save(summary=summary) return True else: pywikibot.bot.suggest_help(missing_generator=True) return False
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 """ gen = None notitle = False fmt = '1' outputlang = None page_get = False base_dir = None encoding = config.textfile_encoding page_target = None overwrite = False summary = 'listpages-save-list' # Process global args and prepare generator args parser local_args = pywikibot.handle_args(args) genFactory = GeneratorFactory() for arg in local_args: if arg == '-notitle': notitle = True elif arg.startswith('-format:'): fmt = arg[len('-format:'):] fmt = fmt.replace(u'\\03{{', u'\03{{') elif arg.startswith('-outputlang:'): outputlang = arg[len('-outputlang:'):] elif arg == '-get': page_get = True elif arg.startswith('-save'): base_dir = arg.partition(':')[2] or '.' elif arg.startswith('-encode:'): encoding = arg.partition(':')[2] elif arg.startswith('-put:'): page_target = arg.partition(':')[2] elif arg.startswith('-overwrite'): overwrite = True elif arg.startswith('-summary:'): summary = arg.partition(':')[2] else: genFactory.handleArg(arg) if base_dir: base_dir = os.path.expanduser(base_dir) if not os.path.isabs(base_dir): base_dir = os.path.normpath(os.path.join(os.getcwd(), base_dir)) if not os.path.exists(base_dir): pywikibot.output(u'Directory "{0!s}" does not exist.'.format(base_dir)) choice = pywikibot.input_yn( u'Do you want to create it ("No" to continue without saving)?') if choice: os.makedirs(base_dir, mode=0o744) else: base_dir = None elif not os.path.isdir(base_dir): # base_dir is a file. pywikibot.warning(u'Not a directory: "%s"\n' u'Skipping saving ...' % base_dir) base_dir = None if page_target: site = pywikibot.Site() page_target = pywikibot.Page(site, page_target) if not overwrite and page_target.exists(): pywikibot.bot.suggest_help( additional_text='Page "{0}" already exists.'.format( page_target.title())) return False if re.match('^[a-z_-]+$', summary): summary = i18n.twtranslate(site, summary) gen = genFactory.getCombinedGenerator() if gen: i = 0 output_list = [] for i, page in enumerate(gen, start=1): if not notitle: page_fmt = Formatter(page, outputlang) output_list += [page_fmt.output(num=i, fmt=fmt)] pywikibot.stdout(output_list[-1]) if page_get: try: pywikibot.stdout(page.text) except pywikibot.Error as err: pywikibot.output(err) if base_dir: filename = os.path.join(base_dir, page.title(as_filename=True)) pywikibot.output(u'Saving {0!s} to {1!s}'.format(page.title(), filename)) with open(filename, mode='wb') as f: f.write(page.text.encode(encoding)) pywikibot.output(u"{0:d} page(s) found".format(i)) if page_target: page_target.text = '\n'.join(output_list) page_target.save(summary=summary) return True else: pywikibot.bot.suggest_help(missing_generator=True) return False
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 """ gen = None notitle = False fmt = '1' outputlang = None page_get = False base_dir = None encoding = config.textfile_encoding page_target = None overwrite = False summary = 'listpages-save-list' # Process global args and prepare generator args parser local_args = pywikibot.handle_args(args) genFactory = GeneratorFactory() for arg in local_args: if arg == '-notitle': notitle = True elif arg.startswith('-format:'): fmt = arg[len('-format:'):] fmt = fmt.replace(u'\\03{{', u'\03{{') elif arg.startswith('-outputlang:'): outputlang = arg[len('-outputlang:'):] elif arg == '-get': page_get = True elif arg.startswith('-save'): base_dir = arg.partition(':')[2] or '.' elif arg.startswith('-encode:'): encoding = arg.partition(':')[2] elif arg.startswith('-put:'): page_target = arg.partition(':')[2] elif arg.startswith('-overwrite'): overwrite = True elif arg.startswith('-summary:'): summary = arg.partition(':')[2] else: genFactory.handleArg(arg) if base_dir: base_dir = os.path.expanduser(base_dir) if not os.path.isabs(base_dir): base_dir = os.path.normpath(os.path.join(os.getcwd(), base_dir)) if not os.path.exists(base_dir): pywikibot.output(u'Directory "%s" does not exist.' % base_dir) choice = pywikibot.input_yn( u'Do you want to create it ("No" to continue without saving)?') if choice: os.makedirs(base_dir, mode=0o744) else: base_dir = None elif not os.path.isdir(base_dir): # base_dir is a file. pywikibot.warning(u'Not a directory: "%s"\n' u'Skipping saving ...' % base_dir) base_dir = None if page_target: site = pywikibot.Site() page_target = pywikibot.Page(site, page_target) if not overwrite and page_target.exists(): pywikibot.bot.suggest_help( additional_text='Page "{0}" already exists.'.format( page_target.title())) return False if re.match('^[a-z_-]+$', summary): summary = i18n.twtranslate(site, summary) gen = genFactory.getCombinedGenerator() if gen: i = 0 output_list = [] for i, page in enumerate(gen, start=1): if not notitle: page_fmt = Formatter(page, outputlang) output_list += [page_fmt.output(num=i, fmt=fmt)] pywikibot.stdout(output_list[-1]) if page_get: try: pywikibot.output(page.text, toStdout=True) except pywikibot.Error as err: pywikibot.output(err) if base_dir: filename = os.path.join(base_dir, page.title(as_filename=True)) pywikibot.output(u'Saving %s to %s' % (page.title(), filename)) with open(filename, mode='wb') as f: f.write(page.text.encode(encoding)) pywikibot.output(u"%i page(s) found" % i) if page_target: page_target.text = '\n'.join(output_list) page_target.save(summary=summary) return True else: pywikibot.bot.suggest_help(missing_generator=True) return False
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 """ gen = None notitle = False fmt = '1' outputlang = None page_get = False base_dir = None encoding = config.textfile_encoding # Process global args and prepare generator args parser local_args = pywikibot.handle_args(args) genFactory = GeneratorFactory() for arg in local_args: if arg == '-notitle': notitle = True elif arg.startswith('-format:'): fmt = arg[len('-format:'):] fmt = fmt.replace(u'\\03{{', u'\03{{') elif arg.startswith('-outputlang:'): outputlang = arg[len('-outputlang:'):] elif arg == '-get': page_get = True elif arg.startswith('-save'): base_dir = arg.partition(':')[2] or '.' elif arg.startswith('-encode:'): encoding = arg.partition(':')[2] else: genFactory.handleArg(arg) if base_dir: base_dir = os.path.expanduser(base_dir) if not os.path.isabs(base_dir): base_dir = os.path.normpath(os.path.join(os.getcwd(), base_dir)) if not os.path.exists(base_dir): pywikibot.output(u'Directory "%s" does not exist.' % base_dir) choice = pywikibot.input_yn( u'Do you want to create it ("No" to continue without saving)?') if choice: os.makedirs(base_dir, mode=0o744) else: base_dir = None elif not os.path.isdir(base_dir): # base_dir is a file. pywikibot.warning(u'Not a directory: "%s"\n' u'Skipping saving ...' % base_dir) base_dir = None gen = genFactory.getCombinedGenerator() if gen: i = 0 for i, page in enumerate(gen, start=1): if not notitle: page_fmt = Formatter(page, outputlang) pywikibot.stdout(page_fmt.output(num=i, fmt=fmt)) if page_get: try: pywikibot.output(page.text, toStdout=True) except pywikibot.Error as err: pywikibot.output(err) if base_dir: filename = os.path.join(base_dir, page.title(as_filename=True)) pywikibot.output(u'Saving %s to %s' % (page.title(), filename)) with open(filename, mode='wb') as f: f.write(page.text.encode(encoding)) pywikibot.output(u"%i page(s) found" % i) else: pywikibot.showHelp()