Skip to content

Commit

Permalink
Handle a lot more things with padatious
Browse files Browse the repository at this point in the history
  • Loading branch information
nielstron authored Feb 14, 2018
1 parent 8a11bae commit 935b1a4
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 47 deletions.
100 changes: 53 additions & 47 deletions __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from multi_key_dict import multi_key_dict
from mycroft.dialog import DialogLoader
from mycroft.api import Api
from mycroft.skills.core import MycroftSkill, intent_handler
from mycroft.skills.core import MycroftSkill, intent_handler, intent_file_handler
from mycroft.util.log import LOG
from mycroft.util.parse import extract_datetime
from mycroft.util.format import nice_number
Expand Down Expand Up @@ -54,6 +54,9 @@ def __init__(self):
self.observation = ObservationParser()
self.forecast = ForecastParser()

def initialize(self):
self.register_entity_file('location.entity')

def build_query(self, params):
params.get("query").update({"lang": self.lang})
return params.get("query")
Expand Down Expand Up @@ -140,8 +143,7 @@ def __init__(self):
self.owm = OWMApi()

# Handle: what is the weather like?
@intent_handler(IntentBuilder("CurrentWeatherIntent").require(
"Weather").optionally("Location").build())
@intent_file_handler('weather.intent')
def handle_current_weather(self, message):
try:
# Get a date from requests like "weather for next Tuesday"
Expand Down Expand Up @@ -175,13 +177,12 @@ def handle_current_weather(self, message):

self.__report_weather("current", report)
except HTTPError as e:
self.__api_error(e)
self.speak_dialog('location.not.found', data={"location":report['location']})
except Exception as e:
LOG.error("Error: {0}".format(e))

# Handle: What is the weather forecast?
@intent_handler(IntentBuilder("WeatherForecast").require(
"Forecast").optionally("Location").build())
@intent_file_handler('forecast.intent')
def handle_forecast(self, message):
try:
report = self.__initialize_report(message)
Expand Down Expand Up @@ -215,15 +216,13 @@ def handle_forecast(self, message):

self.__report_weather("forecast", report)
except HTTPError as e:
self.__api_error(e)
self.speak_dialog('location.not.found', data={"location":report['location']})
except Exception as e:
LOG.error("Error: {0}".format(e))

# Handle: When will it rain again? | Will it rain on Tuesday?
@intent_handler(IntentBuilder("NextPrecipitationIntent").require(
"Next").require("Precipitation").optionally("Location").build())
@intent_handler(IntentBuilder("CurrentRainSnowIntent").require(
"Query").require("Precipitation").optionally("Location").build())
@intent_file_handler('next.precipitation.intent')
@intent_file_handler('current.precipitation.intent')
def handle_next_precipitation(self, message):
report = self.__initialize_report(message)

Expand All @@ -243,43 +242,48 @@ def handle_next_precipitation(self, message):
}

# search the forecast for precipitation
for weather in self.owm.daily_forecast(
report['full_location'],
report['lat'],
report['lon']).get_forecast().get_weathers():
try:
for weather in self.owm.daily_forecast(
report['full_location'],
report['lat'],
report['lon']).get_forecast().get_weathers():

forecastDate = datetime.fromtimestamp(weather.get_reference_time())

# TODO: "will it rain tomorrow" returns forecast for today, if it rains today
if when != today:
# User asked about a specific date, is this it?
whenGMT = self.__to_GMT(when)
if forecastDate.date() != whenGMT.date():
continue

rain = weather.get_rain()
if rain and rain["all"] > 0:
data["precip"] = "rain"
data["day"] = self.__to_day(forecastDate)
if rain["all"] < 10:
data["modifier"] = self.__translate("light")
elif rain["all"] > 20:
data["modifier"] = self.__translate("heavy")

break

snow = weather.get_snow()
if snow and snow["all"] > 0:
data["precip"] = "snow"
data["day"] = self.__to_day(forecastDate)
if snow["all"] < 10:
data["modifier"] = self.__translate("light")
elif snow["all"] > 20:
data["modifier"] = self.__translate("heavy")

break
forecastDate = datetime.fromtimestamp(weather.get_reference_time())

self.__report_precipitation(timeframe, data)
# TODO: "will it rain tomorrow" returns forecast for today, if it rains today
if when != today:
# User asked about a specific date, is this it?
whenGMT = self.__to_GMT(when)
if forecastDate.date() != whenGMT.date():
continue

rain = weather.get_rain()
if rain and rain["all"] > 0:
data["precip"] = "rain"
data["day"] = self.__to_day(forecastDate)
if rain["all"] < 10:
data["modifier"] = self.__translate("light")
elif rain["all"] > 20:
data["modifier"] = self.__translate("heavy")

break

snow = weather.get_snow()
if snow and snow["all"] > 0:
data["precip"] = "snow"
data["day"] = self.__to_day(forecastDate)
if snow["all"] < 10:
data["modifier"] = self.__translate("light")
elif snow["all"] > 20:
data["modifier"] = self.__translate("heavy")

break

self.__report_precipitation(timeframe, data)
except HTTPError as e:
self.speak_dialog('location.not.found', data={"location":report['location']})
except Exception as e:
LOG.error("Error: {0}".format(e))

# Handle: What's the weather later?
@intent_handler(IntentBuilder("NextHoursWeatherIntent").require(
Expand All @@ -306,7 +310,7 @@ def handle_next_hour(self, message):

self.__report_weather("hour", report)
except HTTPError as e:
self.__api_error(e)
self.speak_dialog('location.not.found', data={"location":report['location']})
except Exception as e:
LOG.error("Error: {0}".format(e))

Expand Down Expand Up @@ -464,6 +468,8 @@ def __get_location(self, message):
# is found return the default location instead.
try:
location = message.data.get("Location", None)
if location is None:
location = message.data.get("location", None)
if location:
return None, None, location, location

Expand Down
8 changes: 8 additions & 0 deletions vocab/en-us/current.precipitation.intent
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
is it (raining|snowing)
does it (snow|rain)
is there (rain|snow)
is it (raining|snowing) (in|at) {location}
does it (snow|rain) (in|at) {location}
is there (rain|snow) (in|at) {location}
should i wear (a coat|an umbrella)
should i take an umbrella (with me|)
2 changes: 2 additions & 0 deletions vocab/en-us/forecast.intent
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
what is the forecast :0
what is the weather (going to be like|forecast)
1 change: 1 addition & 0 deletions vocab/en-us/location.entity
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
:0
2 changes: 2 additions & 0 deletions vocab/en-us/next.precipitation.intent
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
(when |)will it (rain|snow|be raining|be snowing)( again|)( in {location}|)( on|)
when (will|is) (the next|there be)( a|) (rain|storm|snow|snow storm|thunder|thunder storm)( going to|) be( again|)( in {location}|)
7 changes: 7 additions & 0 deletions vocab/en-us/weather.intent
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
what is the weather (like|) (here|around|in {location}|)
how (cold|hot|warm|chilly) is it (here|around|outside |in {location}|)
how is the weather (in {location}|)
what is the (outside|) temperature (here|around|outside |in {location}|)
how is it outside
what is it like outside
what will the weather be like

0 comments on commit 935b1a4

Please sign in to comment.