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.

101 lines
4.5 KiB

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