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.

136 lines
5.7 KiB

  1. # -*- coding: utf-8 -*-
  2. # Report the status of AFC submissions, either as an automatic message on join or a request via !status.
  3. import json
  4. import re
  5. import urllib
  6. from config.irc_config import *
  7. from irc.base_command import BaseCommand
  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 == "status" or
  15. data.command == "count" or data.command == "num" or
  16. data.command == "number" or data.command == "afc_status"):
  17. return True
  18. if data.line[1] == "JOIN" and data.chan in AFC_CHANS:
  19. return True
  20. return False
  21. def process(self, data):
  22. if data.line[1] == "JOIN":
  23. notice = self.get_join_notice()
  24. self.connection.notice(data.nick, notice)
  25. return
  26. if data.args:
  27. if data.args[0].startswith("sub") or data.args[0] == "s":
  28. subs = self.count_submissions()
  29. self.connection.reply(data, "there are currently %s pending AfC submissions." % subs)
  30. elif data.args[0].startswith("redir") or data.args[0] == "r":
  31. redirs = self.count_redirects()
  32. self.connection.reply(data, "there are currently %s open redirect requests." % redirs)
  33. elif data.args[0].startswith("file") or data.args[0] == "f":
  34. files = self.count_redirects()
  35. self.connection.reply(data, "there are currently %s open file upload requests." % files)
  36. elif data.args[0].startswith("agg") or data.args[0] == "a":
  37. try:
  38. agg_num = int(data.args[1])
  39. except IndexError:
  40. agg_data = (self.count_submissions(), self.count_redirects(), self.count_files())
  41. agg_num = self.get_aggregate_number(agg_data)
  42. except ValueError:
  43. self.connection.reply(data, "\x0303%s\x0301 isn't a number!" % data.args[1])
  44. return
  45. aggregate = self.get_aggregate(agg_num)
  46. self.connection.reply(data, "aggregate is currently %s (AfC %s)." % (agg_num, aggregate))
  47. elif data.args[0].startswith("join") or data.args[0] == "j":
  48. notice = self.get_join_notice()
  49. self.connection.reply(data, notice)
  50. else:
  51. self.connection.reply(data, "unknown argument: \x0303%s\x0301. Valid args are 'subs', 'redirs', 'files', 'agg', and 'join'." % data.args[0])
  52. else:
  53. subs = self.count_submissions()
  54. redirs = self.count_redirects()
  55. files = self.count_files()
  56. self.connection.reply(data, "there are currently %s pending submissions, %s open redirect requests, and %s open file upload requests."
  57. % (subs, redirs, files))
  58. def get_join_notice(self):
  59. subs = self.count_submissions()
  60. redirs = self.count_redirects()
  61. files = self.count_files()
  62. agg_num = self.get_aggregate_number((subs, redirs, files))
  63. aggregate = self.get_aggregate(agg_num)
  64. return ("\x02Current status:\x0F Articles for Creation %s (\x0302AFC\x0301: \x0305%s\x0301; \x0302AFC/R\x0301: \x0305%s\x0301; \x0302FFU\x0301: \x0305%s\x0301)"
  65. % (aggregate, subs, redirs, files))
  66. def count_submissions(self):
  67. params = {'action': 'query', 'list': 'categorymembers', 'cmlimit':'500', 'format': 'json'}
  68. params['cmtitle'] = "Category:Pending_AfC_submissions"
  69. data = urllib.urlencode(params)
  70. raw = urllib.urlopen("http://en.wikipedia.org/w/api.php", data).read()
  71. res = json.loads(raw)
  72. subs = len(res['query']['categorymembers'])
  73. subs -= 2 # remove [[Wikipedia:Articles for creation/Redirects]] and [[Wikipedia:Files for upload]], which aren't real submissions
  74. return subs
  75. def count_redirects(self):
  76. content = self.get_page("Wikipedia:Articles_for_creation/Redirects")
  77. total = len(re.findall("==\s*(Redirect|Category) request: \[\[(.*?)\]\]\s*==", content))
  78. closed = content.lower().count("{{afc-c|b}}")
  79. redirs = total - closed
  80. return redirs
  81. def count_files(self):
  82. content = self.get_page("Wikipedia:Files_for_upload")
  83. total = len(re.findall("^\s*==(.*?)==\s*$", content, re.MULTILINE))
  84. closed = content.lower().count("{{ifu-c|b}}")
  85. files = total - closed
  86. return files
  87. def get_page(self, pagename):
  88. params = {'action': 'query', 'prop': 'revisions', 'rvprop':'content', 'rvlimit':'1', 'format': 'json'}
  89. params['titles'] = pagename
  90. data = urllib.urlencode(params)
  91. raw = urllib.urlopen("http://en.wikipedia.org/w/api.php", data).read()
  92. res = json.loads(raw)
  93. pageid = res['query']['pages'].keys()[0]
  94. content = res['query']['pages'][pageid]['revisions'][0]['*']
  95. return content
  96. def get_aggregate(self, num):
  97. if num == 0:
  98. agg = "is \x02\x0303clear\x0301\x0F"
  99. elif num < 60:
  100. agg = "is \x0303almost clear\x0301"
  101. elif num < 125:
  102. agg = "has a \x0312small backlog\x0301"
  103. elif num < 175:
  104. agg = "has an \x0307average backlog\x0301"
  105. elif num < 250:
  106. agg = "is \x0304backlogged\x0301"
  107. elif num < 300:
  108. agg = "is \x02\x0304heavily backlogged\x0301\x0F"
  109. else:
  110. agg = "is \x02\x1F\x0304severely backlogged\x0301\x0F"
  111. return agg
  112. def get_aggregate_number(self, (subs, redirs, files)):
  113. num = (subs * 5) + (redirs * 2) + (files * 2)
  114. return num