A Python robot that edits Wikipedia and interacts with people over IRC https://en.wikipedia.org/wiki/User:EarwigBot
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

115 řádky
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 import wiki
  24. from earwigbot.commands import BaseCommand
  25. class Command(BaseCommand):
  26. """Get information about an AFC submission by name."""
  27. name = "report"
  28. def process(self, data):
  29. self.site = self.bot.wiki.get_site()
  30. self.data = data
  31. try:
  32. self.statistics = self.bot.tasks.get("afc_statistics")
  33. except KeyError:
  34. e = "Cannot run command: requires afc_statistics task (from earwigbot_plugins)"
  35. self.logger.error(e)
  36. msg = "command requires afc_statistics task (from earwigbot_plugins)"
  37. self.reply(data, msg)
  38. return
  39. if not data.args:
  40. msg = "what submission do you want me to give information about?"
  41. self.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. self.reply(data, "submission \x0302{0}\x0301 not found.".format(title))
  61. def get_page(self, title):
  62. page = self.site.get_page(title, follow_redirects=False)
  63. if page.exists()[0]:
  64. return page
  65. def report(self, page):
  66. url = page.url().replace("en.wikipedia.org/wiki", "enwp.org")
  67. short = self.statistics.get_short_title(page.title())
  68. status = self.get_status(page)
  69. user = self.site.get_user(page.creator())
  70. user_name = user.name()
  71. user_url = user.get_talkpage().url()
  72. msg1 = "AfC submission report for \x0302{0}\x0301 ({1}):"
  73. msg2 = "Status: \x0303{0}\x0301"
  74. msg3 = "Submitted by \x0302{0}\x0301 ({1})"
  75. if status == "accepted":
  76. msg3 = "Reviewed by \x0302{0}\x0301 ({1})"
  77. self.reply(self.data, msg1.format(short, url))
  78. self.say(self.data.chan, msg2.format(status))
  79. self.say(self.data.chan, msg3.format(user_name, user_url))
  80. def get_status(self, page):
  81. if page.is_redirect():
  82. target = page.get_redirect_target()
  83. if self.site.get_page(target).namespace() == wiki.NS_MAIN:
  84. return "accepted"
  85. return "redirect"
  86. statuses = self.statistics.get_statuses(page.get())
  87. if "R" in statuses:
  88. return "being reviewed"
  89. elif "H" in statuses:
  90. return "pending draft"
  91. elif "P" in statuses:
  92. return "pending submission"
  93. elif "T" in statuses:
  94. return "unsubmitted draft"
  95. elif "D" in statuses:
  96. return "declined"
  97. return "unkown"