コード例 #1
0
class OWMApi(Api):
    ''' Wrapper that defaults to the Mycroft cloud proxy so user's don't need
        to get their own OWM API keys '''
    def __init__(self):
        super(OWMApi, self).__init__("owm")
        # This is the language code for the interaction between python and the
        # OpenWeatherMap API. This value does not need to be localized as data is fetched
        # in English from the OWM API and later on translated at the WeatherSkill
        # class:
        self.lang = "en"
        self.observation = ObservationParser()
        self.forecast = ForecastParser()

    def build_query(self, params):
        params.get("query").update({"lang": self.lang})
        return params.get("query")

    def get_data(self, response):
        return response.text

    def weather_at_place(self, name, lat, lon):
        if lat and lon:
            q = {"lat": lat, "lon": lon}
        else:
            q = {"q": name}

        data = self.request({"path": "/weather", "query": q})
        return self.observation.parse_JSON(data)

    def three_hours_forecast(self, name, lat, lon):
        if lat and lon:
            q = {"lat": lat, "lon": lon}
        else:
            q = {"q": name}

        data = self.request({"path": "/forecast", "query": q})
        return self.to_forecast(data, "3h")

    def daily_forecast(self, name, lat, lon, limit=None):
        if lat and lon:
            q = {"lat": lat, "lon": lon}
        else:
            q = {"q": name}

        if limit is not None:
            q["cnt"] = limit
        data = self.request({"path": "/forecast/daily", "query": q})
        return self.to_forecast(data, "daily")

    def to_forecast(self, data, interval):
        forecast = self.forecast.parse_JSON(data)
        if forecast is not None:
            forecast.set_interval(interval)
            return Forecaster(forecast)
        else:
            return None
コード例 #2
0
ファイル: __init__.py プロジェクト: nielstron/skill-weather
class OWMApi(Api):
    ''' Wrapper that defaults to the Mycroft cloud proxy so user's don't need
        to get their own OWM API keys '''
    def __init__(self):
        super(OWMApi, self).__init__("owm")
        self.lang = "en"
        self.observation = ObservationParser()
        self.forecast = ForecastParser()

    def build_query(self, params):
        params.get("query").update({"lang": self.lang})
        return params.get("query")

    def get_data(self, response):
        return response.text

    def weather_at_place(self, name, lat, lon):
        if lat and lon:
            q = {"lat": lat, "lon": lon}
        else:
            q = {"q": name}

        data = self.request({"path": "/weather", "query": q})
        return self.observation.parse_JSON(data)

    def three_hours_forecast(self, name, lat, lon):
        if lat and lon:
            q = {"lat": lat, "lon": lon}
        else:
            q = {"q": name}

        data = self.request({"path": "/forecast", "query": q})
        return self.to_forecast(data, "3h")

    def daily_forecast(self, name, lat, lon, limit=None):
        if lat and lon:
            q = {"lat": lat, "lon": lon}
        else:
            q = {"q": name}

        if limit is not None:
            q["cnt"] = limit
        data = self.request({"path": "/forecast/daily", "query": q})
        return self.to_forecast(data, "daily")

    def to_forecast(self, data, interval):
        forecast = self.forecast.parse_JSON(data)
        if forecast is not None:
            forecast.set_interval(interval)
            return Forecaster(forecast)
        else:
            return None
コード例 #3
0
ファイル: __init__.py プロジェクト: forslund/mycroft-core
class OWMApi(Api):
    def __init__(self):
        super(OWMApi, self).__init__("owm")
        self.lang = "en"
        self.observation = ObservationParser()
        self.forecast = ForecastParser()

    def build_query(self, params):
        params.get("query").update({"lang": self.lang})
        return params.get("query")

    def get_data(self, response):
        return response.text

    def weather_at_place(self, name):
        data = self.request({
            "path": "/weather",
            "query": {"q": name}
        })
        return self.observation.parse_JSON(data)

    def three_hours_forecast(self, name):
        data = self.request({
            "path": "/forecast",
            "query": {"q": name}
        })
        return self.to_forecast(data, "3h")

    def daily_forecast(self, name, limit=None):
        query = {"q": name}
        if limit is not None:
            query["cnt"] = limit
        data = self.request({
            "path": "/forecast/daily",
            "query": query
        })
        return self.to_forecast(data, "daily")

    def to_forecast(self, data, interval):
        forecast = self.forecast.parse_JSON(data)
        if forecast is not None:
            forecast.set_interval(interval)
            return Forecaster(forecast)
        else:
            return None
コード例 #4
0
class OWMApi(Api):
    def __init__(self, lang):
        super(OWMApi, self).__init__("owm")
        if (lang == "en-us"):
            self.lang = "en"
        else:
            self.lang = lang
        self.observation = ObservationParser()
        self.forecast = ForecastParser()

    def build_query(self, params):
        params.get("query").update({"lang": self.lang})
        return params.get("query")

    def get_data(self, response):
        return response.text

    def weather_at_place(self, name):
        data = self.request({"path": "/weather", "query": {"q": name}})
        return self.observation.parse_JSON(data)

    def three_hours_forecast(self, name):
        data = self.request({"path": "/forecast", "query": {"q": name}})
        return self.to_forecast(data, "3h")

    def daily_forecast(self, name, limit=None):
        query = {"q": name}
        if limit is not None:
            query["cnt"] = limit
        data = self.request({"path": "/forecast/daily", "query": query})
        return self.to_forecast(data, "daily")

    def to_forecast(self, data, interval):
        forecast = self.forecast.parse_JSON(data)
        if forecast is not None:
            forecast.set_interval(interval)
            return Forecaster(forecast)
        else:
            return None
コード例 #5
0
class OWMApi(Api):
    ''' Wrapper that defaults to the Mycroft cloud proxy so user's don't need
        to get their own OWM API keys '''

    def __init__(self):
        super(OWMApi, self).__init__("owm")
        self.owmlang = "en"
        self.encoding = "utf8"
        self.observation = ObservationParser()
        self.forecast = ForecastParser()

    def build_query(self, params):
        params.get("query").update({"lang": self.owmlang})
        return params.get("query")

    def get_data(self, response):
        return response.text

    def weather_at_place(self, name, lat, lon):
        if lat and lon:
            q = {"lat": lat, "lon": lon}
        else:
            q = {"q": name}

        data = self.request({
            "path": "/weather",
            "query": q
        })
        return self.observation.parse_JSON(data)

    def three_hours_forecast(self, name, lat, lon):
        if lat and lon:
            q = {"lat": lat, "lon": lon}
        else:
            q = {"q": name}

        data = self.request({
            "path": "/forecast",
            "query": q
        })
        return self.to_forecast(data, "3h")

    def daily_forecast(self, name, lat, lon, limit=None):
        if lat and lon:
            q = {"lat": lat, "lon": lon}
        else:
            q = {"q": name}

        if limit is not None:
            q["cnt"] = limit
        data = self.request({
            "path": "/forecast/daily",
            "query": q
        })
        return self.to_forecast(data, "daily")

    def to_forecast(self, data, interval):
        forecast = self.forecast.parse_JSON(data)
        if forecast is not None:
            forecast.set_interval(interval)
            return Forecaster(forecast)
        else:
            return None

    def set_OWM_language(self, lang):
        self.owmlang = lang

        # Certain OWM condition information is encoded using non-utf8
        # encodings. If another language needs similar solution add them to the
        # encodings dictionary
        encodings = {
            'se': 'latin1'
        }
        self.encoding = encodings.get(lang, 'utf8')