A Python robot that edits Wikipedia and interacts with people over IRC https://en.wikipedia.org/wiki/User:EarwigBot
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

168 linhas
7.3 KiB

  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2009-2012 by Ben Kurtovic <ben.kurtovic@verizon.net>
  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. __all__ = ["AFCStatus"]
  25. class AFCStatus(Command):
  26. """Get the number of pending AfC submissions, open redirect requests, and
  27. open file upload requests."""
  28. name = "status"
  29. commands = ["status", "count", "num", "number"]
  30. hooks = ["join", "msg"]
  31. def check(self, data):
  32. if data.is_command and data.command in self.commands:
  33. return True
  34. try:
  35. if data.line[1] == "JOIN" and data.chan == "#wikipedia-en-afc":
  36. if data.nick != self.config.irc["frontend"]["nick"]:
  37. return True
  38. except IndexError:
  39. pass
  40. return False
  41. def process(self, data):
  42. self.site = self.bot.wiki.get_site()
  43. if data.line[1] == "JOIN":
  44. status = " ".join(("\x02Current status:\x0F", self.get_status()))
  45. self.notice(data.nick, status)
  46. return
  47. if data.args:
  48. action = data.args[0].lower()
  49. if action.startswith("sub") or action == "s":
  50. subs = self.count_submissions()
  51. msg = "there are \x0305{0}\x0301 pending AfC submissions (\x0302WP:AFC\x0301)."
  52. self.reply(data, msg.format(subs))
  53. elif action.startswith("redir") or action == "r":
  54. redirs = self.count_redirects()
  55. msg = "there are \x0305{0}\x0301 open redirect requests (\x0302WP:AFC/R\x0301)."
  56. self.reply(data, msg.format(redirs))
  57. elif action.startswith("file") or action == "f":
  58. files = self.count_redirects()
  59. msg = "there are \x0305{0}\x0301 open file upload requests (\x0302WP:FFU\x0301)."
  60. self.reply(data, msg.format(files))
  61. elif action.startswith("agg") or action == "a":
  62. try:
  63. agg_num = int(data.args[1])
  64. except IndexError:
  65. agg_data = (self.count_submissions(),
  66. self.count_redirects(), self.count_files())
  67. agg_num = self.get_aggregate_number(agg_data)
  68. except ValueError:
  69. msg = "\x0303{0}\x0301 isn't a number!"
  70. self.reply(data, msg.format(data.args[1]))
  71. return
  72. aggregate = self.get_aggregate(agg_num)
  73. msg = "aggregate is \x0305{0}\x0301 (AfC {1})."
  74. self.reply(data, msg.format(agg_num, aggregate))
  75. elif action.startswith("nocolor") or action == "n":
  76. self.reply(data, self.get_status(color=False))
  77. else:
  78. msg = "unknown argument: \x0303{0}\x0301. Valid args are 'subs', 'redirs', 'files', 'agg', 'nocolor'."
  79. self.reply(data, msg.format(data.args[0]))
  80. else:
  81. self.reply(data, self.get_status())
  82. def get_status(self, color=True):
  83. subs = self.count_submissions()
  84. redirs = self.count_redirects()
  85. files = self.count_files()
  86. agg_num = self.get_aggregate_number((subs, redirs, files))
  87. aggregate = self.get_aggregate(agg_num)
  88. if color:
  89. msg = "Articles for creation {0} (\x0302AFC\x0301: \x0305{1}\x0301; \x0302AFC/R\x0301: \x0305{2}\x0301; \x0302FFU\x0301: \x0305{3}\x0301)."
  90. else:
  91. msg = "Articles for creation {0} (AFC: {1}; AFC/R: {2}; FFU: {3})."
  92. return msg.format(aggregate, subs, redirs, files)
  93. def count_submissions(self):
  94. """Returns the number of open AFC submissions (count of CAT:PEND)."""
  95. cat = self.site.get_category("Pending AfC submissions")
  96. subs = len(cat.get_members(use_sql=True))
  97. # Remove [[Wikipedia:Articles for creation/Redirects]] and
  98. # [[Wikipedia:Files for upload]], which aren't real submissions:
  99. return subs - 2
  100. def count_redirects(self):
  101. """Returns the number of open redirect submissions. Calculated as the
  102. total number of submissions minus the closed ones."""
  103. title = "Wikipedia:Articles for creation/Redirects"
  104. content = self.site.get_page(title).get()
  105. total = len(re.findall("^\s*==(.*?)==\s*$", content, re.MULTILINE))
  106. closed = content.lower().count("{{afc-c|b}}")
  107. redirs = total - closed
  108. return redirs
  109. def count_files(self):
  110. """Returns the number of open WP:FFU (Files For Upload) requests.
  111. Calculated as the total number of requests minus the closed ones."""
  112. content = self.site.get_page("Wikipedia:Files for upload").get()
  113. total = len(re.findall("^\s*==(.*?)==\s*$", content, re.MULTILINE))
  114. closed = content.lower().count("{{ifu-c|b}}")
  115. files = total - closed
  116. return files
  117. def get_aggregate(self, num):
  118. """Returns a human-readable AFC status based on the number of pending
  119. AFC submissions, open redirect requests, and open FFU requests. This
  120. does not match {{AFC status}} directly because the algorithm factors in
  121. WP:AFC/R and WP:FFU while the template only looks at the main
  122. submissions. The reasoning is that AFC/R and FFU are still part of
  123. the project, so even if there are no pending submissions, a backlog at
  124. FFU (for example) indicates that our work is *not* done and the
  125. project-wide backlog is most certainly *not* clear."""
  126. if num == 0:
  127. return "is \x02\x0303clear\x0301\x0F"
  128. elif num <= 200:
  129. return "is \x0303almost clear\x0301"
  130. elif num <= 400:
  131. return "is \x0312normal\x0301"
  132. elif num <= 600:
  133. return "is \x0307lightly backlogged\x0301"
  134. elif num <= 900:
  135. return "is \x0304backlogged\x0301"
  136. elif num <= 1200:
  137. return "is \x02\x0304heavily backlogged\x0301\x0F"
  138. else:
  139. return "is \x02\x1F\x0304severely backlogged\x0301\x0F"
  140. def get_aggregate_number(self, (subs, redirs, files)):
  141. """Returns an 'aggregate number' based on the real number of pending
  142. submissions in CAT:PEND (subs), open redirect submissions in WP:AFC/R
  143. (redirs), and open files-for-upload requests in WP:FFU (files)."""
  144. num = subs + (redirs / 2) + (files / 2)
  145. return num