A Python robot that edits Wikipedia and interacts with people over IRC https://en.wikipedia.org/wiki/User:EarwigBot
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

152 righe
6.4 KiB

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