A Python robot that edits Wikipedia and interacts with people over IRC https://en.wikipedia.org/wiki/User:EarwigBot
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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 __repr__(self):
  39. """Returns the canonical string representation of the User."""
  40. return "User(name={0!r}, site={1!r})".format(self._name, self._site)
  41. def __str__(self):
  42. """Returns a nice string representation of the User."""
  43. return '<User "{0}" of {1}>'.format(self.name(), str(self._site))
  44. def _get_attribute(self, attr, force):
  45. """Internally used to get an attribute by name.
  46. We'll call _load_attributes() to get this (and all other attributes)
  47. from the API if it is not already defined. If `force` is True, we'll
  48. re-load them even if they've already been loaded.
  49. Raises UserNotFoundError if a nonexistant user prevents us from
  50. returning a certain attribute.
  51. """
  52. if not hasattr(self, attr) or force:
  53. self._load_attributes()
  54. if self._exists is False:
  55. e = "User '{0}' does not exist.".format(self._name)
  56. raise UserNotFoundError(e)
  57. return getattr(self, attr)
  58. def _load_attributes(self):
  59. """Internally used to load all attributes from the API.
  60. Normally, this is called by _get_attribute() when a requested attribute
  61. is not defined. This defines it.
  62. """
  63. params = {"action": "query", "list": "users", "ususers": self._name,
  64. "usprop": "blockinfo|groups|rights|editcount|registration|emailable|gender"}
  65. result = self._site._api_query(params)
  66. res = result["query"]["users"][0]
  67. # normalize our username in case it was entered oddly
  68. self._name = res["name"]
  69. try:
  70. self._userid = res["userid"]
  71. except KeyError: # userid is missing, so user does not exist
  72. self._exists = False
  73. return
  74. self._exists = True
  75. try:
  76. self._blockinfo = {
  77. "by": res["blockedby"],
  78. "reason": res["blockreason"],
  79. "expiry": res["blockexpiry"]
  80. }
  81. except KeyError:
  82. self._blockinfo = False
  83. self._groups = res["groups"]
  84. try:
  85. self._rights = res["rights"].values()
  86. except AttributeError:
  87. self._rights = res["rights"]
  88. self._editcount = res["editcount"]
  89. reg = res["registration"]
  90. try:
  91. self._registration = strptime(reg, "%Y-%m-%dT%H:%M:%SZ")
  92. except TypeError:
  93. # Sometimes the API doesn't give a date; the user's probably really
  94. # old. There's nothing else we can do!
  95. self._registration = gmtime(0)
  96. try:
  97. res["emailable"]
  98. except KeyError:
  99. self._emailable = False
  100. else:
  101. self._emailable = True
  102. self._gender = res["gender"]
  103. def name(self, force=False):
  104. """Returns the user's name.
  105. If `force` is True, we will load the name from the API and return that.
  106. This could potentially return a "normalized" version of the name - for
  107. example, without a "User:" prefix or without underscores. Unlike other
  108. attribute getters, this will never make an API query without `force`.
  109. Note that if another attribute getter, like exists(), has already been
  110. called, then the username has already been normalized.
  111. """
  112. if force:
  113. self._load_attributes()
  114. return self._name
  115. def exists(self, force=False):
  116. """Returns True if the user exists, or False if they do not.
  117. Makes an API query if `force` is True or if we haven't made one
  118. already.
  119. """
  120. if not hasattr(self, "_exists") or force:
  121. self._load_attributes()
  122. return self._exists
  123. def userid(self, force=False):
  124. """Returns an integer ID used by MediaWiki to represent the user.
  125. Raises UserNotFoundError if the user does not exist. Makes an API query
  126. if `force` is True or if we haven't made one already.
  127. """
  128. return self._get_attribute("_userid", force)
  129. def blockinfo(self, force=False):
  130. """Returns information about a current block on the user.
  131. If the user is not blocked, returns False. If they are, returns a dict
  132. with three keys: "by" is the blocker's username, "reason" is the reason
  133. why they were blocked, and "expiry" is when the block expires.
  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("_blockinfo", force)
  138. def groups(self, force=False):
  139. """Returns a list of groups this user is in, including "*".
  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("_groups", force)
  144. def rights(self, force=False):
  145. """Returns a list of this user's rights.
  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("_rights", force)
  150. def editcount(self, force=False):
  151. """Returns the number of edits made by the user.
  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("_editcount", force)
  156. def registration(self, force=False):
  157. """Returns the time the user registered as a time.struct_time object.
  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("_registration", force)
  162. def emailable(self, force=False):
  163. """Returns True if the user can be emailed, or False if they cannot.
  164. Raises UserNotFoundError if the user does not exist. Makes an API query
  165. if `force` is True or if we haven't made one already.
  166. """
  167. return self._get_attribute("_emailable", force)
  168. def gender(self, force=False):
  169. """Returns the user's gender.
  170. Can return either "male", "female", or "unknown", if they did not
  171. specify it.
  172. Raises UserNotFoundError if the user does not exist. Makes an API query
  173. if `force` is True or if we haven't made one already.
  174. """
  175. return self._get_attribute("_gender", force)
  176. def get_userpage(self):
  177. """Returns a Page object representing the user's userpage.
  178. No checks are made to see if it exists or not. Proper site namespace
  179. conventions are followed.
  180. """
  181. prefix = self._site.namespace_id_to_name(NS_USER)
  182. pagename = ':'.join((prefix, self._name))
  183. return Page(self._site, pagename)
  184. def get_talkpage(self):
  185. """Returns a Page object representing the user's talkpage.
  186. No checks are made to see if it exists or not. Proper site namespace
  187. conventions are followed.
  188. """
  189. prefix = self._site.namespace_id_to_name(NS_USER_TALK)
  190. pagename = ':'.join((prefix, self._name))
  191. return Page(self._site, pagename)