A Python robot that edits Wikipedia and interacts with people over IRC https://en.wikipedia.org/wiki/User:EarwigBot
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

afc_status.py 5.8 KiB

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