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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. # -*- coding: utf-8 -*-
  2. from time import 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. self._registration = strptime(reg, "%Y-%m-%dT%H:%M:%SZ")
  85. try:
  86. res["emailable"]
  87. except KeyError:
  88. self._emailable = False
  89. else:
  90. self._emailable = True
  91. self._gender = res["gender"]
  92. def name(self, force=False):
  93. """Returns the user's name.
  94. If `force` is True, we will load the name from the API and return that.
  95. This could potentially return a "normalized" version of the name - for
  96. example, without a "User:" prefix or without underscores. Unlike other
  97. attribute getters, this will never make an API query without `force`.
  98. Note that if another attribute getter, like exists(), has already been
  99. called, then the username has already been normalized.
  100. """
  101. if force:
  102. self._load_attributes()
  103. return self._name
  104. def exists(self, force=False):
  105. """Returns True if the user exists, or False if they do not.
  106. Makes an API query if `force` is True or if we haven't made one
  107. already.
  108. """
  109. if not hasattr(self, "_exists") or force:
  110. self._load_attributes()
  111. return self._exists
  112. def userid(self, force=False):
  113. """Returns an integer ID used by MediaWiki to represent the user.
  114. Raises UserNotFoundError if the user does not exist. Makes an API query
  115. if `force` is True or if we haven't made one already.
  116. """
  117. return self._get_attribute("_userid", force)
  118. def blockinfo(self, force=False):
  119. """Returns information about a current block on the user.
  120. If the user is not blocked, returns False. If they are, returns a dict
  121. with three keys: "by" is the blocker's username, "reason" is the reason
  122. why they were blocked, and "expiry" is when the block expires.
  123. Raises UserNotFoundError if the user does not exist. Makes an API query
  124. if `force` is True or if we haven't made one already.
  125. """
  126. return self._get_attribute("_blockinfo", force)
  127. def groups(self, force=False):
  128. """Returns a list of groups this user is in, including "*".
  129. Raises UserNotFoundError if the user does not exist. Makes an API query
  130. if `force` is True or if we haven't made one already.
  131. """
  132. return self._get_attribute("_groups", force)
  133. def rights(self, force=False):
  134. """Returns a list of this user's rights.
  135. Raises UserNotFoundError if the user does not exist. Makes an API query
  136. if `force` is True or if we haven't made one already.
  137. """
  138. return self._get_attribute("_rights", force)
  139. def editcount(self, force=False):
  140. """Returns the number of edits made by the user.
  141. Raises UserNotFoundError if the user does not exist. Makes an API query
  142. if `force` is True or if we haven't made one already.
  143. """
  144. return self._get_attribute("_editcount", force)
  145. def registration(self, force=False):
  146. """Returns the time the user registered as a time.struct_time object.
  147. Raises UserNotFoundError if the user does not exist. Makes an API query
  148. if `force` is True or if we haven't made one already.
  149. """
  150. return self._get_attribute("_registration", force)
  151. def emailable(self, force=False):
  152. """Returns True if the user can be emailed, or False if they cannot.
  153. Raises UserNotFoundError if the user does not exist. Makes an API query
  154. if `force` is True or if we haven't made one already.
  155. """
  156. return self._get_attribute("_emailable", force)
  157. def gender(self, force=False):
  158. """Returns the user's gender.
  159. Can return either "male", "female", or "unknown", if they did not
  160. specify it.
  161. Raises UserNotFoundError if the user does not exist. Makes an API query
  162. if `force` is True or if we haven't made one already.
  163. """
  164. return self._get_attribute("_gender", force)
  165. def get_userpage(self):
  166. """Returns a Page object representing the user's userpage.
  167. No checks are made to see if it exists or not. Proper site namespace
  168. conventions are followed.
  169. """
  170. prefix = self._site.namespace_id_to_name(NS_USER)
  171. pagename = ':'.join((prefix, self._name))
  172. return Page(self._site, pagename)
  173. def get_talkpage(self):
  174. """Returns a Page object representing the user's talkpage.
  175. No checks are made to see if it exists or not. Proper site namespace
  176. conventions are followed.
  177. """
  178. prefix = self._site.namespace_id_to_name(NS_USER_TALK)
  179. pagename = ':'.join((prefix, self._name))
  180. return Page(self._site, pagename)