How to use pywikibot - 10 common examples

To help you get started, we’ve selected a few pywikibot examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github wikimedia / pywikibot / tests / wikibase_edit_tests.py View on Github external
def test_WbGeoShape_edit(self):
        """Attempt adding a geo-shape with valid input."""
        # Clean the slate in preparation for test.
        testsite = self.get_repo()
        item = self._clean_item(testsite, 'P27199')

        # set new claim
        claim = pywikibot.page.Claim(testsite, 'P27199', datatype='geo-shape')
        commons_site = pywikibot.Site('commons', 'commons')
        page = pywikibot.Page(commons_site, 'Data:Lyngby Hovedgade.map')
        target = pywikibot.WbGeoShape(page)
        claim.setTarget(target)
        item.addClaim(claim)

        # confirm new claim
        item.get(force=True)
        claim = item.claims['P27199'][0]
        self.assertEqual(claim.getTarget(), target)
github wikimedia / pywikibot / tests / api_tests.py View on Github external
self.skipTest(
                '{0} is not running on a replicated database cluster.'
                .format(mysite)
            )
        mythrottle = DummyThrottle(mysite)
        mysite._throttle = mythrottle
        params = {'action': 'query',
                  'titles': self.get_mainpage().title(),
                  'maxlag': -1}
        req = api.Request(site=mysite, parameters=params)
        try:
            req.submit()
        except SystemExit:
            pass  # expected exception from DummyThrottle instance
        except api.APIError as e:
            pywikibot.warning(
                'Wrong api.lagpattern regex, cannot retrieve lag value')
            raise e
        self.assertIsInstance(mythrottle._lagvalue, int)
        self.assertGreaterEqual(mythrottle._lagvalue, 0)
        self.assertIsInstance(mythrottle.retry_after, int)
        self.assertGreaterEqual(mythrottle.retry_after, 0)
github wikimedia / pywikibot / tests / archivebot_tests.py View on Github external
{{User:MiszaBot/config
         |archive = Talk:Main_Page/archive
         |algo = old(30d)
         }}
         
         == A ==
         foo bar
         
         == B ==
         foo bar bar bar
        """
        site = self.get_site()
        page = pywikibot.Page(site, 'Talk:For-pywikibot-archivebot')
        tmpl = pywikibot.Page(site, 'User:MiszaBot/config')
        archiver = archivebot.PageArchiver(
            page=page, template=tmpl, salt='', force=False)
        page = archivebot.DiscussionPage(page, archiver)
        page.load_page()
        self.assertEqual([x.title for x in page.threads], ['A', 'B'])
github wikimedia / pywikibot / tests / page_tests.py View on Github external
def testImageAndDataRepository(self):
        """Test image_repository and data_repository page attributes."""
        site = self.get_site()
        p1 = pywikibot.Page(site, 'Help:Test page#Testing')
        self.assertIsInstance(p1.image_repository, pywikibot.site.APISite)
        self.assertEqual(p1.image_repository,
                         pywikibot.site.APISite('commons', 'commons'))

        p2 = pywikibot.Page(site, 'File:Jean-Léon Gérôme 003.jpg')
        self.assertIsInstance(p2.data_repository, pywikibot.site.APISite)
        self.assertEqual(p2.data_repository,
                         pywikibot.site.APISite('wikidata', 'wikidata'))
github wikimedia / pywikibot / tests / site_tests.py View on Github external
def testConstructors(self):
        """Test cases for site constructors."""
        if isinstance(self.site.family, pywikibot.family.WikimediaFamily):
            site = self.site
        else:
            site = None
        self.assertEqual(pywikibot.site.APISite.fromDBName('enwiki', site),
                         pywikibot.Site('en', 'wikipedia'))
        self.assertEqual(pywikibot.site.APISite.fromDBName('eswikisource', site),
                         pywikibot.Site('es', 'wikisource'))
        self.assertEqual(pywikibot.site.APISite.fromDBName('dewikinews', site),
                         pywikibot.Site('de', 'wikinews'))
        self.assertEqual(pywikibot.site.APISite.fromDBName('ukwikivoyage', site),
                         pywikibot.Site('uk', 'wikivoyage'))
        self.assertEqual(pywikibot.site.APISite.fromDBName('metawiki', site),
                         pywikibot.Site('meta', 'meta'))
        self.assertEqual(pywikibot.site.APISite.fromDBName('commonswiki', site),
                         pywikibot.Site('commons', 'commons'))
        self.assertEqual(pywikibot.site.APISite.fromDBName('wikidatawiki', site),
                         pywikibot.Site('wikidata', 'wikidata'))
github metakgp / metakgp-wiki / jobs / pywikibot / scripts / updatestatistics.py View on Github external
def update_list_of_pages(template, pages):
    template_page = pywikibot.Page(pywikibot.Link(template), pywikibot.Site())
    text = " This page is automatically generated. Changes will be overwritten, so '''do not modify'''.\n"
    for p in pages:
        text += "*[[%s]]\n" % p
    text = text.rstrip()
    if template_page.text == text:
        print template, 'unchanged, no edit made.'
        return
    else:
        print template, 'changed:'
        print text
        #diff = difflib.ndiff(template_page.text.splitlines(1),
        #                     text.splitlines(1))
        #for d in diff:
        #    print d,
        template_page.text = text
        template_page.save('Updated on ' +
github wikimedia / pywikibot / tests / pagegenerators_tests.py View on Github external
def test_recentchanges_ns(self):
        """Test recentchanges generator with namespace."""
        gf = pagegenerators.GeneratorFactory(site=self.site)
        gf.handleArg('-ns:1')
        gf.handleArg('-recentchanges:10')
        gen = gf.getCombinedGenerator()
        self.assertIsNotNone(gen)
        self.assertPagesInNamespaces(gen, 1)
github wikimedia / pywikibot / tests / pagegenerators_tests.py View on Github external
def test_prefixing_ns(self):
        """Test prefixindex generator with namespace filter."""
        gf = pagegenerators.GeneratorFactory(site=self.site)
        gf.handleArg('-ns:1')
        gf.handleArg('-prefixindex:a')
        gf.handleArg('-limit:10')
        gen = gf.getCombinedGenerator()
        self.assertIsNotNone(gen)
        self.assertPagesInNamespaces(gen, 1)
github wikimedia / pywikibot / tests / pagegenerators_tests.py View on Github external
def test_deque_preloading(self):
        """Test pages being added to a DequePreloadingGenerator."""
        mainpage = self.get_mainpage()

        pages = pywikibot.tools.DequeGenerator([mainpage])
        gen = pagegenerators.DequePreloadingGenerator(pages)
        pages_out = []
        for page in gen:
            pages_out.append(page)
            # Add a page to the generator
            if not page.isTalkPage():
                pages.extend([page.toggleTalkPage()])

        self.assertTrue(all(isinstance(page,
                                       pywikibot.Page) for page in pages_out))
        self.assertIn(mainpage, pages_out)
        self.assertIn(mainpage.toggleTalkPage(), pages_out)
        self.assertEqual(len(pages_out), 2)
        self.assertTrue(pages_out[1].isTalkPage())
github wikimedia / pywikibot / tests / pagegenerators_tests.py View on Github external
def test_brackets(self):
        """Test TextfilePageGenerator with brackets."""
        filename = join_data_path('pagelist-brackets.txt')
        site = self.get_site()
        titles = list(pagegenerators.TextfilePageGenerator(filename, site))
        self.assertEqual(len(titles), len(self.expected_titles))
        expected_titles = [
            expected_title[self.title_columns[site.namespaces[page.namespace()]
                                              .case]]
            for expected_title, page in zip(self.expected_titles, titles)]
        self.assertPageTitlesEqual(titles, expected_titles)