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.

133 lines
5.6 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 = data.args[1]
  39. except KeyError:
  40. agg_data = (self.count_submissions(), self.count_redirects(), self.count_files())
  41. agg_num = self.get_aggregate_number(agg_data)
  42. aggregate = self.get_aggregate(agg_num)
  43. self.connection.reply(data, "aggregate is currently %s (AfC %s)." % (agg_num, aggregate))
  44. elif data.args[0].startswith("join") or data.args[0] == "j":
  45. notice = self.get_join_notice()
  46. self.connection.reply(data, notice)
  47. else:
  48. self.connection.reply(data, "unknown argument: \x0303%s\x0301. Valid args are 'subs', 'redirs', 'files', 'agg', and 'join'." % data.args[0])
  49. else:
  50. subs = self.count_submissions()
  51. redirs = self.count_redirects()
  52. files = self.count_files()
  53. self.connection.reply(data, "there are currently %s pending submissions, %s open redirect requests, and %s open file upload requests."
  54. % (subs, redirs, files))
  55. def get_join_notice(self):
  56. subs = self.count_submissions()
  57. redirs = self.count_redirects()
  58. files = self.count_files()
  59. agg_num = self.get_aggregate_number((subs, redirs, files))
  60. aggregate = self.get_aggregate(agg_num)
  61. return ("\x02Current status:\x0F Articles for Creation %s (\x0302AFC\x0301: \x0305%s\x0301; \x0302AFC/R\x0301: \x0305%s\x0301; \x0302FFU\x0301: \x0305%s\x0301)"
  62. % (aggregate, subs, redirs, files))
  63. def count_submissions(self):
  64. params = {'action': 'query', 'list': 'categorymembers', 'cmlimit':'500', 'format': 'json'}
  65. params['cmtitle'] = "Category:Pending_AfC_submissions"
  66. data = urllib.urlencode(params)
  67. raw = urllib.urlopen("http://en.wikipedia.org/w/api.php", data).read()
  68. res = json.loads(raw)
  69. subs = len(res['query']['categorymembers'])
  70. subs -= 2 # remove [[Wikipedia:Articles for creation/Redirects]] and [[Wikipedia:Files for upload]], which aren't real submissions
  71. return subs
  72. def count_redirects(self):
  73. content = self.get_page("Wikipedia:Articles_for_creation/Redirects")
  74. total = len(re.findall("==\s*(Redirect|Category) request: \[\[(.*?)\]\]\s*==", content))
  75. closed = content.lower().count("{{afc-c|b}}")
  76. redirs = total - closed
  77. return redirs
  78. def count_files(self):
  79. content = self.get_page("Wikipedia:Files_for_upload")
  80. total = len(re.findall("^\s*==(.*?)==\s*$", content, re.MULTILINE))
  81. closed = content.lower().count("{{ifu-c|b}}")
  82. files = total - closed
  83. return files
  84. def get_page(self, pagename):
  85. params = {'action': 'query', 'prop': 'revisions', 'rvprop':'content', 'rvlimit':'1', 'format': 'json'}
  86. params['titles'] = pagename
  87. data = urllib.urlencode(params)
  88. raw = urllib.urlopen("http://en.wikipedia.org/w/api.php", data).read()
  89. res = json.loads(raw)
  90. pageid = res['query']['pages'].keys()[0]
  91. content = res['query']['pages'][pageid]['revisions'][0]['*']
  92. return content
  93. def get_aggregate(self, num):
  94. if num == 0:
  95. agg = "is \x02\x0303clear\x0301\x0F"
  96. elif num < 60:
  97. agg = "is \x0303almost clear\x0301"
  98. elif num < 125:
  99. agg = "has a \x0312small backlog\x0301"
  100. elif num < 175:
  101. agg = "has an \x0307average backlog\x0301"
  102. elif num < 250:
  103. agg = "is \x0304backlogged\x0301"
  104. elif num < 300:
  105. agg = "is \x02\x0304heavily backlogged\x0301\x0F"
  106. else:
  107. agg = "is \x02\x1F\x0304severely backlogged\x0301\x0F"
  108. return agg
  109. def get_aggregate_number(self, (subs, redirs, files)):
  110. num = (subs * 5) + (redirs * 2) + (files * 2)
  111. return num