def test_get_latlon_failure(self, mocked_requests_get):
        """
            Test when Geocodio API returns an error
        """
        for test_case in self.test_edge_case_get_latlon:
            mocked_requests_get.return_value = MockedRequestResposneFailure()
            response = get_latlon(test_case[KEY_INPUT])
            expected = test_case[KEY_EXPECTED]

            self.assertDictEqual(response, expected)
    def test_get_latlon_(self, mocked_requests_get):
        """
            Test when API is returns an successful response
        """
        for test_case in self.test_get_latlon_success:
            mocked_requests_get.return_value = MockedRequestResposneSuccess(
                40.728157, -74.077644)
            response = get_latlon(test_case[KEY_INPUT])
            expected = test_case[KEY_EXPECTED]

            self.assertDictEqual(response, expected)
예제 #3
0
def send_update_location(text):
    """send updated location to map module"""
    if not text.isdigit():
        request_name = text.lower()

    zip_codes = {}
    with open("weather_resources/zip_dict.json") as zip_dict:
        zip_codes = json.load(zip_dict)
    zip_dict.close()

    cities = {}
    with open("weather_resources/city_list.txt", "r") as city_file:
        cities = {line.strip() for line in city_file}
    city_file.close()

    if text.isdigit() and text in zip_codes:
        request_name = zip_codes[text]
        coordinates = forward_geocoding.get_latlon(request_name)
        flask_socketio.emit("location_update", coordinates)
    elif text in cities:
        coordinates = forward_geocoding.get_latlon(text)
        flask_socketio.emit("location_update", coordinates)
예제 #4
0
def fetch_weather(city):
    """
    Make request to weather API
    using the passed city param
    """

    try:
        geo_coordinates = get_latlon(city)
        payload = {
            KEY_LATITUDE: geo_coordinates[KEY_LATITUDE],
            KEY_LONGITUDE: geo_coordinates["lng"],
            KEY_EXCLUDE: "minutely,alerts",
            KEY_UNITS: "imperial",
            KEY_COUNT: 3,
            KEY_API_KEY: OPEN_WEATHER_API_KEY,
        }
        resposne = requests.get(OPEN_WEATHER_API_BASE_URL, params=payload)
        response = resposne.json()
        response = {
            KEY_CURRENT: {
                KEY_TIMEZONE:
                response[KEY_TIMEZONE],
                KEY_TIME:
                int(
                    format_time_to_ny(
                        response[KEY_CURRENT][KEY_DT]).strftime("%H")),
                KEY_SUNRISE:
                format_time_to_ny(
                    response[KEY_CURRENT][KEY_SUNRISE]).strftime("%H:%M"),
                KEY_SUNSET:
                format_time_to_ny(
                    response[KEY_CURRENT][KEY_SUNSET]).strftime("%H:%M"),
                KEY_TEMP:
                response[KEY_CURRENT][KEY_TEMP],
                KEY_FEELS_LIKE:
                response[KEY_CURRENT][KEY_FEELS_LIKE],
                KEY_DESCRIPTON:
                response[KEY_CURRENT][KEY_WEATHER][0][KEY_DESCRIPTON],
                KEY_ICON:
                "https://openweathermap.org/img/wn/" +
                response[KEY_CURRENT][KEY_WEATHER][0][KEY_ICON] + "@2x.png",
            },
            KEY_HOURLY: format_hourly_response(response[KEY_HOURLY]),
        }
        return response
    except KeyError:
        return {}