A Python robot that edits Wikipedia and interacts with people over IRC https://en.wikipedia.org/wiki/User:EarwigBot
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. # -*- coding: utf-8 -*-
  2. import re
  3. import urllib
  4. from classes import BaseCommand
  5. class Command(BaseCommand):
  6. """A somewhat advanced calculator: see http://futureboy.us/fsp/frink.fsp
  7. for details."""
  8. name = "calc"
  9. def process(self, data):
  10. if not data.args:
  11. self.connection.reply(data, "what do you want me to calculate?")
  12. return
  13. query = ' '.join(data.args)
  14. query = self.cleanup(query)
  15. url = "http://futureboy.us/fsp/frink.fsp?fromVal={0}"
  16. url = url.format(urllib.quote(query))
  17. result = urllib.urlopen(url).read()
  18. r_result = re.compile(r'(?i)<A NAME=results>(.*?)</A>')
  19. r_tag = re.compile(r'<\S+.*?>')
  20. match = r_result.search(result)
  21. if not match:
  22. self.connection.reply(data, "Calculation error.")
  23. return
  24. result = match.group(1)
  25. result = r_tag.sub("", result) # strip span.warning tags
  26. result = result.replace("&gt;", ">")
  27. result = result.replace("(undefined symbol)", "(?) ")
  28. result = result.strip()
  29. if not result:
  30. result = '?'
  31. elif " in " in query:
  32. result += " " + query.split(" in ", 1)[1]
  33. res = "%s = %s" % (query, result)
  34. self.connection.reply(data, res)
  35. def cleanup(self, query):
  36. fixes = [
  37. (' in ', ' -> '),
  38. (' over ', ' / '),
  39. (u'£', 'GBP '),
  40. (u'€', 'EUR '),
  41. ('\$', 'USD '),
  42. (r'\bKB\b', 'kilobytes'),
  43. (r'\bMB\b', 'megabytes'),
  44. (r'\bGB\b', 'kilobytes'),
  45. ('kbps', '(kilobits / second)'),
  46. ('mbps', '(megabits / second)')
  47. ]
  48. for original, fix in fixes:
  49. query = re.sub(original, fix, query)
  50. return query.strip()