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.

116 lines
4.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.classes import BaseCommand
  24. from earwigbot import tasks
  25. from earwigbot import wiki
  26. class Command(BaseCommand):
  27. """Get information about an AFC submission by name."""
  28. name = "report"
  29. def process(self, data):
  30. self.site = wiki.get_site()
  31. self.site._maxlag = None
  32. self.data = data
  33. try:
  34. self.statistics = tasks.get("afc_statistics")
  35. except KeyError:
  36. e = "Cannot run command: requires afc_statistics task."
  37. self.logger.error(e)
  38. return
  39. if not data.args:
  40. msg = "what submission do you want me to give information about?"
  41. self.connection.reply(data, msg)
  42. return
  43. title = " ".join(data.args)
  44. title = title.replace("http://en.wikipedia.org/wiki/", "")
  45. title = title.replace("http://enwp.org/", "").strip()
  46. # Given '!report Foo', first try [[Foo]]:
  47. page = self.get_page(title)
  48. if page:
  49. return self.report(page)
  50. # Then try [[Wikipedia:Articles for creation/Foo]]:
  51. newtitle = "/".join(("Wikipedia:Articles for creation", title))
  52. page = self.get_page(newtitle)
  53. if page:
  54. return self.report(page)
  55. # Then try [[Wikipedia talk:Articles for creation/Foo]]:
  56. newtitle = "/".join(("Wikipedia talk:Articles for creation", title))
  57. page = self.get_page(newtitle)
  58. if page:
  59. return self.report(page)
  60. msg = "submission \x0302{0}\x0301 not found.".format(title)
  61. self.connection.reply(data, msg)
  62. def get_page(self, title):
  63. page = self.site.get_page(title, follow_redirects=False)
  64. if page.exists()[0]:
  65. return page
  66. def report(self, page):
  67. url = page.url().replace("en.wikipedia.org/wiki", "enwp.org")
  68. short = self.statistics.get_short_title(page.title())
  69. status = self.get_status(page)
  70. user = self.site.get_user(page.creator())
  71. user_name = user.name()
  72. user_url = user.get_talkpage().url()
  73. msg1 = "AfC submission report for \x0302{0}\x0301 ({1}):"
  74. msg2 = "Status: \x0303{0}\x0301"
  75. msg3 = "Submitted by \x0302{0}\x0301 ({1})"
  76. if status == "accepted":
  77. msg3 = "Reviewed by \x0302{0}\x0301 ({1})"
  78. self.connection.reply(self.data, msg1.format(short, url))
  79. self.connection.say(self.data.chan, msg2.format(status))
  80. self.connection.say(self.data.chan, msg3.format(user_name, user_url))
  81. def get_status(self, page):
  82. if page.is_redirect():
  83. target = page.get_redirect_target()
  84. if self.site.get_page(target).namespace() == wiki.NS_MAIN:
  85. return "accepted"
  86. return "redirect"
  87. statuses = self.statistics.get_statuses(page.get())
  88. if "R" in statuses:
  89. return "being reviewed"
  90. elif "H" in statuses:
  91. return "pending draft"
  92. elif "P" in statuses:
  93. return "pending submission"
  94. elif "T" in statuses:
  95. return "unsubmitted draft"
  96. elif "D" in statuses:
  97. return "declined"
  98. return "unkown"