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.

84 lines
2.9 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. import urllib
  24. from earwigbot.commands import Command
  25. class Calc(Command):
  26. """A somewhat advanced calculator: see http://futureboy.us/fsp/frink.fsp
  27. for details."""
  28. name = "calc"
  29. def process(self, data):
  30. if not data.args:
  31. self.reply(data, "What do you want me to calculate?")
  32. return
  33. query = ' '.join(data.args)
  34. query = self.cleanup(query)
  35. url = "http://futureboy.us/fsp/frink.fsp?fromVal={0}"
  36. url = url.format(urllib.quote(query))
  37. result = urllib.urlopen(url).read()
  38. r_result = re.compile(r'(?i)<A NAME=results>(.*?)</A>')
  39. r_tag = re.compile(r'<\S+.*?>')
  40. match = r_result.search(result)
  41. if not match:
  42. self.reply(data, "Calculation error.")
  43. return
  44. result = match.group(1)
  45. result = r_tag.sub("", result) # strip span.warning tags
  46. result = result.replace("&gt;", ">")
  47. result = result.replace("(undefined symbol)", "(?) ")
  48. result = result.strip()
  49. if not result:
  50. result = '?'
  51. elif " in " in query:
  52. result += " " + query.split(" in ", 1)[1]
  53. res = "%s = %s" % (query, result)
  54. self.reply(data, res)
  55. def cleanup(self, query):
  56. fixes = [
  57. (' in ', ' -> '),
  58. (' over ', ' / '),
  59. (u'£', 'GBP '),
  60. (u'€', 'EUR '),
  61. ('\$', 'USD '),
  62. (r'\bKB\b', 'kilobytes'),
  63. (r'\bMB\b', 'megabytes'),
  64. (r'\bGB\b', 'gigabytes'),
  65. ('kbps', '(kilobits / second)'),
  66. ('mbps', '(megabits / second)')
  67. ]
  68. for original, fix in fixes:
  69. query = re.sub(original, fix, query)
  70. return query.strip()