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.

71 lines
2.6 KiB

  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2009-2014 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. from datetime import datetime
  23. from math import floor
  24. from time import time
  25. from earwigbot import importer
  26. from earwigbot.commands import Command
  27. pytz = importer.new("pytz")
  28. class Time(Command):
  29. """Report the current time in any timezone (UTC default), UNIX epoch time,
  30. or beat time."""
  31. name = "time"
  32. commands = ["time", "beats", "swatch", "epoch"]
  33. def process(self, data):
  34. if data.command in ["beats", "swatch"]:
  35. self.do_beats(data)
  36. return
  37. if data.command == "epoch":
  38. self.reply(data, time())
  39. return
  40. if data.args:
  41. timezone = data.args[0]
  42. else:
  43. timezone = "UTC"
  44. if timezone in ["beats", "swatch"]:
  45. self.do_beats(data)
  46. else:
  47. self.do_time(data, timezone)
  48. def do_beats(self, data):
  49. beats = ((time() + 3600) % 86400) / 86.4
  50. beats = int(floor(beats))
  51. self.reply(data, "@{0:0>3}".format(beats))
  52. def do_time(self, data, timezone):
  53. try:
  54. tzinfo = pytz.timezone(timezone)
  55. except ImportError:
  56. msg = "This command requires the 'pytz' package: http://pytz.sourceforge.net/"
  57. self.reply(data, msg)
  58. return
  59. except pytz.exceptions.UnknownTimeZoneError:
  60. self.reply(data, "Unknown timezone: {0}.".format(timezone))
  61. return
  62. now = pytz.utc.localize(datetime.utcnow()).astimezone(tzinfo)
  63. self.reply(data, now.strftime("%Y-%m-%d %H:%M:%S %Z"))