How to use the pywikibot.Category function in pywikibot

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 / category_tests.py View on Github external
def test_redirects(self):
        """Test the redirects method."""
        site = self.get_site()
        cat1 = pywikibot.Category(site, 'Category:Fonts')
        cat2 = pywikibot.Category(site, 'Category:Typefaces')

        self.assertTrue(cat1.isCategoryRedirect())
        self.assertFalse(cat2.isCategoryRedirect())

        # The correct target category if fetched.
        tgt = cat1.getCategoryRedirectTarget()
        self.assertEqual(tgt, cat2)

        # Raise exception if target is fetched for non Category redirects.
        self.assertRaisesRegex(pywikibot.IsNotRedirectPage,
                               self.NOREDIRECTPAGE_RE,
                               cat2.getCategoryRedirectTarget)
github wikimedia / pywikibot / tests / textlib_tests.py View on Github external
def test_category_format_Category(self):
        """Test formatting categories as Category instances."""
        data = [pywikibot.Category(self.site, 'Cat1'),
                pywikibot.Category(self.site, 'Cat2')]
        self.assertEqual(self.catresult,
                         textlib.categoryFormat(data, self.site))
github wikimedia / pywikibot / tests / category_tests.py View on Github external
def test_init_dry(self):
        """Test the category's __init__."""
        site = self.get_site()
        cat_normal = pywikibot.Category(site, 'Category:Foo')
        self.assertEqual(cat_normal.title(with_ns=False), 'Foo')
        self.assertEqual(cat_normal.namespace(), 14)

        cat_missing = pywikibot.Category(site, 'Foo')
        self.assertEqual(cat_missing.title(with_ns=False), 'Foo')
        self.assertEqual(cat_missing.namespace(), 14)

        cat_duplicate = pywikibot.Category(site, 'Category:Category:Foo')
        self.assertEqual(cat_duplicate.title(with_ns=False), 'Category:Foo')
        self.assertEqual(cat_duplicate.namespace(), 14)

        cat_dup_ns = pywikibot.Category(site, 'Category:Wikipedia:Test')
        self.assertTrue(cat_dup_ns.title(with_ns=False), 'Page:Foo')
        self.assertTrue(cat_dup_ns.namespace(), 14)

        self.assertRaisesRegex(ValueError, self.NOCATEGORYNAMESPACE_RE,
github wikimedia / pywikibot / tests / category_tests.py View on Github external
def test_aslink(self):
        """Test the title method with as_link=True."""
        site = self.get_site()
        cat = pywikibot.Category(site, 'Category:Wikipedia Categories')
        self.assertEqual(cat.title(as_link=True, insite=cat.site),
                         '[[Category:Wikipedia Categories]]')
        cat_section = pywikibot.Category(site,
                                         'Category:Wikipedia Categories#Foo')
        self.assertEqual(
            cat_section.title(as_link=True, insite=cat_section.site),
            '[[Category:Wikipedia Categories#Foo]]')
        cat_dup = pywikibot.Category(site, 'Category:Wikipedia:Test')
        self.assertEqual(cat_dup.title(as_link=True, insite=cat_dup.site),
                         '[[Category:Wikipedia:Test]]')
github wikimedia / pywikibot / tests / category_tests.py View on Github external
def test_is_empty(self):
        """Test if category is empty or not."""
        site = self.get_site()
        cat_empty = pywikibot.Category(site, 'Category:foooooo')
        cat_not_empty = pywikibot.Category(site,
                                           'Category:Wikipedia categories')
        self.assertTrue(cat_empty.isEmptyCategory())
        self.assertFalse(cat_not_empty.isEmptyCategory())
github wikimedia / pywikibot / tests / category_tests.py View on Github external
def test_redirects(self):
        """Test the redirects method."""
        site = self.get_site()
        cat1 = pywikibot.Category(site, 'Category:Fonts')
        cat2 = pywikibot.Category(site, 'Category:Typefaces')

        self.assertTrue(cat1.isCategoryRedirect())
        self.assertFalse(cat2.isCategoryRedirect())

        # The correct target category if fetched.
        tgt = cat1.getCategoryRedirectTarget()
        self.assertEqual(tgt, cat2)

        # Raise exception if target is fetched for non Category redirects.
        self.assertRaisesRegex(pywikibot.IsNotRedirectPage,
                               self.NOREDIRECTPAGE_RE,
                               cat2.getCategoryRedirectTarget)
github wikimedia / pywikibot / pywikibot / textlib.py View on Github external
catLinks = []
    for category in categories:
        if isinstance(category, basestring):
            category, separator, sortKey = category.strip('[]').partition('|')
            sortKey = sortKey if separator else None
            # whole word if no ":" is present
            prefix = category.split(':', 1)[0]
            if prefix not in insite.namespaces[14]:
                category = '{0}:{1}'.format(insite.namespace(14), category)
            category = pywikibot.Category(pywikibot.Link(category,
                                                         insite,
                                                         default_namespace=14),
                                          sort_key=sortKey)
        # Make sure a category is casted from Page to Category.
        elif not isinstance(category, pywikibot.Category):
            category = pywikibot.Category(category)
        link = category.aslink()
        catLinks.append(link)

    if insite.category_on_one_line():
        sep = ' '
    else:
        sep = config.line_separator
    # Some people don't like the categories sorted
    # catLinks.sort()
    return sep.join(catLinks) + config.line_separator
github wikimedia / pywikibot / pywikibot / textlib.py View on Github external
catLinks = []
    for category in categories:
        if isinstance(category, basestring):
            category, separator, sortKey = category.strip('[]').partition('|')
            sortKey = sortKey if separator else None
            # whole word if no ":" is present
            prefix = category.split(':', 1)[0]
            if prefix not in insite.namespaces[14]:
                category = '{0}:{1}'.format(insite.namespace(14), category)
            category = pywikibot.Category(pywikibot.Link(category,
                                                         insite,
                                                         default_namespace=14),
                                          sort_key=sortKey)
        # Make sure a category is casted from Page to Category.
        elif not isinstance(category, pywikibot.Category):
            category = pywikibot.Category(category)
        link = category.aslink()
        catLinks.append(link)

    if insite.category_on_one_line():
        sep = ' '
    else:
        sep = config.line_separator
    # Some people don't like the categories sorted
    # catLinks.sort()
    return sep.join(catLinks) + config.line_separator
github wikimedia / pywikibot / scripts / archive / featured.py View on Github external
def DATA(site, name, hide):
    dp = pywikibot.ItemPage(site.data_repository(), name)
    try:
        title = dp.getSitelink(site)
    except pywikibot.NoPage:
        return
    cat = pywikibot.Category(site, title)
    if isinstance(hide, dict):
        hide = hide.get(site.code)
    for article in cat.articles(endsort=hide):
        yield article
    if hide:
        for article in cat.articles(startsort=unichr(ord(hide) + 1)):
            yield article
github wikimedia / pywikibot / scripts / blockpageschecker.py View on Github external
TTP = i18n.translate(site, templateTotalProtection)
    TSMP = i18n.translate(site, templateSemiMoveProtection)
    TTMP = i18n.translate(site, templateTotalMoveProtection)
    TNR = i18n.translate(site, templateNoRegex)
    TU = i18n.translate(site, templateUnique)

    categories = i18n.translate(site, categoryToCheck)
    commentUsed = i18n.twtranslate(site, 'blockpageschecker-summary')
    if not generator:
        generator = genFactory.getCombinedGenerator()
    if not generator:
        generator = []
        pywikibot.output('Loading categories...')
        # Define the category if no other generator has been setted
        for CAT in categories:
            cat = pywikibot.Category(site, CAT)
            # Define the generator
            gen = pagegenerators.CategorizedPageGenerator(cat)
            for pageCat in gen:
                generator.append(pageCat)
        pywikibot.output('Categories loaded, start!')
    # Main Loop
    if not genFactory.nopreload:
        generator = pagegenerators.PreloadingGenerator(generator,
                                                       groupsize=60)
    for page in generator:
        pagename = page.title(as_link=True)
        pywikibot.output('Loading %s...' % pagename)
        try:
            text = page.text
        except pywikibot.NoPage:
            pywikibot.output("%s doesn't exist! Skipping..." % pagename)