def main(): opts = {} for arg in pywikibot.handleArgs(): if arg.startswith('-hours:'): opts['hours'] = float(arg[7:]) opts['no_repeat'] = False elif arg.startswith('-delay:'): opts['delay'] = int(arg[7:]) elif arg.startswith('-page'): if len(arg) == 5: opts['page'] = pywikibot.input( u'Which page do you want to change?') else: opts['page'] = arg[6:] elif arg.startswith('-text'): if len(arg) == 5: opts['text'] = pywikibot.input( u'What text do you want to substitute?') else: opts['text'] = arg[6:] elif arg == '-user': opts['user'] = True elif arg.startswith('-summary'): if len(arg) == len('-summary'): opts['summary'] = pywikibot.input(u'Enter the summary:') else: opts['summary'] = arg[9:] else: pywikibot.showHelp('clean_sandbox') return bot = SandboxBot(**opts) bot.run()
def main(): genFactory = pagegenerators.GeneratorFactory() pageName = '' summary = None always = False undelete = False generator = None # read command line parameters local_args = pywikibot.handleArgs() mysite = pywikibot.Site() for arg in local_args: if arg == '-always': always = True elif arg.startswith('-summary'): if len(arg) == len('-summary'): summary = pywikibot.input(u'Enter a reason for the deletion:') else: summary = arg[len('-summary:'):] elif arg.startswith('-images'): pywikibot.output('\n\03{lightred}-image option is deprecated. ' 'Please use -imageused instead.\03{default}\n') local_args.append('-imageused' + arg[7:]) elif arg.startswith('-undelete'): undelete = True else: genFactory.handleArg(arg) found = arg.find(':') + 1 if found: pageName = arg[found:] if not summary: if pageName: if arg.startswith('-cat') or arg.startswith('-subcats'): summary = i18n.twtranslate(mysite, 'delete-from-category', {'page': pageName}) elif arg.startswith('-links'): summary = i18n.twtranslate(mysite, 'delete-linked-pages', {'page': pageName}) elif arg.startswith('-ref'): summary = i18n.twtranslate(mysite, 'delete-referring-pages', {'page': pageName}) elif arg.startswith('-imageused'): summary = i18n.twtranslate(mysite, 'delete-images', {'page': pageName}) elif arg.startswith('-file'): summary = i18n.twtranslate(mysite, 'delete-from-file') generator = genFactory.getCombinedGenerator() # We are just deleting pages, so we have no need of using a preloading # page generator to actually get the text of those pages. if generator: if summary is None: summary = pywikibot.input(u'Enter a reason for the %sdeletion:' % ['', 'un'][undelete]) bot = DeletionRobot(generator, summary, always, undelete) bot.run() else: # Show help text from the top of this file pywikibot.showHelp()
def main(*args): # bot_class=InfoboxMigratingBot options = {} local_args = pywikibot.handle_args(args) genFactory = pagegenerators.GeneratorFactory() for arg in local_args: if genFactory.handleArg(arg): continue if arg.startswith('-'): arg, sep, value = arg.partition(':') if value != '': options[arg[1:]] = value if not value.isdigit() else int(value) else: options[arg[1:]] = True while not options.get('template', None): options['template'] = pywikibot.input( 'Enter the template you would like to work on:') options['new_template'] = pywikibot.input( 'Enter the template to replace the previous one:') if not options.get('new_template', None): options['new_template'] = options['template'] generator = genFactory.getCombinedGenerator() if not generator: genFactory.handleArg('-transcludes:%s' % options['template']) generator = genFactory.getCombinedGenerator() bot = InfoboxMigratingBot(generator=generator, **options) # bot_class bot.run()
def main(args): ''' Main loop. ''' start_id = 3 end_id = 18473 single_id = 0 site = pywikibot.getSite('commons', 'commons') for arg in pywikibot.handleArgs(): if arg.startswith('-start_id'): if len(arg) == 9: start_id = pywikibot.input(u'What is the id of the search page 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 search page you want to end at?') else: end_id = arg[8:] elif arg.startswith('-id'): if len(arg) == 3: single_id = pywikibot.input(u'What is the id of the search page you want to transfer?') else: single_id = arg[4:] if single_id > 0: start_id = single_id end_id = int(single_id) + 1 reader = HABSReader(start=int(start_id), end=int(end_id)) bot = data_ingestion.DataIngestionBot(reader, "%(item_title)s - LOC - %(resources_id)s.%(_ext)s", "HABS data ingestion layout", pywikibot.getSite('commons', 'commons')) bot.run()
def main(): options = {} local_args = pywikibot.handleArgs() genFactory = pagegenerators.GeneratorFactory() site = pywikibot.Site() for arg in local_args: if arg.startswith('-enable'): if len(arg) == 7: options['enablePage'] = pywikibot.input( u'Would you like to check if the bot should run or not?') else: options['enablePage'] = arg[8:] elif arg.startswith('-disambig'): if len(arg) == 9: options['disambigPage'] = pywikibot.input( u'In which page should the bot save the disambig pages?') else: options['disambigPage'] = arg[10:] elif arg == '-always': options['always'] = True else: genFactory.handleArg(arg) generator = genFactory.getCombinedGenerator() # If the generator is not given, use the default one if not generator: generator = site.lonelypages(total=genFactory.limit) bot = LonelyPagesBot(generator, **options) bot.run()
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(): lang=pywikibot.getSite().language() start = None # Loading the arguments for arg in pywikibot.handleArgs(): if arg.startswith('-lang'): if len(arg) == 5: lang = pywikibot.input(u'What language do you want to use?') else: lang = arg[6:] elif arg.startswith('-start'): if len(arg) == 6: start = pywikibot.input(u'What article do you want to start with?') else: start = arg[7:] bot = o2wVillageData() # bot.tl2Dict(bot.extractTemplate(u"""{{Infocaseta Așezare # | nume = Bogdănești # | alt_nume = # | tip_asezare = Sat # | imagine = # | imagine_dimensiune = 250px # | imagine_descriere = Bogdănești # | stemă = # | hartă = # | pushpin_map = # | pushpin_label_position = right # | tip_subdiviziune = Țară # | nume_subdiviziune = {{ROU}} # | tip_subdiviziune1 = [[Județele României|Județ]] # | nume_subdiviziune1 = [[județul Vaslui|Vaslui]] # | tip_subdiviziune3 = [[Comunele României|Comună]] # | nume_subdiviziune3 = [[Comuna Bogdănești, Vaslui|Bogdănești]] # | titlu_atestare = Prima atestare # | atestare = # | suprafață_totală_km2 = # | altitudine = # | latd = 46 # | latm = 26 # | lats = 58 # | latNS = N # | longd = 27 # | longm = 43 # | longs = 36 # | longEV = E # | recensământ = 2002 # | populație = # | populație_note_subsol = # | tip_cod_poștal = [[Cod poștal]] # | codpoștal = # | camp_gol_nume = # | camp_gol_info = # }} # '''Bogdănești''' este o localitate în [[județul Vaslui]], [[Moldova]], [[România]] # """, u"Infocaseta Așezare")) # print bot._dict bot.putCoordOnWiki(lang, start)
def main(): xmlFilename = None always = False ignorepdf = False limit = None namespaces = [] generator = None summary = None # Process global args and prepare generator args parser local_args = pywikibot.handleArgs() genFactory = pagegenerators.GeneratorFactory() for arg in local_args: if arg.startswith('-namespace:'): try: namespaces.append(int(arg[11:])) except ValueError: namespaces.append(arg[11:]) elif arg.startswith('-summary:'): summary = arg[9:] elif arg == '-always': always = True elif arg == '-ignorepdf': ignorepdf = True elif arg.startswith('-limit:'): limit = int(arg[7:]) elif arg.startswith('-xmlstart'): if len(arg) == 9: xmlStart = pywikibot.input( u'Please enter the dumped article to start with:') else: xmlStart = arg[10:] elif arg.startswith('-xml'): if len(arg) == 4: xmlFilename = pywikibot.input( u'Please enter the XML dump\'s filename:') else: xmlFilename = arg[5:] else: genFactory.handleArg(arg) if xmlFilename: try: xmlStart except NameError: xmlStart = None generator = XmlDumpPageGenerator(xmlFilename, xmlStart, namespaces) if not generator: generator = genFactory.getCombinedGenerator() if not generator: # syntax error, show help text from the top of this file pywikibot.showHelp() return generator = pagegenerators.PreloadingGenerator(generator, step=50) generator = pagegenerators.RedirectFilterPageGenerator(generator) bot = ReferencesRobot(generator, always, limit, ignorepdf, summary) 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 """ xmlFilename = None xmlStart = None options = {} generator = None # Process global args and prepare generator args parser local_args = pywikibot.handle_args(args) genFactory = pagegenerators.GeneratorFactory() for arg in local_args: if arg.startswith('-summary:'): options['summary'] = arg[9:] elif arg == '-always': options['always'] = True elif arg == '-ignorepdf': options['ignorepdf'] = True elif arg.startswith('-limit:'): options['limit'] = int(arg[7:]) elif arg.startswith('-xmlstart'): if len(arg) == 9: xmlStart = pywikibot.input( u'Please enter the dumped article to start with:') else: xmlStart = arg[10:] elif arg.startswith('-xml'): if len(arg) == 4: xmlFilename = pywikibot.input( u'Please enter the XML dump\'s filename:') else: xmlFilename = arg[5:] else: genFactory.handleArg(arg) if xmlFilename: generator = XmlDumpPageGenerator(xmlFilename, xmlStart, genFactory.namespaces) if not generator: generator = genFactory.getCombinedGenerator() if not generator: pywikibot.bot.suggest_help(missing_generator=True) return False if not genFactory.nopreload: generator = pagegenerators.PreloadingGenerator(generator) generator = pagegenerators.RedirectFilterPageGenerator(generator) bot = ReferencesRobot(generator, **options) bot.run() return True
def main(): # if -file is not used, this temporary array is used to read the page title. pageTitle = [] page = None gen = None interwiki = False keep_name = False targetLang = None targetFamily = None for arg in pywikibot.handleArgs(): if arg == '-interwiki': interwiki = True elif arg.startswith('-keepname'): keep_name = True elif arg.startswith('-tolang:'): targetLang = arg[8:] elif arg.startswith('-tofamily:'): targetFamily = arg[10:] elif arg.startswith('-file'): if len(arg) == 5: filename = pywikibot.input( u'Please enter the list\'s filename: ') else: filename = arg[6:] gen = pagegenerators.TextfilePageGenerator(filename) else: pageTitle.append(arg) if not gen: # if the page title is given as a command line argument, # connect the title's parts with spaces if pageTitle != []: pageTitle = ' '.join(pageTitle) page = pywikibot.Page(pywikibot.getSite(), pageTitle) # if no page title was given as an argument, and none was # read from a file, query the user if not page: pageTitle = pywikibot.input(u'Which page to check:') page = pywikibot.Page(pywikibot.getSite(), pageTitle) # generator which will yield only a single Page gen = iter([page]) if not targetLang and not targetFamily: targetSite = pywikibot.getSite('commons', 'commons') else: if not targetLang: targetLang = pywikibot.getSite().language if not targetFamily: targetFamily = pywikibot.getSite().family targetSite = pywikibot.getSite(targetLang, targetFamily) bot = ImageTransferBot(gen, interwiki=interwiki, targetSite=targetSite, keep_name=keep_name) bot.run()
def main(*args): """ Main function. Grab a generator and pass it to the bot to work on """ series = None report = None for arg in pywikibot.handle_args(args): if arg.startswith('-series:'): if len(arg) == 8: series = pywikibot.input( u'Please enter the Q id of the series to work on:') else: series = arg[8:] elif arg.startswith('-report:'): if len(arg) == 8: report = pywikibot.input( u'Please enter the name of the page to report on:') else: report = arg[8:] basequery = u"""SELECT DISTINCT ?item WHERE { ?item wdt:P31 wd:Q21191270 . ?item wdt:P179 wd:%s . MINUS { ?item wdt:P345 [] . ?item wdt:P1191 []} #{ ?item wdt:P155 ?otheritem } UNION { ?item wdt:P156 ?otheritem } #?otheritem wdt:P345 [] . }""" repo = pywikibot.Site().data_repository() if series: query = basequery % (series,) generator = pagegenerators.PreloadingItemGenerator(pagegenerators.WikidataSPARQLPageGenerator(query, site=repo)) imdbFinderBot = IMDBFinderBot(generator, series) imdbFinderBot.run() else: seriesquery = u"""SELECT DISTINCT ?item WHERE { ?episode wdt:P31 wd:Q21191270 . ?episode wdt:P179 ?item . MINUS { ?episode wdt:P345 [] . ?item wdt:P1191 []} { ?episode wdt:P155 ?otheritem } UNION { ?episode wdt:P156 ?otheritem } ?otheritem wdt:P345 [] . ?otheritem wdt:P179 ?item . }""" seriesgen = pagegenerators.PreloadingItemGenerator(pagegenerators.WikidataSPARQLPageGenerator(seriesquery, site=repo)) for seriespage in seriesgen: series = seriespage.title() query = basequery % (series,) generator = pagegenerators.PreloadingItemGenerator(pagegenerators.WikidataSPARQLPageGenerator(query, site=repo)) imdbFinderBot = IMDBFinderBot(generator, series) imdbFinderBot.run()
def matcher(): match1 = wikipedia.input(" Rakitra 1:") match2 = wikipedia.input(" Rakitra 2:") outfile= wikipedia.input(" Rakitra avoaka:") infile1 = file('%s.txt'%match1,'r').readlines() infile2 = file('%s.txt'%match2,'r').read() outfile = file('%s.txt'%outfile,'a') for line in infile1: if infile2.find(line)!=-1: infile2 = infile2.replace('%s\n'%line,'') outfile.write(infile2)
def askForCaptcha(self, url): """Show the user a CAPTCHA image and return the answer.""" try: import webbrowser pywikibot.output(u'Opening CAPTCHA in your web browser...') webbrowser.open(url) return pywikibot.input( u'What is the solution of the CAPTCHA that is shown in your web browser?') except: pywikibot.output(u'Error in opening web browser: %s' % sys.exc_info()[0]) return pywikibot.input( u'What is the solution of the CAPTCHA at %s ?' % url)
def askForCaptcha(self, url): """Show the user a CAPTCHA image and return the answer.""" try: import webbrowser pywikibot.output("Opening CAPTCHA in your web browser...") if webbrowser.open(url): return pywikibot.input("What is the solution of the CAPTCHA that is shown in " "your web browser?") else: raise except: pywikibot.output("Error in opening web browser: %s" % sys.exc_info()[0]) pywikibot.output("Please copy this url to your web browser and open it:\n %s" % url) return pywikibot.input("What is the solution of the CAPTCHA at this url ?")
def main(*args): """ Process command line arguments and invoke bot. If args is an empty list, sys.argv is used. @param args: command line arguments @type args: list of unicode """ options = {} # Process global arguments to determine desired site local_args = pywikibot.handle_args(args) # 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. genFactory = pagegenerators.GeneratorFactory() # Parse command line arguments for arg in local_args: # Catch the pagegenerators options if genFactory.handleArg(arg): continue # nothing to do here # Now pick up your own options arg, sep, value = arg.partition(':') option = arg[1:] if option in ('summary', 'text'): if not value: pywikibot.input('Please enter a value for ' + arg) options[option] = value # take the remaining options as booleans. # You will get a hint if they aren't pre-definded in your bot class else: options[option] = True gen = genFactory.getCombinedGenerator() if gen: # The preloading generator is responsible for downloading multiple # pages from the wiki simultaneously. gen = pagegenerators.PreloadingGenerator(gen) # pass generator and private options to the bot bot = BasicBot(gen, **options) bot.run() # guess what it does return True else: pywikibot.bot.suggest_help(missing_generator=True) return False
def main(*args): """Extracting file page information of images to work on and initiate matching.""" images = [] other_family = u'' other_lang = u'' imagePageA = None imagePageB = None # Read commandline parameters. local_args = pywikibot.handle_args(args) for arg in local_args: if arg.startswith('-otherfamily:'): if len(arg) == len('-otherfamily:'): other_family = pywikibot.input(u'What family do you want to use?') else: other_family = arg[len('-otherfamily:'):] elif arg.startswith('-otherlang:'): if len(arg) == len('-otherlang:'): other_lang = pywikibot.input(u'What language do you want to use?') else: other_lang = arg[len('otherlang:'):] else: images.append(arg) if len(images) != 2: pywikibot.showHelp('match_images') pywikibot.error('Require two images to work on.') return else: pass imagePageA = pywikibot.page.FilePage(pywikibot.Site(), images[0]) if other_lang: if other_family: imagePageB = pywikibot.page.FilePage(pywikibot.Site( other_lang, other_family), images[1]) else: imagePageB = pywikibot.page.FilePage(pywikibot.Site( other_lang), images[1]) else: imagePageB = pywikibot.page.FilePage(pywikibot.Site(), images[1]) match_image_pages(imagePageA, imagePageB)
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 """ opts = {} unknown_args = [] local_args = pywikibot.handle_args(args) for arg in local_args: option, sep, value = arg.partition(':') if option.startswith('-'): option = option[1:] if option == 'filename': opts[option] = value or pywikibot.input( 'Enter the filename: ') continue elif option == 'storepath': opts[option] = os.path.abspath(value) or pywikibot.input( 'Enter the store path: ') continue elif option == 'revision': opts[option] = value or pywikibot.input( 'Enter the revision of the dump: ') continue unknown_args += [arg] missing = [] if 'filename' not in opts: missing += ['-filename'] if missing or unknown_args: pywikibot.bot.suggest_help(missing_parameters=missing, unknown_parameters=unknown_args) return 1 site = pywikibot.Site() opts['wikiname'] = site.dbName() bot = DownloadDumpBot(**opts) bot.run() return 0
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 """ password = None sysop = False logall = False logout = False for arg in pywikibot.handle_args(args): if arg.startswith("-pass"): if len(arg) == 5: password = pywikibot.input(u'Password for all accounts (no characters will be shown):', password=True) else: password = arg[6:] elif arg == "-sysop": sysop = True elif arg == "-all": logall = True elif arg == "-force": pywikibot.output(u"To force a re-login, please delete the revelant " u"lines from '%s' (or the entire file) and try again." % join(config.base_dir, 'pywikibot.lwp')) elif arg == "-logout": logout = True else: pywikibot.showHelp('login') return if logall: if sysop: namedict = config.sysopnames else: namedict = config.usernames else: site = pywikibot.Site() namedict = {site.family.name: {site.code: None}} for familyName in namedict: for lang in namedict[familyName]: try: site = pywikibot.Site(code=lang, fam=familyName) if logout: site.logout() else: site.login(sysop) user = site.user() if user: pywikibot.output(u"Logged in on %(site)s as %(user)s." % locals()) else: if logout: pywikibot.output(u"Logged out of %(site)s." % locals()) else: pywikibot.output(u"Not logged in on %(site)s." % locals()) except SiteDefinitionError: pywikibot.output(u'%s.%s is not a valid site, please remove it' u' from your config' % (lang, familyName))
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 setpage(self): """Sets page and page title""" site = pywikibot.getSite() pageTitle = self.options.page or pywikibot.input(u"Page to edit:") self.page = pywikibot.Page(pywikibot.Link(pageTitle, site)) if not self.options.edit_redirect and self.page.isRedirectPage(): self.page = self.page.getRedirectTarget()
def login(self, retry=False): if not self.password: # As we don't want the password to appear on the screen, we set # password = True self.password = pywikibot.input( u'Password for user %(name)s on %(site)s (no characters will ' u'be shown):' % {'name': self.username, 'site': self.site}, password=True) # self.password = self.password.encode(self.site.encoding()) pywikibot.output(u"Logging in to %(site)s as %(name)s" % {'name': self.username, 'site': self.site}) try: cookiedata = self.getCookie() except pywikibot.data.api.APIError as e: pywikibot.error(u"Login failed (%s)." % e.code) if retry: self.password = None return self.login(retry=True) else: return False self.storecookiedata(cookiedata) pywikibot.log(u"Should be logged in now") ## # Show a warning according to the local bot policy ## FIXME: disabled due to recursion; need to move this to the Site object after ## login ## if not self.botAllowed(): ## logger.error( ## u"Username '%(name)s' is not listed on [[%(page)s]]." ## % {'name': self.username, ## 'page': botList[self.site.family.name][self.site.code]}) ## logger.error( ##"Please make sure you are allowed to use the robot before actually using it!") ## return False return True
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 treat(self, page): self.current_page = page try: text = page.get() for author in self.authors: if re.search(r'(\b%s\b)' % re.escape(author), text): if self.authors[author] == author: text = text.replace( author, u'{{AutoreCitato|%s}}' % author) else: text = text.replace( author, u'{{AutoreCitato|%s|%s}}' % (self.authors[author], author)) if page.text != text: if self.auto: page.text = text page.save(summary) else: pywikibot.showDiff(page.text, text) choice = pywikibot.input('Agree? [Y]es, [N]o') if choice.lower() == 'y': page.text = text page.save(summary) except pywikibot.NoPage: pywikibot.output(u"Page %s does not exist?!" % page.title(asLink=True)) except pywikibot.IsRedirectPage: pywikibot.output(u"Page %s is a redirect; skipping." % page.title(asLink=True)) except pywikibot.LockedPage: pywikibot.output(u"Page %s is locked?!" % page.title(asLink=True))
def main(): genFactory = pagegenerators.GeneratorFactory() commandline_arguments = list() templateTitle = u'' for arg in pywikibot.handleArgs(): if arg.startswith('-template'): if len(arg) == 9: templateTitle = pywikibot.input( u'Please enter the template to work on:') else: templateTitle = arg[10:] elif genFactory.handleArg(arg): continue else: commandline_arguments.append(arg) if len(commandline_arguments) % 2 or not templateTitle: raise ValueError # or something. fields = dict() for i in xrange(0, len(commandline_arguments), 2): fields[commandline_arguments[i]] = commandline_arguments[i + 1] if templateTitle: gen = pagegenerators.ReferringPageGenerator( pywikibot.Page(pywikibot.getSite(), "Template:%s" % templateTitle), onlyTemplateInclusion=True) else: gen = genFactory.getCombinedGenerator() if not gen: # TODO: Build a transcluding generator based on templateTitle return bot = HarvestRobot(gen, templateTitle, fields) bot.run()
def main(): gen = pg.GeneratorFactory() commandline_arguments = list() templateTitle = u'' for arg in pywikibot.handleArgs(): if arg.startswith('-template'): if len(arg) == 9: templateTitle = pywikibot.input( u'Please enter the template to work on:') else: templateTitle = arg[10:] elif gen.handleArg(arg): continue else: commandline_arguments.append(arg) if len(commandline_arguments) % 2 or not templateTitle: raise ValueError # or something. fields = dict() for i in xrange(0, len(commandline_arguments), 2): fields[commandline_arguments[i]] = commandline_arguments[i + 1] generator = gen.getCombinedGenerator() if not generator: # TODO: Build a transcluding generator based on templateTitle return bot = HarvestRobot(generator, templateTitle, fields) 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 choosecats(pagetext): chosen = [] done = False length = 1000 # TODO: → input_choice pywikibot.output("""Give the new categories, one per line. Empty line: if the first, don't change. Otherwise: Ready. -: I made a mistake, let me start over. ?: Give the text of the page with GUI. ??: Give the text of the page in console. xx: if the first, remove all categories and add no new. q: quit.""") while not done: choice = pywikibot.input(u"?") if choice == "": done = True elif choice == "-": chosen = choosecats(pagetext) done = True elif choice == "?": from pywikibot import editor as editarticle editor = editarticle.TextEditor() editor.edit(pagetext) elif choice == "??": pywikibot.output(pagetext[0:length]) length = length + 500 elif choice == "xx" and chosen == []: chosen = None done = True elif choice == "q": raise QuitKeyboardInterrupt else: chosen.append(choice) return chosen
def process_filename(self): """Return base filename portion of self.url""" # Isolate the pure name filename = self.url # Filename may be either a local file path or a URL if "://" in filename: # extract the path portion of the URL filename = urlparse.urlparse(filename).path filename = os.path.basename(filename) if self.useFilename: filename = self.useFilename if not self.keepFilename: pywikibot.output( u"The filename on the target wiki will default to: %s" % filename) # FIXME: these 2 belong somewhere else, presumably in family forbidden = '/' # to be extended allowed_formats = (u'gif', u'jpg', u'jpeg', u'mid', u'midi', u'ogg', u'png', u'svg', u'xcf', u'djvu') # ask until it's valid while True: newfn = pywikibot.input( u'Enter a better name, or press enter to accept:') if newfn == "": newfn = filename break ext = os.path.splitext(newfn)[1].lower().strip('.') # are any chars in forbidden also in newfn? invalid = set(forbidden) & set(newfn) if invalid: c = "".join(invalid) print "Invalid character(s): %s. Please try again" % c continue if ext not in allowed_formats: choice = pywikibot.inputChoice( u"File format is not one of [%s], but %s. Continue?" % (u' '.join(allowed_formats), ext), ['yes', 'no'], ['y', 'N'], 'N') if choice == 'n': continue break if newfn != '': filename = newfn # A proper description for the submission. pywikibot.output(u"The suggested description is:") pywikibot.output(self.description) if self.verifyDescription: newDescription = u'' choice = pywikibot.inputChoice( u'Do you want to change this description?', ['Yes', 'No'], ['y', 'N'], 'n') if choice == 'y': import editarticle editor = editarticle.TextEditor() newDescription = editor.edit(self.description) # if user saved / didn't press Cancel if newDescription: self.description = newDescription return filename
def input(twtitle, parameters=None, password=False, fallback_prompt=None): """ Ask the user a question, return the user's answer. The prompt message is retrieved via L{twtranslate} and uses the config variable 'userinterface_lang'. @param twtitle: The TranslateWiki string title, in <package>-<key> format @param parameters: The values which will be applied to the translated text @param password: Hides the user's input (for password entry) @param fallback_prompt: The English prompt if i18n is not available. @rtype: unicode string """ if not messages_available(): if not fallback_prompt: raise TranslationError( 'Unable to load messages package %s for bundle %s' % (_messages_package_name, twtitle)) else: prompt = fallback_prompt else: code = config.userinterface_lang prompt = twtranslate(code, twtitle, parameters) return pywikibot.input(prompt, password)
def run(self): for page in self.generator: if self.interwiki: imagelist = [] for linkedPage in page.interwiki(): imagelist += linkedPage.imagelinks(followRedirects=True) elif page.isImage(): imagePage = pywikibot.ImagePage(page.site(), page.title()) imagelist = [imagePage] else: imagelist = page.imagelinks(followRedirects=True) while imagelist: self.showImageList(imagelist) if len(imagelist) == 1: # no need to query the user, only one possibility todo = 0 else: pywikibot.output( u"Give the number of the image to transfer.") todo = pywikibot.input(u"To end uploading, press enter:") if not todo: break todo = int(todo) if todo in range(len(imagelist)): if imagelist[todo].fileIsOnCommons(): pywikibot.output( u'The image is already on Wikimedia Commons.') else: self.transferImage(imagelist[todo]) # remove the selected image from the list imagelist = imagelist[:todo] + imagelist[todo + 1:] else: pywikibot.output(u'No such image number.')
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 = {} gen = None local_args = pywikibot.handle_args(args) # 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. gen_factory = pagegenerators.GeneratorFactory(positional_arg_name='page') for arg in local_args: option, sep, value = arg.partition(':') if option == '-xml': filename = value or pywikibot.input( "Please enter the XML dump's filename:") gen = TableXmlDumpPageGenerator(filename) elif option == '-auto': issue_deprecation_warning('The usage of "-auto"', '-always', 1, ArgumentDeprecationWarning, since='20170205') options['always'] = True elif option in ['-always', '-quiet', '-skipwarning']: options[option[1:]] = True else: if option in ('-sql', '-mysqlquery'): if option == '-sql': issue_deprecation_warning('The usage of "-sql"', '-mysqlquery', 1, ArgumentDeprecationWarning, since='20170205') query = value or """ SELECT page_namespace, page_title FROM page JOIN text ON (page_id = old_id) WHERE old_text LIKE '%<table%' """ arg = '-mysqlquery:' + query gen_factory.handleArg(arg) if gen: gen = pagegenerators.NamespaceFilterPageGenerator( gen, gen_factory.namespaces) else: gen = gen_factory.getCombinedGenerator() if gen: if not gen_factory.nopreload: gen = pagegenerators.PreloadingGenerator(gen) bot = Table2WikiRobot(generator=gen, **options) bot.run() return True else: suggest_help(missing_generator=True) return False
def login(self, retry=False): """ Attempt to log into the server. @param retry: infinitely retry if the API returns an unknown error @type retry: bool @raises NoUsername: Username is not recognised by the site. """ if not self.password: # First check that the username exists, # to avoid asking for a password that will not work. self.check_user_exists() # As we don't want the password to appear on the screen, we set # password = True self.password = pywikibot.input( u'Password for user %(name)s on %(site)s (no characters will ' u'be shown):' % {'name': self.login_name, 'site': self.site}, password=True) pywikibot.output(u"Logging in to %(site)s as %(name)s" % {'name': self.login_name, 'site': self.site}) try: cookiedata = self.getCookie() except pywikibot.data.api.APIError as e: pywikibot.error(u"Login failed (%s)." % e.code) if e.code == 'NotExists': raise NoUsername(u"Username '%s' does not exist on %s" % (self.login_name, self.site)) elif e.code == 'Illegal': raise NoUsername(u"Username '%s' is invalid on %s" % (self.login_name, self.site)) elif e.code == 'readapidenied': raise NoUsername( 'Username "{0}" does not have read permissions on ' '{1}'.format(self.login_name, self.site)) elif e.code == 'Failed': raise NoUsername( 'Username "{0}" does not have read permissions on ' '{1}\n.{2}'.format(self.login_name, self.site, e.info)) # TODO: investigate other unhandled API codes (bug T75539) if retry: self.password = None return self.login(retry=True) else: return False self.storecookiedata(cookiedata) pywikibot.log(u"Should be logged in now") # # Show a warning according to the local bot policy # FIXME: disabled due to recursion; need to move this to the Site object after # login # if not self.botAllowed(): # logger.error( # u"Username '%(name)s' is not listed on [[%(page)s]]." # % {'name': self.username, # 'page': botList[self.site.family.name][self.site.code]}) # logger.error( # "Please make sure you are allowed to use the robot before actually using it!") # return False 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: 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 add_text(page=None, addText=None, summary=None, regexSkip=None, regexSkipUrl=None, always=False, up=False, putText=True, oldTextGiven=None, reorderEnabled=True, create=False): if not addText: raise NoEnoughData('You have to specify what text you want to add!') if not summary: summary = i18n.twtranslate(pywikibot.Site(), 'add_text-adding', {'adding': addText[:200]}) # When a page is tagged as "really well written" it has a star in the # interwiki links. This is a list of all the templates used (in regex # format) to make the stars appear. errorCount = 0 site = pywikibot.Site() pathWiki = site.family.nicepath(site.code) if putText: pywikibot.output(u'Loading %s...' % page.title()) if oldTextGiven is None: try: text = page.get() except pywikibot.NoPage: if create: pywikibot.output(u"%s doesn't exist, creating it!" % page.title()) text = u'' else: pywikibot.output(u"%s doesn't exist, skip!" % page.title()) return (False, False, always) except pywikibot.IsRedirectPage: pywikibot.output(u"%s is a redirect, skip!" % page.title()) return (False, False, always) else: text = oldTextGiven # Understand if the bot has to skip the page or not # In this way you can use both -except and -excepturl if regexSkipUrl is not None: url = '%s%s' % (pathWiki, page.title(asUrl=True)) result = re.findall(regexSkipUrl, site.getUrl(url)) if result != []: pywikibot.output( u'''Exception! regex (or word) used with -exceptUrl is in the page. Skip! Match was: %s''' % result) return (False, False, always) if regexSkip is not None: result = re.findall(regexSkip, text) if result != []: pywikibot.output( u'''Exception! regex (or word) used with -except is in the page. Skip! Match was: %s''' % result) return (False, False, always) # If not up, text put below if not up: newtext = text # Translating the \\n into binary \n addText = addText.replace('\\n', config.line_separator) if (reorderEnabled): # Getting the categories categoriesInside = pywikibot.getCategoryLinks(newtext, site) # Deleting the categories newtext = pywikibot.removeCategoryLinks(newtext, site) # Getting the interwiki interwikiInside = pywikibot.getLanguageLinks(newtext, site) # Removing the interwiki newtext = pywikibot.removeLanguageLinks(newtext, site) # Adding the text newtext += u"%s%s" % (config.line_separator, addText) # Reputting the categories newtext = pywikibot.replaceCategoryLinks(newtext, categoriesInside, site, True) # Dealing the stars' issue allstars = [] starstext = pywikibot.removeDisabledParts(text) for star in starsList: regex = re.compile( '(\{\{(?:template:|)%s\|.*?\}\}[\s]*)' % star, re.I) found = regex.findall(starstext) if found != []: newtext = regex.sub('', newtext) allstars += found if allstars != []: newtext = newtext.strip() + config.line_separator * 2 allstars.sort() for element in allstars: newtext += '%s%s' % (element.strip(), config.LS) # Adding the interwiki newtext = pywikibot.replaceLanguageLinks(newtext, interwikiInside, site) else: newtext += u"%s%s" % (config.line_separator, addText) else: newtext = addText + config.line_separator + text if putText and text != newtext: pywikibot.output(u"\n\n>>> \03{lightpurple}%s\03{default} <<<" % page.title()) pywikibot.showDiff(text, newtext) # Let's put the changes. while True: # If someone load it as module, maybe it's not so useful to put the # text in the page if putText: if not always: choice = pywikibot.inputChoice( u'Do you want to accept these changes?', ['Yes', 'No', 'All', 'open in Browser'], ['y', 'n', 'a', 'b'], 'n') if choice == 'a': always = True elif choice == 'n': return (False, False, always) elif choice == 'b': webbrowser.open("http://%s%s" % (page.site.hostname(), page.site.nice_get_address(page.title()))) pywikibot.input("Press Enter when finished in browser.") if always or choice == 'y': try: if always: page.put(newtext, summary, minorEdit=page.namespace() != 3) else: page.put_async(newtext, summary, minorEdit=page.namespace() != 3) except pywikibot.EditConflict: pywikibot.output(u'Edit conflict! skip!') return (False, False, always) except pywikibot.ServerError: errorCount += 1 if errorCount < config.max_retries: pywikibot.output(u'Server Error! Wait..') time.sleep(config.retry_wait) continue else: raise pywikibot.ServerError(u'Fifth Server Error!') except pywikibot.SpamfilterError as e: pywikibot.output( u'Cannot change %s because of blacklist entry %s' % (page.title(), e.url)) return (False, False, always) except pywikibot.PageNotSaved as error: pywikibot.output(u'Error putting page: %s' % error.args) return (False, False, always) except pywikibot.LockedPage: pywikibot.output(u'Skipping %s (locked page)' % page.title()) return (False, False, always) else: # Break only if the errors are one after the other... errorCount = 0 return (True, True, always) else: return (text, newtext, always)
def main(): # If none, the var is setted only for check purpose. summary = None addText = None regexSkip = None regexSkipUrl = None generator = None always = False textfile = None talkPage = False reorderEnabled = True namespaces = [] # Put the text above or below the text? up = False # Process global args and prepare generator args parser local_args = pywikibot.handleArgs() 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('-page'): if len(arg) == 5: generator = [ pywikibot.Page( pywikibot.Site(), pywikibot.input(u'What page do you want to use?')) ] else: generator = [pywikibot.Page(pywikibot.Site(), arg[6:])] 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: f = codecs.open(textfile, 'r', config.textfile_encoding) addText = f.read() f.close() if not generator: generator = genFactory.getCombinedGenerator() if not generator: raise NoEnoughData( 'You have to specify the generator you want to use for the script!' ) if talkPage: generator = pagegenerators.PageWithTalkPageGenerator(generator) site = pywikibot.Site() for namespace in site.namespaces(): index = site.getNamespaceIndex(namespace) if index % 2 == 1 and index > 0: namespaces += [index] generator = pagegenerators.NamespaceFilterPageGenerator( generator, namespaces) for page in generator: (text, newtext, always) = add_text(page, addText, summary, regexSkip, regexSkipUrl, always, up, True, reorderEnabled=reorderEnabled, create=talkPage)
def main(): gen = None prefix = None oldName = None noredirect = False movetalkpage = True always = False skipredirects = False summary = None fromToPairs = [] # Process global args and prepare generator args parser local_args = pywikibot.handleArgs() genFactory = pagegenerators.GeneratorFactory() for arg in local_args: if arg.startswith('-pairs'): if len(arg) == len('-pairs'): filename = pywikibot.input( u'Enter the name of the file containing pairs:') else: filename = arg[len('-pairs:'):] oldName1 = None for page in pagegenerators.TextfilePageGenerator(filename): if oldName1: fromToPairs.append([oldName1, page.title()]) oldName1 = None else: oldName1 = page.title() if oldName1: pywikibot.warning( u'file %s contains odd number of links' % filename) elif arg == '-noredirect': noredirect = True elif arg == '-notalkpage': movetalkpage = False elif arg == '-always': always = True elif arg == '-skipredirects': skipredirects = True elif arg.startswith('-from:'): if oldName: pywikibot.warning(u'-from:%s without -to:' % oldName) oldName = arg[len('-from:'):] elif arg.startswith('-to:'): if oldName: fromToPairs.append([oldName, arg[len('-to:'):]]) oldName = None else: pywikibot.warning(u'%s without -from' % arg) elif arg.startswith('-prefix'): if len(arg) == len('-prefix'): prefix = pywikibot.input(u'Enter the prefix:') else: prefix = arg[8:] elif arg.startswith('-summary'): if len(arg) == len('-summary'): summary = pywikibot.input(u'Enter the summary:') else: summary = arg[9:] else: genFactory.handleArg(arg) if oldName: pywikibot.warning(u'-from:%s without -to:' % oldName) for pair in fromToPairs: page = pywikibot.Page(pywikibot.Site(), pair[0]) bot = MovePagesBot(None, prefix, noredirect, movetalkpage, always, skipredirects, summary) bot.moveOne(page, pair[1]) if not gen: gen = genFactory.getCombinedGenerator() if gen: preloadingGen = pagegenerators.PreloadingGenerator(gen) bot = MovePagesBot(preloadingGen, prefix, noredirect, movetalkpage, always, skipredirects, summary) bot.run() elif not fromToPairs: pywikibot.showHelp()
def treat(self, page): # Show the title of the page we're working on. # Highlight the title in purple. pywikibot.output(u"\n\n>>> \03{lightpurple}%s\03{default} <<<" % page.title()) if self.skipredirects and page.isRedirectPage(): pywikibot.output(u'Page %s is a redirect; skipping.' % page.title()) return pagetitle = page.title(withNamespace=False) namesp = page.site.namespace(page.namespace()) if self.appendAll: newPageTitle = (u'%s%s%s' % (self.pagestart, pagetitle, self.pageend)) if not self.noNamespace and namesp: newPageTitle = (u'%s:%s' % (namesp, newPageTitle)) elif self.regexAll: newPageTitle = self.regex.sub(self.replacePattern, pagetitle) if not self.noNamespace and namesp: newPageTitle = (u'%s:%s' % (namesp, newPageTitle)) if self.addprefix: newPageTitle = (u'%s%s' % (self.addprefix, pagetitle)) if self.addprefix or self.appendAll or self.regexAll: if not self.always: choice2 = pywikibot.inputChoice( u'Change the page title to "%s"?' % newPageTitle, ['yes', 'no', 'all', 'quit'], ['y', 'n', 'a', 'q']) if choice2 == 'y': self.moveOne(page, newPageTitle) elif choice2 == 'a': self.always = True self.moveOne(page, newPageTitle) elif choice2 == 'q': sys.exit() elif choice2 == 'n': pass else: self.treat(page) else: self.moveOne(page, newPageTitle) else: choice = pywikibot.inputChoice(u'What do you want to do?', ['change page name', 'append to page name', 'use a regular expression', 'next page', 'quit'], ['c', 'a', 'r', 'n', 'q']) if choice == 'c': newPageTitle = pywikibot.input(u'New page name:') self.moveOne(page, newPageTitle) elif choice == 'a': self.pagestart = pywikibot.input(u'Append this to the start:') self.pageend = pywikibot.input(u'Append this to the end:') newPageTitle = (u'%s%s%s' % (self.pagestart, pagetitle, self.pageend)) if namesp: choice2 = pywikibot.inputChoice( u'Do you want to remove the namespace prefix "%s:"?' % namesp, ['yes', 'no'], ['y', 'n']) if choice2 == 'y': self.noNamespace = True else: newPageTitle = (u'%s:%s' % (namesp, newPageTitle)) choice2 = pywikibot.inputChoice( u'Change the page title to "%s"?' % newPageTitle, ['yes', 'no', 'all', 'quit'], ['y', 'n', 'a', 'q']) if choice2 == 'y': self.moveOne(page, newPageTitle) elif choice2 == 'a': self.appendAll = True self.moveOne(page, newPageTitle) elif choice2 == 'q': sys.exit() elif choice2 == 'n': pass else: self.treat(page) elif choice == 'r': searchPattern = pywikibot.input(u'Enter the search pattern:') self.replacePattern = pywikibot.input( u'Enter the replace pattern:') self.regex = re.compile(searchPattern) if page.title() == page.title(withNamespace=False): newPageTitle = self.regex.sub(self.replacePattern, page.title()) else: choice2 = pywikibot.inputChoice( u'Do you want to remove the namespace prefix "%s:"?' % namesp, ['yes', 'no'], ['y', 'n']) if choice2 == 'y': newPageTitle = self.regex.sub( self.replacePattern, page.title(withNamespace=False)) self.noNamespace = True else: newPageTitle = self.regex.sub(self.replacePattern, page.title()) choice2 = pywikibot.inputChoice( u'Change the page title to "%s"?' % newPageTitle, ['yes', 'no', 'all', 'quit'], ['y', 'n', 'a', 'q']) if choice2 == 'y': self.moveOne(page, newPageTitle) elif choice2 == 'a': self.regexAll = True self.moveOne(page, newPageTitle) elif choice2 == 'q': sys.exit() elif choice2 == 'n': pass else: self.treat(page) elif choice == 'n': pass elif choice == 'q': sys.exit() else: self.treat(page)