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.

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