A Python robot that edits Wikipedia and interacts with people over IRC https://en.wikipedia.org/wiki/User:EarwigBot
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

235 lines
8.7 KiB

  1. # -*- coding: utf-8 -*-
  2. from time import gmtime, strptime
  3. from wiki.constants import *
  4. from wiki.exceptions import UserNotFoundError
  5. from wiki.page import Page
  6. class User(object):
  7. """
  8. EarwigBot's Wiki Toolset: User Class
  9. Represents a User on a given Site. Has methods for getting a bunch of
  10. information about the user, such as editcount and user rights, methods for
  11. returning the user's userpage and talkpage, etc.
  12. Public methods:
  13. name -- returns the user's username
  14. exists -- returns True if the user exists, False if they do not
  15. userid -- returns an integer ID representing the user
  16. blockinfo -- returns information about a current block on the user
  17. groups -- returns a list of the user's groups
  18. rights -- returns a list of the user's rights
  19. editcount -- returns the number of edits made by the user
  20. registration -- returns the time the user registered as a time.struct_time
  21. emailable -- returns True if you can email the user, False if you cannot
  22. gender -- returns the user's gender ("male", "female", or "unknown")
  23. get_userpage -- returns a Page object representing the user's userpage
  24. get_talkpage -- returns a Page object representing the user's talkpage
  25. """
  26. def __init__(self, site, name):
  27. """Constructor for new User instances.
  28. Takes two arguments, a Site object (necessary for doing API queries),
  29. and the name of the user, preferably without "User:" in front, although
  30. this prefix will be automatically removed by the API if given.
  31. You can also use site.get_user() instead, which returns a User object,
  32. and is preferred.
  33. We won't do any API queries yet for basic information about the user -
  34. save that for when the information is requested.
  35. """
  36. self._site = site
  37. self._name = name
  38. def _get_attribute(self, attr, force):
  39. """Internally used to get an attribute by name.
  40. We'll call _load_attributes() to get this (and all other attributes)
  41. from the API if it is not already defined. If `force` is True, we'll
  42. re-load them even if they've already been loaded.
  43. Raises UserNotFoundError if a nonexistant user prevents us from
  44. returning a certain attribute.
  45. """
  46. if not hasattr(self, attr) or force:
  47. self._load_attributes()
  48. if self._exists is False:
  49. e = "User '{0}' does not exist.".format(self._name)
  50. raise UserNotFoundError(e)
  51. return getattr(self, attr)
  52. def _load_attributes(self):
  53. """Internally used to load all attributes from the API.
  54. Normally, this is called by _get_attribute() when a requested attribute
  55. is not defined. This defines it.
  56. """
  57. params = {"action": "query", "list": "users", "ususers": self._name,
  58. "usprop": "blockinfo|groups|rights|editcount|registration|emailable|gender"}
  59. result = self._site._api_query(params)
  60. res = result["query"]["users"][0]
  61. # normalize our username in case it was entered oddly
  62. self._name = res["name"]
  63. try:
  64. self._userid = res["userid"]
  65. except KeyError: # userid is missing, so user does not exist
  66. self._exists = False
  67. return
  68. self._exists = True
  69. try:
  70. self._blockinfo = {
  71. "by": res["blockedby"],
  72. "reason": res["blockreason"],
  73. "expiry": res["blockexpiry"]
  74. }
  75. except KeyError:
  76. self._blockinfo = False
  77. self._groups = res["groups"]
  78. try:
  79. self._rights = res["rights"].values()
  80. except AttributeError:
  81. self._rights = res["rights"]
  82. self._editcount = res["editcount"]
  83. reg = res["registration"]
  84. try:
  85. self._registration = strptime(reg, "%Y-%m-%dT%H:%M:%SZ")
  86. except TypeError:
  87. # Sometimes the API doesn't give a date; the user's probably really
  88. # old. There's nothing else we can do!
  89. self._registration = gmtime(0)
  90. try:
  91. res["emailable"]
  92. except KeyError:
  93. self._emailable = False
  94. else:
  95. self._emailable = True
  96. self._gender = res["gender"]
  97. def name(self, force=False):
  98. """Returns the user's name.
  99. If `force` is True, we will load the name from the API and return that.
  100. This could potentially return a "normalized" version of the name - for
  101. example, without a "User:" prefix or without underscores. Unlike other
  102. attribute getters, this will never make an API query without `force`.
  103. Note that if another attribute getter, like exists(), has already been
  104. called, then the username has already been normalized.
  105. """
  106. if force:
  107. self._load_attributes()
  108. return self._name
  109. def exists(self, force=False):
  110. """Returns True if the user exists, or False if they do not.
  111. Makes an API query if `force` is True or if we haven't made one
  112. already.
  113. """
  114. if not hasattr(self, "_exists") or force:
  115. self._load_attributes()
  116. return self._exists
  117. def userid(self, force=False):
  118. """Returns an integer ID used by MediaWiki to represent the user.
  119. Raises UserNotFoundError if the user does not exist. Makes an API query
  120. if `force` is True or if we haven't made one already.
  121. """
  122. return self._get_attribute("_userid", force)
  123. def blockinfo(self, force=False):
  124. """Returns information about a current block on the user.
  125. If the user is not blocked, returns False. If they are, returns a dict
  126. with three keys: "by" is the blocker's username, "reason" is the reason
  127. why they were blocked, and "expiry" is when the block expires.
  128. Raises UserNotFoundError if the user does not exist. Makes an API query
  129. if `force` is True or if we haven't made one already.
  130. """
  131. return self._get_attribute("_blockinfo", force)
  132. def groups(self, force=False):
  133. """Returns a list of groups this user is in, including "*".
  134. Raises UserNotFoundError if the user does not exist. Makes an API query
  135. if `force` is True or if we haven't made one already.
  136. """
  137. return self._get_attribute("_groups", force)
  138. def rights(self, force=False):
  139. """Returns a list of this user's rights.
  140. Raises UserNotFoundError if the user does not exist. Makes an API query
  141. if `force` is True or if we haven't made one already.
  142. """
  143. return self._get_attribute("_rights", force)
  144. def editcount(self, force=False):
  145. """Returns the number of edits made by the user.
  146. Raises UserNotFoundError if the user does not exist. Makes an API query
  147. if `force` is True or if we haven't made one already.
  148. """
  149. return self._get_attribute("_editcount", force)
  150. def registration(self, force=False):
  151. """Returns the time the user registered as a time.struct_time object.
  152. Raises UserNotFoundError if the user does not exist. Makes an API query
  153. if `force` is True or if we haven't made one already.
  154. """
  155. return self._get_attribute("_registration", force)
  156. def emailable(self, force=False):
  157. """Returns True if the user can be emailed, or False if they cannot.
  158. Raises UserNotFoundError if the user does not exist. Makes an API query
  159. if `force` is True or if we haven't made one already.
  160. """
  161. return self._get_attribute("_emailable", force)
  162. def gender(self, force=False):
  163. """Returns the user's gender.
  164. Can return either "male", "female", or "unknown", if they did not
  165. specify it.
  166. Raises UserNotFoundError if the user does not exist. Makes an API query
  167. if `force` is True or if we haven't made one already.
  168. """
  169. return self._get_attribute("_gender", force)
  170. def get_userpage(self):
  171. """Returns a Page object representing the user's userpage.
  172. No checks are made to see if it exists or not. Proper site namespace
  173. conventions are followed.
  174. """
  175. prefix = self._site.namespace_id_to_name(NS_USER)
  176. pagename = ':'.join((prefix, self._name))
  177. return Page(self._site, pagename)
  178. def get_talkpage(self):
  179. """Returns a Page object representing the user's talkpage.
  180. No checks are made to see if it exists or not. Proper site namespace
  181. conventions are followed.
  182. """
  183. prefix = self._site.namespace_id_to_name(NS_USER_TALK)
  184. pagename = ':'.join((prefix, self._name))
  185. return Page(self._site, pagename)