Exemple #1
0
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
    })
Exemple #2
0
    def prepare_hourly_12h_forecast(self):
        hours = []
        curr = from_utc()
        curr = curr.replace(minute=0, second=0)
        for _ in range(4):
            hours.append(timestamps.next_three_hours(curr))
            curr = hours[-1]

        return [(h, self.owm_object["forecast"].get_weather_at(h))
                for h in hours]
Exemple #3
0
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
Exemple #4
0
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})
Exemple #5
0
 def test_next_three_hours_after_specified_time(self):
     d = datetime(2013, 12, 7, 15, 46, 12)
     expected = d + timedelta(hours=3)
     result = timestamps.next_three_hours(d)
     self.assertAlmostEqual(expected, result)
Exemple #6
0
 def test_next_three_hours(self):
     result = timestamps.next_three_hours()
     expected = datetime.now() + timedelta(hours=3)
     self.assertAlmostEqual(float(formatting.to_UNIXtime(expected)),
                            float(formatting.to_UNIXtime(result)))
Exemple #7
0
import boto3
import datetime
import random
from pyowm.owm import OWM
from pyowm.utils import timestamps
from data import *
#setting up Open Weather Map dependencies
owm = OWM(OWM_Key)
mgr = owm.weather_manager()
weather = mgr.weather_at_place('Bergen,NO').weather
#Finding temperatures for Bergen
temp_dict_celsius = weather.temperature('celsius') 
#Requesting weather report in 3h intervals for the next 5 days
h3_forecaster = mgr.forecast_at_coords(60.39, 5.32, "3h")
#Getting timestamps for the next 3 hours
next3h = timestamps.next_three_hours()
#Checking if it will rain in the next 3 hours
rain = h3_forecaster.will_be_rainy_at(next3h)
#Composing message
intro_msg = "God Morgen! Din " + random.choice(kos_msg) + "."
max_temp_msg = "Max temp i dag er: " + str(temp_dict_celsius["temp_max"])
min_temp_msg = "Min temp i dag er: " + str(temp_dict_celsius["temp_min"])
if rain == True:
	rain_msg = "Ta med paraply!"
else:
	rain_msg = "La paraplyen stå :)" 
message = intro_msg + "\n" + max_temp_msg + "\n" + min_temp_msg + "\n" + rain_msg
#Sending message to recipients
client = boto3.client("sns")
for recipient in tlf_nr:
	client.publish(PhoneNumber=recipient, Message=message)