import datetime
import requests
from will.plugin import WillPlugin
from will.decorators import respond_to, periodic, hear, randomly, route, rendered_template, require_settings
from will import settings


class TimePlugin(WillPlugin):

    @respond_to("what time is it in (?P<place>.*)")
    def what_time_is_it_in(self, message, place):
        """what time is it in ___: Say the time in almost any city on earth."""
        if (
                not hasattr(settings, "WORLD_WEATHER_ONLINE_KEY") and
                not hasattr(settings, "WORLD_WEATHER_ONLINE_V2_KEY")
        ):
            self.say(
                "I need a world weather online key to do that.\n"
                "You can get one at http://developer.worldweatheronline.com, "
                "and then set the key as WORLD_WEATHER_ONLINE_V2_KEY",
                message=message
            )
        else:
            if hasattr(settings, "WORLD_WEATHER_ONLINE_V2_KEY"):
                r = requests.get(
                    "http://api2.worldweatheronline.com/free/v2/tz.ashx?q=%s&format=json&key=%s" %
                    (place, settings.WORLD_WEATHER_ONLINE_V2_KEY)
                )
            elif hasattr(settings, "WORLD_WEATHER_ONLINE_KEY"):
                r = requests.get(
                    "http://api2.worldweatheronline.com/free/v1/tz.ashx?q=%s&format=json&key=%s" %
                    (place, settings.WORLD_WEATHER_ONLINE_KEY)
                )
            resp = r.json()
            if "request" in resp["data"] and len(resp["data"]["request"]) > 0:
                place = resp["data"]["request"][0]["query"]
                current_time = self.parse_natural_time(resp["data"]["time_zone"][0]["localtime"])

                self.say("It's %s in %s." % (self.to_natural_day_and_time(current_time), place), message=message)
            else:
                self.say("I couldn't find anywhere named %s." % (place, ), message=message)

    @respond_to("what time is it(\?)?$", multiline=False)
    def what_time_is_it(self, message):
        """what time is it: Say the time where I am."""
        now = datetime.datetime.now()
        self.say("It's %s." % self.to_natural_day_and_time(now, with_timezone=True), message=message)

import datetime
import pytz
import requests
import time

from will.plugin import WillPlugin
from will.decorators import respond_to, periodic, hear, randomly, route, rendered_template, require_settings
from will import settings


def get_location(place):
    payload = {'address': place, 'sensor': False}
    r = requests.get('http://maps.googleapis.com/maps/api/geocode/json', params=payload)
    resp = r.json()
    location = resp["results"][0]["geometry"]["location"]
    return location


def get_timezone(lat, lng):
    payload = {'location': "%s,%s" % (lat, lng), 'timestamp': int(time.time()), 'sensor': False}
    r = requests.get('https://maps.googleapis.com/maps/api/timezone/json', params=payload)
    resp = r.json()
    tz = resp['timeZoneId']
    return tz


class TimePlugin(WillPlugin):

    @respond_to("what time is it in (?P<place>.*)")
    def what_time_is_it_in(self, message, place):
        """what time is it in ___: Say the time in almost any city on earth."""
        location = get_location(place)
        tz = get_timezone(location['lat'], location['lng'])
        ct = datetime.datetime.now(tz=pytz.timezone(tz))
        self.say("It's %s in %s." % (self.to_natural_day_and_time(ct), place), message=message)

    @respond_to("what time is it(\?)?$", multiline=False)
    def what_time_is_it(self, message):
        """what time is it: Say the time where I am."""
        now = datetime.datetime.now()
        self.say("It's %s." % self.to_natural_day_and_time(now, with_timezone=True), message=message)

Fix world time plugin