Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Isolated OWM provider info to fix #22 #23

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 0 additions & 64 deletions snipsowm/owm.py

This file was deleted.

153 changes: 153 additions & 0 deletions snipsowm/provider/owm_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,156 @@
import json
import requests
from weather_provider import WeatherProvider, WeatherProviderError, WeatherProviderConnectivityError, WeatherProviderInvalidAPIKey
from enum import Enum
from snipsowm.utils import _convert_to_unix_case
from snipsowm.weather import WeatherConditions
from snipsowm.weather_condition import WeatherConditionDescriptor

class OWMWeatherConditions(Enum):
THUNDERSTORM_WITH_LIGHT_RAIN = 200
THUNDERSTORM_WITH_RAIN = 201
THUNDERSTORM_WITH_HEAVY_RAIN = 202
LIGHT_THUNDERSTORM = 210
THUNDERSTORM = 211
HEAVY_THUNDERSTORM = 212
RAGGED_THUNDERSTORM = 221
THUNDERSTORM_WITH_LIGHT_DRIZZLE = 230
THUNDERSTORM_WITH_DRIZZLE = 231
THUNDERSTORM_WITH_HEAVY_DRIZZLE = 232
LIGHT_INTENSITY_DRIZZLE = 300
DRIZZLE = 301
HEAVY_INTENSITY_DRIZZLE = 302
LIGHT_INTENSITY_DRIZZLE_RAIN = 310
DRIZZLE_RAIN = 311
HEAVY_INTENSITY_DRIZZLE_RAIN = 312
SHOWER_RAIN_AND_DRIZZLE = 313
HEAVY_SHOWER_RAIN_AND_DRIZZLE = 314
SHOWER_DRIZZLE = 321
LIGHT_RAIN = 500
MODERATE_RAIN = 501
HEAVY_INTENSITY_RAIN = 502
VERY_HEAVY_RAIN = 503
EXTREME_RAIN = 504
FREEZING_RAIN = 511
LIGHT_INTENSITY_SHOWER_RAIN = 520
SHOWER_RAIN = 521
HEAVY_INTENSITY_SHOWER_RAIN = 522
RAGGED_SHOWER_RAIN = 531
LIGHT_SNOW = 600
SNOW = 601
HEAVY_SNOW = 602
SLEET = 611
SHOWER_SLEET = 612
LIGHT_RAIN_AND_SNOW = 615
RAIN_AND_SNOW = 616
LIGHT_SHOWER_SNOW = 620
SHOWER_SNOW = 621
HEAVY_SHOWER_SNOW = 622
MIST = 701
SMOKE = 711
HAZE = 721
SAND_DUST_WHIRLS = 731
FOG = 741
SAND = 751
DUST = 761
VOLCANIC_ASH = 762
SQUALLS = 771
TORNAD = 781
CLEAR_SKY = 800
FEW_CLOUDS = 801
SCATTERED_CLOUDS = 802
BROKEN_CLOUDS = 803
OVERCAST_CLOUDS = 804
TORNADO = 900
TROPICAL_STORM = 901
HURRICANE = 902
COLD = 903
HOT = 904
WINDY = 905
HAIL = 906

class OWMToWeatherConditionMapper(object):
mappings = {
OWMWeatherConditions.THUNDERSTORM_WITH_LIGHT_RAIN: WeatherConditions.THUNDERSTORM,
OWMWeatherConditions.THUNDERSTORM_WITH_RAIN: WeatherConditions.THUNDERSTORM,
OWMWeatherConditions.THUNDERSTORM_WITH_HEAVY_RAIN: WeatherConditions.THUNDERSTORM,
OWMWeatherConditions.LIGHT_THUNDERSTORM: WeatherConditions.THUNDERSTORM,
OWMWeatherConditions.THUNDERSTORM: WeatherConditions.THUNDERSTORM,
OWMWeatherConditions.HEAVY_THUNDERSTORM: WeatherConditions.THUNDERSTORM,
OWMWeatherConditions.RAGGED_THUNDERSTORM: WeatherConditions.THUNDERSTORM,
OWMWeatherConditions.THUNDERSTORM_WITH_LIGHT_DRIZZLE: WeatherConditions.THUNDERSTORM,
OWMWeatherConditions.THUNDERSTORM_WITH_DRIZZLE: WeatherConditions.THUNDERSTORM,
OWMWeatherConditions.THUNDERSTORM_WITH_HEAVY_DRIZZLE: WeatherConditions.THUNDERSTORM,
OWMWeatherConditions.LIGHT_INTENSITY_DRIZZLE: WeatherConditions.DRIZZLE,
OWMWeatherConditions.DRIZZLE: WeatherConditions.DRIZZLE,
OWMWeatherConditions.HEAVY_INTENSITY_DRIZZLE: WeatherConditions.DRIZZLE,
OWMWeatherConditions.LIGHT_INTENSITY_DRIZZLE_RAIN: WeatherConditions.DRIZZLE,
OWMWeatherConditions.DRIZZLE_RAIN: WeatherConditions.DRIZZLE,
OWMWeatherConditions.HEAVY_INTENSITY_DRIZZLE_RAIN: WeatherConditions.DRIZZLE,
OWMWeatherConditions.SHOWER_RAIN_AND_DRIZZLE: WeatherConditions.DRIZZLE,
OWMWeatherConditions.HEAVY_SHOWER_RAIN_AND_DRIZZLE: WeatherConditions.DRIZZLE,
OWMWeatherConditions.SHOWER_DRIZZLE: WeatherConditions.DRIZZLE,
OWMWeatherConditions.LIGHT_RAIN: WeatherConditions.RAIN,
OWMWeatherConditions.MODERATE_RAIN: WeatherConditions.RAIN,
OWMWeatherConditions.HEAVY_INTENSITY_RAIN: WeatherConditions.RAIN,
OWMWeatherConditions.VERY_HEAVY_RAIN: WeatherConditions.RAIN,
OWMWeatherConditions.EXTREME_RAIN: WeatherConditions.RAIN,
OWMWeatherConditions.FREEZING_RAIN: WeatherConditions.RAIN,
OWMWeatherConditions.LIGHT_INTENSITY_SHOWER_RAIN: WeatherConditions.RAIN,
OWMWeatherConditions.SHOWER_RAIN: WeatherConditions.RAIN,
OWMWeatherConditions.HEAVY_INTENSITY_SHOWER_RAIN: WeatherConditions.RAIN,
OWMWeatherConditions.RAGGED_SHOWER_RAIN: WeatherConditions.RAIN,
OWMWeatherConditions.LIGHT_SNOW: WeatherConditions.SNOW,
OWMWeatherConditions.SNOW: WeatherConditions.SNOW,
OWMWeatherConditions.HEAVY_SNOW: WeatherConditions.SNOW,
OWMWeatherConditions.SLEET: WeatherConditions.SNOW,
OWMWeatherConditions.SHOWER_SLEET: WeatherConditions.RAIN,
OWMWeatherConditions.LIGHT_RAIN_AND_SNOW: WeatherConditions.RAIN,
OWMWeatherConditions.RAIN_AND_SNOW: WeatherConditions.RAIN,
OWMWeatherConditions.LIGHT_SHOWER_SNOW: WeatherConditions.SNOW,
OWMWeatherConditions.SHOWER_SNOW: WeatherConditions.SNOW,
OWMWeatherConditions.HEAVY_SHOWER_SNOW: WeatherConditions.SNOW,
OWMWeatherConditions.MIST: WeatherConditions.FOG,
OWMWeatherConditions.SMOKE: WeatherConditions.FOG,
OWMWeatherConditions.HAZE: WeatherConditions.FOG,
OWMWeatherConditions.SAND_DUST_WHIRLS: WeatherConditions.UNKNOWN,
OWMWeatherConditions.FOG: WeatherConditions.FOG,
OWMWeatherConditions.SAND: WeatherConditions.UNKNOWN,
OWMWeatherConditions.DUST: WeatherConditions.UNKNOWN,
OWMWeatherConditions.VOLCANIC_ASH: WeatherConditions.UNKNOWN,
OWMWeatherConditions.SQUALLS: WeatherConditions.UNKNOWN,
OWMWeatherConditions.TORNAD: WeatherConditions.UNKNOWN,
OWMWeatherConditions.FEW_CLOUDS: WeatherConditions.CLOUDS,
OWMWeatherConditions.SCATTERED_CLOUDS: WeatherConditions.CLOUDS,
OWMWeatherConditions.BROKEN_CLOUDS: WeatherConditions.CLOUDS,
OWMWeatherConditions.OVERCAST_CLOUDS: WeatherConditions.CLOUDS,
OWMWeatherConditions.CLEAR_SKY: WeatherConditions.SUN,
OWMWeatherConditions.TORNADO: WeatherConditions.STORM,
OWMWeatherConditions.TROPICAL_STORM: WeatherConditions.STORM,
OWMWeatherConditions.HURRICANE: WeatherConditions.STORM,
OWMWeatherConditions.COLD: WeatherConditions.UNKNOWN,
OWMWeatherConditions.HOT: WeatherConditions.UNKNOWN,
OWMWeatherConditions.WINDY: WeatherConditions.UNKNOWN,
OWMWeatherConditions.HAIL: WeatherConditions.UNKNOWN
}

def __init__(self, key):
self.value = None
if type(key) is OWMWeatherConditions:
self.value = key
elif type(key) is int:
try:
self.value = OWMWeatherConditions(key)
except:
pass
elif type(key) is str:
key = _convert_to_unix_case(key)
if key in OWMWeatherConditions.__members__:
self.value = OWMWeatherConditions[key]

def resolve(self):
if self.value is None: return WeatherConditionDescriptor(WeatherConditions.UNKNOWN)
return WeatherConditionDescriptor(self.mappings[self.value])


class OWMWeatherProvider(WeatherProvider):
Expand Down Expand Up @@ -82,6 +232,9 @@ def get_weather(self, location, datetime=None):
else:
return self.get_current_weather(location)

def map_WeatherCondition(self, object):
return OWMToWeatherConditionMapper(object).resolve()

def check_owm_forecast_limit(self, dt):
"""

Expand Down
3 changes: 3 additions & 0 deletions snipsowm/provider/weather_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ def get_forecast_weather(self):
def get_weather(self):
pass

@abc.abstractmethod
def map_WeatherCondition(self, object):
pass

class WeatherProviderError(Exception):
pass
Expand Down
4 changes: 2 additions & 2 deletions snipsowm/snipsowm.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def speak_condition(self, assumed_condition, date, POI=None, Locality=None, Regi
actual_condition, temperature = self.provider.get_weather(locality, date)

# We retrieve the weather from our weather provider
actual_condition_group = weather_condition.OWMToWeatherConditionMapper(actual_condition).resolve()
actual_condition_group = self.provider.map_WeatherCondition(actual_condition)

if assumed_condition:
# We find the category (group) of the received weather description
Expand Down Expand Up @@ -204,7 +204,7 @@ def speak_item(self, item_name, date, POI=None, Locality=None, Region=None, Coun
actual_condition, temperature = self.provider.get_weather(locality, date)

# We retrieve the weather from our weather provider
actual_condition_group = weather_condition.OWMToWeatherConditionMapper(actual_condition).resolve()
actual_condition_group = self.provider.map_WeatherCondition(actual_condition)

if item_name:
# We find the category (group) of the received weather description
Expand Down
88 changes: 1 addition & 87 deletions snipsowm/weather_condition.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# -*- coding: utf-8 -*-
from wagnerfischerpp import WagnerFischer
from owm import OWMWeatherConditions
from snips import SnipsWeatherConditions, mappings
import random
from utils import _convert_to_unix_case
Expand Down Expand Up @@ -163,91 +162,6 @@ def resolve(self):
if self.value is None: return WeatherConditionDescriptor(WeatherConditions.UNKNOWN)
return WeatherConditionDescriptor(self.mappings[self.value])


class OWMToWeatherConditionMapper(object):
mappings = {
OWMWeatherConditions.THUNDERSTORM_WITH_LIGHT_RAIN: WeatherConditions.THUNDERSTORM,
OWMWeatherConditions.THUNDERSTORM_WITH_RAIN: WeatherConditions.THUNDERSTORM,
OWMWeatherConditions.THUNDERSTORM_WITH_HEAVY_RAIN: WeatherConditions.THUNDERSTORM,
OWMWeatherConditions.LIGHT_THUNDERSTORM: WeatherConditions.THUNDERSTORM,
OWMWeatherConditions.THUNDERSTORM: WeatherConditions.THUNDERSTORM,
OWMWeatherConditions.HEAVY_THUNDERSTORM: WeatherConditions.THUNDERSTORM,
OWMWeatherConditions.RAGGED_THUNDERSTORM: WeatherConditions.THUNDERSTORM,
OWMWeatherConditions.THUNDERSTORM_WITH_LIGHT_DRIZZLE: WeatherConditions.THUNDERSTORM,
OWMWeatherConditions.THUNDERSTORM_WITH_DRIZZLE: WeatherConditions.THUNDERSTORM,
OWMWeatherConditions.THUNDERSTORM_WITH_HEAVY_DRIZZLE: WeatherConditions.THUNDERSTORM,
OWMWeatherConditions.LIGHT_INTENSITY_DRIZZLE: WeatherConditions.DRIZZLE,
OWMWeatherConditions.DRIZZLE: WeatherConditions.DRIZZLE,
OWMWeatherConditions.HEAVY_INTENSITY_DRIZZLE: WeatherConditions.DRIZZLE,
OWMWeatherConditions.LIGHT_INTENSITY_DRIZZLE_RAIN: WeatherConditions.DRIZZLE,
OWMWeatherConditions.DRIZZLE_RAIN: WeatherConditions.DRIZZLE,
OWMWeatherConditions.HEAVY_INTENSITY_DRIZZLE_RAIN: WeatherConditions.DRIZZLE,
OWMWeatherConditions.SHOWER_RAIN_AND_DRIZZLE: WeatherConditions.DRIZZLE,
OWMWeatherConditions.HEAVY_SHOWER_RAIN_AND_DRIZZLE: WeatherConditions.DRIZZLE,
OWMWeatherConditions.SHOWER_DRIZZLE: WeatherConditions.DRIZZLE,
OWMWeatherConditions.LIGHT_RAIN: WeatherConditions.RAIN,
OWMWeatherConditions.MODERATE_RAIN: WeatherConditions.RAIN,
OWMWeatherConditions.HEAVY_INTENSITY_RAIN: WeatherConditions.RAIN,
OWMWeatherConditions.VERY_HEAVY_RAIN: WeatherConditions.RAIN,
OWMWeatherConditions.EXTREME_RAIN: WeatherConditions.RAIN,
OWMWeatherConditions.FREEZING_RAIN: WeatherConditions.RAIN,
OWMWeatherConditions.LIGHT_INTENSITY_SHOWER_RAIN: WeatherConditions.RAIN,
OWMWeatherConditions.SHOWER_RAIN: WeatherConditions.RAIN,
OWMWeatherConditions.HEAVY_INTENSITY_SHOWER_RAIN: WeatherConditions.RAIN,
OWMWeatherConditions.RAGGED_SHOWER_RAIN: WeatherConditions.RAIN,
OWMWeatherConditions.LIGHT_SNOW: WeatherConditions.SNOW,
OWMWeatherConditions.SNOW: WeatherConditions.SNOW,
OWMWeatherConditions.HEAVY_SNOW: WeatherConditions.SNOW,
OWMWeatherConditions.SLEET: WeatherConditions.SNOW,
OWMWeatherConditions.SHOWER_SLEET: WeatherConditions.RAIN,
OWMWeatherConditions.LIGHT_RAIN_AND_SNOW: WeatherConditions.RAIN,
OWMWeatherConditions.RAIN_AND_SNOW: WeatherConditions.RAIN,
OWMWeatherConditions.LIGHT_SHOWER_SNOW: WeatherConditions.SNOW,
OWMWeatherConditions.SHOWER_SNOW: WeatherConditions.SNOW,
OWMWeatherConditions.HEAVY_SHOWER_SNOW: WeatherConditions.SNOW,
OWMWeatherConditions.MIST: WeatherConditions.FOG,
OWMWeatherConditions.SMOKE: WeatherConditions.FOG,
OWMWeatherConditions.HAZE: WeatherConditions.FOG,
OWMWeatherConditions.SAND_DUST_WHIRLS: WeatherConditions.UNKNOWN,
OWMWeatherConditions.FOG: WeatherConditions.FOG,
OWMWeatherConditions.SAND: WeatherConditions.UNKNOWN,
OWMWeatherConditions.DUST: WeatherConditions.UNKNOWN,
OWMWeatherConditions.VOLCANIC_ASH: WeatherConditions.UNKNOWN,
OWMWeatherConditions.SQUALLS: WeatherConditions.UNKNOWN,
OWMWeatherConditions.TORNAD: WeatherConditions.UNKNOWN,
OWMWeatherConditions.FEW_CLOUDS: WeatherConditions.CLOUDS,
OWMWeatherConditions.SCATTERED_CLOUDS: WeatherConditions.CLOUDS,
OWMWeatherConditions.BROKEN_CLOUDS: WeatherConditions.CLOUDS,
OWMWeatherConditions.OVERCAST_CLOUDS: WeatherConditions.CLOUDS,
OWMWeatherConditions.CLEAR_SKY: WeatherConditions.SUN,
OWMWeatherConditions.TORNADO: WeatherConditions.STORM,
OWMWeatherConditions.TROPICAL_STORM: WeatherConditions.STORM,
OWMWeatherConditions.HURRICANE: WeatherConditions.STORM,
OWMWeatherConditions.COLD: WeatherConditions.UNKNOWN,
OWMWeatherConditions.HOT: WeatherConditions.UNKNOWN,
OWMWeatherConditions.WINDY: WeatherConditions.UNKNOWN,
OWMWeatherConditions.HAIL: WeatherConditions.UNKNOWN
}

def __init__(self, key):
self.value = None
if type(key) is OWMWeatherConditions:
self.value = key
elif type(key) is int:
try:
self.value = OWMWeatherConditions(key)
except:
pass
elif type(key) is str:
key = _convert_to_unix_case(key)
if key in OWMWeatherConditions.__members__:
self.value = OWMWeatherConditions[key]

def resolve(self):
if self.value is None: return WeatherConditionDescriptor(WeatherConditions.UNKNOWN)
return WeatherConditionDescriptor(self.mappings[self.value])


class SlotValueResolver(object):
def normalize_input(self, input_string):
return input_string.lower().strip()
Expand All @@ -266,4 +180,4 @@ def get_condition_candidates(self, locale, condition_name):


if __name__ == "__main__":
print SlotValueResolver().fuzzy_match("fr_FR", u'HUMID')
print SlotValueResolver().fuzzy_match("fr_FR", u'HUMID')