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.

85 lines
3.1 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 urllib import quote
  24. from earwigbot.commands import BaseCommand
  25. class Command(BaseCommand):
  26. """Convert a Wikipedia page name into a URL."""
  27. name = "link"
  28. def check(self, data):
  29. # if ((data.is_command and data.command == "link") or
  30. # (("[[" in data.msg and "]]" in data.msg) or
  31. # ("{{" in data.msg and "}}" in data.msg))):
  32. if data.is_command and data.command == "link":
  33. return True
  34. return False
  35. def process(self, data):
  36. msg = data.msg
  37. if re.search("(\[\[(.*?)\]\])|(\{\{(.*?)\}\})", msg):
  38. links = self.parse_line(msg)
  39. links = " , ".join(links)
  40. self.reply(data, links)
  41. elif data.command == "link":
  42. if not data.args:
  43. self.reply(data, "what do you want me to link to?")
  44. return
  45. pagename = ' '.join(data.args)
  46. link = self.parse_link(pagename)
  47. self.reply(data, link)
  48. def parse_line(self, line):
  49. results = []
  50. # Destroy {{{template parameters}}}:
  51. line = re.sub("\{\{\{(.*?)\}\}\}", "", line)
  52. # Find all [[links]]:
  53. links = re.findall("(\[\[(.*?)(\||\]\]))", line)
  54. if links:
  55. # re.findall() returns a list of tuples, but we only want the 2nd
  56. # item in each tuple:
  57. links = [i[1] for i in links]
  58. results = map(self.parse_link, links)
  59. # Find all {{templates}}
  60. templates = re.findall("(\{\{(.*?)(\||\}\}))", line)
  61. if templates:
  62. templates = [i[1] for i in templates]
  63. results.extend(map(self.parse_template, templates))
  64. return results
  65. def parse_link(self, pagename):
  66. link = quote(pagename.replace(" ", "_"), safe="/:")
  67. return "".join(("http://enwp.org/", link))
  68. def parse_template(self, pagename):
  69. pagename = "".join(("Template:", pagename))
  70. return self.parse_link(pagename)