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.

afc_report.py 3.1 KiB

13 年之前
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. # -*- coding: utf-8 -*-
  2. import re
  3. from classes import BaseCommand
  4. import tasks
  5. import wiki
  6. class Command(BaseCommand):
  7. """Get information about an AFC submission by name."""
  8. name = "report"
  9. def process(self, data):
  10. self.site = wiki.get_site()
  11. self.site._maxlag = None
  12. self.data = data
  13. try:
  14. self.statistics = tasks.get("afc_statistics")
  15. except KeyError:
  16. e = "Cannot run command: requires afc_statistics task."
  17. self.logger.error(e)
  18. return
  19. if not data.args:
  20. msg = "what submission do you want me to give information about?"
  21. self.connection.reply(data, msg)
  22. return
  23. title = " ".join(data.args)
  24. title = title.replace("http://en.wikipedia.org/wiki/", "")
  25. title = title.replace("http://enwp.org/", "").strip()
  26. # Given '!report Foo', first try [[Foo]]:
  27. page = self.get_page(title)
  28. if page:
  29. return self.report(page)
  30. # Then try [[Wikipedia:Articles for creation/Foo]]:
  31. newtitle = "/".join(("Wikipedia:Articles for creation", title))
  32. page = self.get_page(newtitle)
  33. if page:
  34. return self.report(page)
  35. # Then try [[Wikipedia talk:Articles for creation/Foo]]:
  36. newtitle = "/".join(("Wikipedia talk:Articles for creation", title))
  37. page = self.get_page(newtitle)
  38. if page:
  39. return self.report(page)
  40. msg = "submission \x0302{0}\x0301 not found.".format(title)
  41. self.connection.reply(data, msg)
  42. def get_page(self, title):
  43. page = self.site.get_page(title, follow_redirects=False)
  44. if page.exists()[0]:
  45. return page
  46. def report(self, page):
  47. url = page.url().replace("en.wikipedia.org/wiki", "enwp.org")
  48. short = self.statistics.get_short_title(page.title())
  49. status = self.get_status(page)
  50. user = self.site.get_user(page.creator())
  51. user_name = user.name()
  52. user_url = user.get_talkpage().url()
  53. msg1 = "AfC submission report for \x0302{0}\x0301 ({1}):"
  54. msg2 = "Status: \x0303{0}\x0301"
  55. msg3 = "Submitted by \x0302{0}\x0301 ({1})"
  56. if status == "accepted":
  57. msg3 = "Reviewed by \x0302{0}\x0301 ({1})"
  58. self.connection.reply(self.data, msg1.format(short, url))
  59. self.connection.say(self.data.chan, msg2.format(status))
  60. self.connection.say(self.data.chan, msg3.format(user_name, user_url))
  61. def get_status(self, page):
  62. if page.is_redirect():
  63. target = page.get_redirect_target()
  64. if self.site.get_page(target).namespace() == wiki.NS_MAIN:
  65. return "accepted"
  66. return "redirect"
  67. statuses = self.statistics.get_statuses(page.get())
  68. if "R" in statuses:
  69. return "being reviewed"
  70. elif "H" in statuses:
  71. return "pending draft"
  72. elif "P" in statuses:
  73. return "pending submission"
  74. elif "T" in statuses:
  75. return "unsubmitted draft"
  76. elif "D" in statuses:
  77. return "declined"
  78. return "unkown"