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.

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