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.

user.py 4.1 KiB

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