def forecast(city: str, timestamp: str): time_to_start = time.time() global programMetrics global processing_time_of_the_request_forecast_weather programMetrics[2] += 1 mgr = owm.weather_manager() observation = mgr.forecast_at_place( city, "3h" ) #данной командой стягивается прогноз погоды на ближайшие 5 дней с частотой 3 часа if timestamp == "1h": timest = timestamps.next_hour() elif timestamp == "3h": timest = timestamps.next_three_hours() elif timestamp == "tomorrow": timest = timestamps.tomorrow() elif timestamp == "yesterday": timest = timestamps.yesterday() else: timest = timestamps.now() w = observation.get_weather_at(timest) temp = w.temperature('celsius')['temp'] #вывод в консоль print(" request: " + city + "\ttime: " + str(timest) + "\t" + w.detailed_status + "\t" + str(temp)) processing_time_of_the_request_forecast_weather.append(time.time() - time_to_start) return json.dumps({ "city": city, "unit": "celsius", "temperature": temp, "id_service": my_id })
def forecast_weather(city: str, timestamp: str): #обращение к внешнему сервису mgr = owm.weather_manager() observation = mgr.forecast_at_place(city, "3h") #данной командой стягивается прогноз погоды на ближайшие 5 дней с частотой 3 часа if timestamp == "1h": time = timestamps.next_hour() elif timestamp == "3h": time = timestamps.next_three_hours() else: time = timestamps.now(); w = observation.get_weather_at(time) temp = w.temperature('celsius')['temp'] #занесение в бд данных (не совсем корректно, ведь у нас прогноз погоды...) rc.set(city, temp, ex=time_storage) #возвращение текущей погоды return temp
def forecast(city: str, timestamp: str): mgr = owm.weather_manager() observation = mgr.forecast_at_place( city, "3h" ) #данной командой стягивается прогноз погоды на ближайшие 5 дней с частотой 3 часа if timestamp == "1h": time = timestamps.next_hour() elif timestamp == "3h": time = timestamps.next_three_hours() elif timestamp == "tomorrow": time = timestamps.tomorrow() elif timestamp == "yesterday": time = timestamps.yesterday() else: time = timestamps.now() w = observation.get_weather_at(time) temp = w.temperature('celsius')['temp'] #вывод в консоль print(" request: " + city + "\ttime: " + str(time) + "\t" + w.detailed_status + "\t" + str(temp)) return json.dumps({"city": city, "unit": "celsius", "temperature": temp})
def test_next_hour_after_specified_time(self): d = datetime(2013, 12, 7, 15, 46, 12) expected = d + timedelta(hours=1) result = timestamps.next_hour(d) self.assertAlmostEqual(expected, result)
def test_next_hour(self): result = timestamps.next_hour() expected = datetime.now() + timedelta(hours=1) self.assertAlmostEqual(float(formatting.to_UNIXtime(expected)), float(formatting.to_UNIXtime(result)))