A Python robot that edits Wikipedia and interacts with people over IRC https://en.wikipedia.org/wiki/User:EarwigBot
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

144 rindas
3.9 KiB

  1. # -*- coding: utf-8 -*-
  2. from wiki.tools.exceptions import UserNotFoundError
  3. from wiki.tools.page import Page
  4. class User(object):
  5. """
  6. EarwigBot's Wiki Toolset: User Class
  7. """
  8. def __init__(self, site, name):
  9. """
  10. Docstring needed
  11. """
  12. # Public attributes
  13. self.site = site # Site instance, for doing API queries, etc
  14. self.name = name # our username
  15. # Attributes filled in by an API query
  16. self._exists = None
  17. self._userid = None
  18. self._blockinfo = None
  19. self._groups = None
  20. self._rights = None
  21. self._editcount = None
  22. self._registration = None
  23. self._emailable = None
  24. self._gender = None
  25. def _get_attribute_from_api(self, attr, force):
  26. """
  27. Docstring needed
  28. """
  29. if self._exists is None or force:
  30. self._load_attributes_from_api()
  31. if self._exists is False:
  32. raise UserNotFoundError(self.name)
  33. return getattr(self, attr)
  34. def _load_attributes_from_api(self):
  35. """
  36. Docstring needed
  37. """
  38. params = {"action": "query", "list": "users", "ususers": self.name,
  39. "usprop": "blockinfo|groups|rights|editcount|registration|emailable|gender"}
  40. result = self.site.api_query(params)
  41. # normalize our username in case it was entered oddly
  42. self.name = result["query"]["users"][0]["name"]
  43. try:
  44. self._userid = result["query"]["users"][0]["userid"]
  45. except KeyError: # userid is missing, so user does not exist
  46. self._exists = False
  47. return
  48. self._exists = True
  49. res = result['query']['users'][0]
  50. self._groups = res["groups"]
  51. self._rights = res["rights"]
  52. self._editcount = res["editcount"]
  53. self._registration = res["registration"]
  54. self._gender = res["gender"]
  55. try:
  56. res["emailable"]
  57. except KeyError:
  58. self._emailable = False
  59. else:
  60. self._emailable = True
  61. try:
  62. self._blockinfo = {"by": res["blockedby"],
  63. "reason": res["blockreason"], "expiry": res["blockexpiry"]}
  64. except KeyError:
  65. self._blockinfo = False
  66. def exists(self, force=False):
  67. """
  68. Docstring needed
  69. """
  70. return self._get_attribute_from_api("_exists", force)
  71. def get_userid(self, force=False):
  72. """
  73. Docstring needed
  74. """
  75. return self._get_attribute_from_api("_userid", force)
  76. def get_blockinfo(self, force=False):
  77. """
  78. Docstring needed
  79. """
  80. return self._get_attribute_from_api("_blockinfo", force)
  81. def get_groups(self, force=False):
  82. """
  83. Docstring needed
  84. """
  85. return self._get_attribute_from_api("_groups", force)
  86. def get_rights(self, force=False):
  87. """
  88. Docstring needed
  89. """
  90. return self._get_attribute_from_api("_rights", force)
  91. def get_editcount(self, force=False):
  92. """
  93. Docstring needed
  94. """
  95. return self._get_attribute_from_api("_editcount", force)
  96. def get_registration(self, force=False):
  97. """
  98. Docstring needed
  99. """
  100. return self._get_attribute_from_api("_registration", force)
  101. def get_emailable(self, force=False):
  102. """
  103. Docstring needed
  104. """
  105. return self._get_attribute_from_api("_emailable", force)
  106. def get_gender(self, force=False):
  107. """
  108. Docstring needed
  109. """
  110. return self._get_attribute_from_api("_gender", force)
  111. def get_userpage(self):
  112. """
  113. Docstring needed
  114. """
  115. return Page(self.site, "User:" + self.name) # Namespace checking!
  116. def get_talkpage(self):
  117. """
  118. Docstring needed
  119. """
  120. return Page(self.site, "User talk:" + self.name) # Namespace checking!