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 """ # Generate the question text questions = '\n' questionlist = {} pywikibot.handle_args(*args) site = pywikibot.Site() if (site.lang not in list(templates.keys()) and site.lang not in list(done.keys())): pywikibot.output( '\nScript is not localised for {0}. Terminating program.' ''.format(site)) else: for i, t in enumerate(pywikibot.translate(site.lang, templates)): questions += (u'%s) %s\n' % (i, t)) questionlist[i] = t bot = CleaningBot(questions, questionlist) bot.run()
def main(args): letter = None force_dump = False bot_args = [] for arg in args: print('\u05D0'.encode('utf-8')) #print(arg.decode('utf-8')) l = re.compile(r'^-letter:(.+)$').match(arg) print(l) if l: letter = l.group(1) elif arg == '--get-dump': force_dump = True # download the latest dump if it doesnt exist else: bot_args.append(arg) print(bot_args) pywikibot.handle_args(bot_args) site = pywikibot.Site('he', 'wiktionary') if force_dump \ or not os.path.exists(DUMP_FILE) \ or file_age_by_days(DUMP_FILE) > 30: get_dump() all_wiktionary = XmlDump(DUMP_FILE).parse() all_wiktionary = filter(lambda page: page.ns == '0' \ and not page.title.endswith('(שורש)') \ and not re.compile(r'[a-zA-Z]').search(page.title) \ and (not letter or page.title.startswith(letter)) and not page.isredirect, all_wiktionary) gen = (pywikibot.Page(site, p.title) for p in all_wiktionary) gen = pagegenerators.PreloadingGenerator(gen) words = [] for page in gen: if not page.exists() or page.isRedirectPage(): continue for lex in hewiktionary.lexeme_title_regex_grouped.findall(page.get()): words.append("* [[%s#%s|%s]]" % (page.title(),lex,lex)) report_content = 'סך הכל %s ערכים\n' % str(len(words)) report_content += '\n'.join(['%s' % p for p in words]) report_content += "\n\n[[קטגוריה: ויקימילון - תחזוקה]]" if letter: report_page = pywikibot.Page(site, 'ויקימילון:תחזוקה/%s/%s' % ('רשימת_כל_המילים',letter)) else: report_page = pywikibot.Page(site, 'ויקימילון:תחזוקה/%s' % ('רשימת_כל_המילים')) report_page.text = report_content report_page.save("סריקה עם בוט ")
def main(*args): """Script entry point.""" env = None if args: import pywikibot pywikibot.handle_args(args) env = locals() import code code.interact("""Welcome to the Pywikibot interactive shell!""", local=env)
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 """ # Process global args and prepare generator args parser local_args = pywikibot.handle_args(args) generator_factory = pagegenerators.GeneratorFactory() wdproperty = u'P18' for arg in local_args: if arg.startswith('-property'): if len(arg) == 9: wdproperty = pywikibot.input( u'Please enter the property you want to add:') else: wdproperty = arg[10:] else: generator_factory.handleArg(arg) generator = generator_factory.getCombinedGenerator(preload=True) if not generator: pywikibot.bot.suggest_help(missing_generator=True) return False bot = IllustrateRobot(generator, wdproperty) bot.run() return True
def main(*args): generator = None local_args = pywikibot.handle_args(args) site = pywikibot.Site() if str(site) != "commons:commons": pywikibot.warning("The script has not been tested on sites other that " "commons:commons.") gen_factory = pagegenerators.GeneratorFactory(site) for local_arg in local_args: if gen_factory.handleArg(local_arg): continue arg, sep, value = local_arg.partition(':') if arg in ('-showcats',): options[arg[1:]] = True else: raise ValueError('Unknown argument: ' + local_arg) generator = gen_factory.getCombinedGenerator(gen=generator) if not generator: pywikibot.bot.suggest_help(missing_generator=True) else: pregenerator = pagegenerators.PreloadingGenerator(generator) for i, page in enumerate(pregenerator): if page.exists(): log = handle_page(page) pywikibot.output('\n'.join(log)) pywikibot.output("")
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 """ local_args = pywikibot.handle_args(args) start = None force = False # Parse command line arguments for arg in local_args: if arg.startswith('-start:'): start = arg[7:] elif arg == '-force': force = True else: pywikibot.warning( u'argument "%s" not understood; ignoring.' % arg) bot = StatesRedirectBot(start, force) 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 """ all = False new = False sysop = False for arg in pywikibot.handle_args(args): if arg in ('-all', '-update'): all = True elif arg == '-new': new = True elif arg == '-sysop': sysop = True if all: refresh_all(sysop=sysop) elif new: refresh_new(sysop=sysop) else: site = pywikibot.Site() watchlist = refresh(site, sysop=sysop) pywikibot.output(u'{0:d} pages in the watchlist.'.format(len(watchlist))) for page in watchlist: try: pywikibot.stdout(page.title()) except pywikibot.InvalidTitle: pywikibot.exception()
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 """ parent = None basename = None options = {} # Process global args and prepare generator args parser local_args = pywikibot.handle_args(args) genFactory = pagegenerators.GeneratorFactory() for arg in local_args: if arg == '-always': options['always'] = True elif arg.startswith('-parent:'): parent = arg[len('-parent:'):].strip() elif arg.startswith('-basename'): basename = arg[len('-basename:'):].strip() else: genFactory.handleArg(arg) generator = genFactory.getCombinedGenerator() if generator and parent and basename: bot = CreateCategoriesBot(generator, parent, basename, **options) bot.run() pywikibot.output(u'All done') else: pywikibot.output(u'No pages to work on') pywikibot.showHelp()
def main(*args): # Process global args and prepare generator args parser local_args = pywikibot.handle_args(args) googlecat = False collectionid = False for arg in local_args: if arg.startswith('-googlecat'): if len(arg) == 10: googlecat = pywikibot.input( u'Please enter the category you want to work on:') else: googlecat = arg[11:] elif arg.startswith('-collectionid'): if len(arg) == 13: collectionid = pywikibot.input( u'Please enter the collectionid you want to work on:') else: collectionid = arg[14:] #else: # generator_factory.handleArg(arg) if googlecat and collectionid: imageFindBot = ImageFindBot(googlecat, collectionid) imageFindBot.run() else: pywikibot.output(u'Usage: pwb.py add_google_images.py -googlecat:<category name> -collectionid:Q<123>')
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 """ global __simulate, __sys_argv for arg in pywikibot.handle_args(args): pywikibot.showHelp("script_wui") return __simulate = pywikibot.config.simulate __sys_argv = sys.argv site = pywikibot.Site() site.login() chan = "#" + site.language() + "." + site.family.name bot = ScriptWUIBot(site, chan, site.user() + "_WUI", "irc.wikimedia.org") try: bot.start() except: bot.t.cancel() raise
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 """ local_args = pywikibot.handle_args(args) genFactory = pagegenerators.GeneratorFactory() options = {} for arg in local_args: if genFactory.handleArg(arg): continue option, sep, value = arg.partition(':') option = option[1:] if option.startswith('-') else None if option == 'summary': options[option] = value else: options[option] = True site = pywikibot.Site() generator = genFactory.getCombinedGenerator() if generator: generator = pagegenerators.PreloadingGenerator(generator) bot = IWBot(generator=generator, site=site, **options) bot.run() else: 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 """ global __simulate, __sys_argv unknown_args = pywikibot.handle_args(args) if unknown_args: pywikibot.bot.suggest_help(unknown_parameters=unknown_args) return False __simulate = pywikibot.config.simulate __sys_argv = sys.argv site = pywikibot.Site() site.login() chan = '#' + site.code + '.' + site.family.name bot = ScriptWUIBot(site, chan, site.user() + "_WUI", "irc.wikimedia.org") try: bot.start() except: bot.t.cancel() raise
def main(*args): """ Do a query and have the bot process the items :param args: :return: """ # The queries for paintings without a creator, all or a specific collection query = u'SELECT ?item WHERE { ?item wdt:P31 wd:Q3305213 . MINUS { ?item wdt:P170 [] } }' querycollection = u"""SELECT ?item WHERE { ?item wdt:P31 wd:Q3305213 . ?item wdt:P195 wd:%s . MINUS { ?item wdt:P170 [] } }""" for arg in pywikibot.handle_args(args): print arg if arg.startswith('-collectionid'): if len(arg) == 13: collectionid = pywikibot.input( u'Please enter the collectionid you want to work on:') else: collectionid = arg[14:] query = querycollection % (collectionid,) repo = pywikibot.Site().data_repository() generator = pagegenerators.PreloadingItemGenerator(pagegenerators.WikidataSPARQLPageGenerator(query, site=repo)) paintingBot = PaintingBot(generator, change=False) paintingBot.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 = {} for arg in pywikibot.handle_args(args): if arg == '-replacealways': options['replace'] = True options['replacealways'] = True elif arg == '-hash': options['use_hash'] = True elif arg == '-autonomous': pywikibot.warning(u"The '-autonomous' argument is DEPRECATED," u" use '-always' instead.") options['always'] = True elif arg.startswith('-'): if arg[1:] in ('always', 'replace', 'replaceloose', 'replaceonly'): options[arg[1:]] = True bot = NowCommonsDeleteBot(**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 """ # Process global args and prepare generator args parser local_args = pywikibot.handle_args(args) gen = pagegenerators.GeneratorFactory() options = {} for arg in local_args: if ( arg.startswith('-pageage:') or arg.startswith('-lastedit:')): key, val = arg.split(':', 1) options[key[1:]] = int(val) elif gen.handleArg(arg): pass else: options[arg[1:].lower()] = True generator = gen.getCombinedGenerator() if not generator: pywikibot.bot.suggest_help(missing_generator=True) return False bot = NewItemRobot(generator, **options) bot.run() return True
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 """ local_args = pywikibot.handle_args(args) start = local_args[0] if local_args else '!' mysite = pywikibot.Site() try: mysite.disambcategory() except pywikibot.Error as e: pywikibot.bot.suggest_help(exception=e) return False generator = pagegenerators.CategorizedPageGenerator( mysite.disambcategory(), start=start, content=True, namespaces=[0]) bot = DisambiguationRedirectBot(generator=generator) 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 """ all = False new = False sysop = False for arg in pywikibot.handle_args(args): if arg in ('-all', '-update'): all = True elif arg == '-new': new = True elif arg == '-sysop': sysop = True if all: refresh_all(sysop=sysop) elif new: refresh_new(sysop=sysop) else: site = pywikibot.Site() refresh(site, sysop=sysop) watchlist = get(site) pywikibot.output(u'%i pages in the watchlist.' % len(watchlist)) for pageName in watchlist: pywikibot.output(pageName, toStdout=True)
def main(args): local_args = pywikibot.handle_args(args) site = pywikibot.Site('he', 'wiktionary') maintain_page = pywikibot.Page(site, "ויקימילון:תחזוקה/דפים_עם_סעיפים_שאינם_מהרשימה_הסגורה") genFactory = pagegenerators.GeneratorFactory() options = {} parser = argparse.ArgumentParser(description="replace wrong subsection titles in the Hebrew Wiktionary", epilog="Options include also global pywikibot options and all generators options") parser.add_argument("--article",type=str, required=False) parser.add_argument("-always",action='store_false', required=False) args, factory_args = parser.parse_known_args(local_args) options['always'] = args.always options['site'] = site for arg in factory_args: genFactory.handleArg(arg) genFactory.handleArg('-intersect') if args.article: print(args.article[::-1])#the terminal shows hebrew left to write :( gen = [pywikibot.Page(site, args.article)] gen = pagegenerators.PreloadingGenerator(gen) else: gen = pagegenerators.LinkedPageGenerator(maintain_page, content = True) gen = genFactory.getCombinedGenerator(gen) bot = SectionTitleReplacerBot(generator = gen, **options) bot.run() print('_____________________DONE____________________')
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 options = {} # Process global args and prepare generator args parser local_args = pywikibot.handle_args(args) genFactory = pagegenerators.GeneratorFactory() bot_class = TouchBot for arg in local_args: if arg == '-purge': bot_class = PurgeBot elif arg == '-redir': pywikibot.output(u'-redirect option is deprecated, ' 'do not use it anymore.') elif not genFactory.handleArg(arg) and arg.startswith("-"): options[arg[1:].lower()] = True gen = genFactory.getCombinedGenerator() if gen: preloadingGen = pagegenerators.PreloadingGenerator(gen) bot = bot_class(generator=preloadingGen, **options) pywikibot.Site().login() bot.run() 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 """ # Process global arguments to determine desired site local_args = pywikibot.handle_args(args) genFactory = pagegenerators.GeneratorFactory() # If dry is True, doesn't do any real changes, but only show # what would have been changed. dry = False frompage = u'' topage = u'' # Parse command line arguments for arg in local_args: if arg.startswith("-dry"): dry = True else: genFactory.handleArg(arg) bot = ThisDayBot(dry, frompage, topage) 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 """ # the option that's always selected when the bot wonders what to do with # a link. If it's None, the user is prompted (default behaviour). always = None main_only = False firstPageTitle = None for arg in pywikibot.handle_args(args): if arg.startswith('-always:'): always = arg[8:] elif arg.startswith('-start'): if len(arg) == 6: firstPageTitle = pywikibot.input( u'At which page do you want to start?') else: firstPageTitle = arg[7:] elif arg == '-main': main_only = True bot = MisspellingRobot(always, firstPageTitle, main_only) 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 """ my_args = pywikibot.handle_args(args) parser = ArgumentParser(add_help=False) parser.add_argument("-r", "--replace", action="store_true", help="actually replace pages (without this " "option you will only get an overview page)") parser.add_argument("-o", "--original", dest="original_wiki", help="original wiki") parser.add_argument('destination_wiki', metavar='destination', type=str, nargs='+', help='destination wiki(s)') parser.add_argument("-ns", "--namespace", dest="namespace", help="specify namespace") parser.add_argument("-dns", "--dest-namespace", dest="dest_namespace", help="destination namespace (if different)") options = parser.parse_args(my_args) sync = SyncSites(options) sync.check_sysops() sync.check_namespaces() sync.generate_overviews()
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 @rtype: bool """ exists_arg = '' commandline_claims = list() # Process global args and prepare generator args parser local_args = pywikibot.handle_args(args) gen = pagegenerators.GeneratorFactory() for arg in local_args: # Handle args specifying how to handle duplicate claims if arg.startswith('-exists:'): exists_arg = arg.split(':')[1] continue # Handle page generator args if gen.handleArg(arg): continue commandline_claims.append(arg) if len(commandline_claims) % 2: pywikibot.error('Incomplete command line property-value pair.') return False claims = list() repo = pywikibot.Site().data_repository() for i in range(0, len(commandline_claims), 2): claim = pywikibot.Claim(repo, commandline_claims[i]) if claim.type == 'wikibase-item': target = pywikibot.ItemPage(repo, commandline_claims[i + 1]) elif claim.type == 'string': target = commandline_claims[i + 1] elif claim.type == 'globe-coordinate': coord_args = [float(c) for c in commandline_claims[i + 1].split(',')] if len(coord_args) >= 3: precision = coord_args[2] else: precision = 0.0001 # Default value (~10 m at equator) target = pywikibot.Coordinate(coord_args[0], coord_args[1], precision=precision) else: raise NotImplementedError( "%s datatype is not yet supported by claimit.py" % claim.type) claim.setTarget(target) claims.append(claim) generator = gen.getCombinedGenerator() if not generator: pywikibot.bot.suggest_help(missing_generator=True) return False bot = ClaimRobot(generator, claims, exists_arg) bot.run() return True
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 = {} total = None local_args = pywikibot.handle_args(args) for arg in local_args: arg, sep, value = arg.partition(':') if arg == '-total': total = value else: options[arg[1:]] = True site = pywikibot.Site() gen = pagegenerators.UnusedFilesGenerator(total=total, site=site) gen = pagegenerators.PreloadingGenerator(gen) bot = UnusedFilesBot(site, generator=gen, **options) try: bot.run() except pywikibot.Error as e: pywikibot.bot.suggest_help(exception=e) return False else: return True
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 = {} local_args = pywikibot.handle_args(args) genFactory = pagegenerators.GeneratorFactory() for arg in local_args: if arg == '-always': options['always'] = True elif arg == '-titlecase': options['titlecase'] = True else: genFactory.handleArg(arg) gen = genFactory.getCombinedGenerator() if gen: preloadingGen = pagegenerators.PreloadingGenerator(gen) bot = CapitalizeBot(preloadingGen, **options) bot.run() 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 """ # This temporary string is used to read the title # of the page that should be unlinked. page_title = None options = {} for arg in pywikibot.handle_args(args): if arg.startswith('-namespace:'): if 'namespaces' not in options: options['namespaces'] = [] try: options['namespaces'].append(int(arg[11:])) except ValueError: options['namespaces'].append(arg[11:]) elif arg == '-always': options['always'] = True else: page_title = arg if page_title: page = pywikibot.Page(pywikibot.Site(), page_title) bot = UnlinkBot(page, **options) bot.run() return True else: pywikibot.bot.suggest_help(missing_parameters=['page title']) 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 """ # Process global arguments to determine desired site local_args = pywikibot.handle_args(args) genFactory = pagegenerators.GeneratorFactory() # If dry is True, doesn't do any real changes, but only show # what would have been changed. dry = False targetpagesingles = u'' targetpagedoubles = u'' url = u'' # Parse command line arguments for arg in local_args: if arg.startswith("-dry"): dry = True if arg.startswith("-targetpagesingles"): targetpagesingles = arg[len('-targetpagesingles:'):] if arg.startswith("-targetpagedoubles"): targetpagedoubles = arg[len('-targetpagedoubles:'):] if arg.startswith("-url"): url = arg[len('-url:'):] else: genFactory.handleArg(arg) bot = StaticBot(dry, targetpagesingles, targetpagedoubles, url) 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 """ options = {} local_args = pywikibot.handle_args(args) genFactory = pagegenerators.GeneratorFactory() for arg in local_args: if genFactory.handleArg(arg): continue if arg.startswith('-summary:'): options['summary'] = arg[9:] elif arg.startswith('-tmp:'): options['tmp'] = arg[5:] elif arg == '-force': options['force'] = True elif arg == '-always': options['always'] = True gen = genFactory.getCombinedGenerator() if gen: bot = BadgesBot(generator=gen, **options) bot.run() else: pywikibot.showHelp()
def mainloop(): limit = None entities = sorted(const.PROPERTY_IDS.keys()) for arg in wp.handle_args(): if arg.startswith('-limit'): limit = int(arg[len('-limit:'):]) elif arg.startswith("-entities"): entities = arg[len("-entities:"):].split(",") const.MUSICBRAINZ_CLAIM.setTarget(const.MUSICBRAINZ_WIKIDATAPAGE) today = datetime.datetime.today() date = wp.WbTime(year=today.year, month=today.month, day=today.day) const.RETRIEVED_CLAIM.setTarget(date) setup_db() for entitytype in entities: processed_table_query = create_processed_table_query(entitytype) create_table(processed_table_query) bot = Bot() while True: const.WIKIDATA.login() for entitytype in entities: entity_type_loop(bot, entitytype, limit) bot.update_rate_limits() sleep(settings.sleep_time_in_seconds)
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 """ global __simulate, __sys_argv unknown_args = pywikibot.handle_args(args) if unknown_args: pywikibot.bot.suggest_help(unknown_parameters=unknown_args) return False __simulate = pywikibot.config.simulate __sys_argv = sys.argv site = pywikibot.Site() site.login() chan = '#' + site.code + '.' + site.family.name bot_user_name = pywikibot.config.usernames[pywikibot.config.family][ pywikibot.config.mylang] for key, value in bot_config.items(): if hasattr(value, 'format'): bot_config[key] = value.format(username=bot_user_name) bot = ScriptWUIBot(site, chan, site.user() + "_WUI", "irc.wikimedia.org") try: bot.start() except BaseException: bot.t.cancel() raise
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 """ local_args = pywikibot.handle_args(args) # default values for options extra_summary = None for arg in local_args: option, sep, value = arg.partition(':') if option == '-summary': extra_summary = value else: pywikibot.warning("Unrecognized option {}".format(option)) def check_option(option, value): if not value: pywikibot.error("Missing argument for option '{}'".format(option)) return False return True page_number_regex = re.compile('\|([1-9][0-9]*)\}') filename_number_regex = re.compile('([1-9][0-9]*)') templates_ready = ['QC image', 'File information', 'Self'] site = pywikibot.Site() looked_at = set() pages = request_pages() for p in pages: if p['title'] in looked_at: pywikibot.output("Done.") break else: looked_at.add(p['title']) try: page_title = 'File:' + p['title'] page = pywikibot.Page(site, page_title) click_url = ROOT_URL + 'wiki/' + page.title(underscore=True) pywikibot.output("Page '{0}', id={1} | {2}".format(page_title, p['id'], click_url)) ts = page.templatesWithParams() if len(ts) > 0: found_ready = False for t in ts: for r in templates_ready: if r in t[0].title(): pywikibot.output(color_format("Page {lightgreen}{0}{default} has template: {1}", page_title, t[0])) found_ready = True break if found_ready: pywikibot.output("\tSkipping.") continue old_text = page.get() # categories = getCategoryLinks(old_text, site) # categories_text = '\n'.join(map(lambda c:c.aslink(), categories)) (header, body, footer) = extract_sections(old_text, site) summary = None licensing = None description = None for section in body: if 'ummary' in section[0] or 'escription' in section[0]: summary = section[1] if 'icens' in section[0]: licensing = section[1] got_summary_from_header = False if summary is None: got_summary_from_header = True summary = header new_text = None pywikibot.output(color_format("Editing page {lightblue}{0}{default}.", page_title)) if summary is not None and len(summary.strip()) > 0: summary = summary.strip() pywikibot.output("Have \"Summary\":\n\t{}".format(summary)) i = summary.find('{') if i > 0: summary = summary[0:i] i = summary.find(' in ') if i > 0: summary = summary[0:i] summary = summary.strip() if summary[-1] == '.': summary = summary[0:-1] pywikibot.output("Will have \"Summary\":\n\t{}".format(summary)) choice = pywikibot.input_choice("Is it a good summary?", [('Yes', 'y'), ('No', 'n'), ('open in Browser', 'b')], 'n') if choice == 'y': description = summary elif choice == 'n': pass elif choice == 'b': pywikibot.bot.open_webbrowser(page) if description is None: pywikibot.output("Type '[s]kip' to skip the image completely.") description = pywikibot.input("Please describe the file:") if description in ['s', 'skip']: continue if licensing is not None: pywikibot.output("Have \"Licensing\":\n\t{}".format(licensing.strip())) comic_num = None m = page_number_regex.search(old_text) if m: try: comic_num = int(m.group(1)) except: pass if comic_num is None: m = filename_number_regex.search(page.title()) if m: try: comic_num = int(m.group(1)) except: pass if comic_num is not None: pywikibot.output("Have comic #:\n\t{}".format(comic_num)) choice = pywikibot.input_choice("Is it a good comic number?", [('Yes', 'y'), ('No', 'n'), ('open in Browser', 'b')], 'n') if choice == 'y': pass else: comic_num = None if choice == 'b': pywikibot.bot.open_webbrowser(page) while comic_num is None: try: pywikibot.output("Need comic number. Type 0 to skip") comic_num = int(pywikibot.input("Comic number: ")) except ValueError: pass if comic_num == 0: comic_num = '' new_text = dedent(""" == Summary == {{{{QC image|{0}|{1}}}}} == Licensing == {{{{Fairuse}}}} """.format(description, comic_num)).strip() header = header.strip() if not got_summary_from_header and len(header) > 0: new_text = header + '\n\n' + new_text footer = footer.strip() if len(footer) > 0: new_text += '\n\n' + footer # check if the edit is sensible if old_text == new_text: pywikibot.output("No changes. Nothing to do.") continue # report what will happen pywikibot.showDiff(old_text, new_text, context=3) summary = "add [[Template:QC image]]; mark as fair use " + \ "([[User:AndrybakBot#Image maintenance|Image maintenance bot task]])" if extra_summary: summary = summary + " ({})".format(extra_summary) pywikibot.output(color_format("Summary will be" + "\n\t{lightblue}{0}{default}", summary)) choice = pywikibot.input_choice( "Do you want to accept these changes?", [('Yes', 'y'), ('No', 'n'), ('open in Browser', 'b')], 'n') # if choice == 'y': # pywikibot.output("Test run, doing nothing.") # continue if choice == 'n': pywikibot.output("Okay, doing nothing.") continue elif choice == 'b': pywikibot.bot.open_webbrowser(page) elif choice == 'y': error_count = 0 while True: result = put_text(page, new_text, summary, error_count) if result is not None: pywikibot.output("Got result of saving: {}".format(result)) break error_count += 1 continue elif choice == 'q': break except pywikibot.NoPage: pywikibot.error("{} doesn't exist, skipping.".format(page.title())) continue except pywikibot.IsRedirectPage: pywikibot.error("{} is a redirect, skipping".format(page.title())) continue except pywikibot.Error as e: pywikibot.bot.suggest_help(exception=e) continue except QuitKeyboardInterrupt: sys.exit("User quit bot run.") else: pass
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 """ url = u'' description = [] keepFilename = False always = False useFilename = None verifyDescription = True aborts = set() ignorewarn = set() chunk_size = 0 chunk_size_regex = r'^-chunked(?::(\d+(?:\.\d+)?)[ \t]*(k|ki|m|mi)?b?)?$' chunk_size_regex = re.compile(chunk_size_regex, re.I) recursive = False # process all global bot args # returns a list of non-global args, i.e. args for upload.py for arg in pywikibot.handle_args(args): if arg: if arg == '-always': keepFilename = True always = True verifyDescription = False elif arg == '-recursive': recursive = True elif arg.startswith('-keep'): keepFilename = True elif arg.startswith('-filename:'): useFilename = arg[10:] elif arg.startswith('-noverify'): verifyDescription = False elif arg.startswith('-abortonwarn'): if len(arg) > len('-abortonwarn:') and aborts is not True: aborts.add(arg[len('-abortonwarn:'):]) else: aborts = True elif arg.startswith('-ignorewarn'): if len(arg) > len('-ignorewarn:') and ignorewarn is not True: ignorewarn.add(arg[len('-ignorewarn:'):]) else: ignorewarn = True elif arg.startswith('-chunked'): match = chunk_size_regex.match(arg) if match: if match.group(1): # number was in there base = float(match.group(1)) if match.group(2): # suffix too suffix = match.group(2).lower() if suffix == "k": suffix = 1000 elif suffix == "m": suffix = 1000000 elif suffix == "ki": suffix = 1 << 10 elif suffix == "mi": suffix = 1 << 20 else: pass # huh? else: suffix = 1 chunk_size = math.trunc(base * suffix) else: chunk_size = 1 << 20 # default to 1 MiB else: pywikibot.error('Chunk size parameter is not valid.') elif url == u'': url = arg else: description.append(arg) description = u' '.join(description) while not ("://" in url or os.path.exists(url)): if not url: error = 'No input filename given.' else: error = 'Invalid input filename given.' if not always: error += ' Try again.' if always: url = None break else: pywikibot.output(error) url = pywikibot.input(u'URL, file or directory where files are now:') if always and ((aborts is not True and ignorewarn is not True) or not description or url is None): additional = '' missing = [] if url is None: missing += ['filename'] additional = error + ' ' if description is None: missing += ['description'] if aborts is not True and ignorewarn is not True: additional += ('Either -ignorewarn or -abortonwarn must be ' 'defined for all codes. ') additional += 'Unable to run in -always mode' suggest_help(missing_parameters=missing, additional_text=additional) return False if os.path.isdir(url): file_list = [] for directory_info in os.walk(url): if not recursive: # Do not visit any subdirectories directory_info[1][:] = [] for dir_file in directory_info[2]: file_list.append(os.path.join(directory_info[0], dir_file)) url = file_list else: url = [url] bot = UploadRobot(url, description=description, useFilename=useFilename, keepFilename=keepFilename, verifyDescription=verifyDescription, aborts=aborts, ignoreWarning=ignorewarn, chunk_size=chunk_size, always=always) bot.run()
def main(*args: str) -> None: """ Process command line arguments and invoke bot. If args is an empty list, sys.argv is used. :param args: command line arguments """ gen = None xmlFilename = None HTTPignore = [] # Process global args and prepare generator args parser local_args = pywikibot.handle_args(args) genFactory = pagegenerators.GeneratorFactory() for arg in local_args: if arg == '-talk': config.report_dead_links_on_talk = True elif arg == '-notalk': config.report_dead_links_on_talk = False elif arg == '-repeat': gen = RepeatPageGenerator() elif arg.startswith('-ignore:'): HTTPignore.append(int(arg[8:])) elif arg.startswith('-day:'): config.weblink_dead_days = int(arg[5:]) elif arg.startswith('-xmlstart'): if len(arg) == 9: xmlStart = pywikibot.input( 'Please enter the dumped article to start with:') else: xmlStart = arg[10:] elif arg.startswith('-xml'): if len(arg) == 4: xmlFilename = i18n.input('pywikibot-enter-xml-filename') else: xmlFilename = arg[5:] else: genFactory.handle_arg(arg) if xmlFilename: try: xmlStart except NameError: xmlStart = None gen = XmlDumpPageGenerator(xmlFilename, xmlStart, genFactory.namespaces) if not gen: gen = genFactory.getCombinedGenerator() if gen: if not genFactory.nopreload: # fetch at least 240 pages simultaneously from the wiki, but more # if a high thread number is set. pageNumber = max(240, config.max_external_links * 2) gen = pagegenerators.PreloadingGenerator(gen, groupsize=pageNumber) gen = pagegenerators.RedirectFilterPageGenerator(gen) bot = WeblinkCheckerRobot(HTTPignore, config.weblink_dead_days, generator=gen) try: bot.run() except ImportError: suggest_help(missing_dependencies=('memento_client',)) return finally: waitTime = 0 # Don't wait longer than 30 seconds for threads to finish. while countLinkCheckThreads() > 0 and waitTime < 30: try: pywikibot.output('Waiting for remaining {} threads to ' 'finish, please wait...' .format(countLinkCheckThreads())) # wait 1 second time.sleep(1) waitTime += 1 except KeyboardInterrupt: pywikibot.output('Interrupted.') break if countLinkCheckThreads() > 0: pywikibot.output('Remaining {} threads will be killed.' .format(countLinkCheckThreads())) # Threads will die automatically because they are daemonic. if bot.history.reportThread: bot.history.reportThread.shutdown() # wait until the report thread is shut down; the user can # interrupt it by pressing CTRL-C. try: while bot.history.reportThread.is_alive(): time.sleep(0.1) except KeyboardInterrupt: pywikibot.output('Report thread interrupted.') bot.history.reportThread.kill() pywikibot.output('Saving history...') bot.history.save() else: suggest_help(missing_generator=True)
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) 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 perform task. If args is an empty list, sys.argv is used. @param args: command line arguments @type args: str """ # Loading the comments global categoryToCheck, project_inserted # always, define a generator to understand if the user sets one, # defining what's genFactory always = False generator = False show = False moveBlockCheck = False protectedpages = False protectType = 'edit' namespace = 0 # Process global args and prepare generator args parser local_args = pywikibot.handle_args(args) genFactory = pagegenerators.GeneratorFactory() # Process local args for arg in local_args: option, sep, value = arg.partition(':') if option == '-always': always = True elif option == '-move': moveBlockCheck = True elif option == '-show': show = True elif option in ('-protectedpages', '-moveprotected'): protectedpages = True if option == '-moveprotected': protectType = 'move' if value: namespace = int(value) else: genFactory.handleArg(arg) if config.mylang not in project_inserted: pywikibot.output('Your project is not supported by this script.\n' 'You have to edit the script and add it!') return site = pywikibot.Site() if protectedpages: generator = site.protectedpages(namespace=namespace, type=protectType) # Take the right templates to use, the category and the comment TSP = i18n.translate(site, templateSemiProtection) TTP = i18n.translate(site, templateTotalProtection) TSMP = i18n.translate(site, templateSemiMoveProtection) TTMP = i18n.translate(site, templateTotalMoveProtection) TNR = i18n.translate(site, templateNoRegex) TU = i18n.translate(site, templateUnique) categories = i18n.translate(site, categoryToCheck) commentUsed = i18n.twtranslate(site, 'blockpageschecker-summary') if not generator: generator = genFactory.getCombinedGenerator() if not generator: generator = [] pywikibot.output('Loading categories...') # Define the category if no other generator has been set for CAT in categories: cat = pywikibot.Category(site, CAT) # Define the generator gen = pagegenerators.CategorizedPageGenerator(cat) for pageCat in gen: generator.append(pageCat) pywikibot.output('Categories loaded, start!') # Main Loop if not genFactory.nopreload: generator = pagegenerators.PreloadingGenerator(generator, groupsize=60) for page in generator: pagename = page.title(as_link=True) pywikibot.output('Loading %s...' % pagename) try: text = page.text except pywikibot.NoPage: pywikibot.output("%s doesn't exist! Skipping..." % pagename) continue except pywikibot.IsRedirectPage: pywikibot.output('{} is a redirect! Skipping...'.format(pagename)) if show: showQuest(page) continue # FIXME: This check does not work : # PreloadingGenerator cannot set correctly page.editRestriction # (see bug T57322) # if not page.has_permission(): # pywikibot.output( # "%s is sysop-protected : this account can't edit " # "it! Skipping..." % pagename) # continue restrictions = page.protection() try: editRestr = restrictions['edit'] except KeyError: editRestr = None if not page.has_permission(): pywikibot.output('%s is protected: ' "this account can't edit it! Skipping..." % pagename) continue # Understand, according to the template in the page, what should be the # protection and compare it with what there really is. TemplateInThePage = understandBlock(text, TTP, TSP, TSMP, TTMP, TU) # Only to see if the text is the same or not... oldtext = text # keep track of the changes for each step (edit then move) changes = -1 if not editRestr: # page is not edit-protected # Deleting the template because the page doesn't need it. if not (TTP or TSP): raise pywikibot.Error( 'This script is not localized to use it on \n{0}. ' 'Missing "templateSemiProtection" or' '"templateTotalProtection"'.format(site.sitename)) if TU: replaceToPerform = '|'.join(TTP + TSP + TU) else: replaceToPerform = '|'.join(TTP + TSP) text, changes = re.subn( '<noinclude>(%s)</noinclude>' % replaceToPerform, '', text) if changes == 0: text, changes = re.subn('(%s)' % replaceToPerform, '', text) msg = 'The page is editable for all' if not moveBlockCheck: msg += ', deleting the template..' pywikibot.output(msg + '.') elif editRestr[0] == 'sysop': # total edit protection if (TemplateInThePage[0] == 'sysop-total' and TTP) or \ (TemplateInThePage[0] == 'unique' and TU): msg = 'The page is protected to the sysop' if not moveBlockCheck: msg += ', skipping...' pywikibot.output(msg) else: if not TNR or TU and not TNR[4] or not (TU or TNR[1]): raise pywikibot.Error( 'This script is not localized to use it on \n{0}. ' 'Missing "templateNoRegex"'.format(site.sitename)) pywikibot.output('The page is protected to the sysop, but the ' 'template seems not correct. Fixing...') if TU: text, changes = re.subn(TemplateInThePage[1], TNR[4], text) else: text, changes = re.subn(TemplateInThePage[1], TNR[1], text) elif TSP or TU: # implicitly editRestr[0] = 'autoconfirmed', edit-Semi-protection if TemplateInThePage[0] == 'autoconfirmed-total' or \ TemplateInThePage[0] == 'unique': msg = 'The page is editable only for the autoconfirmed users' if not moveBlockCheck: msg += ', skipping...' pywikibot.output(msg) else: if not TNR or TU and not TNR[4] or not (TU or TNR[1]): raise pywikibot.Error( 'This script is not localized to use it on \n{0}. ' 'Missing "templateNoRegex"'.format(site.sitename)) pywikibot.output('The page is editable only for the ' 'autoconfirmed users, but the template ' 'seems not correct. Fixing...') if TU: text, changes = re.subn(TemplateInThePage[1], TNR[4], text) else: text, changes = re.subn(TemplateInThePage[1], TNR[0], text) if changes == 0: # We tried to fix edit-protection templates, but it did not work. pywikibot.warning('No edit-protection template could be found') if moveBlockCheck and changes > -1: # checking move protection now try: moveRestr = restrictions['move'] except KeyError: moveRestr = False changes = -1 if not moveRestr: pywikibot.output('The page is movable for all, deleting the ' 'template...') # Deleting the template because the page doesn't need it. if TU: replaceToPerform = '|'.join(TSMP + TTMP + TU) else: replaceToPerform = '|'.join(TSMP + TTMP) text, changes = re.subn( '<noinclude>(%s)</noinclude>' % replaceToPerform, '', text) if changes == 0: text, changes = re.subn('({})'.format(replaceToPerform), '', text) elif moveRestr[0] == 'sysop': # move-total-protection if (TemplateInThePage[0] == 'sysop-move' and TTMP) or \ (TemplateInThePage[0] == 'unique' and TU): pywikibot.output('The page is protected from moving to ' 'the sysop, skipping...') if TU: # no changes needed, better to revert the old text. text = oldtext else: pywikibot.output('The page is protected from moving to ' 'the sysop, but the template seems not ' 'correct. Fixing...') if TU: text, changes = re.subn(TemplateInThePage[1], TNR[4], text) else: text, changes = re.subn(TemplateInThePage[1], TNR[3], text) elif TSMP or TU: # implicitly moveRestr[0] = 'autoconfirmed', # move-semi-protection if TemplateInThePage[0] == 'autoconfirmed-move' or \ TemplateInThePage[0] == 'unique': pywikibot.output('The page is movable only for the ' 'autoconfirmed users, skipping...') if TU: # no changes needed, better to revert the old text. text = oldtext else: pywikibot.output('The page is movable only for the ' 'autoconfirmed users, but the template ' 'seems not correct. Fixing...') if TU: text, changes = re.subn(TemplateInThePage[1], TNR[4], text) else: text, changes = re.subn(TemplateInThePage[1], TNR[2], text) if changes == 0: # We tried to fix move-protection templates but it did not work pywikibot.warning('No move-protection template could be found') if oldtext != text: # Ok, asking if the change has to be performed and do it if yes. pywikibot.output( color_format('\n\n>>> {lightpurple}{0}{default} <<<', page.title())) pywikibot.showDiff(oldtext, text) if not always: choice = pywikibot.input_choice( 'Do you want to accept these ' 'changes?', [('Yes', 'y'), ('No', 'n'), ('All', 'a')], 'n') if choice == 'a': always = True if always or choice == 'y': save_page(page, text, commentUsed)
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 """ options = {} gen_options = {} # what the bot should do (either resolve double redirs, or process broken # redirs) action = None namespaces = set() source = set() for arg in pywikibot.handle_args(args): arg, sep, value = arg.partition(':') option = arg.partition('-')[2] # bot options if arg == 'do': action = 'double' elif arg == 'br': action = 'broken' elif arg in ('both', 'broken', 'double'): action = arg elif option in ('always', 'delete'): options[option] = True elif option == 'sdtemplate': options['sdtemplate'] = value or pywikibot.input( 'Which speedy deletion template to use?') # generator options elif option in ('fullscan', 'moves'): gen_options[option] = True source.add(arg) elif option == 'xml': gen_options[option] = value or i18n.input( 'pywikibot-enter-xml-filename') source.add(arg) elif option == 'namespace': # "-namespace:" does NOT yield -namespace:0 further down the road! ns = value or i18n.input('pywikibot-enter-namespace-number') # TODO: at least for some generators enter a namespace by its name # or number if ns == '': ns = '0' try: ns = int(ns) except ValueError: # -namespace:all Process all namespaces. # Only works with the API read interface. pass else: namespaces.add(ns) elif option == 'offset': gen_options[option] = int(value) elif option in ('page', 'start', 'until'): gen_options[option] = value elif option in ('limit', 'total'): options['limit'] = gen_options['limit'] = int(value) if option == 'total': issue_deprecation_warning('The usage of "{0}"'.format(arg), '-limit', 2, ArgumentDeprecationWarning, since='20190120') else: pywikibot.output('Unknown argument: ' + arg) if namespaces: gen_options['namespaces'] = namespaces if len(source) > 1: problem = 'You can only use one of {0} options.'.format( ' or '.join(source)) pywikibot.bot.suggest_help(additional_text=problem, missing_action=not action) return if not action: pywikibot.bot.suggest_help(missing_action=True) else: pywikibot.Site().login() options['generator'] = RedirectGenerator(action, **gen_options) bot = RedirectRobot(action, **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 """ options = {} # what the bot should do (either resolve double redirs, or delete broken # redirs) action = None # where the bot should get his infos from (either None to load the # maintenance special page from the live wiki, or the filename of a # local XML dump file) xmlFilename = None # Which namespace should be processed when using a XML dump # default to -1 which means all namespaces will be processed namespaces = [] # at which redirect shall we start searching double redirects again # (only with dump); default to -1 which means all redirects are checked offset = -1 moved_pages = False fullscan = False start = '' until = '' number = None step = None pagename = None for arg in pywikibot.handle_args(args): if arg == 'double' or arg == 'do': action = 'double' elif arg == 'broken' or arg == 'br': action = 'broken' elif arg == 'both': action = 'both' elif arg == '-fullscan': fullscan = True elif arg.startswith('-xml'): if len(arg) == 4: xmlFilename = i18n.input('pywikibot-enter-xml-filename') else: xmlFilename = arg[5:] elif arg.startswith('-moves'): moved_pages = True elif arg.startswith('-namespace:'): ns = arg[11:] if ns == '': # "-namespace:" does NOT yield -namespace:0 further down the road! ns = i18n.input('pywikibot-enter-namespace-number') # TODO: at least for some generators enter a namespace by its name # or number if ns == '': ns = '0' try: ns = int(ns) except ValueError: # -namespace:all Process all namespaces. # Only works with the API read interface. pass if ns not in namespaces: namespaces.append(ns) elif arg.startswith('-offset:'): offset = int(arg[8:]) elif arg.startswith('-start:'): start = arg[7:] elif arg.startswith('-until:'): until = arg[7:] elif arg.startswith('-total:'): number = int(arg[7:]) elif arg.startswith('-step:'): step = int(arg[6:]) elif arg.startswith('-page:'): pagename = arg[6:] elif arg == '-always': options['always'] = True elif arg == '-delete': options['delete'] = True else: pywikibot.output(u'Unknown argument: %s' % arg) if (not action or xmlFilename and moved_pages or fullscan and xmlFilename): pywikibot.showHelp() else: pywikibot.Site().login() gen = RedirectGenerator(xmlFilename, namespaces, offset, moved_pages, fullscan, start, until, number, step, pagename) bot = RedirectRobot(action, gen, number=number, **options) bot.run()
from pywikibot.data.sparql import * def get_sources(page): wiki = pywikibot.Claim(repo, 'P143', is_reference=True) wiki.setTarget(pywikibot.ItemPage(repo, 'Q191168')) url = pywikibot.Claim(repo, 'P4656', is_reference=True) url.setTarget('https:' + page.permalink()) return [wiki, url] def tidy(value): return removeDisabledParts(str(value), site=site).strip() args = pywikibot.handle_args() site = pywikibot.Site('cs', 'wikipedia') repo = site.data_repository() image_repo = site.image_repository() genFactory = pagegenerators.GeneratorFactory(site=site) genFactory.handleArg('-ns:0') for arg in args: genFactory.handleArg(arg) generator = genFactory.getCombinedGenerator(preload=True) if not generator: genFactory.handleArg('-ref:Template:Památky v Česku') generator = genFactory.getCombinedGenerator(preload=True) pywikibot.output('Loading all identifiers...')
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 """ # If none, the var is setted only for check purpose. summary = None addText = None regexSkip = None regexSkipUrl = None always = False textfile = None talkPage = False reorderEnabled = True # Put the text above or below the text? up = False # Process global args and prepare generator args parser local_args = pywikibot.handle_args(args) genFactory = pagegenerators.GeneratorFactory() # Loading the arguments for arg in local_args: if arg.startswith('-textfile'): if len(arg) == 9: textfile = pywikibot.input( u'Which textfile do you want to add?') else: textfile = arg[10:] elif arg.startswith('-text'): if len(arg) == 5: addText = pywikibot.input(u'What text do you want to add?') else: addText = arg[6:] elif arg.startswith('-summary'): if len(arg) == 8: summary = pywikibot.input(u'What summary do you want to use?') else: summary = arg[9:] elif arg.startswith('-excepturl'): if len(arg) == 10: regexSkipUrl = pywikibot.input(u'What text should I skip?') else: regexSkipUrl = arg[11:] elif arg.startswith('-except'): if len(arg) == 7: regexSkip = pywikibot.input(u'What text should I skip?') else: regexSkip = arg[8:] elif arg == '-up': up = True elif arg == '-noreorder': reorderEnabled = False elif arg == '-always': always = True elif arg == '-talk' or arg == '-talkpage': talkPage = True else: genFactory.handleArg(arg) if textfile and not addText: with codecs.open(textfile, 'r', config.textfile_encoding) as f: addText = f.read() generator = genFactory.getCombinedGenerator() if not generator: pywikibot.showHelp() return if not addText: pywikibot.error("The text to add wasn't given.") return if talkPage: generator = pagegenerators.PageWithTalkPageGenerator(generator, True) for page in generator: (text, newtext, always) = add_text(page, addText, summary, regexSkip, regexSkipUrl, always, up, True, reorderEnabled=reorderEnabled, create=talkPage)
def main(*args): """ Process command line arguments and perform task. If args is an empty list, sys.argv is used. @param args: command line arguments @type args: unicode """ cfd_page = DEFAULT_CFD_PAGE local_args = pywikibot.handle_args(args) for arg in local_args: if arg.startswith('-page'): if len(arg) == len('-page'): cfd_page = pywikibot.input( 'Enter the CFD working page to use:') else: cfd_page = arg[len('-page:'):] page = pywikibot.Page(pywikibot.Site(), cfd_page) try: page.get() except pywikibot.NoPage: pywikibot.error( 'CFD working page "{0}" does not exist!'.format(cfd_page)) sys.exit(1) # Variable declarations day = 'None' mode = 'None' summary = '' action_summary = '' robot = None m = ReCheck() for line in page.text.split('\n'): if nobots.search(line): # NO BOTS!!! pass elif example.search(line): # Example line pass elif speedymode.search(line): mode = 'Speedy' day = 'None' elif movemode.search(line): mode = 'Move' day = 'None' elif emptymode.search(line): mode = 'Empty' day = 'None' elif deletemode.search(line): mode = 'Delete' day = 'None' elif maintenance.search(line): # It's probably best not to try to handle these in an automated # fashion. mode = 'None' day = 'None' elif m.check(dateheader, line): day = m.result.group(1) pywikibot.output('Found day header: {}'.format(day)) elif m.check(movecat, line): src = m.result.group(1) dest = m.result.group(2) thisDay = findDay(src, day) if mode == 'Move' and thisDay != 'None': summary = ( 'Robot - Moving category ' + src + ' to [[:Category:' + dest + ']] per [[WP:CFD|CFD]] at ' + thisDay + '.') action_summary = \ 'Robot - Result of [[WP:CFD|CFD]] at ' + thisDay + '.' elif mode == 'Speedy': summary = ( 'Robot - Speedily moving category ' + src + ' to [[:Category:' + dest + ']] per [[WP:CFDS|CFDS]].') action_summary = 'Robot - Speedily moved per [[WP:CFDS|CFDS]].' else: continue # If the category is redirect, we do NOT want to move articles to # it. The safest thing to do here is abort and wait for human # intervention. destpage = pywikibot.Page(page.site, dest, ns=14) if destpage.isCategoryRedirect(): summary = 'CANCELED. Destination is redirect: ' + summary pywikibot.stdout(summary) robot = None else: deletion_comment_same = ( CategoryMoveBot.DELETION_COMMENT_SAME_AS_EDIT_COMMENT) robot = CategoryMoveBot(oldcat=src, newcat=dest, batch=True, comment=summary, inplace=True, move_oldcat=True, delete_oldcat=True, deletion_comment=deletion_comment_same, move_comment=action_summary) elif m.check(deletecat, line): src = m.result.group(1) # I currently don't see any reason to handle these two cases # separately, though if are guaranteed that the category in the # "Delete" case is empty, it might be easier to call delete.py on # it. thisDay = findDay(src, day) if (mode == 'Empty' or mode == 'Delete') and thisDay != 'None': summary = ( 'Robot - Removing category {0} per [[WP:CFD|CFD]] ' 'at {1}.'.format(src, thisDay)) action_summary = \ 'Robot - Result of [[WP:CFD|CFD]] at ' + thisDay + '.' else: continue robot = CategoryMoveBot(oldcat=src, batch=True, comment=summary, deletion_comment=action_summary, inplace=True) if summary and robot is not None: pywikibot.stdout(summary) # Run, robot, run! robot.run() summary = '' robot = None
def main(*args) -> None: """ 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 """ oldName = None options = {} fromToPairs = [] # Process global args and prepare generator args parser local_args = pywikibot.handle_args(args) genFactory = pagegenerators.GeneratorFactory() local_args = genFactory.handle_args(local_args) for arg in local_args: if arg.startswith('-pairsfile'): if len(arg) == len('-pairsfile'): filename = pywikibot.input( 'Enter the name of the file containing pairs:') else: filename = arg[len('-pairsfile:'):] oldName1 = None for page in pagegenerators.TextIOPageGenerator(filename): if oldName1: fromToPairs.append([oldName1, page.title()]) oldName1 = None else: oldName1 = page.title() if oldName1: pywikibot.warning( 'file {} contains odd number of links'.format(filename)) elif arg == '-noredirect': options['noredirect'] = True elif arg == '-notalkpage': options['movetalkpage'] = False elif arg == '-always': options['always'] = True elif arg == '-skipredirects': options['skipredirects'] = True elif arg.startswith('-from:'): if oldName: pywikibot.warning('-from:{} without -to:'.format(oldName)) oldName = arg[len('-from:'):] elif arg.startswith('-to:'): if oldName: fromToPairs.append([oldName, arg[len('-to:'):]]) oldName = None else: pywikibot.warning('{} without -from'.format(arg)) elif arg.startswith('-prefix'): if len(arg) == len('-prefix'): options['prefix'] = pywikibot.input('Enter the prefix:') else: options['prefix'] = arg[8:] elif arg.startswith('-summary'): if len(arg) == len('-summary'): options['summary'] = pywikibot.input('Enter the summary:') else: options['summary'] = arg[9:] if oldName: pywikibot.warning('-from:{} without -to:'.format(oldName)) site = pywikibot.Site() if not site.logged_in(): site.login() for pair in fromToPairs: page = pywikibot.Page(site, pair[0]) bot = MovePagesBot(**options) bot.moveOne(page, pair[1]) gen = genFactory.getCombinedGenerator(preload=True) if gen: bot = MovePagesBot(generator=gen, **options) bot.run() elif not fromToPairs: pywikibot.bot.suggest_help(missing_generator=True)
def main(*args): suggestions = u'' sources = { u'Q190804': { u'collectienaam': u'Rijksmuseum', u'replacements': [(u'^(A|C)\s*(\d+)$', u'SK-\\1-\\2'), (u'^[sS][kK]\s*-?(A|C)-?\s*(\d+)$', u'SK-\\1-\\2'), (u'^cat\.(A|C)\s*(\d+)$', u'SK-\\1-\\2')], u'pageTitle': u'Wikidata:WikiProject sum of all paintings/RKD to match/Rijksmuseum', }, u'Q221092': { u'collectienaam': u'Koninklijk Kabinet van Schilderijen Mauritshuis', u'replacements': [], u'pageTitle': u'Wikidata:WikiProject sum of all paintings/RKD to match/Mauritshuis', }, u'Q1820897': { u'collectienaam': u'Amsterdam Museum', u'replacements': [ (u'^S?(A|B)\s*(\d+)$', u'S\\1 \\2'), ], u'pageTitle': u'Wikidata:WikiProject sum of all paintings/RKD to match/Amsterdam Museum', }, u'Q679527': { u'collectienaam': u'Museum Boijmans Van Beuningen', u'replacements': [ (u'^(\d+)$', u'\\1 (MK)'), ], u'pageTitle': u'Wikidata:WikiProject sum of all paintings/RKD to match/Boijmans', }, u'Q924335': { u'collectienaam': u'Stedelijk Museum Amsterdam', u'replacements': [ (u'^(\d+)$', u'A \\1'), # Switch to B at some point (u'^A(\d+)$', u'A \\1'), (u'^B(\d+)$', u'B \\1'), ], u'pageTitle': u'Wikidata:WikiProject sum of all paintings/RKD to match/Stedelijk', }, u'Q160236': { u'collectienaam': u'Metropolitan Museum of Art, The', u'replacements': [], u'pageTitle': u'Wikidata:WikiProject sum of all paintings/RKD to match/MET', }, u'Q214867': { u'collectienaam': u'National Gallery of Art (Washington)', u'replacements': [ (u'^(\d+\.\d+\.\d+)[^\d]+.+$', u'\\1'), ], u'pageTitle': u'Wikidata:WikiProject sum of all paintings/RKD to match/NGA', }, u'Q132783': { u'collectienaam': u'Hermitage', u'replacements': [ (u'^(\d+)$', u'ГЭ-\\1'), ], u'pageTitle': u'Wikidata:WikiProject sum of all paintings/RKD to match/Hermitage', }, u'Q260913': { u'collectienaam': u'Centraal Museum', u'replacements': [], u'pageTitle': u'Wikidata:WikiProject sum of all paintings/RKD to match/Centraal Museum', }, u'Q1499958': { u'collectienaam': u'Gemeentemuseum Den Haag', u'replacements': [ (u'^(\d+) / .+$', u'\\1'), # Multiple inventory numbers (u'^.+ / (\d+)$', u'\\1'), ], # And vanished from website u'pageTitle': u'Wikidata:WikiProject sum of all paintings/RKD to match/Gemeentemuseum Den Haag', }, u'Q1542668': { u'collectienaam': u'Groninger Museum', u'replacements': [], u'pageTitle': u'Wikidata:WikiProject sum of all paintings/RKD to match/Groninger Museum', }, u'Q574961': { u'collectienaam': u'Frans Halsmuseum', u'replacements': [ #(u'^(\d+[a-z]?)$', u'os I-\\1'), #(u'^(\d+-\d+[a-z]?)$', u'os I-\\1'), (u'^(I-\d+[a-z]?)$', u'os \\1'), (u'^(I-\d+-\d+[a-z]?)$', u'os \\1'), (u'^OK?S[ -]?([^\s]+)$', u'os \\1'), ], u'pageTitle': u'Wikidata:WikiProject sum of all paintings/RKD to match/Frans Halsmuseum', }, u'Q842858': { u'collectienaam': u'Nationalmuseum Stockholm', u'replacements': [], u'pageTitle': u'Wikidata:WikiProject sum of all paintings/RKD to match/Nationalmuseum', }, u'Q671384': { u'collectienaam': u'SMK - National Gallery of Denmark', u'replacements': [], #(u'^(\d+)$', u'KMS\\1'), # Mostly done, left overs manual #(u'^KMS (\d+)$', u'KMS\\1'),], u'pageTitle': u'Wikidata:WikiProject sum of all paintings/RKD to match/SMK', }, u'Q95569': { u'collectienaam': u'Kunsthistorisches Museum', u'replacements': [ (u'^(\d+)$', u'GG_\\1'), (u'^GG (\d+)$', u'GG_\\1'), ], u'pageTitle': u'Wikidata:WikiProject sum of all paintings/RKD to match/Kunsthistorisches Museum', }, u'Q160112': { u'collectienaam': u'Museo Nacional del Prado', u'replacements': [ (u'^(\d\d\d\d)$', u'P0\\1'), (u'^(\d\d\d)$', u'P00\\1'), (u'^PO? ?(\d\d\d\d)(\s*\(cat\. 2006\))?$', u'P0\\1'), ], u'pageTitle': u'Wikidata:WikiProject sum of all paintings/RKD to match/Prado', }, u'Q180788': { u'collectienaam': u'National Gallery (London)', u'replacements': [ (u'^(\d+)$', u'NG\\1'), (u'^NG (\d+)$', u'NG\\1'), ], u'pageTitle': u'Wikidata:WikiProject sum of all paintings/RKD to match/National Gallery', }, u'Q1471477': { u'collectienaam': u'Koninklijk Museum voor Schone Kunsten Antwerpen', u'replacements': [], u'pageTitle': u'Wikidata:WikiProject sum of all paintings/RKD to match/KMSKA', }, u'Q2874177': { u'collectienaam': u'Dordrechts Museum', u'replacements': [], # TODO: Add better regex u'pageTitle': u'Wikidata:WikiProject sum of all paintings/RKD to match/Dordrechts Museum', }, u'Q2098586': { u'collectienaam': u'Stedelijk Museum De Lakenhal', u'replacements': [ (u'^(\d+)$', u'S \\1'), ], u'pageTitle': u'Wikidata:WikiProject sum of all paintings/RKD to match/Lakenhal', }, u'Q2130225': { u'collectienaam': u'Het Schielandshuis', u'replacements': [], u'pageTitle': u'Wikidata:WikiProject sum of all paintings/RKD to match/Museum Rotterdam', }, u'Q224124': { u'collectienaam': u'Van Gogh Museum', u'replacements': [ (u'^F (\d+.+)$', u'F\\1'), ], # A lot of them use F numbers u'pageTitle': u'Wikidata:WikiProject sum of all paintings/RKD to match/Van Gogh Museum', }, u'Q3044768': { u'collectienaam': u'Musée du Louvre', u'replacements': [], u'pageTitle': u'Wikidata:WikiProject sum of all paintings/RKD to match/Louvre', }, u'Q154568': { u'collectienaam': u'Alte Pinakothek', u'replacements': [], u'pageTitle': u'Wikidata:WikiProject sum of all paintings/RKD to match/Alte Pinakothek', }, u'Q848313': { u'collectienaam': u'Fries Museum', u'replacements': [ (u'^(\d\d)$', u'S000\\1'), (u'^(\d\d\d)$', u'S00\\1'), (u'^(\d\d\d\d)$', u'S0\\1'), (u'^(\d.+)$', u'S\\1'), (u'^S (\d.+)$', u'S\\1'), (u'^FM (\d.+)$', u'S\\1'), ], u'pageTitle': u'Wikidata:WikiProject sum of all paintings/RKD to match/Fries Museum', }, u'Q510324': { u'collectienaam': u'Philadelphia Museum of Art - John G. Johnson Collection', u'replacements': [ (u'^(\d+)$', u'Cat. \\1'), (u'^cat\. (\d+)$', u'Cat. \\1'), ], u'pageTitle': u'Wikidata:WikiProject sum of all paintings/RKD to match/Philadelphia Museum of Art', }, u'Q1051928': { u'collectienaam': u'Kröller-Müller Museum', u'replacements': [ (u'^KM([^\s]+.+)$', u'KM \\1'), ], u'pageTitle': u'Wikidata:WikiProject sum of all paintings/RKD to match/Kröller-Müller', }, u'Q239303': { u'collectienaam': u'Art Institute of Chicago, The', u'replacements': [], u'pageTitle': u'Wikidata:WikiProject sum of all paintings/RKD to match/Art Institute of Chicago', }, u'Q1201549': { u'collectienaam': u'Detroit Institute of Arts', u'replacements': [], u'pageTitle': u'Wikidata:WikiProject sum of all paintings/RKD to match/Detroit Institute of Arts', }, u'Q49133': { u'collectienaam': u'Museum of Fine Arts Boston', u'replacements': [], u'pageTitle': u'Wikidata:WikiProject sum of all paintings/RKD to match/Museum of Fine Arts Boston', }, u'Q2365880': { u'collectienaam': u'Museum voor Schone Kunsten Gent', u'replacements': [], u'pageTitle': u'Wikidata:WikiProject sum of all paintings/RKD to match/MSK Gent', }, u'Q12013217': { u'collectienaam': u'Noordbrabants Museum', u'replacements': [], u'pageTitle': u'Wikidata:WikiProject sum of all paintings/RKD to match/Noordbrabants Museum', }, u'Q1459037': { u'collectienaam': u'Royal Collection, The', # Royal Collection - Windsor Castle u'replacements': [], u'pageTitle': u'Wikidata:WikiProject sum of all paintings/RKD to match/Royal Collection', }, u'Q153306': { u'collectienaam': u'Muzeum Narodowe w Warszawie', u'replacements': [], u'pageTitle': u'Wikidata:WikiProject sum of all paintings/RKD to match/National Museum in Warsaw', }, u'Q2051997': { u'collectienaam': u'Scottish National Gallery', u'replacements': [ (u'^(\d+)$', u'NG \\1'), ], u'pageTitle': u'Wikidata:WikiProject sum of all paintings/RKD to match/National Galleries of Scotland', }, u'Q1641836': { u'collectienaam': u'Los Angeles County Museum of Art', u'replacements': [], u'pageTitle': u'Wikidata:WikiProject sum of all paintings/RKD to match/LACMA', }, u'Q176251': { u'collectienaam': u'Museo Thyssen-Bornemisza', u'replacements': [], u'pageTitle': u'Wikidata:WikiProject sum of all paintings/RKD to match/Museo Thyssen-Bornemisza', }, u'Q2066737': { u'collectienaam': u'Instituut Collectie Nederland', u'replacements': [ (u'^(.+)\s(.+)$', u'\\1\\2'), ], # Remove the extra space u'pageTitle': u'Wikidata:WikiProject sum of all paintings/RKD to match/Instituut Collectie Nederland', }, u'Q28045660': { u'collectienaam': u'Dienst voor \'s Rijks Verspreide Kunstvoorwerpen', u'replacements': [ (u'^(.+)\s(.+)$', u'\\1\\2'), ], # Remove the extra space u'pageTitle': u'Wikidata:WikiProject sum of all paintings/RKD to match/Dienst Verspreide Rijkscollecties', }, u'Q28045665': { u'collectienaam': u'Stichting Nederlands Kunstbezit', u'replacements': [ #(u'^(\d+)$', u'NK\\1'), (u'^S?N[kK] (\d+)$', u'NK\\1'), ], u'pageTitle': u'Wikidata:WikiProject sum of all paintings/RKD to match/Stichting Nederlands Kunstbezit', }, u'Q28045674': { u'collectienaam': u'Rijksdienst Beeldende Kunst', u'replacements': [ (u'^(.+)\s(.+)$', u'\\1\\2'), ], # Remove the extra space u'pageTitle': u'Wikidata:WikiProject sum of all paintings/RKD to match/Rijksdienst Beeldende Kunst', }, u'Q1053735': { u'collectienaam': u'Central Collecting Point', u'replacements': [], u'pageTitle': u'Wikidata:WikiProject sum of all paintings/RKD to match/Munich Central Collecting Point', }, u'Q1241163': { u'collectienaam': u'Dulwich Picture Gallery', u'replacements': [ (u'^DPG\s?(\d\d)$', u'DPG0\\1'), (u'^DPG (\d+)$', u'DPG\\1'), (u'^(\d\d)$', u'DPG0\\1'), (u'^(\d+)$', u'DPG\\1'), ], u'pageTitle': u'Wikidata:WikiProject sum of all paintings/RKD to match/Dulwich Picture Gallery', }, u'Q18600731': { u'collectienaam': u'Instituut Collectie Nederland', #u'Rijksdienst voor het Cultureel Erfgoed', u'replacements': [ (u'^(.+)\s(.+)$', u'\\1\\2'), ], # Remove the extra space u'pageTitle': u'Wikidata:WikiProject sum of all paintings/RKD to match/Rijksdienst voor het Cultureel Erfgoed', }, u'Q2324618': { u'collectienaam': u'Staatliches Museum Schwerin', u'replacements': [], u'pageTitle': u'Wikidata:WikiProject sum of all paintings/RKD to match/Staatliches Museum Schwerin', }, u'Q2284748': { u'collectienaam': u'Goudstikker, Jacques', u'replacements': [], u'pageTitle': u'Wikidata:WikiProject sum of all paintings/RKD to match/Jacques Goudstikker collection', }, u'Q28065304': { u'collectienaam': u'Goudstikker, erven Jacques', u'replacements': [], u'pageTitle': u'Wikidata:WikiProject sum of all paintings/RKD to match/Goudstikker heirs collection', }, u'Q28097342': { u'collectienaam': u'Hoop, Adriaan van der', u'replacements': [], u'pageTitle': u'Wikidata:WikiProject sum of all paintings/RKD to match/Adriaan van der Hoop collection', }, u'Q51252': { u'collectienaam': u'Uffizi, Galleria degli', u'replacements': [], u'pageTitle': u'Wikidata:WikiProject sum of all paintings/RKD to match/Uffizi', }, u'Q1954426': { u'collectienaam': u'Museum Catharijneconvent', u'replacements': [], u'pageTitle': u'Wikidata:WikiProject sum of all paintings/RKD to match/Museum Catharijneconvent', }, u'Q892727': { u'collectienaam': u'Bonnefantenmuseum', u'replacements': [], u'pageTitle': u'Wikidata:WikiProject sum of all paintings/RKD to match/Bonnefantenmuseum', }, u'Q281903': { u'collectienaam': u'Stedelijk Museum Het Prinsenhof', u'replacements': [], u'pageTitle': u'Wikidata:WikiProject sum of all paintings/RKD to match/Prinsenhof', }, u'Q1976985': { u'collectienaam': u'Nelson-Atkins Museum of Art, The', u'replacements': [], u'pageTitle': u'Wikidata:WikiProject sum of all paintings/RKD to match/Nelson-Atkins Museum of Art', }, u'Q731126': { u'collectienaam': u'J. Paul Getty Museum, The', u'replacements': [], u'pageTitle': u'Wikidata:WikiProject sum of all paintings/RKD to match/J. Paul Getty Museum', }, u'Q2628596': { u'collectienaam': u'Palais des Beaux-Arts de Lille', u'replacements': [], u'pageTitle': u'Wikidata:WikiProject sum of all paintings/RKD to match/Palais des Beaux-Arts de Lille', }, u'Q1948674': { u'collectienaam': u'Groeningemuseum', u'replacements': [], u'pageTitle': u'Wikidata:WikiProject sum of all paintings/RKD to match/Groeningemuseum', }, u'Q430682': { u'collectienaam': u'Tate Gallery', u'replacements': [], u'pageTitle': u'Wikidata:WikiProject sum of all paintings/RKD to match/Tate', }, u'Q1565911': { u'collectienaam': u'Museum of Fine Arts Houston', u'replacements': [], u'pageTitle': u'Wikidata:WikiProject sum of all paintings/RKD to match/Museum of Fine Arts Houston', }, u'Q474563': { u'collectienaam': u'Teylers Museum', u'replacements': [], u'pageTitle': u'Wikidata:WikiProject sum of all paintings/RKD to match/Teylers Museum', }, u'Q657415': { u'collectienaam': u'Cleveland Museum of Art, The', u'replacements': [], u'pageTitle': u'Wikidata:WikiProject sum of all paintings/RKD to match/Cleveland Museum of Art', }, u'Q866498': { u'collectienaam': u'Galleria Palatina (Palazzo Pitti)', u'replacements': [], u'pageTitle': u'Wikidata:WikiProject sum of all paintings/RKD to match/Galleria Palatina', }, u'Q1700481': { u'collectienaam': u'Minneapolis Institute of Arts', u'replacements': [], u'pageTitle': u'Wikidata:WikiProject sum of all paintings/RKD to match/Minneapolis Institute of Arts', }, u'Q238587': { u'collectienaam': u'National Portrait Gallery', u'replacements': [], u'pageTitle': u'Wikidata:WikiProject sum of all paintings/RKD to match/National Portrait Gallery', }, u'Q23402': { u'collectienaam': u'Musée d\'Orsay', u'replacements': [], u'pageTitle': u'Wikidata:WikiProject sum of all paintings/RKD to match/Musée d\'Orsay', }, u'Q1416890': { u'collectienaam': u'Fine Arts Museums of San Francisco', u'replacements': [], u'pageTitle': u'Wikidata:WikiProject sum of all paintings/RKD to match/Fine Arts Museums of San Francisco', }, u'Q1117704': { u'collectienaam': u'Indianapolis Museum of Art', u'replacements': [], u'pageTitle': u'Wikidata:WikiProject sum of all paintings/RKD to match/Indianapolis Museum of Art', }, u'Q2970522': { u'collectienaam': u'Cincinnati Art Museum', u'replacements': [], u'pageTitle': u'Wikidata:WikiProject sum of all paintings/RKD to match/Cincinnati Art Museum', }, u'Q210081': { u'collectienaam': u'Walters Art Museum', u'replacements': [], u'pageTitle': u'Wikidata:WikiProject sum of all paintings/RKD to match/Walters Art Museum', }, #u'Q768717' : { u'collectienaam' : u'Private collection', # Probably still too big # u'replacements' : [], # u'pageTitle' : u'Wikidata:WikiProject sum of all paintings/RKD to match/Private collection', # }, } collectionid = None autoadd = 0 for arg in pywikibot.handle_args(args): if arg.startswith('-collectionid:'): if len(arg) == 14: collectionid = pywikibot.input( u'Please enter the collectionid you want to work on:') else: collectionid = arg[14:] elif arg.startswith('-autoadd:'): if len(arg) == 9: autoadd = int( pywikibot.input( u'Please enter the number of items you want to update automatically:' )) else: autoadd = int(arg[9:]) if collectionid: if collectionid not in sources.keys(): pywikibot.output(u'%s is not a valid collectionid!' % (collectionid, )) return worksources = [ collectionid, ] else: worksources = sources.keys() workstatistics = [] for collectionid in worksources: collectionstats = processCollection( collectionid, sources[collectionid][u'collectienaam'], sources[collectionid][u'replacements'], sources[collectionid][u'pageTitle'], autoadd, ) workstatistics.append(collectionstats) if len(workstatistics) > 1: publishStatistics(workstatistics)
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 """ index = None djvu_path = '.' # default djvu file directory pages = '1-' options = {} # Parse command line arguments. local_args = pywikibot.handle_args(args) for arg in local_args: if arg.startswith('-index:'): index = arg[7:] elif arg.startswith('-djvu:'): djvu_path = arg[len('-djvu:'):] elif arg.startswith('-pages:'): pages = arg[7:] elif arg.startswith('-summary:'): options['summary'] = arg[len('-summary:'):] elif arg == '-force': options['force'] = True elif arg == '-always': options['always'] = True else: pywikibot.output('Unknown argument %s' % arg) # index is mandatory. if not index: pywikibot.bot.suggest_help(missing_parameters=['-index']) return False # If djvu_path is not a fle, build djvu_path from dir+index. djvu_path = os.path.expanduser(djvu_path) djvu_path = os.path.abspath(djvu_path) if not os.path.exists(djvu_path): pywikibot.error('No such file or directory: %s' % djvu_path) return False if os.path.isdir(djvu_path): djvu_path = os.path.join(djvu_path, index) # Check the djvu file exists and, if so, create the DjVuFile wrapper. djvu = DjVuFile(djvu_path) if not djvu.has_text(): pywikibot.error('No text layer in djvu file %s' % djvu.file_djvu) return False # Parse pages param. pages = pages.split(',') for interval in range(len(pages)): start, sep, end = pages[interval].partition('-') start = 1 if not start else int(start) if not sep: end = start else: end = int(end) if end else djvu.number_of_images() pages[interval] = (start, end) site = pywikibot.Site() if not site.has_extension('ProofreadPage'): pywikibot.error('Site %s must have ProofreadPage extension.' % site) return False index_page = pywikibot.Page(site, index, ns=site.proofread_index_ns) if not index_page.exists(): raise pywikibot.NoPage(index) pywikibot.output('uploading text from %s to %s' % (djvu.file_djvu, index_page.title(asLink=True))) bot = DjVuTextBot(djvu, index_page, pages, **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: str """ options = {} message_properties = {} protections = {} default_level = 'sysop' default_summaries = { 'cat': 'category', 'links': 'links', 'ref': 'ref', 'imageused': 'images', 'file': 'simple', } # read command line parameters local_args = pywikibot.handle_args(args) site = pywikibot.Site() generator_type = None protection_levels = site.protection_levels() if '' in protection_levels: protection_levels.add('all') protection_types = site.protection_types() gen_factory = pagegenerators.GeneratorFactory() for arg in local_args: option, sep, value = arg.partition(':') if not option.startswith('-'): continue option = option[1:] if option == 'always': options[option] = True elif option == 'summary': options[option] = value or None elif option == 'expiry': options[option] = value or pywikibot.input( 'Enter a protection expiry:') elif option == 'unprotect': default_level = 'all' elif option == 'default': default_level = value if sep else 'sysop' elif option in protection_types and value: protections[option] = value else: if not gen_factory.handle_arg(arg): raise ValueError('Unknown parameter "{}"'.format(arg)) if value: message_properties.update({'cat': value, 'page': value}) if 'summary' not in options: generator_type = option if generator_type in default_summaries: message_type = default_summaries[generator_type] if message_type == 'simple' or message_properties: if default_level == 'all': options['summary'] = i18n.twtranslate( site, 'unprotect-{}'.format(message_type), message_properties) else: options['summary'] = i18n.twtranslate( site, 'protect-{}'.format(message_type), message_properties) generator = gen_factory.getCombinedGenerator() # We are just protecting pages, so we have no need of using a preloading # page generator to actually get the text of those pages. if generator: default_level = check_protection_level('Default level', default_level, protection_levels) # set the default value for all # None (not the string 'none') will be ignored by Site.protect() combined_protections = { p_type: default_level for p_type in protection_types } for p_type, level in protections.items(): level = check_protection_level(p_type, level, protection_levels, default_level) # '' is equivalent to 'all' if level in ('none', ''): level = 'all' combined_protections[p_type] = level if not options.get('summary'): options['summary'] = pywikibot.input( 'Enter a reason for the protection change:') bot = ProtectionRobot(combined_protections, generator=generator, **options) bot.run() else: pywikibot.bot.suggest_help(missing_generator=True)
def main(*args): """ Process command line arguments and perform task. If args is an empty list, sys.argv is used. @param args: command line arguments @type args: list of unicode """ always = False namespaces = [] spamSite = '' protocol = 'http' summary = None for arg in pywikibot.handle_args(args): if arg == "-always": always = True elif arg.startswith('-namespace:'): try: namespaces.append(int(arg[len('-namespace:'):])) except ValueError: namespaces.append(arg[len('-namespace:'):]) elif arg.startswith('-protocol:'): protocol = arg.partition(':')[2] elif arg.startswith('-summary:'): summary = arg.partition(':')[2] else: spamSite = arg if not spamSite: pywikibot.bot.suggest_help(missing_parameters=['spam site']) return False mysite = pywikibot.Site() pages = mysite.exturlusage(spamSite, protocol=protocol, namespaces=namespaces, content=True) if not summary: summary = i18n.twtranslate(mysite, 'spamremove-remove', {'url': spamSite}) for i, p in enumerate(pages, 1): text = p.text if spamSite not in text: continue # Show the title of the page we're working on. # Highlight the title in purple. pywikibot.output( color_format('\n\n>>> {lightpurple}{0}{default} <<<', p.title())) lines = text.split('\n') newpage = [] lastok = "" for line in lines: if spamSite in line: if lastok: pywikibot.output(lastok) pywikibot.output(color_format('{lightred}{0}{default}', line)) lastok = None else: newpage.append(line) if line.strip(): if lastok is None: pywikibot.output(line) lastok = line if always: answer = "y" else: answer = pywikibot.input_choice(u'\nDelete the red lines?', [('yes', 'y'), ('no', 'n'), ('edit', 'e')], 'n', automatic_quit=False) if answer == "n": continue elif answer == "e": editor = TextEditor() newtext = editor.edit(text, highlight=spamSite, jumpIndex=text.find(spamSite)) else: newtext = "\n".join(newpage) if newtext != text: p.text = newtext p.save(summary) else: if "i" not in locals(): pywikibot.output('No page found.') elif i == 1: pywikibot.output('1 pages done.') else: pywikibot.output('%d pages done.' % i)
def main(*args): """Process command line arguments and perform task.""" # imagerecat.initLists() photoset = u'' # public (popular photos), full (all photos), user ID number start_id = u'' end_id = u'' addCategory = u'' autonomous = False totalPhotos = 0 uploadedPhotos = 0 # Do we mark the images as reviewed right away? if config.panoramio['review']: panoramioreview = config.panoramio['review'] else: panoramioreview = False # Set the Panoramio reviewer if config.panoramio['reviewer']: reviewer = config.panoramio['reviewer'] elif 'commons' in config.sysopnames['commons']: reviewer = config.sysopnames['commons']['commons'] elif 'commons' in config.usernames['commons']: reviewer = config.usernames['commons']['commons'] else: reviewer = u'' # Should be renamed to overrideLicense or something like that override = u'' local_args = pywikibot.handle_args(args) for arg in local_args: if arg.startswith('-set'): if len(arg) == 4: photoset = pywikibot.input(u'What is the set?') else: photoset = arg[5:] elif arg.startswith('-start_id'): if len(arg) == 9: start_id = pywikibot.input( u'What is the id of the photo you want to start at?') else: start_id = arg[10:] elif arg.startswith('-end_id'): if len(arg) == 7: end_id = pywikibot.input( u'What is the id of the photo you want to end at?') else: end_id = arg[8:] elif arg == '-panoramioreview': panoramioreview = True elif arg.startswith('-reviewer'): if len(arg) == 9: reviewer = pywikibot.input(u'Who is the reviewer?') else: reviewer = arg[10:] elif arg.startswith('-override'): if len(arg) == 9: override = pywikibot.input(u'What is the override text?') else: override = arg[10:] elif arg.startswith('-addcategory'): if len(arg) == 12: addCategory = pywikibot.input( u'What category do you want to add?') else: addCategory = arg[13:] elif arg == '-autonomous': autonomous = True if photoset: site = pywikibot.Site() if site != pywikibot.Site('commons', 'commons'): pywikibot.warning( 'Using {0} instead of Wikimedia Commons'.format(site)) for photoInfo in getPhotos(photoset, start_id, end_id): photoInfo = getLicense(photoInfo) # time.sleep(10) uploadedPhotos += processPhoto(photoInfo, panoramioreview, reviewer, override, addCategory, autonomous, site=site) totalPhotos += 1 else: usage() pywikibot.output(u'Finished running') pywikibot.output(u'Total photos: ' + str(totalPhotos)) pywikibot.output(u'Uploaded photos: ' + str(uploadedPhotos))
for code in codes: if code not in obsolete: site = pywikibot.Site(code, family) # page title does not care futures.add(executor.submit(pywikibot.Page, site, 'Main page')) wait(futures) pywikibot.output(msg.format(family, ' completed.')) def preload_families(families, worker): """Preload all sites of all given family files.""" start = datetime.now() with ThreadPoolExecutor(worker) as executor: futures = { executor.submit(preload_family, family, executor) for family in families } wait(futures) pywikibot.output('Loading time used: {}'.format(datetime.now() - start)) if __name__ == '__main__': fam = set() worker = None for arg in pywikibot.handle_args(): if arg in families_list: fam.add(arg) elif arg.startswith('-worker'): worker = int(arg.partition(':')[2]) preload_families(fam or families_list, worker)
def main(*args): """Process command line arguments and invoke bot.""" always = False category = '' delete_after_done = False # Load a lot of default generators local_args = pywikibot.handle_args(args) genFactory = pagegenerators.GeneratorFactory() for arg in local_args: if arg == '-always': always = True elif arg.startswith('-setcat:'): category = arg[len('-setcat:'):] elif arg == '-delete': delete_after_done = True else: genFactory.handleArg(arg) pregenerator = genFactory.getCombinedGenerator(preload=True) if not pregenerator: pywikibot.bot.suggest_help(missing_generator=True) return False load_global_archivo() for page in pregenerator: skip = False if page.exists() and page.namespace() == 6 \ and not page.isRedirectPage(): imagepage = pywikibot.FilePage(page.site, page.title()) # First do autoskip. if doiskip(imagepage.get()): pywikibot.output('Skipping ' + page.title()) skip = True else: # The first upload is last in the list. try: username = imagepage.getLatestUploader()[0] except NotImplementedError: # No API, using the page file instead (datetime, username, resolution, size, comment) = imagepage.getFileVersionHistory().pop() if always: newname = imagepage.title(with_ns=False) CommonsPage = pywikibot.Page(pywikibot.Site('commons', 'commons'), 'File:' + newname) if CommonsPage.exists(): skip = True else: while True: # Do the TkdialogIC to accept/reject and change te name newname, skip = TkdialogIC( imagepage.title(with_ns=False), imagepage.get(), username, imagepage.permalink(with_protocol=True), imagepage.templates()).getnewname() if skip: pywikibot.output('Skipping this image') break # Did we enter a new name? if len(newname) == 0: # Take the old name newname = imagepage.title(with_ns=False) else: newname = newname.decode('utf-8') # Check if the image already exists CommonsPage = pywikibot.Page( imagepage.site.image_repository(), 'File:' + newname) if not CommonsPage.exists(): break else: pywikibot.output( 'Image already exists, pick another name or ' 'skip this image') # We dont overwrite images, pick another name, go to # the start of the loop if not skip: imageTransfer(imagepage, newname, category, delete_after_done).start() pywikibot.output('Still ' + str(threading.activeCount()) + ' active threads, lets wait') for openthread in threading.enumerate(): if openthread != threading.currentThread(): openthread.join() pywikibot.output('All threads are done')
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 """ for arg in pywikibot.handle_args(args): arg, sep, val = arg.partition(':') if arg == '-edit': globalvar.attachEditCount = int(val or pywikibot.input( 'After how many edits would you like to welcome new users? ' '(0 is allowed)')) elif arg == '-timeoffset': globalvar.timeoffset = int(val or pywikibot.input( 'Which time offset (in minutes) for new users would you like ' 'to use?')) elif arg == '-time': globalvar.timeRecur = int(val or pywikibot.input( 'For how many seconds would you like to bot to sleep before ' 'checking again?')) elif arg == '-offset': if not val: val = pywikibot.input( 'Which time offset for new users would you like to use? ' '(yyyymmddhhmmss)') try: globalvar.offset = pywikibot.Timestamp.fromtimestampformat(val) except ValueError: # upon request, we could check for software version here raise ValueError( "Mediawiki has changed, -offset:# is not supported " "anymore, but -offset:TIMESTAMP is, assuming TIMESTAMP " "is yyyymmddhhmmss. -timeoffset is now also supported. " "Please read this script source header for documentation.") elif arg == '-file': globalvar.randomSign = True globalvar.signFileName = val or pywikibot.input( 'Where have you saved your signatures?') elif arg == '-sign': globalvar.defaultSign = val or pywikibot.input( 'Which signature to use?') globalvar.defaultSign += timeselected elif arg == '-break': globalvar.recursive = False elif arg == '-nlog': globalvar.makeWelcomeLog = False elif arg == '-ask': globalvar.confirm = True elif arg == '-filter': globalvar.filtBadName = True elif arg == '-savedata': globalvar.saveSignIndex = True elif arg == '-random': globalvar.randomSign = True elif arg == '-sul': globalvar.welcomeAuto = True elif arg == '-limit': globalvar.queryLimit = int(val or pywikibot.input( u'How many of the latest new users would you like to load?')) elif arg == '-numberlog': globalvar.dumpToLog = int(val or pywikibot.input( 'After how many welcomed users would you like to update the ' 'welcome log?')) elif arg == '-quiet': globalvar.quiet = True elif arg == '-quick': issue_deprecation_warning('The usage of "-quick" option', None, 2, since='20160211') # Filename and Pywikibot path # file where is stored the random signature index filename = pywikibot.config.datafilepath( 'welcome-%s-%s.data' % (pywikibot.Site().family.name, pywikibot.Site().code)) if globalvar.offset and globalvar.timeoffset: pywikibot.warning( 'both -offset and -timeoffset were provided, ignoring -offset') globalvar.offset = 0 try: bot = WelcomeBot() except KeyError as error: # site not managed by welcome.py pywikibot.bot.suggest_help(exception=error) return False try: bot.run() except KeyboardInterrupt: if bot.welcomed_users: showStatus() pywikibot.output("Put welcomed users before quit...") bot.makelogpage(bot.welcomed_users) pywikibot.output("\nQuitting...") finally: # If there is the savedata, the script must save the number_user. if globalvar.randomSign and globalvar.saveSignIndex and \ bot.welcomed_users: if sys.version_info[0] > 2: import pickle as cPickle else: import cPickle with open(filename, 'wb') as f: cPickle.dump(bot.welcomed_users, f, protocol=config.pickle_protocol)
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 """ url = '' description = [] summary = None keep_filename = False always = False use_filename = None filename_prefix = None verify_description = True aborts = set() ignorewarn = set() chunk_size = 0 chunk_size_regex = re.compile( r'^-chunked(?::(\d+(?:\.\d+)?)[ \t]*(k|ki|m|mi)?b?)?$', re.I) recursive = False description_file = None # process all global bot args # returns a list of non-global args, i.e. args for upload.py local_args = pywikibot.handle_args(args) for option in local_args: arg, _, value = option.partition(':') if arg == '-always': keep_filename = True always = True verify_description = False elif arg == '-recursive': recursive = True elif arg == '-keep': keep_filename = True elif arg == '-filename': use_filename = value elif arg == '-prefix': filename_prefix = value elif arg == '-summary': summary = value elif arg == '-noverify': verify_description = False elif arg == '-abortonwarn': if value and aborts is not True: aborts.add(value) else: aborts = True elif arg == '-ignorewarn': if value and ignorewarn is not True: ignorewarn.add(value) else: ignorewarn = True elif arg == '-chunked': match = chunk_size_regex.match(option) chunk_size = get_chunk_size(match) elif arg == '-descfile': description_file = value elif not url: url = option else: description.append(option) description = ' '.join(description) if description_file: if description: pywikibot.error('Both a description and a -descfile were ' 'provided. Please specify only one of those.') return False with codecs.open(description_file, encoding=pywikibot.config.textfile_encoding) as f: description = f.read().replace('\r\n', '\n') while not ('://' in url or os.path.exists(url)): if not url: error = 'No input filename given.' else: error = 'Invalid input filename given.' if not always: error += ' Try again.' if always: url = None break else: pywikibot.output(error) url = pywikibot.input('URL, file or directory where files are now:') if always and (aborts is not True and ignorewarn is not True or not description or url is None): additional = '' missing = [] if url is None: missing += ['filename'] additional = error + ' ' if description is None: missing += ['description'] if aborts is not True and ignorewarn is not True: additional += ('Either -ignorewarn or -abortonwarn must be ' 'defined for all codes. ') additional += 'Unable to run in -always mode' suggest_help(missing_parameters=missing, additional_text=additional) return False if os.path.isdir(url): file_list = [] for directory_info in os.walk(url): if not recursive: # Do not visit any subdirectories directory_info[1][:] = [] for dir_file in directory_info[2]: file_list.append(os.path.join(directory_info[0], dir_file)) url = file_list else: url = [url] bot = UploadRobot(url, description=description, useFilename=use_filename, keepFilename=keep_filename, verifyDescription=verify_description, aborts=aborts, ignoreWarning=ignorewarn, chunk_size=chunk_size, always=always, summary=summary, filename_prefix=filename_prefix) bot.run()
def main(*args): collectioncode = None dryrun = False create = False for arg in pywikibot.handle_args(args): if arg.startswith('-collectioncode:'): if len(arg) == 16: collectioncode = pywikibot.input( 'Please enter the collectioncode you want to work on:') else: collectioncode = arg[16:] elif arg.startswith('-dry'): dryrun = True elif arg.startswith('-create'): create = True painting_types = ['Gemälde', 'Malerei', 'Malerei/Gemälde'] collections = { 'GGFremdbesitz': { 'name': 'Gemäldegalerie - owned my someone else', 'collectioncode': 'GGFremdbesitz', 'collectionqid': 'Q165631', 'collectionshort': 'GG', 'locationqid': 'Q165631', }, # 129, not owned by Gemäldegalerie 'GGLeihnahmen': { 'name': 'Gemäldegalerie - loans', 'collectioncode': 'GGLeihnahmen', 'collectionqid': 'Q165631', 'collectionshort': 'GG', 'locationqid': 'Q165631', }, # 67, loans at the Gemäldegalerie 'GGMalerei': { 'name': 'Gemäldegalerie - main collection', 'collectioncode': 'GGMalerei', 'collectionqid': 'Q165631', 'collectionshort': 'GG', 'locationqid': 'Q165631', }, # 2737, main collection Gemäldegalerie 'GGVerlust': { 'name': 'Gemäldegalerie - lost works', 'collectioncode': 'GGVerlust', 'collectionqid': 'Q165631', 'collectionshort': 'GG', 'locationqid': 'Q165631', }, # 561, works lost by Gemäldegalerie mostly in 1945 'NGAlteNationalgalerie': { 'name': 'Alte Nationalgalerie - main collection', 'collectioncode': 'NGAlteNationalgalerie', 'collectionqid': 'Q162111', 'collectionshort': 'NG', 'locationqid': 'Q162111', }, # 1900, main collection Alte Nationalgalerie 'NGHamburgerBahnhofMuseumfurGegenwart': { 'name': 'Hamburger Bahnhof - main collection', 'collectioncode': 'NGHamburgerBahnhofMuseumfurGegenwart', 'collectionqid': 'Q584756', 'collectionshort': 'NGHB', 'locationqid': 'Q584756', }, # 155, only small part of the collection? 'NGMuseumBerggruen': { 'name': 'Berggruen Museum', 'collectioncode': 'NGMuseumBerggruen', 'collectionqid': 'Q641630', 'collectionshort': 'Berggruen', 'locationqid': 'Q641630', }, # 45, Berggruen Museum 'NGNeueNationalgalerie': { 'name': 'Neue Nationalgalerie - main collection', 'collectioncode': 'NGNeueNationalgalerie', 'collectionqid': 'Q32659772', 'collectionshort': 'NNG', 'locationqid': 'Q32659772', }, # 1795, main collection Neue Nationalgalerie 'NGSammlungScharfGerstenberg': { 'name': 'Scharf-Gerstenberg Collection', 'collectioncode': 'NGSammlungScharfGerstenberg', 'collectionqid': 'Q322010', 'collectionshort': 'NGSG', 'locationqid': 'Q322010', }, # NGSammlungScharfGerstenberg # 26, # NGehemSammlung # 1201, former collection (Alte) Nationalgalerie? It's a mess and missing inventory numbers } if dryrun and not collectioncode: i = 0 bereich = {} objekttyp = {} for work in getWorksFromCsv(): if work.get('objekttyp'): if work.get('objekttyp') not in objekttyp: objekttyp[work.get('objekttyp')] = 0 objekttyp[work.get('objekttyp')] += 1 if work.get('objekttyp') in painting_types: i += 1 if work.get('bereich'): if work.get('bereich') not in bereich: bereich[work.get('bereich')] = 0 bereich[work.get('bereich')] += 1 if work.get('bereich') == 'NGAlteNationalgalerie': for field in work: print('* %s - %s' % (field, work.get(field))) #print (work) #time.sleep(1) print(i) for objekttypname in sorted(objekttyp): print('* %s - %s' % (objekttypname, objekttyp.get(objekttypname))) for beriechname in sorted(bereich): print('* %s - %s' % (beriechname, bereich.get(beriechname))) #print (bereich) return if collectioncode: if collectioncode not in collections.keys(): pywikibot.output('%s is not a valid collectioncode!' % (collectioncode, )) return processCollection(collections[collectioncode], dryrun=dryrun, create=create) else: collectionlist = list(collections.keys()) random.shuffle(collectionlist) # Different order every time we run for collectioncode in collectionlist: processCollection(collections[collectioncode], dryrun=dryrun, create=create)
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()
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 """ template_names = [] 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() gen_factory = 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( "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 gen_factory.handleArg(arg): template_name = pywikibot.Page(site, arg, ns=10) template_names.append(template_name.title(with_ns=False)) if not template_names: pywikibot.bot.suggest_help(missing_parameters=['templates']) return False if options.get('subst', False) ^ options.get('remove', False): for template_name in template_names: templates[template_name] = None else: try: for i in range(0, len(template_names), 2): templates[template_names[i]] = template_names[i + 1] except IndexError: pywikibot.output('Unless using solely -subst or -remove, ' 'you must give an even number of template names.') return old_templates = [] for template_name in templates.keys(): old_template = pywikibot.Page(site, template_name, ns=10) old_templates.append(old_template) if xmlfilename: builder = textlib._MultiTemplateMatchBuilder(site) predicate = builder.search_any_predicate(old_templates) gen = XMLDumpPageGenerator( xmlfilename, site=site, text_predicate=predicate) else: gen = gen_factory.getCombinedGenerator() if not gen: gens = ( pagegenerators.ReferringPageGenerator(t, onlyTemplateInclusion=True) for t in old_templates ) gen = chain(*gens) gen = filter_unique(gen, key=lambda p: '{}:{}:{}'.format(*p._cmpkey())) if user: gen = pagegenerators.UserEditFilterGenerator(gen, user, timestamp, skip, max_revision_depth=100, show_filtered=True) if not gen_factory.gens: # make sure that proper namespace filtering etc. is handled gen = gen_factory.getCombinedGenerator(gen) if not gen_factory.nopreload: gen = pagegenerators.PreloadingGenerator(gen) bot = TemplateRobot(gen, templates, site=site, **options) bot.run()
def main(*args) -> None: """ 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 """ password = None logall = False logout = False oauth = False autocreate = False unknown_args = [] for arg in pywikibot.handle_args(args): if arg.startswith('-pass'): if len(arg) == 5: password = pywikibot.input( 'Password for all accounts (no characters will be shown):', password=True) else: password = arg[6:] elif arg == '-all': logall = True elif arg == '-force': pywikibot.output('To force a re-login, please delete the ' "revelant lines from '{0}' (or the entire file) " 'and try again.'.format( join(config.base_dir, 'pywikibot.lwp'))) elif arg == '-logout': logout = True elif arg == '-oauth': oauth = True elif arg == '-autocreate': autocreate = True else: unknown_args += [arg] if pywikibot.bot.suggest_help(unknown_parameters=unknown_args): return if password is not None: pywikibot.warning('The -pass argument is not implemented yet. See: ' 'https://phabricator.wikimedia.org/T102477') if logall: namedict = config.usernames else: site = pywikibot.Site() namedict = {site.family.name: {site.code: None}} for family_name in namedict: for lang in namedict[family_name]: try: site = pywikibot.Site(code=lang, fam=family_name) if oauth: _oauth_login(site) continue if logout: site.logout() else: site.login(autocreate=autocreate) user = site.user() if user: pywikibot.output('Logged in on {0} as {1}.'.format( site, user)) else: if logout: pywikibot.output('Logged out of {0}.'.format(site)) else: pywikibot.output('Not logged in on {0}.'.format(site)) except SiteDefinitionError: pywikibot.output('{0}.{1} is not a valid site, ' 'please remove it from your config'.format( lang, family_name))
def handle_args(args): """Process command line arguments. If args is an empty list, sys.argv is used. @param args: command line arguments @type args: str """ mapping = { # option: (attribute, value), '-break': ('recursive', False), '-nlog': ('makeWelcomeLog', False), '-ask': ('confirm', True), '-filter': ('filtBadName', True), '-savedata': ('saveSignIndex', True), '-random': ('randomSign', True), '-sul': ('welcomeAuto', True), '-quiet': ('quiet', True), } for arg in pywikibot.handle_args(args): arg, _, val = arg.partition(':') if arg == '-edit': globalvar.attachEditCount = int( val if val.isdigit() else pywikibot.input( 'After how many edits would you like to welcome new users?' ' (0 is allowed)')) elif arg == '-timeoffset': globalvar.timeoffset = int( val if val.isdigit() else pywikibot. input('Which time offset (in minutes) for new users would you ' 'like to use?')) elif arg == '-time': globalvar.timeRecur = int( val if val.isdigit() else pywikibot. input('For how many seconds would you like to bot to sleep ' 'before checking again?')) elif arg == '-offset': _handle_offset(val) elif arg == '-file': globalvar.randomSign = True globalvar.signFileName = val or pywikibot.input( 'Where have you saved your signatures?') elif arg == '-sign': globalvar.defaultSign = val or pywikibot.input( 'Which signature to use?') globalvar.defaultSign += timeselected elif arg == '-limit': globalvar.queryLimit = int( val if val.isdigit() else pywikibot. input('How many of the latest new users would you like to ' 'load?')) elif arg == '-numberlog': globalvar.dumpToLog = int( val if val.isdigit() else pywikibot. input('After how many welcomed users would you like to update ' 'the welcome log?')) elif arg in mapping: setattr(globalvar, *mapping[arg]) else: pywikibot.warning('Unknown option "{}"'.format(arg))
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 """ index = None pages = '1-' options = {} # Parse command line arguments. local_args = pywikibot.handle_args(args) for arg in local_args: arg, sep, value = arg.partition(':') if arg == '-index': index = value elif arg == '-pages': pages = value elif arg == '-showdiff': options['showdiff'] = True elif arg == '-summary': options['summary'] = value elif arg == '-ocr': options['ocr'] = value or 'phetools' elif arg == '-threads': options['threads'] = int(value) elif arg == '-force': options['force'] = True elif arg == '-always': options['always'] = True else: pywikibot.output('Unknown argument ' + arg) # index is mandatory. if not index: pywikibot.bot.suggest_help(missing_parameters=['-index']) return # '-force' can be used with '-ocr' only. if 'force' in options and 'ocr' not in options: pywikibot.error("'-force' can be used with '-ocr' option only.") return site = pywikibot.Site() if not site.has_extension('ProofreadPage'): pywikibot.error( 'Site {} must have ProofreadPage extension.'.format(site)) return index = IndexPage(site, index) if not index.exists(): pywikibot.error("Page {} doesn't exist.".format(index)) return # Parse pages param. # Create a list of (start, end) tuples. pages = pages.split(',') for interval in range(len(pages)): start, sep, end = pages[interval].partition('-') start = 1 if not start else int(start) if not sep: end = start else: end = int(end) if end else index.num_pages pages[interval] = (start, end) # gen yields ProofreadPage objects. gen_list = [] for start, end in sorted(pages): gen = index.page_gen(start=start, end=end, filter_ql=[1], content=True) gen_list.append(gen) gen = itertools.chain(*gen_list) pywikibot.output('\nUploading text to {}\n'.format( index.title(as_link=True))) bot = UploadTextBot(gen, site=index.site, **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 """ filename = None pagename = None namespace = None salt = '' force = False calc = None args = [] def if_arg_value(arg, name): if arg.startswith(name): yield arg[len(name) + 1:] for arg in pywikibot.handle_args(args): for v in if_arg_value(arg, '-file'): filename = v for v in if_arg_value(arg, '-locale'): # Required for english month names locale.setlocale(locale.LC_TIME, v.encode('utf8')) for v in if_arg_value(arg, '-timezone'): os.environ['TZ'] = v.timezone # Or use the preset value if hasattr(time, 'tzset'): time.tzset() for v in if_arg_value(arg, '-calc'): calc = v for v in if_arg_value(arg, '-salt'): salt = v for v in if_arg_value(arg, '-force'): force = True for v in if_arg_value(arg, '-filename'): filename = v for v in if_arg_value(arg, '-page'): pagename = v for v in if_arg_value(arg, '-namespace'): namespace = v if not arg.startswith('-'): args.append(arg) site = pywikibot.Site() if calc: if not salt: pywikibot.bot.suggest_help(missing_parameters=['-salt']) return False page = pywikibot.Page(site, calc) if page.exists(): calc = page.title() else: pywikibot.output( u'NOTE: the specified page "%s" does not (yet) exist.' % calc) pywikibot.output('key = %s' % calc_md5_hexdigest(calc, salt)) return if not args: pywikibot.bot.suggest_help( additional_text='No template was specified.') return False for a in args: pagelist = [] a = pywikibot.Page(site, a, ns=10).title() if not filename and not pagename: if namespace is not None: ns = [str(namespace)] else: ns = [] for pg in generate_transclusions(site, a, ns): pagelist.append(pg) if filename: for pg in open(filename, 'r').readlines(): pagelist.append(pywikibot.Page(site, pg, ns=10)) if pagename: pagelist.append(pywikibot.Page(site, pagename, ns=3)) pagelist = sorted(pagelist) for pg in iter(pagelist): pywikibot.output(u'Processing %s' % pg) # Catching exceptions, so that errors in one page do not bail out # the entire process try: archiver = PageArchiver(pg, a, salt, force) archiver.run() except ArchiveBotSiteConfigError as e: # no stack trace for errors originated by pages on-site pywikibot.error( 'Missing or malformed template in page %s: %s' % (pg, e)) except Exception: pywikibot.error(u'Error occurred while processing page %s' % pg) pywikibot.exception(tb=True)
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 """ # the option that's always selected when the bot wonders what to do with # a link. If it's None, the user is prompted (default behaviour). always = None alternatives = [] getAlternatives = True dnSkip = False generator = None pageTitle = None primary = False main_only = False # For sorting the linked pages, case can be ignored minimum = 0 local_args = pywikibot.handle_args(args) for arg in local_args: if arg.startswith('-primary:'): primary = True getAlternatives = False alternatives.append(arg[9:]) elif arg == '-primary': primary = True elif arg.startswith('-always:'): always = arg[8:] elif arg.startswith('-file'): if len(arg) == 5: generator = pagegenerators.TextfilePageGenerator( filename=None) else: generator = pagegenerators.TextfilePageGenerator( filename=arg[6:]) elif arg.startswith('-pos:'): if arg[5] != ':': mysite = pywikibot.Site() page = pywikibot.Page(pywikibot.Link(arg[5:], mysite)) if page.exists(): alternatives.append(page.title()) else: if pywikibot.input_yn( u'Possibility %s does not actually exist. Use it ' 'anyway?' % page.title(), default=False, automatic_quit=False): alternatives.append(page.title()) else: alternatives.append(arg[5:]) elif arg == '-just': getAlternatives = False elif arg == '-dnskip': dnSkip = True elif arg == '-main': main_only = True elif arg.startswith('-min:'): minimum = int(arg[5:]) elif arg.startswith('-start'): try: generator = pagegenerators.CategorizedPageGenerator( pywikibot.Site().disambcategory(), start=arg[7:], namespaces=[0]) except pywikibot.NoPage: pywikibot.output("Disambiguation category for your wiki is not known.") raise elif not pageTitle: pageTitle = arg site = pywikibot.Site() if pageTitle: page = pywikibot.Page(pywikibot.Link(pageTitle, site)) generator = iter([page]) if not generator: pywikibot.bot.suggest_help(missing_generator=True) return False site.login() bot = DisambiguationRobot(always, alternatives, getAlternatives, dnSkip, generator, primary, main_only, minimum=minimum) 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: str """ options = {} message_properties = {} generator = None protections = {} default_level = 'sysop' default_summaries = { 'cat': 'category', 'links': 'links', 'ref': 'ref', 'imageused': 'images', 'file': 'simple', } # read command line parameters local_args = pywikibot.handle_args(args) gen_factory = pagegenerators.GeneratorFactory() site = pywikibot.Site() generator_type = None protection_levels = set(site.protection_levels()) protection_types = site.protection_types() if '' in protection_levels: protection_levels.add('all') for arg in local_args: if arg == '-always': options['always'] = True elif arg.startswith('-summary'): if len(arg) == len('-summary'): # fill dummy value to prevent automatic generation options['summary'] = None else: options['summary'] = arg[len('-summary:'):] elif arg.startswith('-expiry'): if len(arg) == len('-expiry'): options['expiry'] = pywikibot.input( 'Enter a protection expiry:') else: options['expiry'] = arg[len('-expiry:'):] elif arg.startswith('-images'): issue_deprecation_warning('-image', '-imagelinks', 2, ArgumentDeprecationWarning, since='20140213') local_args.append('-imagelinks' + arg[7:]) elif arg.startswith('-unprotect'): default_level = 'all' elif arg.startswith('-default'): if len(arg) == len('-default'): default_level = 'sysop' else: default_level = arg[len('-default:'):] else: is_p_type = False if arg.startswith('-'): delimiter = arg.find(':') if delimiter > 0: p_type_arg = arg[1:delimiter] level = arg[delimiter + 1:] if p_type_arg in protection_types: protections[p_type_arg] = level is_p_type = True if not is_p_type: if not gen_factory.handleArg(arg): raise ValueError('Unknown parameter "{0}"'.format(arg)) found = arg.find(':') if found: message_properties.update({'cat': arg[found + 1:], 'page': arg[found + 1:]}) if 'summary' not in options: generator_type = arg[1:found] if found > 0 else arg[1:] if generator_type in default_summaries: message_type = default_summaries[generator_type] if message_type == 'simple' or message_properties: if default_level == 'all': options['summary'] = i18n.twtranslate( site, 'unprotect-{0}'.format(message_type), message_properties) else: options['summary'] = i18n.twtranslate( site, 'protect-{0}'.format(message_type), message_properties) generator = gen_factory.getCombinedGenerator() # We are just protecting pages, so we have no need of using a preloading # page generator to actually get the text of those pages. if generator: if default_level: default_level = check_protection_level('Default level', default_level, protection_levels) # set the default value for all # None (not the string 'none') will be ignored by Site.protect() combined_protections = {p_type: default_level for p_type in protection_types} for p_type, level in protections.items(): level = check_protection_level(p_type, level, protection_levels, default_level) # '' is equivalent to 'all' if level == 'none' or level == '': level = 'all' combined_protections[p_type] = level if not options.get('summary'): options['summary'] = pywikibot.input( 'Enter a reason for the protection change:') bot = ProtectionRobot(generator, combined_protections, site, **options) bot.run() return True else: pywikibot.bot.suggest_help(missing_generator=True) return False
def main(*args): """Process command line arguments and invoke PatrolBot.""" # This factory is responsible for processing command line arguments # that are also used by other scripts and that determine on which pages # to work on. usercontribs = None gen = None recentchanges = False newpages = False repeat = False gen_factory = pagegenerators.GeneratorFactory() options = {} # Parse command line arguments for arg in pywikibot.handle_args(args): if arg.startswith('-ask'): options['ask'] = True elif arg.startswith('-autopatroluserns'): options['autopatroluserns'] = True elif arg.startswith('-repeat'): repeat = True elif arg.startswith('-newpages'): newpages = True elif arg.startswith('-recentchanges'): recentchanges = True elif arg.startswith('-usercontribs:'): usercontribs = arg[14:] elif arg.startswith('-versionchecktime:'): versionchecktime = arg[len('-versionchecktime:'):] options['versionchecktime'] = int(versionchecktime) elif arg.startswith("-whitelist:"): options['whitelist'] = arg[len('-whitelist:'):] else: generator = gen_factory.handleArg(arg) if not generator: if ':' in arg: m = arg.split(':') options[m[0]] = m[1] site = pywikibot.Site() site.login() if usercontribs: user = pywikibot.User(site, usercontribs) if user.isAnonymous() or user.isRegistered(): pywikibot.output('Processing user: {}'.format(usercontribs)) else: pywikibot.warning('User {} does not exist on site {}.'.format( usercontribs, site)) # default behaviour if not any((newpages, recentchanges, usercontribs)): if site.family.name == 'wikipedia': newpages = True else: recentchanges = True bot = PatrolBot(**options) if newpages or usercontribs: pywikibot.output(u'Newpages:') gen = site.newpages feed = api_feed_repeater(gen, delay=60, repeat=repeat, user=usercontribs, namespaces=gen_factory.namespaces, recent_new_gen=False) bot.run(feed) if recentchanges or usercontribs: pywikibot.output(u'Recentchanges:') gen = site.recentchanges feed = api_feed_repeater(gen, delay=60, repeat=repeat, namespaces=gen_factory.namespaces, user=usercontribs) bot.run(feed) pywikibot.output(u'%d/%d patrolled' % (bot.patrol_counter, bot.rc_item_counter))