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.

163 lines
7.2 KiB

  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2009-2012 Ben Kurtovic <ben.kurtovic@verizon.net>
  4. #
  5. # Permission is hereby granted, free of charge, to any person obtaining a copy
  6. # of this software and associated documentation files (the "Software"), to deal
  7. # in the Software without restriction, including without limitation the rights
  8. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. # copies of the Software, and to permit persons to whom the Software is
  10. # furnished to do so, subject to the following conditions:
  11. #
  12. # The above copyright notice and this permission notice shall be included in
  13. # all copies or substantial portions of the Software.
  14. #
  15. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. # SOFTWARE.
  22. import re
  23. from earwigbot.commands import Command
  24. class AFCStatus(Command):
  25. """Get the number of pending AfC submissions, open redirect requests, and
  26. open file upload requests."""
  27. name = "status"
  28. commands = ["status", "count", "num", "number"]
  29. hooks = ["join", "msg"]
  30. def check(self, data):
  31. if data.is_command and data.command in self.commands:
  32. return True
  33. try:
  34. if data.line[1] == "JOIN" and data.chan == "#wikipedia-en-afc":
  35. if data.nick != self.config.irc["frontend"]["nick"]:
  36. return True
  37. except IndexError:
  38. pass
  39. return False
  40. def process(self, data):
  41. self.site = self.bot.wiki.get_site()
  42. if data.line[1] == "JOIN":
  43. status = " ".join(("\x02Current status:\x0F", self.get_status()))
  44. self.notice(data.nick, status)
  45. return
  46. if data.args:
  47. action = data.args[0].lower()
  48. if action.startswith("sub") or action == "s":
  49. subs = self.count_submissions()
  50. msg = "there are \x0305{0}\x0301 pending AfC submissions (\x0302WP:AFC\x0301)."
  51. self.reply(data, msg.format(subs))
  52. elif action.startswith("redir") or action == "r":
  53. redirs = self.count_redirects()
  54. msg = "there are \x0305{0}\x0301 open redirect requests (\x0302WP:AFC/R\x0301)."
  55. self.reply(data, msg.format(redirs))
  56. elif action.startswith("file") or action == "f":
  57. files = self.count_redirects()
  58. msg = "there are \x0305{0}\x0301 open file upload requests (\x0302WP:FFU\x0301)."
  59. self.reply(data, msg.format(files))
  60. elif action.startswith("agg") or action == "a":
  61. try:
  62. agg_num = int(data.args[1])
  63. except IndexError:
  64. agg_data = (self.count_submissions(),
  65. self.count_redirects(), self.count_files())
  66. agg_num = self.get_aggregate_number(agg_data)
  67. except ValueError:
  68. msg = "\x0303{0}\x0301 isn't a number!"
  69. self.reply(data, msg.format(data.args[1]))
  70. return
  71. aggregate = self.get_aggregate(agg_num)
  72. msg = "aggregate is \x0305{0}\x0301 (AfC {1})."
  73. self.reply(data, msg.format(agg_num, aggregate))
  74. elif action.startswith("nocolor") or action == "n":
  75. self.reply(data, self.get_status(color=False))
  76. else:
  77. msg = "unknown argument: \x0303{0}\x0301. Valid args are 'subs', 'redirs', 'files', 'agg', 'nocolor'."
  78. self.reply(data, msg.format(data.args[0]))
  79. else:
  80. self.reply(data, self.get_status())
  81. def get_status(self, color=True):
  82. subs = self.count_submissions()
  83. redirs = self.count_redirects()
  84. files = self.count_files()
  85. agg_num = self.get_aggregate_number((subs, redirs, files))
  86. aggregate = self.get_aggregate(agg_num)
  87. if color:
  88. msg = "Articles for creation {0} (\x0302AFC\x0301: \x0305{1}\x0301; \x0302AFC/R\x0301: \x0305{2}\x0301; \x0302FFU\x0301: \x0305{3}\x0301)."
  89. else:
  90. msg = "Articles for creation {0} (AFC: {1}; AFC/R: {2}; FFU: {3})."
  91. return msg.format(aggregate, subs, redirs, files)
  92. def count_submissions(self):
  93. """Returns the number of open AFC submissions (count of CAT:PEND)."""
  94. # Subtract two for [[Wikipedia:Articles for creation/Redirects]] and
  95. # [[Wikipedia:Files for upload]], which aren't real submissions:
  96. return self.site.get_category("Pending AfC submissions").pages - 2
  97. def count_redirects(self):
  98. """Returns the number of open redirect submissions. Calculated as the
  99. total number of submissions minus the closed ones."""
  100. title = "Wikipedia:Articles for creation/Redirects"
  101. content = self.site.get_page(title).get()
  102. total = len(re.findall("^\s*==(.*?)==\s*$", content, re.MULTILINE))
  103. closed = content.lower().count("{{afc-c|b}}")
  104. redirs = total - closed
  105. return redirs
  106. def count_files(self):
  107. """Returns the number of open WP:FFU (Files For Upload) requests.
  108. Calculated as the total number of requests minus the closed ones."""
  109. content = self.site.get_page("Wikipedia:Files for upload").get()
  110. total = len(re.findall("^\s*==(.*?)==\s*$", content, re.MULTILINE))
  111. closed = content.lower().count("{{ifu-c|b}}")
  112. files = total - closed
  113. return files
  114. def get_aggregate(self, num):
  115. """Returns a human-readable AFC status based on the number of pending
  116. AFC submissions, open redirect requests, and open FFU requests. This
  117. does not match {{AFC status}} directly because the algorithm factors in
  118. WP:AFC/R and WP:FFU while the template only looks at the main
  119. submissions. The reasoning is that AFC/R and FFU are still part of
  120. the project, so even if there are no pending submissions, a backlog at
  121. FFU (for example) indicates that our work is *not* done and the
  122. project-wide backlog is most certainly *not* clear."""
  123. if num == 0:
  124. return "is \x02\x0303clear\x0301\x0F"
  125. elif num <= 200:
  126. return "is \x0303almost clear\x0301"
  127. elif num <= 400:
  128. return "is \x0312normal\x0301"
  129. elif num <= 600:
  130. return "is \x0307lightly backlogged\x0301"
  131. elif num <= 900:
  132. return "is \x0304backlogged\x0301"
  133. elif num <= 1200:
  134. return "is \x02\x0304heavily backlogged\x0301\x0F"
  135. else:
  136. return "is \x02\x1F\x0304severely backlogged\x0301\x0F"
  137. def get_aggregate_number(self, (subs, redirs, files)):
  138. """Returns an 'aggregate number' based on the real number of pending
  139. submissions in CAT:PEND (subs), open redirect submissions in WP:AFC/R
  140. (redirs), and open files-for-upload requests in WP:FFU (files)."""
  141. num = subs + (redirs / 2) + (files / 2)
  142. return num