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.

105 lines
4.6 KiB

  1. # -*- coding: utf-8 -*-
  2. """
  3. Get information about an AFC submission by name.
  4. """
  5. import json
  6. import re
  7. import urllib
  8. from irc.classes import BaseCommand
  9. class AFCReport(BaseCommand):
  10. def get_hooks(self):
  11. return ["msg"]
  12. def get_help(self, command):
  13. return "Get information about an AFC submission by name."
  14. def check(self, data):
  15. if data.is_command and data.command in ["report", "afc_report"]:
  16. return True
  17. return False
  18. def process(self, data):
  19. self.data = data
  20. if not data.args:
  21. self.connection.reply(data, "what submission do you want me to give information about?")
  22. return
  23. pagename = ' '.join(data.args)
  24. pagename = pagename.replace("http://en.wikipedia.org/wiki/", "").replace("http://enwp.org/", "").replace("_", " ")
  25. pagename = pagename.strip()
  26. if self.page_exists(pagename): # given '!report Foo', first try [[Foo]]
  27. self.report(pagename)
  28. else: # if that doesn't work, try [[Wikipedia:Articles for creation/Foo]]
  29. if self.page_exists("Wikipedia:Articles for creation/" + pagename):
  30. self.report("Wikipedia:Articles for creation/" + pagename)
  31. else: # if that doesn't work, try [[Wikipedia talk:Articles for creation/Foo]]
  32. if self.page_exists("Wikipedia talk:Articles for creation/" + pagename):
  33. self.report("Wikipedia talk:Articles for creation/" + pagename)
  34. else:
  35. self.connection.reply(data, "submission \x0302{0}\x0301 not found.".format(pagename))
  36. def report(self, pagename):
  37. data = self.data
  38. shortname = pagename.replace("Wikipedia:Articles for creation/", "").replace("Wikipedia talk:Articles for creation/", "")
  39. url = "http://enwp.org/" + urllib.quote(pagename.replace(" ", "_"))
  40. status = self.get_status(pagename)
  41. user, user_url = self.get_creator(pagename)
  42. self.connection.reply(data, "AfC submission report for \x0302{0}\x0301 ({1}):".format(shortname, url))
  43. self.connection.say(data.chan, "Status: \x0303{0}\x0301".format(status))
  44. if status == "accepted": # the first edit will be the redirect [[WT:AFC/Foo]] -> [[Foo]], NOT the creation of the submission
  45. self.connection.say(data.chan, "Reviewed by \x0302{0}\x0301 ({1})".format(user, user_url))
  46. else:
  47. self.connection.say(data.chan, "Submitted by \x0302{0}\x0301 ({1})".format(user, user_url))
  48. def page_exists(self, pagename):
  49. params = {'action': 'query', 'format': 'json', 'titles': pagename}
  50. data = urllib.urlencode(params)
  51. raw = urllib.urlopen("http://en.wikipedia.org/w/api.php", data).read()
  52. res = json.loads(raw)
  53. try:
  54. res['query']['pages'].values()[0]['missing'] # this key will appear if the page does not exist
  55. return False
  56. except KeyError: # if it's not there, the page exists
  57. return True
  58. def get_status(self, pagename):
  59. params = {'action': 'query', 'prop': 'revisions', 'rvprop':'content', 'rvlimit':'1', 'format': 'json'}
  60. params['titles'] = pagename
  61. data = urllib.urlencode(params)
  62. raw = urllib.urlopen("http://en.wikipedia.org/w/api.php", data).read()
  63. res = json.loads(raw)
  64. pageid = res['query']['pages'].keys()[0]
  65. content = res['query']['pages'][pageid]['revisions'][0]['*']
  66. lcontent = content.lower()
  67. if re.search("\{\{afc submission\|r\|(.*?)\}\}", lcontent):
  68. return "being reviewed"
  69. elif re.search("\{\{afc submission\|\|(.*?)\}\}", lcontent):
  70. return "pending"
  71. elif re.search("\{\{afc submission\|d\|(.*?)\}\}", lcontent):
  72. try:
  73. reason = re.findall("\{\{afc submission\|d\|(.*?)(\||\}\})", lcontent)[0][0]
  74. return "declined with reason \"{0}\"".format(reason)
  75. except IndexError:
  76. return "declined"
  77. else:
  78. if "#redirect" in content:
  79. return "accepted"
  80. else:
  81. return "unkown"
  82. def get_creator(self, pagename):
  83. params = {'action': 'query', 'prop': 'revisions', 'rvprop': 'user', 'rvdir': 'newer', 'rvlimit': '1', 'format': 'json'}
  84. params['titles'] = pagename
  85. data = urllib.urlencode(params)
  86. raw = urllib.urlopen("http://en.wikipedia.org/w/api.php", data).read()
  87. res = json.loads(raw)
  88. user = res['query']['pages'].values()[0]['revisions'][0]['user']
  89. user_url = "http://enwp.org/User_talk:" + urllib.quote(user.replace(" ", "_"))
  90. return user, user_url