A Python robot that edits Wikipedia and interacts with people over IRC https://en.wikipedia.org/wiki/User:EarwigBot
Você não pode selecionar mais de 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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. # -*- coding: utf-8 -*-
  2. """Convert a Wikipedia page name into a URL."""
  3. import re
  4. connection, data = None, None
  5. def call(c, d):
  6. global connection, data
  7. connection, data = c, d
  8. msg = data.msg
  9. if re.search("(\[\[(.*?)\]\])|(\{\{(.*?)\}\})", msg):
  10. links = parse_line(msg)
  11. links = " , ".join(links)
  12. connection.reply(data.chan, data.nick, links)
  13. elif data.command == "!link":
  14. if not data.args:
  15. connection.reply(data.chan, data.nick, "what do you want me to link to?")
  16. return
  17. pagename = ' '.join(data.args)
  18. link = parse_link(pagename)
  19. connection.reply(data.chan, data.nick, link)
  20. def parse_line(line):
  21. results = list()
  22. line = re.sub("\{\{\{(.*?)\}\}\}", "", line) # destroy {{{template parameters}}}
  23. links = re.findall("(\[\[(.*?)(\||\]\]))", line) # find all [[links]]
  24. if links:
  25. links = map(lambda x: x[1], links) # re.findall() returns a list of tuples, but we only want the 2nd item in each tuple
  26. results.extend(map(parse_link, links))
  27. templates = re.findall("(\{\{(.*?)(\||\}\}))", line) # find all {{templates}}
  28. if templates:
  29. templates = map(lambda x: x[1], templates)
  30. results.extend(map(parse_template, templates))
  31. return results
  32. def parse_link(pagename):
  33. pagename = pagename.strip()
  34. link = "http://en.wikipedia.org/wiki/" + pagename
  35. link = link.replace(" ", "_")
  36. return link
  37. def parse_template(pagename):
  38. pagename = "Template:%s" % pagename # TODO: implement an actual namespace check
  39. link = parse_link(pagename)
  40. return link