tzone.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #Simple module to spit out the time in a city specified by user, poorly thrown together by mech
  2. import sys
  3. import json
  4. from event import Event
  5. try:
  6. import requests
  7. except (ImportError, SystemError):
  8. print("Warning: tzone module requires requests")
  9. requests = object
  10. try:
  11. if sys.version_info > (3, 0, 0):
  12. from .basemodule import BaseModule
  13. else:
  14. from basemodule import BaseModule
  15. except (ImportError, SystemError):
  16. from modules.basemodule import BaseModule
  17. class Tzone(BaseModule):
  18. def post_init(self):
  19. tzone = Event("__.tzone__")
  20. tzone.define(msg_definition="^\.tzone")
  21. tzone.subscribe(self)
  22. self.cmd = ".tzone"
  23. self.help = ".tzone <Insert location name/zip/airport(SFO,PDX,etc.)>"
  24. self.bot.register_event(tzone, self)
  25. self.url = "https://dev.virtualearth.net/REST/v1/TimeZone/query="
  26. self.key = "?key=AuEaLSdFYvXwY4u1FnyP-f9l5u5Ul9AUA_U1F-eJ-8O_Fo9Cngl95z6UL0Lr5Nmx"
  27. #TODO put in a minor work around for places like Chico california not working with just '.tzone Chico'
  28. #TODO split out verifying the location request is properly formatted into its own function.
  29. def request_api(self, location):
  30. """Takes the location provided and determines whether its a valid request
  31. and will return either the time of the location or a message instructing you
  32. how to the make the proper call"""
  33. url_query = None
  34. try:
  35. headers = {
  36. 'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36',
  37. }
  38. url_query = self.url + location + self.key
  39. r = requests.get(url_query, headers=headers)
  40. j = json.loads(r.text)
  41. local_time_date = j["resourceSets"][0]["resources"][0]["timeZoneAtLocation"][0]["timeZone"][0]["convertedTime"]["localTime"].split("T")
  42. place = j["resourceSets"][0]["resources"][0]["timeZoneAtLocation"][0]["placeName"]
  43. """Checks to see if request is specific enough for one timezone"""
  44. multiple_locations = j["resourceSets"][0]["resources"][0]["timeZoneAtLocation"][0]["timeZone"]
  45. if len(multiple_locations) > 1:
  46. return "Multiple timezones returned, try being more specific"
  47. else:
  48. return str(place + ": " + local_time_date[1])
  49. except IndexError:
  50. return "Not a valid request, try again."
  51. except ValueError:
  52. return "Not a valid request, try again."
  53. except KeyError:
  54. return "Not a valid request, try again."
  55. def handle(self, event):
  56. try:
  57. if event.msg.startswith(".tzone"):
  58. split_tz = event.msg.split()
  59. if len(split_tz) > 2:
  60. tz = "+".join(split_tz[1:])
  61. else:
  62. tz = split_tz[1].lower()
  63. self.say(event.channel, self.request_api(tz))
  64. except TypeError:
  65. pass # Error gets caught here and in ValueError in request_api function