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.

142 rivejä
6.3 KiB

  1. # -*- coding: utf-8 -*-
  2. """Report the status of AFC submissions, either as an automatic message on join
  3. or a request via !status."""
  4. import re
  5. from core import config
  6. from irc.classes import BaseCommand
  7. from wiki import tools
  8. class AFCStatus(BaseCommand):
  9. def get_hooks(self):
  10. return ["join", "msg"]
  11. def get_help(self, command):
  12. return "Get the number of pending AfC submissions, open redirect requests, and open file upload requests."
  13. def check(self, data):
  14. if data.is_command and data.command in ["status", "count", "num", "number", "afc_status"]:
  15. return True
  16. try:
  17. if data.line[1] == "JOIN" and data.chan == "#wikipedia-en-afc":
  18. if data.nick != config.irc["frontend"]["nick"]:
  19. return True
  20. except IndexError:
  21. pass
  22. return False
  23. def process(self, data):
  24. self.site = tools.get_site()
  25. if data.line[1] == "JOIN":
  26. notice = self.get_join_notice()
  27. self.connection.notice(data.nick, notice)
  28. return
  29. if data.args:
  30. action = data.args[0].lower()
  31. if action.startswith("sub") or action == "s":
  32. subs = self.count_submissions()
  33. self.connection.reply(data, "there are currently %s pending AfC submissions." % subs)
  34. elif action.startswith("redir") or action == "r":
  35. redirs = self.count_redirects()
  36. self.connection.reply(data, "there are currently %s open redirect requests." % redirs)
  37. elif action.startswith("file") or action == "f":
  38. files = self.count_redirects()
  39. self.connection.reply(data, "there are currently %s open file upload requests." % files)
  40. elif action.startswith("agg") or action == "a":
  41. try:
  42. agg_num = int(data.args[1])
  43. except IndexError:
  44. agg_data = (self.count_submissions(), self.count_redirects(), self.count_files())
  45. agg_num = self.get_aggregate_number(agg_data)
  46. except ValueError:
  47. self.connection.reply(data, "\x0303%s\x0301 isn't a number!" % data.args[1])
  48. return
  49. aggregate = self.get_aggregate(agg_num)
  50. self.connection.reply(data, "aggregate is currently %s (AfC %s)." % (agg_num, aggregate))
  51. elif action.startswith("join") or action == "j":
  52. notice = self.get_join_notice()
  53. self.connection.reply(data, notice)
  54. else:
  55. self.connection.reply(data, "unknown argument: \x0303%s\x0301. Valid args are 'subs', 'redirs', 'files', 'agg', and 'join'." % data.args[0])
  56. else:
  57. subs = self.count_submissions()
  58. redirs = self.count_redirects()
  59. files = self.count_files()
  60. self.connection.reply(data, "there are currently %s pending submissions, %s open redirect requests, and %s open file upload requests."
  61. % (subs, redirs, files))
  62. def get_join_notice(self):
  63. subs = self.count_submissions()
  64. redirs = self.count_redirects()
  65. files = self.count_files()
  66. agg_num = self.get_aggregate_number((subs, redirs, files))
  67. aggregate = self.get_aggregate(agg_num)
  68. return ("\x02Current status:\x0F Articles for Creation %s (\x0302AFC\x0301: \x0305%s\x0301; \x0302AFC/R\x0301: \x0305%s\x0301; \x0302FFU\x0301: \x0305%s\x0301)"
  69. % (aggregate, subs, redirs, files))
  70. def count_submissions(self):
  71. """Returns the number of open AFC submissions (count of CAT:PEND)."""
  72. cat = self.site.get_category("Pending AfC submissions")
  73. subs = cat.members(limit=500)
  74. subs -= 2 # remove [[Wikipedia:Articles for creation/Redirects]] and [[Wikipedia:Files for upload]], which aren't real submissions
  75. return subs
  76. def count_redirects(self):
  77. """Returns the number of open redirect submissions. Calculated as the
  78. total number of submissions minus the closed ones."""
  79. content = self.site.get_page("Wikipedia:Articles for creation/Redirects").get()
  80. total = len(re.findall("^\s*==(.*?)==\s*$", content, re.MULTILINE))
  81. closed = content.lower().count("{{afc-c|b}}")
  82. redirs = total - closed
  83. return redirs
  84. def count_files(self):
  85. """Returns the number of open WP:FFU (Files For Upload) requests.
  86. Calculated as the total number of requests minus the closed ones."""
  87. content = self.site.get_page("Wikipedia:Files for upload").get()
  88. total = len(re.findall("^\s*==(.*?)==\s*$", content, re.MULTILINE))
  89. closed = content.lower().count("{{ifu-c|b}}")
  90. files = total - closed
  91. return files
  92. def get_aggregate(self, num):
  93. """Returns a human-readable AFC status based on the number of pending
  94. AFC submissions, open redirect requests, and open FFU requests. This
  95. does not match {{AFC status}} directly because my algorithm factors in
  96. WP:AFC/R and WP:FFU while the template only looks at the main
  97. submissions. My reasoning is that AFC/R and FFU are still part of
  98. the project, so even if there are no pending submissions, a backlog at
  99. FFU (for example) indicates that our work is *not* done and the
  100. project-wide backlog is most certainly *not* clear."""
  101. if num == 0:
  102. return "is \x02\x0303clear\x0301\x0F"
  103. elif num < 125: # < 25 subs
  104. return "is \x0303almost clear\x0301"
  105. elif num < 200: # < 40 subs
  106. return "is \x0312normal\x0301"
  107. elif num < 275: # < 55 subs
  108. return "is \x0307lightly backlogged\x0301"
  109. elif num < 350: # < 70 subs
  110. return "is \x0304backlogged\x0301"
  111. elif num < 500: # < 100 subs
  112. return "is \x02\x0304heavily backlogged\x0301\x0F"
  113. else: # >= 100 subs
  114. return "is \x02\x1F\x0304severely backlogged\x0301\x0F"
  115. def get_aggregate_number(self, (subs, redirs, files)):
  116. """Returns an 'aggregate number' based on the real number of pending
  117. submissions in CAT:PEND (subs), open redirect submissions in WP:AFC/R
  118. (redirs), and open files-for-upload requests in WP:FFU (files)."""
  119. num = (subs * 5) + (redirs * 2) + (files * 2)
  120. return num