A Python robot that edits Wikipedia and interacts with people over IRC https://en.wikipedia.org/wiki/User:EarwigBot
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

category.py 2.4 KiB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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 page 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, use_sql=False):
  22. """Returns a list of page 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. If `use_sql` is True, we will use a SQL query instead of the API. The
  27. limit argument will be ignored, and pages will be returned as tuples
  28. of (title, pageid) instead of just titles.
  29. """
  30. if use_sql:
  31. query = """SELECT page_title, page_namespace, page_id FROM page
  32. JOIN categorylinks ON page_id = cl_from
  33. WHERE cl_to = ?"""
  34. title = self.title().replace(" ", "_").split(":", 1)[1]
  35. result = self.site.sql_query(query, (title,))
  36. members = []
  37. for row in result:
  38. body = row[0].replace("_", " ")
  39. namespace = self.site.namespace_id_to_name(row[1])
  40. title = ":".join(namespace, body)
  41. members.append((title, row[2]))
  42. return members
  43. else:
  44. params = {"action": "query", "list": "categorymembers",
  45. "cmlimit": limit, "cmtitle": self._title}
  46. result = self._site._api_query(params)
  47. members = result['query']['categorymembers']
  48. return [member["title"] for member in members]