Additional IRC commands and bot tasks for EarwigBot https://en.wikipedia.org/wiki/User:EarwigBot
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 

199 satır
8.5 KiB

  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2009-2014 Ben Kurtovic <ben.kurtovic@gmail.com>
  4. #
  5. # Permission is hereby granted, free of charge, to any person obtaining a copy
  6. # of this software and associated documentation files (the "Software"), to deal
  7. # in the Software without restriction, including without limitation the rights
  8. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. # copies of the Software, and to permit persons to whom the Software is
  10. # furnished to do so, subject to the following conditions:
  11. #
  12. # The above copyright notice and this permission notice shall be included in
  13. # all copies or substantial portions of the Software.
  14. #
  15. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. # SOFTWARE.
  22. import re
  23. from earwigbot.commands import Command
  24. class AfCStatus(Command):
  25. """Get the number of pending AfC submissions, open redirect requests, and
  26. open file upload requests."""
  27. name = "status"
  28. commands = ["status", "count", "num", "number"]
  29. hooks = ["join", "msg"]
  30. def setup(self):
  31. try:
  32. self.ignore_list = self.config.commands[self.name]["ignoreList"]
  33. except KeyError:
  34. try:
  35. ignores = self.config.tasks["afc_statistics"]["ignoreList"]
  36. self.ignore_list = ignores
  37. except KeyError:
  38. self.ignore_list = []
  39. def check(self, data):
  40. if data.is_command and data.command in self.commands:
  41. return True
  42. try:
  43. if data.line[1] == "JOIN" and data.chan == "#wikipedia-en-afc":
  44. if data.nick != self.config.irc["frontend"]["nick"]:
  45. return True
  46. except IndexError:
  47. pass
  48. return False
  49. def process(self, data):
  50. self.site = self.bot.wiki.get_site()
  51. if data.line[1] == "JOIN":
  52. status = " ".join(("\x02Current status:\x0F", self.get_status()))
  53. self.notice(data.nick, status)
  54. return
  55. if data.args:
  56. action = data.args[0].lower()
  57. if action.startswith("sub") or action == "s":
  58. subs = self.count_submissions()
  59. msg = "There are \x0305{0}\x0F pending AfC submissions (\x0302WP:AFC\x0F)."
  60. self.reply(data, msg.format(subs))
  61. elif action.startswith("redir") or action == "r":
  62. redirs = self.count_redirects()
  63. msg = "There are \x0305{0}\x0F open redirect requests (\x0302WP:AFC/R\x0F)."
  64. self.reply(data, msg.format(redirs))
  65. elif action.startswith("file") or action == "f":
  66. files = self.count_redirects()
  67. msg = "There are \x0305{0}\x0F open file upload requests (\x0302WP:FFU\x0F)."
  68. self.reply(data, msg.format(files))
  69. elif action.startswith("agg") or action == "a":
  70. try:
  71. agg_num = int(data.args[1])
  72. except IndexError:
  73. agg_data = (self.count_submissions(),
  74. self.count_redirects(), self.count_files())
  75. agg_num = self.get_aggregate_number(agg_data)
  76. except ValueError:
  77. msg = "\x0303{0}\x0F isn't a number!"
  78. self.reply(data, msg.format(data.args[1]))
  79. return
  80. aggregate = self.get_aggregate(agg_num)
  81. msg = "Aggregate is \x0305{0}\x0F (AfC {1})."
  82. self.reply(data, msg.format(agg_num, aggregate))
  83. elif action.startswith("g13_e") or action.startswith("g13e"):
  84. g13_eli = self.count_g13_eligible()
  85. msg = "There are \x0305{0}\x0F CSD:G13-eligible pages."
  86. self.reply(data, msg.format(g13_eli))
  87. elif action.startswith("g13_a") or action.startswith("g13a"):
  88. g13_noms = self.count_g13_active()
  89. msg = "There are \x0305{0}\x0F active CSD:G13 nominations."
  90. self.reply(data, msg.format(g13_noms))
  91. elif action.startswith("nocolor") or action == "n":
  92. self.reply(data, self.get_status(color=False))
  93. else:
  94. msg = "Unknown argument: \x0303{0}\x0F. Valid args are " +\
  95. "'subs', 'redirs', 'files', 'agg', 'nocolor', " +\
  96. "'g13_eligible', 'g13_active'."
  97. self.reply(data, msg.format(data.args[0]))
  98. else:
  99. self.reply(data, self.get_status())
  100. def get_status(self, color=True):
  101. subs = self.count_submissions()
  102. redirs = self.count_redirects()
  103. files = self.count_files()
  104. agg_num = self.get_aggregate_number((subs, redirs, files))
  105. aggregate = self.get_aggregate(agg_num)
  106. if color:
  107. msg = "Articles for creation {0} (\x0302AFC\x0F: \x0305{1}\x0F; \x0302AFC/R\x0F: \x0305{2}\x0F; \x0302FFU\x0F: \x0305{3}\x0F)."
  108. else:
  109. msg = "Articles for creation {0} (AFC: {1}; AFC/R: {2}; FFU: {3})."
  110. return msg.format(aggregate, subs, redirs, files)
  111. def count_g13_eligible(self):
  112. """
  113. Returns the number of G13 Eligible AfC Submissions (count of
  114. Category:G13 eligible AfC submissions)
  115. """
  116. return self.site.get_category("G13 eligible AfC submissions").pages
  117. def count_g13_active(self):
  118. """
  119. Returns the number of active CSD:G13 nominations ( count of
  120. Category:Candidates for speedy deletion as abandoned AfC submissions)
  121. """
  122. catname = "Candidates for speedy deletion as abandoned AfC submissions"
  123. return self.site.get_category(catname).pages
  124. def count_submissions(self):
  125. """Returns the number of open AfC submissions (count of CAT:PEND)."""
  126. minus = len(self.ignore_list)
  127. return self.site.get_category("Pending AfC submissions").pages - minus
  128. def count_redirects(self):
  129. """Returns the number of open redirect submissions. Calculated as the
  130. total number of submissions minus the closed ones."""
  131. title = "Wikipedia:Articles for creation/Redirects"
  132. content = self.site.get_page(title).get()
  133. total = len(re.findall("^\s*==(.*?)==\s*$", content, re.MULTILINE))
  134. closed = content.lower().count("{{afc-c|b}}")
  135. redirs = total - closed
  136. return redirs
  137. def count_files(self):
  138. """Returns the number of open WP:FFU (Files For Upload) requests.
  139. Calculated as the total number of requests minus the closed ones."""
  140. content = self.site.get_page("Wikipedia:Files for upload").get()
  141. total = len(re.findall("^\s*==(.*?)==\s*$", content, re.MULTILINE))
  142. closed = content.lower().count("{{ifu-c|b}}")
  143. files = total - closed
  144. return files
  145. def get_aggregate(self, num):
  146. """Returns a human-readable AfC status based on the number of pending
  147. AfC submissions, open redirect requests, and open FFU requests. This
  148. does not match {{AfC status}} directly because the algorithm factors in
  149. WP:AFC/R and WP:FFU while the template only looks at the main
  150. submissions. The reasoning is that AFC/R and FFU are still part of
  151. the project, so even if there are no pending submissions, a backlog at
  152. FFU (for example) indicates that our work is *not* done and the
  153. project-wide backlog is most certainly *not* clear."""
  154. if num == 0:
  155. return "is \x02\x0303clear\x0F"
  156. elif num <= 200:
  157. return "is \x0303almost clear\x0F"
  158. elif num <= 400:
  159. return "is \x0312normal\x0F"
  160. elif num <= 600:
  161. return "is \x0307lightly backlogged\x0F"
  162. elif num <= 900:
  163. return "is \x0304backlogged\x0F"
  164. elif num <= 1200:
  165. return "is \x02\x0304heavily backlogged\x0F"
  166. else:
  167. return "is \x02\x1F\x0304severely backlogged\x0F"
  168. def get_aggregate_number(self, (subs, redirs, files)):
  169. """Returns an 'aggregate number' based on the real number of pending
  170. submissions in CAT:PEND (subs), open redirect submissions in WP:AFC/R
  171. (redirs), and open files-for-upload requests in WP:FFU (files)."""
  172. num = subs + (redirs / 2) + (files / 2)
  173. return num