A Python robot that edits Wikipedia and interacts with people over IRC https://en.wikipedia.org/wiki/User:EarwigBot
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

40 rader
1.6 KiB

  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 = "Category(title={0!r}, follow_redirects={1!r}, site={2!r})"
  17. return res.format(self._title, self._follow_redirects, self._site)
  18. def __str__(self):
  19. """Returns a nice string representation of the Category."""
  20. return '<Category "{0}" of {1}>'.format(self.title(), str(self._site))
  21. def members(self, limit=50):
  22. """Returns a list of titles in the category.
  23. If `limit` is provided, we will provide this many titles, or less if
  24. the category is too small. `limit` defaults to 50; normal users can go
  25. up to 500, and bots can go up to 5,000 on a single API query.
  26. """
  27. params = {"action": "query", "list": "categorymembers",
  28. "cmlimit": limit, "cmtitle": self._title}
  29. result = self._site._api_query(params)
  30. members = result['query']['categorymembers']
  31. return [member["title"] for member in members]