A Python robot that edits Wikipedia and interacts with people over IRC https://en.wikipedia.org/wiki/User:EarwigBot
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

71 linhas
2.6 KiB

  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2009-2012 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 earwigbot.commands import Command
  24. class Link(Command):
  25. """Convert a Wikipedia page name into a URL."""
  26. name = "link"
  27. def process(self, data):
  28. self.site = self.bot.wiki.get_site()
  29. msg = data.msg
  30. if re.search("(\[\[(.*?)\]\])|(\{\{(.*?)\}\})", msg):
  31. links = self.parse_line(msg)
  32. links = u" , ".join(links)
  33. self.reply(data, links.encode("utf8"))
  34. elif data.command == "link":
  35. if not data.args:
  36. self.reply(data, "what do you want me to link to?")
  37. return
  38. pagename = " ".join(data.args)
  39. link = self.site.get_page(pagename).url.encode("utf8")
  40. self.reply(data, link)
  41. def parse_line(self, line):
  42. results = []
  43. # Destroy {{{template parameters}}}:
  44. line = re.sub("\{\{\{(.*?)\}\}\}", "", line)
  45. # Find all [[links]]:
  46. links = re.findall("(\[\[(.*?)(\||\]\]))", line)
  47. if links:
  48. # re.findall() returns a list of tuples, but we only want the 2nd
  49. # item in each tuple:
  50. results = [self.site.get_page(name[1]).url for name in links]
  51. # Find all {{templates}}
  52. templates = re.findall("(\{\{(.*?)(\||\}\}))", line)
  53. if templates:
  54. templates = [i[1] for i in templates]
  55. results.extend(map(self.parse_template, templates))
  56. return results
  57. def parse_template(self, pagename):
  58. return self.site.get_page("Template:" + pagename).url