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.

80 regels
3.1 KiB

  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2009-2015 Ben Kurtovic <ben.kurtovic@gmail.com>
  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 earwigbot.commands import Command
  24. class Link(Command):
  25. """Convert a Wikipedia page name into a URL."""
  26. name = "link"
  27. def setup(self):
  28. self.last = {}
  29. def check(self, data):
  30. if re.search("(\[\[(.*?)\]\])|(\{\{(.*?)\}\})", data.msg):
  31. self.last[data.chan] = data.msg # Store most recent link
  32. return data.is_command and data.command == self.name
  33. def process(self, data):
  34. self.site = self.bot.wiki.get_site()
  35. if re.search("(\[\[(.*?)\]\])|(\{\{(.*?)\}\})", data.msg):
  36. links = u" , ".join(self.parse_line(data.msg))
  37. self.reply(data, links.encode("utf8"))
  38. elif data.command == "link":
  39. if not data.args:
  40. if data.chan in self.last:
  41. links = u" , ".join(self.parse_line(self.last[data.chan]))
  42. self.reply(data, links.encode("utf8"))
  43. else:
  44. self.reply(data, "What do you want me to link to?")
  45. return
  46. pagename = " ".join(data.args)
  47. link = self.site.get_page(pagename).url.encode("utf8")
  48. self.reply(data, link)
  49. def parse_line(self, line):
  50. """Return a list of links within a line of text."""
  51. results = []
  52. # Destroy {{{template parameters}}}:
  53. line = re.sub("\{\{\{(.*?)\}\}\}", "", line)
  54. # Find all [[links]]:
  55. links = re.findall("(\[\[(.*?)(\||\]\]))", line)
  56. if links:
  57. # re.findall() returns a list of tuples, but we only want the 2nd
  58. # item in each tuple:
  59. results = [self.site.get_page(name[1]).url for name in links]
  60. # Find all {{templates}}
  61. templates = re.findall("(\{\{(.*?)(\||\}\}))", line)
  62. if templates:
  63. p_tmpl = lambda name: self.site.get_page("Template:" + name).url
  64. templates = [p_tmpl(i[1]) for i in templates]
  65. results += templates
  66. return results