def pre(taskid=-1, lock=None, sites=[], continuous=False, main=None): """ Return argument list, site object, and configuration of the script. This function also handles default arguments, generates lockfile and halt the script if lockfile exists before. """ import imp global info info["main"] = main == "__main__" if continuous: lock = False pywikibot.handleArgs("-log") pywikibot.output("start task #%s at %s" % (taskid, getTime())) info["taskid"] = taskid info["lock"] = lock info["lockfile"] = simplifypath([os.environ["WPROBOT_DIR"], "tmp", info["basescript"] + ".lock"]) info["continuous"] = continuous if os.path.exists(info["lockfile"]) and lock: error("lockfile found. unable to execute the script.") if info["main"]: pywikibot.stopme() sys.exit(ExitCode.LockFileError) open(info["lockfile"], "w").close() args = pywikibot.handleArgs() # must be called before Site() site = pywikibot.Site() info["site"] = site confpath = simplifypath([os.environ["WPROBOT_DIR"], "conf", info["basescript"]]) module = imp.load_source("conf", confpath) if os.path.exists(confpath) else None return args, site, module
def main(): db = oursql.connect(db='wikidatawiki_p', host="wikidatawiki-p.rrdb.toolserver.org", read_default_file=os.path.expanduser("~/.my.cnf"), charset=None, use_unicode=False ) pywikibot.handleArgs() cur = db.cursor() cur.execute(query) data=cur.fetchall() for row in data: check_item(row[0]) global REPORT global DUPES rfd() #before dupes pg = pywikibot.Page(wikidata, 'User:Legobot/Dupes') pg.put(REPORT, 'Bot: Updating list of dupes') #save dupes locally for null edits fname = 'wd_null.txt' if os.path.exists(fname): with open(fname, 'r') as f: old = f.read() else: old = '' new = old + DUPES with open(fname, 'w') as f: f.write(new) print 'Saved dupe file'
def main(): ws = WikiStats() stats = ws.sorted('wikipedia', 'good') # rimuove mo.wikipedia.org redirect a ro.wikipedia.org stats = [stat for stat in stats if stat['prefix'] != 'mo'] now = datetime.datetime.now() text = u'<!-- یہاں نئی زبان شامل کرنے سے قبل تبادلہ خیال صفحہ پر گفتگو کر لیں۔ -->\n<onlyinclude>{{#switch:{{{1}}}\n' text += '| data = %d %s %d\n' % (now.day, date.formats['MonthName']['ur']( now.month), now.year) total = { 'good': 0, 'images': 0, 'total': 0, 'users': 0, 'activeusers': 0, 'admins': 0, 'edits': 0 } for stat in stats: text += formatStat(stat) for key in total: total[key] += int(stat[key]) total['prefix'] = 'total' text += formatStat(total) text += u'| 0 }}</onlyinclude>\n<noinclude>\n{{دستاویز}}\n</noinclude>\n' pywikibot.handleArgs() site = pywikibot.Site() page = pywikibot.Page(site, 'Template:NUMBEROF/data') page.put(text, 'خودکار: تجدید شماریات')
def main(): featured = False gen = None # Process global args and prepare generator args parser local_args = pywikibot.handleArgs() genFactory = pagegenerators.GeneratorFactory() for arg in local_args: if arg == '-featured': featured = True else: genFactory.handleArg(arg) mysite = pywikibot.Site() if mysite.sitename() == 'wikipedia:nl': pywikibot.output( u'\03{lightred}There is consensus on the Dutch Wikipedia that bots should not be used to fix redirects.\03{default}' ) sys.exit() if featured: featuredList = i18n.translate(mysite, featured_articles) ref = pywikibot.Page(pywikibot.Site(), featuredList) gen = pagegenerators.ReferringPageGenerator(ref) gen = pagegenerators.NamespaceFilterPageGenerator(gen, [0]) if not gen: gen = genFactory.getCombinedGenerator() if gen: for page in pagegenerators.PreloadingGenerator(gen): workon(page) else: pywikibot.showHelp('fixing_redirects')
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(): dry = False reprise = False titreReprise = None debug = False async = False for arg in pywikibot.handleArgs(): if arg.startswith("-dry"): dry = True elif arg.startswith("-reprise:"): reprise = True titreReprise = arg[len('-reprise:'):].replace('_', ' ') if titreReprise == '': pywikibot.output( u'Syntax: basic4_refonte.py [-reprise:|-dry|-debug]') exit() elif arg.startswith("-debug"): debug = True elif arg.startswith("-async"): async = True else: pywikibot.output( u'Syntax: basic4_refonte.py [-reprise:|-dry|-debug|-async]') exit() bot = InfoboxBot(dry, reprise, titreReprise, debug, async) bot.debugMode() bot.run()
def main(*args): gen = None notitle = False page_get = False # Process global args and prepare generator args parser local_args = pywikibot.handleArgs(*args) genFactory = GeneratorFactory() for arg in local_args: if arg == '-notitle': notitle = True elif arg == '-get': page_get = True else: genFactory.handleArg(arg) gen = genFactory.getCombinedGenerator() if gen: for i, page in enumerate(gen, start=1): if not notitle: pywikibot.stdout("%4d: %s" % (i, page.title())) if page_get: # TODO: catch exceptions pywikibot.output(page.text, toStdout=True) else: pywikibot.showHelp()
def main(*args): url = u'' description = [] keepFilename = False useFilename = None verifyDescription = True # process all global bot args # returns a list of non-global args, i.e. args for upload.py for arg in pywikibot.handleArgs(*args): if arg: if arg.startswith('-keep'): keepFilename = True elif arg.startswith('-filename:'): useFilename = arg[10:] elif arg.startswith('-noverify'): verifyDescription = False elif url == u'': url = arg else: description.append(arg) description = u' '.join(description) bot = UploadRobot(url, description=description, useFilename=useFilename, keepFilename=keepFilename, verifyDescription=verifyDescription) bot.run()
def main(): global mysite, linktrail, page start = [] for arg in pywikibot.handleArgs(): start.append(arg) if start: start = " ".join(start) else: start = "!" mysite = pywikibot.Site() linktrail = mysite.linktrail() try: generator = pagegenerators.CategorizedPageGenerator( mysite.disambcategory(), start=start) except pywikibot.NoPage: pywikibot.output( "The bot does not know the disambiguation category for your wiki.") raise # only work on articles generator = pagegenerators.NamespaceFilterPageGenerator(generator, [0]) generator = pagegenerators.PreloadingGenerator(generator) pagestodo = [] pagestoload = [] for page in generator: if page.isRedirectPage(): continue linked = page.linkedPages() pagestodo.append((page, linked)) pagestoload += linked if len(pagestoload) > 49: pagestoload = pagegenerators.PreloadingGenerator(pagestoload) for page, links in pagestodo: workon(page, links) pagestoload = [] pagestodo = []
def main(): filename = 'fb2w.nt.gz' # Default filename for arg in pywikibot.handleArgs(): if arg.startswith('-filename'): filename = arg[11:] bot = FreebaseMapperRobot(filename) bot.run()
def main(): gen = pagegenerators.GeneratorFactory() commandline_claims = list() for arg in pywikibot.handleArgs(): if gen.handleArg(arg): continue commandline_claims.append(arg) if len(commandline_claims) % 2: raise ValueError # or something. claims = list() repo = pywikibot.Site().data_repository() for i in xrange (0, len(commandline_claims), 2): claim = pywikibot.Claim(repo, commandline_claims[i]) if claim.getType() == 'wikibase-item': target = pywikibot.ItemPage(repo, commandline_claims[i+1]) elif claim.getType() == 'string': target = commandline_claims[i+1] else: raise NotImplementedError("%s datatype is not yet supported by claimit.py" % claim.getType()) claim.setTarget(target) claims.append(claim) generator = gen.getCombinedGenerator() if not generator: # FIXME: Should throw some help return bot = ClaimRobot(generator, claims) bot.run()
def main(*args): url = u'' description = [] keepFilename = False useFilename = None verifyDescription = True aborts = 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) # process all global bot args # returns a list of non-global args, i.e. args for upload.py for arg in pywikibot.handleArgs(*args): if arg: if 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('-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) bot = UploadRobot(url, description=description, useFilename=useFilename, keepFilename=keepFilename, verifyDescription=verifyDescription, aborts=aborts, chunk_size=chunk_size) bot.run()
def main(): """ Script entry point. """ local_args = pywikibot.handleArgs() all = False new = False sysop = False for arg in local_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_all(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(): 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(): global autonomous global replace, replacealways, replaceloose, replaceonly global use_hash autonomous = False replace = False replacealways = False replaceloose = False replaceonly = False use_hash = False for arg in pywikibot.handleArgs(): if arg == '-autonomous': autonomous = True if arg == '-replace': replace = True if arg == '-replacealways': replace = True replacealways = True if arg == '-replaceloose': replaceloose = True if arg == '-replaceonly': replaceonly = True if arg == '-hash': use_hash = True bot = NowCommonsDeleteBot() bot.run()
def main(): # This temporary string is used to read the title # of the page that should be unlinked. page_title = None options = {} for arg in pywikibot.handleArgs(): 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() else: pywikibot.showHelp()
def main(): """The main loop.""" conn = None cursor = None skip_wd = False (conn, cursor) = connect_to_monuments_database() for arg in pywikibot.handleArgs(): option, sep, value = arg.partition(':') if option == '-skip_wd': skip_wd = True else: raise Exception( u'Bad parameters. Expected "-skip_wd" or pywikibot args. ' u'Found "{}"'.format(option)) statistics = {} for (countrycode, lang), countryconfig in mconfig.filtered_countries( skip_wd=skip_wd, skip_wlpa=True): if countrycode not in statistics: statistics[countrycode] = {} if lang not in statistics[countrycode]: statistics[countrycode][lang] = {} for country in getCountries(conn, cursor): for language in getLanguages(country, conn, cursor): statistics[country][language] = get_summary_statistics( country, language, conn, cursor) outputStatistics(statistics) close_database_connection(conn, cursor)
def main(): always = False for arg in pywikibot.handleArgs(): if arg == '-always': always = True mysite = pywikibot.Site() # If anything needs to be prepared, you can do it here template_image = i18n.translate(pywikibot.Site(), template_to_the_image) template_user = i18n.translate(pywikibot.Site(), template_to_the_user) except_text_translated = i18n.translate(pywikibot.Site(), except_text) if not(template_image and template_user and except_text_translated): pywikibot.warning(u'This script is not localized for %s site.' % mysite) return generator = pagegenerators.UnusedFilesGenerator() generator = pagegenerators.PreloadingGenerator(generator) for image in generator: if (except_text_translated.encode('utf-8') not in image.getImagePageHtml() and u'http://' not in image.text): pywikibot.output(u"\n\n>>> \03{lightpurple}%s\03{default} <<<" % image.title()) if template_image in image.text: pywikibot.output(u"%s done already" % image.title(asLink=True)) continue appendtext(image, u"\n\n" + template_image, always) uploader = image.getFileVersionHistory().pop(0)['user'] user = pywikibot.User(mysite, uploader) usertalkpage = user.getUserTalkPage() msg2uploader = template_user % {'title': image.title()} appendtext(usertalkpage, msg2uploader, always)
def main(): ''' Main loop. Get a generator and options. ''' generator = None parent = u'' basename = u'' always = False # Process global args and prepare generator args parser local_args = pywikibot.handleArgs() genFactory = pagegenerators.GeneratorFactory() for arg in local_args: if arg == '-always': 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: for page in generator: createCategory(page, parent, basename) else: pywikibot.output(u'No pages to work on') pywikibot.output(u'All done')
def main(*args): ''' Grab a bunch of images and tag them if they are not categorized. ''' generator = None genFactory = pagegenerators.GeneratorFactory() # use the default imagerepository normally commons site = pywikibot.getSite().image_repository() site.login() for arg in pywikibot.handleArgs(*args): if arg.startswith('-yesterday'): generator = uploadedYesterday(site) elif arg.startswith('-recentchanges'): generator = recentChanges(site=site, delay=120) else: genFactory.handleArg(arg) if not generator: generator = genFactory.getCombinedGenerator() if not generator: pywikibot.output( u'You have to specify the generator you want to use for the program!') else: pregenerator = site.preloadpages(generator) for page in pregenerator: pywikibot.output(page.title()) if page.exists() and (page.namespace() == 6) \ and (not page.isRedirectPage()) : if isUncat(page): addUncat(page)
def create(year): target = str(year)+' in the Palestinian territories' pg1 = pywikibot.Page(site, str(year)+' of the Palestinian territories') pg2 = pywikibot.Page(site, str(year)+' in Palestine') pg3 = pywikibot.Page(site, str(year)+' in the Palestinian National Authority' pg4 = pywikibot.Page(site, str(year)+' of the Palestinian National Authority') pg5 = pywikibot.Page(site, str(year)+' of Palestine') for page in [pg1, pg2, pg3, pg4, pg5]: if page.exists(): continue print 'Creating %s' % page.title(asLink=True) page.put(REDIRECT % target, 'BOT EDIT: Creating redirect to [[%s]]' % target) talk = page.toggleTalkPage() if not talk.exists(): print 'Creating %s' % talk.title(asLink=True) tag = '{{WikiProject Palestine|class=Redirect}}' summary = 'Bot: Tagging for [[Wikipedia:WikiProject Palestine]]' talk.put(tag, summary) def main(): year = 2000 while year < 2012: create(year) year += 1 create(1987) if __name__ == "__main__": pywikibot.handleArgs() main()
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(): # This temporary array is used to read the page title if one single # page that should be unlinked. pageTitle = [] # Which namespaces should be processed? # default to [] which means all namespaces will be processed namespaces = [] always = False for arg in pywikibot.handleArgs(): if arg.startswith('-namespace:'): try: namespaces.append(int(arg[11:])) except ValueError: namespaces.append(arg[11:]) elif arg == '-always': always = True else: pageTitle.append(arg) if pageTitle: page = pywikibot.Page(pywikibot.getSite(), ' '.join(pageTitle)) bot = UnlinkBot(page, namespaces, always) bot.run() else: pywikibot.showHelp('unlink')
def main(): # pywikibot/families/eagle_family.py site = pywikibot.Site('en', 'eagle') repo = site.data_repository() sort = None fileName = None labelLang = 'en' show = 'property' # Handles command-line arguments for pywikibot. for arg in pywikibot.handleArgs(): if arg.startswith('-file:'): fileName = arg.replace('-file:', '') if arg.startswith('-property:'): prop = arg.replace('-property:', '') if arg.startswith('-sort:'): sort = arg.replace('-sort:', '') if arg.startswith('-show:'): show = arg.replace('-show:', '') if arg.startswith('-label-lang:'): labelLang = arg.replace('-label-lang:', '') if sort == None: sort = show output = getItemsForProperty(repo, prop, sort, labelLang, fileName) for i in output: pywikibot.output('* [[' + i['title'] + '|' + i[show] + ']]', toStdout=True)
def main(args): """ Grab a bunch of images and tag them if they are not categorized. """ generator = None genFactory = pagegenerators.GeneratorFactory() site = pywikibot.getSite(u'commons', u'commons') pywikibot.setSite(site) for arg in pywikibot.handleArgs(): if arg.startswith('-yesterday'): generator = uploadedYesterday(site) elif arg.startswith('-recentchanges'): generator = recentChanges(site=site, delay=120) else: genFactory.handleArg(arg) if not generator: generator = genFactory.getCombinedGenerator() if not generator: pywikibot.output(u'You have to specify the generator you want to use ' u'for the program!') else: pregenerator = pagegenerators.PreloadingGenerator(generator) for page in pregenerator: if page.exists() and (page.namespace() == 6) \ and (not page.isRedirectPage()): if isUncat(page): addUncat(page)
def main(*args): gen = None options = {} # If the user chooses to work on a single page, this temporary array is # used to read the words from the page title. The words will later be # joined with spaces to retrieve the full title. pageTitle = [] # Process global args and prepare generator args parser local_args = pywikibot.handleArgs(*args) genFactory = pagegenerators.GeneratorFactory() for arg in local_args: if genFactory.handleArg(arg): continue if arg.startswith("-"): options[arg[1:].lower()] = True else: pageTitle.append(arg) pywikibot.Site().login() gen = genFactory.getCombinedGenerator() if not gen: if pageTitle: # work on a single page page = pywikibot.Page(pywikibot.Link(' '.join(pageTitle))) gen = iter([page]) else: pywikibot.showHelp() return preloadingGen = pagegenerators.PreloadingGenerator(gen) bot = TouchBot(preloadingGen, **options) bot.run()
def main(): countrycode = u'' lang = u'' municipality = u'' minimum = 15 conn = None cursor = None # Connect database, we need that (conn, cursor) = connect_to_monuments_database() for arg in pywikibot.handleArgs(): option, sep, value = arg.partition(':') if option == '-countrycode': countrycode = value elif option == '-langcode': lang = value elif option == '-municipality': municipality = value elif option == '-minimum': minimum = int(value) else: raise Exception( u'Bad parameters. Expected "-countrycode", "-langcode", ' u'"-municipality", "-minimum" or pywikibot args. ' u'Found "{}"'.format(option)) if countrycode and lang and municipality: addresses = getAddresses(countrycode, lang, municipality, conn, cursor) printTopStreets(addresses, minimum) else: print u'Usage' close_database_connection(conn, cursor)
def main(*args): ''' Grab a bunch of images and tag them if they are not categorized. ''' generator = None local_args = pywikibot.handleArgs(*args) # use the default imagerepository normally commons site = pywikibot.Site().image_repository() genFactory = pagegenerators.GeneratorFactory(site) site.login() for arg in local_args: if arg.startswith('-yesterday'): generator = uploadedYesterday(site) elif arg.startswith('-recentchanges'): generator = recentChanges(site=site, delay=120) else: genFactory.handleArg(arg) if not generator: generator = genFactory.getCombinedGenerator() if not generator: pywikibot.output( u'You have to specify the generator you want to use for the program!') else: pregenerator = pagegenerators.PreloadingGenerator(generator) for page in pregenerator: pywikibot.output(page.title()) if page.exists() and (page.namespace() == 6) \ and (not page.isRedirectPage()): if isUncat(page): addUncat(page)
def main(): """Main loop. Get a generator and options.""" parent = None basename = None options = {} # Process global args and prepare generator args parser local_args = pywikibot.handleArgs() 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(): selectedSet = None always = False idProp = insertProp = filename = False # Handles command-line arguments for pywikibot. for arg in pywikibot.handleArgs(): if arg.startswith('-identifier:'): idProp = arg.replace('-identifier:', '') if arg.startswith('-insert:'): insertProp = arg.replace('-insert:', '') if arg.startswith('-file:'): filename = arg.replace('-file:', '') if arg.startswith('-always'): always=True if idProp == None or insertProp == None or filename == None: pywikibot.output('All arguments are required!') return matchDict = {} with open(filename, 'r') as f: reader = csv.reader(f, delimiter=";") for row in reader: matchDict[row[0]] = row[1] bot = PropertyBot(always=always, idProp=idProp, insertProp=insertProp, matchDict=matchDict) bot.run()
def main(): gen = pagegenerators.GeneratorFactory() commandline_claims = list() for arg in pywikibot.handleArgs(): if gen.handleArg(arg): continue commandline_claims.append(arg) if len(commandline_claims) % 2: raise ValueError # or something. claims = list() repo = pywikibot.Site().data_repository() for i in xrange(0, len(commandline_claims), 2): claim = pywikibot.Claim(repo, commandline_claims[i]) if claim.getType() == 'wikibase-item': target = pywikibot.ItemPage(repo, commandline_claims[i + 1]) elif claim.getType() == 'string': target = commandline_claims[i + 1] else: raise NotImplementedError( "%s datatype is not yet supported by claimit.py" % claim.getType()) claim.setTarget(target) claims.append(claim) generator = gen.getCombinedGenerator() if not generator: # FIXME: Should throw some help return bot = ClaimRobot(generator, claims) 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(): local_args = pywikibot.handleArgs() cache_paths = None delete = False command = None for arg in local_args: if command == '': command = arg elif arg == '-delete': delete = True elif arg == '-password': command = 'has_password(entry)' elif arg == '-c': if command: pywikibot.error('Only one command may be executed.') exit(1) command = '' else: if not cache_paths: cache_paths = [arg] else: cache_paths.append(arg) func = None if not cache_paths: cache_paths = ['apicache', 'tests/apicache'] # Also process the base directory, if it isnt the current directory if os.path.abspath(os.getcwd()) != pywikibot.config2.base_dir: cache_paths += [ os.path.join(pywikibot.config2.base_dir, 'apicache')] # Also process the user home cache, if it isnt the config directory if os.path.expanduser('~/.pywikibot') != pywikibot.config2.base_dir: cache_paths += [ os.path.join(os.path.expanduser('~/.pywikibot'), 'apicache')] if delete: action_func = lambda entry: entry._delete() else: action_func = lambda entry: pywikibot.output(entry) if command: try: command_func = eval('lambda entry: ' + command) except: pywikibot.exception() pywikibot.error(u'Can not compile command: %s' % command) exit(1) func = lambda entry: command_func(entry) and action_func(entry) else: func = action_func for cache_path in cache_paths: if len(cache_paths) > 1: pywikibot.output(u'Processing %s' % cache_path) process_entries(cache_path, func)
def main(): always = False for arg in pywikibot.handleArgs(): if arg == '-always': always = True mysite = pywikibot.Site() # If anything needs to be prepared, you can do it here template_image = i18n.translate(pywikibot.Site(), template_to_the_image) template_user = i18n.translate(pywikibot.Site(), template_to_the_user) except_text_translated = i18n.translate(pywikibot.Site(), except_text) if not (template_image and template_user and except_text_translated): pywikibot.warning(u'This script is not localized for %s site.' % mysite) return generator = pagegenerators.UnusedFilesGenerator() generator = pagegenerators.PreloadingGenerator(generator) for image in generator: if (except_text_translated.encode('utf-8') not in image.getImagePageHtml() and u'http://' not in image.text): pywikibot.output(u"\n\n>>> \03{lightpurple}%s\03{default} <<<" % image.title()) if template_image in image.text: pywikibot.output(u"%s done already" % image.title(asLink=True)) continue appendtext(image, u"\n\n" + template_image, always) uploader = image.getFileVersionHistory().pop(0)['user'] user = pywikibot.User(mysite, uploader) usertalkpage = user.getUserTalkPage() msg2uploader = template_user % {'title': image.title()} appendtext(usertalkpage, msg2uploader, always)
def set_options(self, *args): """Parse commandline and set options attribute""" my_args = [] for arg in pywikibot.handleArgs(*args): my_args.append(arg) parser = optparse.OptionParser() parser.add_option("-r", "--edit_redirect", action="store_true", default=False, help="Ignore/edit redirects") parser.add_option("-p", "--page", help="Page to edit") parser.add_option("-w", "--watch", action="store_true", default=False, help="Watch article after edit") #parser.add_option("-n", "--new_data", default="", # help="Automatically generated content") (self.options, args) = parser.parse_args(args=my_args) # for convenience, if we have an arg, stuff it into the opt, so we # can act like a normal editor. if (len(args) == 1): self.options.page = args[0]
def main(args): """ Main loop. Get a generator and options. Work on all images in the generator. """ generator = None onlyFilter = False onlyUncat = False genFactory = pagegenerators.GeneratorFactory() global search_wikis global hint_wiki site = pywikibot.getSite(u'commons', u'commons') pywikibot.setSite(site) for arg in pywikibot.handleArgs(): if arg == '-onlyfilter': onlyFilter = True elif arg == '-onlyuncat': onlyUncat = True elif arg.startswith('-hint:'): hint_wiki = arg[len('-hint:'):] elif arg.startswith('-onlyhint'): search_wikis = arg[len('-onlyhint:'):] else: genFactory.handleArg(arg) generator = genFactory.getCombinedGenerator() if not generator: generator = pagegenerators.CategorizedPageGenerator( catlib.Category(site, u'Category:Media needing categories'), recurse=True) initLists() categorizeImages(generator, onlyFilter, onlyUncat) pywikibot.output(u'All done')
def main(): old_image = None new_image = None options = {} for arg in pywikibot.handleArgs(): if arg == '-always': options['always'] = True elif arg == '-loose': options['loose'] = True elif arg.startswith('-summary'): if len(arg) == len('-summary'): options['summary'] = pywikibot.input(u'Choose an edit summary: ') else: options['summary'] = arg[len('-summary:'):] elif old_image: new_image = arg else: old_image = arg if old_image: site = pywikibot.Site() old_imagepage = pywikibot.FilePage(site, old_image) gen = pagegenerators.FileLinksGenerator(old_imagepage) preloadingGen = pagegenerators.PreloadingGenerator(gen) bot = ImageRobot(preloadingGen, old_image, new_image, **options) bot.run() else: pywikibot.showHelp()
def main(): 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(): #page generator gen = None # If the user chooses to work on a single page, this temporary array is # used to read the words from the page title. The words will later be # joined with spaces to retrieve the full title. pageTitle = [] # 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() for arg in pywikibot.handleArgs(): if not genFactory.handleArg(arg): pageTitle.append(arg) if pageTitle: # work on a single page page = pywikibot.Page(pywikibot.getSite(), ' '.join(pageTitle)) gen = iter([page]) if not gen: gen = genFactory.getCombinedGenerator() if not gen: pywikibot.showHelp('inline_images') else: preloadingGen = pagegenerators.PreloadingGenerator(gen) bot = InlineImagesRobot(preloadingGen) bot.run()
def main(*args): global bot # Disable cosmetic changes because we don't want to modify any page # content, so that we don't flood the histories with minor changes. config.cosmetic_changes = False #page generator gen = None genFactory = pagegenerators.GeneratorFactory() redirs = False # If the user chooses to work on a single page, this temporary array is # used to read the words from the page title. The words will later be # joined with spaces to retrieve the full title. pageTitle = [] for arg in pywikibot.handleArgs(*args): if genFactory.handleArg(arg): continue if arg == '-redir': redirs = True else: pageTitle.append(arg) pywikibot.Site().login() gen = genFactory.getCombinedGenerator() if not gen: if pageTitle: # work on a single page page = pywikibot.Page(pywikibot.Link(' '.join(pageTitle))) gen = iter([page]) else: pywikibot.showHelp() return preloadingGen = pagegenerators.PreloadingGenerator(gen) bot = TouchBot(preloadingGen, redirs) bot.run()
def main(): local_args = pywikibot.handleArgs() cache_dirs = None delete = False command = None for arg in local_args: if command == '': command = arg elif arg == '-delete': delete = True elif arg == '-password': command = 'has_password(entry)' elif arg == '-c': if command: pywikibot.error('Only one command may be executed.') exit(1) command = '' else: cache_dir = [arg] func = None if not cache_dirs: cache_dirs = ['apicache', 'tests/apicache'] # Also process the base directory, if it isnt the current directory if os.path.abspath(os.getcwd()) != pywikibot.config2.base_dir: cache_dirs += [ os.path.join(pywikibot.config2.base_dir, 'apicache') ] # Also process the user home cache, if it isnt the config directory if os.path.expanduser('~/.pywikibot') != pywikibot.config2.base_dir: cache_dirs += [ os.path.join(os.path.expanduser('~/.pywikibot'), 'apicache') ] if delete: action_func = lambda entry: entry._delete() else: action_func = lambda entry: pywikibot.output(entry) if command: try: command_func = eval('lambda entry: ' + command) except: pywikibot.exception() pywikibot.error(u'Can not compile command: %s' % command) exit(1) func = lambda entry: command_func(entry) and action_func(entry) else: func = action_func for cache_dir in cache_dirs: if os.path.isdir(cache_dir): if len(cache_dirs) > 1: pywikibot.output(u'Processing %s' % cache_dir) process_entries(cache_dir, func)
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(): featured = False gen = None # Process global args and prepare generator args parser local_args = pywikibot.handleArgs() genFactory = pagegenerators.GeneratorFactory() for arg in local_args: if arg == '-featured': featured = True else: genFactory.handleArg(arg) mysite = pywikibot.Site() if mysite.sitename() == 'wikipedia:nl': pywikibot.output( u'\03{lightred}There is consensus on the Dutch Wikipedia that bots should not be used to fix redirects.\03{default}') sys.exit() if featured: featuredList = i18n.translate(mysite, featured_articles) ref = pywikibot.Page(pywikibot.Site(), featuredList) gen = pagegenerators.ReferringPageGenerator(ref) gen = pagegenerators.NamespaceFilterPageGenerator(gen, [0]) if not gen: gen = genFactory.getCombinedGenerator() if gen: for page in pagegenerators.PreloadingGenerator(gen): workon(page) else: pywikibot.showHelp('fixing_redirects')
def main(): """ Process command line arguments and invoke BasicBot. """ # Process global arguments to determine desired site local_args = pywikibot.handleArgs() # 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() # The generator gives the pages that should be worked upon. gen = None # If dry is True, doesn't do any real changes, but only show # what would have been changed. dry = False # Parse command line arguments for arg in local_args: if arg.startswith("-dry"): dry = True else: genFactory.handleArg(arg) if not gen: gen = genFactory.getCombinedGenerator() if gen: # The preloading generator is responsible for downloading multiple # pages from the wiki simultaneously. gen = pagegenerators.PreloadingGenerator(gen) bot = BasicBot(gen, dry) bot.run() else: pywikibot.showHelp()
def main(): global mysite, linktrail, page start = [] for arg in pywikibot.handleArgs(): start.append(arg) if start: start = " ".join(start) else: start = "!" mysite = pywikibot.getSite() linktrail = mysite.linktrail() try: generator = pagegenerators.CategorizedPageGenerator( mysite.disambcategory(), start=start) except pywikibot.NoPage: pywikibot.output( "The bot does not know the disambiguation category for your wiki.") raise # only work on articles generator = pagegenerators.NamespaceFilterPageGenerator(generator, [0]) generator = pagegenerators.PreloadingGenerator(generator) pagestodo = [] pagestoload = [] for page in generator: if page.isRedirectPage(): continue linked = page.linkedPages() pagestodo.append((page, linked)) pagestoload += linked if len(pagestoload) > 49: pagestoload = pagegenerators.PreloadingGenerator(pagestoload) for page, links in pagestodo: workon(page, links) pagestoload = [] pagestodo = []
def main(): featured = False gen = None # 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() for arg in pywikibot.handleArgs(): if arg == '-featured': featured = True else: genFactory.handleArg(arg) mysite = pywikibot.getSite() if mysite.sitename() == 'wikipedia:nl': pywikibot.output( u'\03{lightred}There is consensus on the Dutch Wikipedia that bots should not be used to fix redirects.\03{default}') sys.exit() if featured: featuredList = i18n.translate(mysite, featured_articles) ref = pywikibot.Page(pywikibot.getSite(), featuredList) gen = pagegenerators.ReferringPageGenerator(ref) gen = pagegenerators.NamespaceFilterPageGenerator(gen, [0]) if not gen: gen = genFactory.getCombinedGenerator() if gen: for page in pagegenerators.PreloadingGenerator(gen): workon(page) else: pywikibot.showHelp('fixing_redirects')