def test_nobots(self): """Test that {{nobots}} raise the appropriate exception.""" page = pywikibot.Page(self.site, 'User:John Vandenberg/nobots') with patch.object(config, 'ignore_bot_templates', False): self.assertRaisesRegex(OtherPageSaveError, 'nobots', page.save)
import bisect import json import os import re import pymysql os.environ['PYWIKIBOT_DIR'] = os.path.dirname(os.path.realpath(__file__)) import pywikibot import requests from config import config_page_name, host, password, user # pylint: disable=E0611,W0614 site = pywikibot.Site('zh', 'wikipedia') site.login() config_page = pywikibot.Page(site, config_page_name) cfg = config_page.text cfg = json.loads(cfg) print(json.dumps(cfg, indent=4, ensure_ascii=False)) if not cfg['enable']: exit('disabled\n') conn = pymysql.connect(host=host, user=user, password=password, charset="utf8") with conn.cursor() as cur: cur.execute('use zhwiki_p') cur.execute(""" SELECT rc_actor, actor_name, actor_user, up_value AS `nickname` FROM (
def handleArg(self, arg): """Parse one argument at a time. If it is recognized as an argument that specifies a generator, a generator is created and added to the accumulation list, and the function returns true. Otherwise, it returns false, so that caller can try parsing the argument. Call getCombinedGenerator() after all arguments have been parsed to get the final output generator. """ site = pywikibot.getSite() gen = None if arg.startswith('-filelinks'): fileLinksPageTitle = arg[11:] if not fileLinksPageTitle: fileLinksPageTitle = i18n.input( 'pywikibot-enter-file-links-processing') if fileLinksPageTitle.startswith(site.namespace(6) + ":"): fileLinksPage = pywikibot.ImagePage(site, fileLinksPageTitle) else: fileLinksPage = pywikibot.ImagePage(site, 'Image:' + fileLinksPageTitle) gen = FileLinksGenerator(fileLinksPage) elif arg.startswith('-unusedfiles'): if len(arg) == 12: gen = UnusedFilesGenerator() else: gen = UnusedFilesGenerator(number = int(arg[13:])) elif arg.startswith('-unwatched'): if len(arg) == 10: gen = UnwatchedPagesPageGenerator() else: gen = UnwatchedPagesPageGenerator(number = int(arg[11:])) elif arg.startswith('-usercontribs'): gen = UserContributionsGenerator(arg[14:]) elif arg.startswith('-withoutinterwiki'): if len(arg) == 17: gen = WithoutInterwikiPageGenerator() else: gen = WithoutInterwikiPageGenerator(number = int(arg[18:])) elif arg.startswith('-interwiki'): title = arg[11:] if not title: title = i18n.input('pywikibot-enter-page-processing') page = pywikibot.Page(pywikibot.Link(title, pywikibot.Site())) gen = InterwikiPageGenerator(page) elif arg.startswith('-recentchanges'): if len(arg) >= 15: gen = RecentChangesPageGenerator(total=int(arg[15:])) else: gen = RecentChangesPageGenerator(total=60) gen = DuplicateFilterPageGenerator(gen) elif arg.startswith('-file'): textfilename = arg[6:] if not textfilename: textfilename = pywikibot.input( u'Please enter the local file name:') gen = TextfilePageGenerator(textfilename) elif arg.startswith('-namespace'): if len(arg) == len('-namespace'): self.namespaces.append( pywikibot.input(u'What namespace are you filtering on?')) else: self.namespaces.extend(arg[len('-namespace:'):].split(",")) return True elif arg.startswith('-ns'): if len(arg) == len('-ns'): self.namespaces.append( pywikibot.input(u'What namespace are you filtering on?')) else: self.namespaces.extend(arg[len('-ns:'):].split(",")) return True elif arg.startswith('-step'): if len(arg) == len('-step'): self.step = int(pywikibot.input("What is the step value?")) else: self.step = int(arg[len('-step:'):]) return True elif arg.startswith('-limit'): if len(arg) == len('-limit'): self.limit = int(pywikibot.input("What is the limit value?")) else: self.limit = int(arg[len('-limit:'):]) return True elif arg.startswith('-catr'): gen = self.getCategoryGen(arg, len('-catr'), recurse = True) elif arg.startswith('-category'): gen = self.getCategoryGen(arg, len('-category')) elif arg.startswith('-cat'): gen = self.getCategoryGen(arg, len('-cat')) elif arg.startswith('-subcatsr'): gen = self.setSubCategoriesGen(arg, 9, recurse = True) elif arg.startswith('-subcats'): gen = self.setSubCategoriesGen(arg, 8) elif arg.startswith('-page'): if len(arg) == len('-page'): gen = [pywikibot.Page( pywikibot.Link( pywikibot.input( u'What page do you want to use?'), pywikibot.getSite()) )] else: gen = [pywikibot.Page(pywikibot.Link(arg[len('-page:'):], pywikibot.getSite()) )] elif arg.startswith('-uncatfiles'): gen = UnCategorizedImageGenerator() elif arg.startswith('-uncatcat'): gen = UnCategorizedCategoryGenerator() elif arg.startswith('-uncat'): gen = UnCategorizedPageGenerator() elif arg.startswith('-ref'): referredPageTitle = arg[5:] if not referredPageTitle: referredPageTitle = pywikibot.input( u'Links to which page should be processed?') referredPage = pywikibot.Page(pywikibot.Link(referredPageTitle, pywikibot.Site())) gen = ReferringPageGenerator(referredPage) elif arg.startswith('-links'): linkingPageTitle = arg[7:] if not linkingPageTitle: linkingPageTitle = pywikibot.input( u'Links from which page should be processed?') linkingPage = pywikibot.Page(pywikibot.Link(linkingPageTitle, pywikibot.Site())) gen = LinkedPageGenerator(linkingPage) elif arg.startswith('-weblink'): url = arg[9:] if not url: url = pywikibot.input( u'Pages with which weblink should be processed?') gen = LinksearchPageGenerator(url) elif arg.startswith('-transcludes'): transclusionPageTitle = arg[len('-transcludes:'):] if not transclusionPageTitle: transclusionPageTitle = pywikibot.input( u'Pages that transclude which page should be processed?') transclusionPage = pywikibot.Page( pywikibot.Link(transclusionPageTitle, defaultNamespace=10, source=pywikibot.Site())) gen = ReferringPageGenerator(transclusionPage, onlyTemplateInclusion=True) elif arg.startswith('-start'): firstPageTitle = arg[7:] if not firstPageTitle: firstPageTitle = pywikibot.input( u'At which page do you want to start?') firstpagelink = pywikibot.Link(firstPageTitle, pywikibot.Site()) namespace = firstpagelink.namespace firstPageTitle = firstpagelink.title gen = AllpagesPageGenerator(firstPageTitle, namespace, includeredirects=False) elif arg.startswith('-prefixindex'): prefix = arg[13:] namespace = None if not prefix: prefix = pywikibot.input( u'What page names are you looking for?') gen = PrefixingPageGenerator(prefix=prefix) elif arg.startswith('-newimages'): limit = arg[11:] or pywikibot.input( u'How many images do you want to load?') gen = NewimagesPageGenerator(total=int(limit)) elif arg.startswith('-newpages'): if len(arg) >= 10: gen = NewpagesPageGenerator(total=int(arg[10:])) else: gen = NewpagesPageGenerator(total=60) elif arg.startswith('-imagesused'): imagelinkstitle = arg[len('-imagesused:'):] if not imagelinkstitle: imagelinkstitle = pywikibot.input( u'Images on which page should be processed?') imagelinksPage = pywikibot.Page(pywikibot.Link(imagelinkstitle, pywikibot.Site())) gen = ImagesPageGenerator(imagelinksPage) elif arg.startswith('-search'): mediawikiQuery = arg[8:] if not mediawikiQuery: mediawikiQuery = pywikibot.input( u'What do you want to search for?') # In order to be useful, all namespaces are required gen = SearchPageGenerator(mediawikiQuery, namespaces = []) elif arg.startswith('-google'): gen = GoogleSearchPageGenerator(arg[8:]) elif arg.startswith('-titleregex'): if len(arg) == 6: regex = pywikibot.input( u'What page names are you looking for?') else: regex = arg[7:] gen = RegexFilterPageGenerator(pywikibot.Site().allpages(), regex) elif arg.startswith('-yahoo'): gen = YahooSearchPageGenerator(arg[7:]) else: pass if gen: self.gens.append(gen) return True else: return False
if name not in wd: continue if '. gads' in name: continue wddata = wd[name] row = "|-\n| [[{}]] || {} || {}".format(name, article[2], len(wddata)) rows.append(row) counter +=1 """ cur_date = datetime.now().strftime("%Y-%m-%d") table = '\n'.join(rows[:200]) reportpage = """Atjaunināts: {} {{| class="wikitable sortable" |- ! Nr.p.k. !! Raksts !! Raksta garums !! Vērtīgo/labo rakstu<br>skaits citu valodu<br>Vikipēdijās {} |}} """.format(cur_date, table) lvpage = pywikibot.Page(lvsite, 'Dalībnieks:Edgars2007/Īsākie raksti') lvpage.text = reportpage lvpage.save(summary='Bots: atjaunināts', botflag=False, minor=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: str """ global workingcat, parentcats global checked global excludefile checked = set() workingcatname = '' options = {} local_args = pywikibot.handle_args(args) for arg in local_args: option = arg[1:] if not arg.startswith('-'): if not workingcatname: options['catnames'] = workingcatname = arg else: pywikibot.warning( 'Working category "{}" is already given.'.format( workingcatname)) else: options[option] = True if not workingcatname: pywikibot.bot.suggest_help(missing_parameters=['working category']) return mysite = pywikibot.Site() summary = i18n.twtranslate(mysite, 'makecat-create', {'cat': workingcatname}) bot = MakeCatBot(site=mysite, summary=summary, **options) workingcat = pywikibot.Category( mysite, '{0}{1}'.format(mysite.namespaces.CATEGORY, workingcatname)) filename = pywikibot.config.datafilepath( 'category', workingcatname.encode('ascii', 'xmlcharrefreplace').decode('ascii') + '_exclude.txt') try: with codecs.open(filename, 'r', encoding=mysite.encoding()) as f: for line in f.readlines(): # remove leading and trailing spaces, LF and CR line = line.strip() if not line: continue pl = pywikibot.Page(mysite, line) checked.add(pl) excludefile = codecs.open(filename, 'a', encoding=mysite.encoding()) except IOError: # File does not exist excludefile = codecs.open(filename, 'w', encoding=mysite.encoding()) # Get parent categories in order to `removeparent` try: parentcats = workingcat.categories() except pywikibot.Error: parentcats = [] # Do not include articles already in subcats; only checking direct subcats subcatlist = list(workingcat.subcategories()) if subcatlist: subcatlist = pagegenerators.PreloadingGenerator(subcatlist) for cat in subcatlist: artlist = list(cat.articles()) for page in artlist: checked.add(page) # Fetch articles in category, and mark as already checked (seen) # If category is empty, ask user if they want to look for pages # in a different category. articles = list(workingcat.articles(content=True)) if not articles: pywikibot.output('Category {} does not exist or is empty. ' 'Which page to start with?'.format(workingcatname)) answer = pywikibot.input('(Default is [[{}]]):'.format(workingcatname)) if not answer: answer = workingcatname pywikibot.output('' + answer) pl = pywikibot.Page(mysite, answer) articles = [pl] for pl in articles: checked.add(pl) bot.include(pl) bot.run()
def get_all_pages(self): template = pywikibot.Page(self.site, self.template_name, ns=10) return pagegenerators.ReferringPageGenerator( template, onlyTemplateInclusion=True)
def BACK(site, name, hide): p = pywikibot.Page(site, name, ns=10) return [ page for page in p.getReferences(follow_redirects=False, only_template_inclusion=True) ]
def init_page(self, item) -> pywikibot.Page: """Get the tuple and return the page object to be processed.""" title, content = item page = pywikibot.Page(self.site, title) page.text = content.strip() return super().init_page(page)
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 run(self): self.page = pywikibot.Page(self.site, self.change['title'], ns=self.change['namespace']) self.output(u"Handling") if self.page.isRedirectPage(): self.output(u"Redirect") return if self.page.namespace() == 4: # Project pages needs attention (__NEWSECTIONLINK__) if not self.isDiscussion(self.page): self.output(u"Not a discussion") return user = pywikibot.User(self.site, self.change['user']) if self.isOptout(user): self.output(u"%s opted-out" % user) return # diff-reading. if self.change['type'] == 'new': old_text = '' else: old_text = self.page.getOldVersion(self.change['revision']['old']) new_text = self.page.getOldVersion(self.change['revision']['new']) if "{{speedy" in new_text.lower(): self.output(u"{{speedy -- ignored") return diff = PatchManager(old_text.split("\n") if old_text else [], new_text.split("\n"), by_letter=True) diff.print_hunks() tosignstr = False tosignnum = False for block in diff.blocks: if block[0] < 0: continue hunk = diff.hunks[block[0]] group = hunk.group # print ("%7s a[%d:%d] (%s) b[%d:%d] (%s)" % # (block[0], block[1][0], block[1][1], hunk.a[block[1][0]:block[1][1]], block[2][0], block[2][1], hunk.b[block[2][0]:block[2][1]])) for tag, i1, i2, j1, j2 in group: # print ("%7s a[%d:%d] (%s) b[%d:%d] (%s)" % # (tag, i1, i2, hunk.a[i1:i2], j1, j2, hunk.b[j1:j2])) if tag == 'insert': for j in range(j1, j2): line = hunk.b[j] if self.page == user.getUserTalkPage( ) or self.page.title().startswith( user.getUserTalkPage().title() + '/'): if "{{" in line.lower(): self.output( u"User adding templates to his own talk page -- ignored" ) return excluderegextest = self.matchExcludeRegex(line) if excluderegextest is not None: self.output(u"%s -- ignored" % excluderegextest) return if self.isComment(line): tosignnum = j tosignstr = line if self.isSigned(user, tosignstr): self.output(u"Signed") return if tosignstr is False: self.output(u"No inserts") return if self.isSigned(user, tosignstr): self.output(u"Signed") return if not self.isFreqpage(self.page): self.output(u"Waiting") time.sleep(60) pass currenttext = self.page.get(force=True).split("\n") if currenttext[tosignnum] == tosignstr: currenttext[tosignnum] += self.getSignature(tosignstr, user) elif currenttext.count(tosignstr) == 1: currenttext[currenttext.index(tosignstr)] += self.getSignature( tosignstr, user) else: self.output(u"Line no longer found, probably signed") return summary = u'Signing comment by %s - "%s"' % (self.userlink(user), self.change['comment']) self.userPut(self.page, self.page.get(), u"\n".join(currenttext), comment=summary) # self.notify(user) {{subst:Please sign}} -- ignore {{bots}} if self.controller.checknotify(user): self.output(u"Notifying %s" % user) talk = user.getUserTalkPage() if talk.isRedirectPage(): talk = talk.getRedirectTarget() try: talktext = talk.get(force=True, get_redirect=True) + u'\n\n' except pywikibot.NoPage: talktext = '' talktext += u'{{subst:Please sign}} --~~~~' self.userPut( talk, talk.text, talktext, comment= u"Added {{subst:[[Template:Please sign|Please sign]]}} note.", minor=False)
def target_page(self): """Return target page object.""" if not hasattr(self, '_target_page'): self._target_page = pywikibot.Page(self.site, self.target_title) return self._target_page
import json import sys import csv from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer import urlparse ''' variables will add in input later ''' PORT_NUMBER = 8080 varietal = "Merlot" region = "Long Island" year = '2008' enwp = pywikibot.Site('en', 'wikipedia') page = pywikibot.Page(enwp, varietal) wikitext=page.get() wikicode=mwparserfromhell.parse(wikitext) templates=wikicode.filter_templates() infobox = templates[1] climate = "" flavor = "" detail_desc = "" vintage = "" def summary(): ''' Use the Google Knowledge Graph API to get a general summary about this wine. Right now, the summary can be more than a sentence, but it can be trimmed down. '''
def test_nocreate(self): """Test that Page.save with nocreate fails if page does not exist.""" page = pywikibot.Page(self.site, 'User:John_Vandenberg/no_recreate') self.assertRaises(NoCreateError, page.save, nocreate=True)
def test_createonly(self): """Test that Page.save with createonly fails if page exists.""" page = pywikibot.Page(self.site, 'User:Xqt/sandbox') self.assertRaises(PageCreatedConflict, page.save, createonly=True)
def test_nobots(self): """Test that {{nobots}} raise the appropriate exception.""" page = pywikibot.Page(self.site, 'User:John Vandenberg/nobots') self.assertRaisesRegex(OtherPageSaveError, 'nobots', page.save)
def __init__(self, generator, templates, **kwargs): """ Constructor. @param generator: the pages to work on @type generator: iterable @param templates: a dictionary which maps old template names to their replacements. If remove or subst is True, it maps the names of the templates that should be removed/resolved to None. @type templates: dict """ self.availableOptions.update({ 'subst': False, 'remove': False, 'summary': None, 'addedCat': None, }) Bot.__init__(self, generator=generator, **kwargs) self.templates = templates # get edit summary message if it's empty if not self.getOption('summary'): comma = self.site.mediawiki_message('comma-separator') params = {'list': comma.join(self.templates.keys()), 'num': len(self.templates)} if self.getOption('remove'): self.options['summary'] = i18n.twtranslate( self.site, 'template-removing', params) elif self.getOption('subst'): self.options['summary'] = i18n.twtranslate( self.site, 'template-substituting', params) else: self.options['summary'] = i18n.twtranslate( self.site, 'template-changing', params) replacements = [] exceptions = {} builder = textlib._MultiTemplateMatchBuilder(self.site) for old, new in self.templates.items(): templateRegex = builder.pattern(old) if self.getOption('subst') and self.getOption('remove'): replacements.append((templateRegex, r'{{subst:%s\g<parameters>}}' % new)) exceptions['inside-tags'] = ['ref', 'gallery', 'poem', 'pagelist', ] elif self.getOption('subst'): replacements.append((templateRegex, r'{{subst:%s\g<parameters>}}' % old)) exceptions['inside-tags'] = ['ref', 'gallery', 'poem', 'pagelist', ] elif self.getOption('remove'): replacements.append((templateRegex, '')) else: template = pywikibot.Page(self.site, new, ns=10) if not template.exists(): pywikibot.warning(u'Template "%s" does not exist.' % new) if not pywikibot.input_yn('Do you want to proceed anyway?', default=False, automatic_quit=False): continue replacements.append((templateRegex, r'{{%s\g<parameters>}}' % new)) super(TemplateRobot, self).__init__( generator, replacements, exceptions, always=self.getOption('always'), addedCat=self.getOption('addedCat'), summary=self.getOption('summary'))
def process_one_page(self, page_title): page = pywikibot.Page(self.site, page_title) pywikibot.output("Processing page %s" % page.title()) self.process_page(page)
def main(*args): """ Process command line arguments and invoke bot. If args is an empty list, sys.argv is used. @param args: command line arguments @type args: list of unicode """ templateNames = [] templates = {} options = {} # If xmlfilename is None, references will be loaded from the live wiki. xmlfilename = None user = None skip = False timestamp = None # read command line parameters local_args = pywikibot.handle_args(args) # Avoid conflicts with pagegenerators.py parameters. if any(arg.startswith('-category:') for arg in local_args): warn('-category (to append a category to each edited page) has been' ' renamed to -addcat; make sure you are using the correct param.', ArgumentDeprecationWarning) site = pywikibot.Site() genFactory = pagegenerators.GeneratorFactory() for arg in local_args: if arg == '-remove': options['remove'] = True elif arg == '-subst': options['subst'] = True elif arg == '-assubst': options['subst'] = options['remove'] = True elif arg == '-always': options['always'] = True elif arg.startswith('-xml'): if len(arg) == 4: xmlfilename = pywikibot.input( u'Please enter the XML dump\'s filename: ') else: xmlfilename = arg[5:] elif arg.startswith('-addcat:'): options['addedCat'] = arg[len('-addcat:'):] elif arg.startswith('-summary:'): options['summary'] = arg[len('-summary:'):] elif arg.startswith('-onlyuser:'******'-onlyuser:'******'-skipuser:'******'-skipuser:'******'-timestamp:'): timestamp = arg[len('-timestamp:'):] else: if not genFactory.handleArg(arg): templateName = pywikibot.Page(site, arg, ns=10) templateNames.append(templateName.title(withNamespace=False)) if not templateNames: pywikibot.bot.suggest_help(missing_parameters=['templates']) return False if options.get('subst', False) ^ options.get('remove', False): for templateName in templateNames: templates[templateName] = None else: try: for i in range(0, len(templateNames), 2): templates[templateNames[i]] = templateNames[i + 1] except IndexError: pywikibot.output('Unless using solely -subst or -remove, ' 'you must give an even number of template names.') return oldTemplates = [] for templateName in templates.keys(): oldTemplate = pywikibot.Page(site, templateName, ns=10) oldTemplates.append(oldTemplate) if xmlfilename: builder = textlib._MultiTemplateMatchBuilder(site) predicate = builder.search_any_predicate(oldTemplates) gen = XMLDumpPageGenerator( xmlfilename, site=site, text_predicate=predicate) else: gen = genFactory.getCombinedGenerator() if not gen: gens = [ pagegenerators.ReferringPageGenerator(t, onlyTemplateInclusion=True) for t in oldTemplates ] gen = pagegenerators.CombinedPageGenerator(gens) gen = pagegenerators.DuplicateFilterPageGenerator(gen) if user: gen = pagegenerators.UserEditFilterGenerator(gen, user, timestamp, skip, max_revision_depth=100, show_filtered=True) if not genFactory.gens: # make sure that proper namespace filtering etc. is handled gen = genFactory.getCombinedGenerator(gen) if not genFactory.nopreload: gen = pagegenerators.PreloadingGenerator(gen) bot = TemplateRobot(gen, templates, site=site, **options) bot.run()
def findTranslated(self, page, oursite=None): quiet = self.getOption('quiet') if not oursite: oursite = self.site if page.isRedirectPage(): page = page.getRedirectTarget() ourpage = None for link in page.iterlanglinks(): if link.site == oursite: ourpage = pywikibot.Page(link) break if not ourpage: if not quiet: pywikibot.output('%s -> no corresponding page in %s' % (page.title(), oursite)) elif ourpage.section(): pywikibot.output('%s -> our page is a section link: %s' % (page.title(), ourpage.title())) elif not ourpage.exists(): pywikibot.output("%s -> our page doesn't exist: %s" % (page.title(), ourpage.title())) else: if ourpage.isRedirectPage(): ourpage = ourpage.getRedirectTarget() pywikibot.output('%s -> corresponding page is %s' % (page.title(), ourpage.title())) if ourpage.namespace() != 0: pywikibot.output('%s -> not in the main namespace, skipping' % page.title()) elif ourpage.isRedirectPage(): pywikibot.output('%s -> double redirect, skipping' % page.title()) elif not ourpage.exists(): pywikibot.output("%s -> page doesn't exist, skipping" % ourpage.title()) else: backpage = None for link in ourpage.iterlanglinks(): if link.site == page.site: backpage = pywikibot.Page(link) break if not backpage: pywikibot.output('%s -> no back interwiki ref' % page.title()) elif backpage == page: # everything is ok yield ourpage elif backpage.isRedirectPage(): backpage = backpage.getRedirectTarget() if backpage == page: # everything is ok yield ourpage else: pywikibot.output( '%s -> back interwiki ref target is redirect to %s' % (page.title(), backpage.title())) else: pywikibot.output('%s -> back interwiki ref target is %s' % (page.title(), backpage.title()))
def main(): "The main function." wiki = pywikibot.Site("en", "wikipedia") wiki.login() global wikitools_wiki wikitools_login() wpgo = pywikibot.Page(wiki, "Wikipedia:Goings-on") wpgo_content = wpgo.get() new_fc = wpgo_content[wpgo_content.find("==New featured content=="):] # Trim it down to just the list of featured content new_fc = new_fc[:new_fc.find("|-") - 2] # Remove the section heading new_fc = new_fc[len("==New featured content=="):] # Create fc_cats, which looks like this: {type: [title of content]} fc_cats = dict() for fc_cat in re.finditer(WP_GO_HEADING, new_fc): fc_cat_name = fc_cat.groups()[0] fc_cat_raw_list = new_fc[fc_cat.start():] fc_cat_raw_list = fc_cat_raw_list[len(fc_cat_name) + 1:] next_heading = re.search(WP_GO_HEADING, fc_cat_raw_list) if next_heading: fc_cat_raw_list = fc_cat_raw_list[:next_heading.start()] fc_cat_raw_list = fc_cat_raw_list.strip() # Now that we have just the list, parse out the items for fc_item in re.finditer(WP_GO_ITEM, fc_cat_raw_list): name, _, label, date = fc_item.groups() print u"{} (a {}) was promoted on {}".format( label if label else name, fc_cat_name[:-1], date) fc_cats[fc_cat_name] = fc_cats.get(fc_cat_name, []) + [(name, label, date)] # Get notification metadata for fc_cat, fc_items in fc_cats.items(): def add_metadata(fc_item): name, label, date = fc_item nom_link = "Wikipedia:Featured " + fc_cat[:-1] + " candidates/" if fc_cat == "pictures": nom_link += label[2:-2] if "''" in label else label #if not WikitoolsPage(wikitools_wiki, title=nom_link).exists: if not wiki.page_exists(nom_link): print(nom_link + " DOESN'T EXIST") else: nom_link += name[2:-2] if "''" in name else name nom_link += "/archive1" return (name, label, date, nom_link) fc_cats[fc_cat] = map(add_metadata, fc_items) # Build "report" report = "" for fc_cat, fc_items in fc_cats.items(): report += "\n\n===Featured {}===".format(fc_cat) report += "\n{} {} were promoted this week.".format( len(fc_items), FC_LINKS[fc_cat]) for fc_item in fc_items: name, label, date, nom_link = fc_item piped = "|" + label if label else "" report += u"\n* '''[[{}{}]]''' <small>([[{}|nominated]] by [[User:Example|Example]])</small> Description.".format( name, piped, nom_link) report = report.strip() # Write report to Wikipedia report_page = WikitoolsPage(wikitools_wiki, title="User:APersonBot/sandbox") print("Editing report page...") result = report_page.edit(text=report.encode("ascii", "ignore"), bot=True, summary="Test FC report") if result[u"edit"][u"result"] == u"Success": print "Success!" else: print "Error! Couldn't write report - result: {}".format(str(result))
name = article[0].replace('_',' ') if name not in wd: continue if '. gads' in name: continue wddata = wd[name] row = "|-\n| [[{}]] || {} || {}".format(name, article[2], len(wddata)) rows.append(row) counter +=1 """ cur_date = datetime.now().strftime("%Y-%m-%d") table = '\n'.join(rows[:200]) reportpage = """Upd: {} {{| class="wikitable sortable" |- ! Nr.p.k. !! Raksts !! Raksta garums !! FA/GA {} |}} """.format(cur_date, table) #with open("isakieraksti.txt", "w", encoding='utf-8') as file: # file.write(str(for_list)) lvpage = pywikibot.Page(lvsite, u'User:Edgars2007/Shortest') lvpage.text = reportpage lvpage.save(summary=u'Bots: upd', botflag=False, minor=False)
def __init__(self, generator, templates: dict, **kwargs) -> None: """ Initializer. @param generator: the pages to work on @type generator: iterable @param templates: a dictionary which maps old template names to their replacements. If remove or subst is True, it maps the names of the templates that should be removed/resolved to None. """ self.available_options.update({ 'addcat': None, 'remove': False, 'subst': False, 'summary': None, }) SingleSiteBot.__init__(self, generator=generator, **kwargs) self.templates = templates # get edit summary message if it's empty if not self.opt.summary: comma = self.site.mediawiki_message('comma-separator') params = { 'list': comma.join(self.templates.keys()), 'num': len(self.templates) } if self.opt.remove: tw_key = 'template-removing' elif self.opt.subst: tw_key = 'template-substituting' else: tw_key = 'template-changing' self.opt.summary = i18n.twtranslate(self.site, tw_key, params) replacements = [] exceptions = {} builder = textlib._MultiTemplateMatchBuilder(self.site) for old, new in self.templates.items(): template_regex = builder.pattern(old) if self.opt.subst and self.opt.remove: replacements.append( (template_regex, r'{{subst:%s\g<parameters>}}' % new)) exceptions['inside-tags'] = [ 'ref', 'gallery', 'poem', 'pagelist', ] elif self.opt.subst: replacements.append( (template_regex, r'{{%s:%s\g<parameters>}}' % (self.opt.subst, old))) exceptions['inside-tags'] = [ 'ref', 'gallery', 'poem', 'pagelist', ] elif self.opt.remove: separate_line_regex = re.compile( r'^[*#:]* *{0} *\n'.format(template_regex.pattern), re.DOTALL | re.MULTILINE) replacements.append((separate_line_regex, '')) spaced_regex = re.compile( r' +{0} +'.format(template_regex.pattern), re.DOTALL) replacements.append((spaced_regex, ' ')) replacements.append((template_regex, '')) else: template = pywikibot.Page(self.site, new, ns=10) if not template.exists(): pywikibot.warning( 'Template "{0}" does not exist.'.format(new)) if not pywikibot.input_yn('Do you want to proceed anyway?', default=False, automatic_quit=False): continue replacements.append( (template_regex, r'{{%s\g<parameters>}}' % new)) super().__init__(generator, replacements, exceptions, always=self.opt.always, addcat=self.opt.addcat, summary=self.opt.summary)
def treat_page(self): """Work on current page and ask to add article to category.""" global checked global excludefile pl = self.current_page ctoshow = 500 pywikibot.output('') pywikibot.output('== {} =='.format(pl.title())) while True: answer = pywikibot.input_choice('Add to category {}?'.format( self.workingcatname), self.treat_options, default='i') if answer == 'y': self.include(pl) break if answer == 'c': self.include(pl, realinclude=False) break if answer == 'k': if pl.exists() and not pl.isRedirectPage(): linkterm = pywikibot.input( 'In what manner should it be alphabetized?') self.include(pl, linkterm=linkterm) break self.include(pl) break if answer == 'n': excludefile.write('%s\n' % pl.title()) break if answer == 'i': break if answer in 'er': self.treat_options = self.option_bar[answer] elif answer == 'h': pywikibot.output(""" [y]es: Add the page and check links') [n]o: Never add the page, saved to exclusion list [i]gnore: Neither do not add the page not check links [m]ore: show more content of the page starting from the beginning sort [k]ey: Add with sort key like [[Category|Title]] [s]kip: Add the page, but skip checking links [c]heck: Do not add the page, but do check links [o]ther: Add another page [l]ist: Show a list of the pages to check [e]xtend: A more extended option list [r]educe: Reduce option list [q]uit: Save exclusion list and exit this script """) elif answer == 'o': pagetitle = pywikibot.input('Specify page to add:') page = pywikibot.Page(pywikibot.Site(), pagetitle) if page not in checked: self.include(page) elif answer == 's': if not pl.exists(): pywikibot.output('Page does not exist; not added.') elif pl.isRedirectPage(): pywikibot.output( 'Redirect page. Will be included normally.') self.include(pl, realinclude=False) else: self.include(pl, checklinks=False) break elif answer == 'l': length = len(self.tocheck) pywikibot.output( 'Number of pages still to check: {}'.format(length)) if length: pywikibot.output('Pages to be checked:') pywikibot.output( fill(' - '.join(page.title() for page in self.tocheck))) self.highlight_title(page) elif answer == 'm': self.highlight_title(pl, ctoshow > 500) if pl.exists(): pywikibot.output(pl.text[0:ctoshow]) else: pywikibot.output('Page does not exist.') ctoshow += 500
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 """ 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) site = pywikibot.Site() gen_factory = pagegenerators.GeneratorFactory() for arg in local_args: if arg == '-remove': options['remove'] = True elif arg.startswith('-subst'): options['subst'] = arg[len('-subst:'):] + 'subst' assert options['subst'] in ('subst', 'safesubst') elif arg == '-assubst': options['subst'] = '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['addcat'] = 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.handle_arg(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 bool(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 = (t.getReferences(only_template_inclusion=True, follow_redirects=False) 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()
# put list of alternatives into listbox self.list = list # find required area laenge = len(list) maxbreite = 0 for i in range(laenge): # cycle through all listitems to find maxlength if len(list[i]) + len(str(i)) > maxbreite: maxbreite = len(list[i]) + len(str(i)) # show list as formerly in DOS-window self.listbox.insert(Tkinter.END, str(i) + ' - ' + list[i]) # set optimized height & width self.listbox.config(height=laenge, width=maxbreite + 2) # wait for user to push a button which will destroy (close) the window return self.list if __name__ == "__main__": import pywikibot try: root = Tkinter.Tk() root.resizable(width=Tkinter.FALSE, height=Tkinter.FALSE) root.title("pywikibot GUI") page = pywikibot.Page(pywikibot.Site(), u'Main Page') content = page.get() myapp = EditBoxWindow(root) myapp.bind("<Control-d>", myapp.debug) v = myapp.edit(content, highlight=page.title()) finally: pywikibot.stopme()
def test_protected(self): """Test that protected titles raise the appropriate exception.""" if self.site._username[1]: raise unittest.SkipTest('Testing failure of edit protected with a sysop account') page = pywikibot.Page(self.site, 'Wikipedia:Create a new page') self.assertRaises(LockedPage, page.save)
import os import re import time from datetime import datetime os.environ['PYWIKIBOT_DIR'] = os.path.dirname(os.path.realpath(__file__)) import pywikibot from config import config_page_name # pylint: disable=E0611,W0614 os.environ['TZ'] = 'UTC' site = pywikibot.Site() site.login() config_page = pywikibot.Page(site, config_page_name) cfg = config_page.text cfg = json.loads(cfg) print(json.dumps(cfg, indent=4, ensure_ascii=False)) if not cfg["enable"]: exit("disabled\n") mainPage = pywikibot.Page(site, cfg["main_page_name"]) text = mainPage.text rndstr = hashlib.md5(str(time.time()).encode()).hexdigest() text = re.sub(r'^(===[^=]+===)$', rndstr + r'\1', text, flags=re.M) text = text.split(rndstr) mainPageText = text[0].strip()
def test_spam(self): """Test that spam in content raise the appropriate exception.""" page = pywikibot.Page(self.site, 'Wikipedia:Sandbox') page.text = 'http://badsite.com' self.assertRaisesRegex(SpamfilterError, 'badsite.com', page.save)
def InterwikiPageGenerator(page): """Iterator over all interwiki (non-language) links on a page.""" for link in page.interwiki(): yield pywikibot.Page(link)
def test_titleblacklist(self): """Test that title blacklist raise the appropriate exception.""" page = pywikibot.Page(self.site, 'User:UpsandDowns1234/Blacklisttest') self.assertRaises(TitleblacklistError, page.save)