A Python robot that edits Wikipedia and interacts with people over IRC https://en.wikipedia.org/wiki/User:EarwigBot
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

category.py 1.6 KiB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. # -*- coding: utf-8 -*-
  2. from wiki.page import Page
  3. class Category(Page):
  4. """
  5. EarwigBot's Wiki Toolset: Category Class
  6. Represents a Category on a given Site, a subclass of Page. Provides
  7. additional methods, but Page's own methods should work fine on Category
  8. objects. Site.get_page() will return a Category instead of a Page if the
  9. given title is in the category namespace; get_category() is shorthand,
  10. because it accepts category names without the namespace prefix.
  11. Public methods:
  12. members -- returns a list of titles in the category
  13. """
  14. def __repr__(self):
  15. """Returns the canonical string representation of the Category."""
  16. res = ", ".join(("Category(title={0!r}", "follow_redirects={1!r}",
  17. "site={2!r})"))
  18. return res.format(self._title, self._follow_redirects, self._site)
  19. def __str__(self):
  20. """Returns a nice string representation of the Category."""
  21. return '<Category "{0}" of {1}>'.format(self.title(), str(self._site))
  22. def members(self, limit=50):
  23. """Returns a list of titles in the category.
  24. If `limit` is provided, we will provide this many titles, or less if
  25. the category is too small. `limit` defaults to 50; normal users can go
  26. up to 500, and bots can go up to 5,000 on a single API query.
  27. """
  28. params = {"action": "query", "list": "categorymembers",
  29. "cmlimit": limit, "cmtitle": self._title}
  30. result = self._site._api_query(params)
  31. members = result['query']['categorymembers']
  32. return [member["title"] for member in members]