def extractcurrent(current_json): """ Extracts data from current weather json :param current_json: JSON of current weather :return: list of current weather info [time, weather_description, temperature, wind_speed, sunrise, sunset, pressure, humidity] """ weatherinfo = json.loads(current_json) city = weatherinfo["name"] + ", " + weatherinfo["sys"]["country"] time = uc.datetimeconverter(weatherinfo["dt"]) # .strftime("%H:%M") wd = (weatherinfo["weather"][0])["description"] temp = uc.temperatureconverter(weatherinfo["main"]["temp"]) # degrees pressure = weatherinfo["main"]["pressure"] # hPa humidity = weatherinfo["main"]["humidity"] # % windspeed = int(weatherinfo["wind"]["speed"]) # m / s winddir = uc.winddirection(weatherinfo["wind"]["deg"]) sunrise = uc.datetimeconverter(weatherinfo["sys"]["sunrise"]) sunset = uc.datetimeconverter(weatherinfo["sys"]["sunset"]) return [ time, city, wd, temp, windspeed, winddir, sunrise, sunset, pressure, humidity ]
def extracthistory(history_json): """ Extracts datetime and temperature from history json :param history_json: history json :return: list of datetimes and temperatures [time, weather_description, temperature, wind_speed, wind_direction, rain_fall] """ historyinfo = json.loads(history_json) ndata = historyinfo["cnt"] histdatetemp = [] for i in range(ndata): # if i == 0: # print (historyinfo["list"])[i] # print "\n" htime = uc.datetimeconverter((historyinfo["list"])[i]["dt"]) hwdes = ((historyinfo["list"])[i]["weather"][0]["description"]).lower() htemp = uc.temperatureconverter((historyinfo["list"])[i]["main"]["temp"]) hwspe = int((historyinfo["list"])[i]["wind"]["speed"]) hwdir = uc.winddirection((historyinfo["list"])[i]["wind"]["deg"]) hrain = "n/a" histdatetemp.append([htime, hwdes, htemp, hwspe, hwdir, hrain]) return histdatetemp
def extracthistory(history_json): """ Extracts datetime and temperature from history json :param history_json: history json :return: list of datetimes and temperatures [time, weather_description, temperature, wind_speed, wind_direction, rain_fall] """ historyinfo = json.loads(history_json) ndata = historyinfo["cnt"] histdatetemp = [] for i in range(ndata): # if i == 0: # print (historyinfo["list"])[i] # print "\n" htime = uc.datetimeconverter((historyinfo["list"])[i]["dt"]) hwdes = ((historyinfo["list"])[i]["weather"][0]["description"]).lower() htemp = uc.temperatureconverter( (historyinfo["list"])[i]["main"]["temp"]) hwspe = int((historyinfo["list"])[i]["wind"]["speed"]) hwdir = uc.winddirection((historyinfo["list"])[i]["wind"]["deg"]) hrain = "n/a" histdatetemp.append([htime, hwdes, htemp, hwspe, hwdir, hrain]) return histdatetemp
def extractforecast(forecast_json): """ Extracts next 24hrs of forecast json :param forecast_json: forecast json :return: list of forecast data [time, weather_description, temperature, wind_speed, wind_direction, rain_fall] """ forecastinfo = json.loads(forecast_json) ndata = forecastinfo["cnt"] firsttime = uc.datetimeconverter((forecastinfo["list"])[0]["dt"]) secsinday = 60 * 60 * 24 foreinfo = [] for i in range(ndata): forewd = ((( forecastinfo["list"])[i]["weather"])[0]["description"]).lower() foretime = uc.datetimeconverter((forecastinfo["list"])[i]["dt"]) foretemp = uc.temperatureconverter( (forecastinfo["list"])[i]["main"]["temp"]) forewind = int((forecastinfo["list"])[i]["wind"]["speed"]) forewdir = uc.winddirection((forecastinfo["list"])[i]["wind"]["deg"]) try: forerain = (forecastinfo["list"])[i]["rain"] if len(forerain) == 0: forerain = 0.0 else: forerain = forerain["3h"] except KeyError as e: forerain = 0.0 diffindays = (foretime - firsttime).total_seconds() / secsinday if diffindays < 1.0: foreinfo.append( [foretime, forewd, foretemp, forewind, forewdir, forerain]) return foreinfo
def extractforecast(forecast_json): """ Extracts next 24hrs of forecast json :param forecast_json: forecast json :return: list of forecast data [time, weather_description, temperature, wind_speed, wind_direction, rain_fall] """ forecastinfo = json.loads(forecast_json) ndata = forecastinfo["cnt"] firsttime = uc.datetimeconverter((forecastinfo["list"])[0]["dt"]) secsinday = 60 * 60 * 24 foreinfo = [] for i in range(ndata): forewd = (((forecastinfo["list"])[i]["weather"])[0]["description"]).lower() foretime = uc.datetimeconverter((forecastinfo["list"])[i]["dt"]) foretemp = uc.temperatureconverter((forecastinfo["list"])[i]["main"]["temp"]) forewind = int((forecastinfo["list"])[i]["wind"]["speed"]) forewdir = uc.winddirection((forecastinfo["list"])[i]["wind"]["deg"]) try: forerain = (forecastinfo["list"])[i]["rain"] if len(forerain) == 0: forerain = 0.0 else: forerain = forerain["3h"] except KeyError as e: forerain = 0.0 diffindays = (foretime - firsttime).total_seconds() / secsinday if diffindays < 1.0: foreinfo.append([foretime, forewd, foretemp, forewind, forewdir, forerain]) return foreinfo
def extractcurrent(current_json): """ Extracts data from current weather json :param current_json: JSON of current weather :return: list of current weather info [time, weather_description, temperature, wind_speed, sunrise, sunset, pressure, humidity] """ weatherinfo = json.loads(current_json) city = weatherinfo["name"] + ", " + weatherinfo["sys"]["country"] time = uc.datetimeconverter(weatherinfo["dt"]) # .strftime("%H:%M") wd = (weatherinfo["weather"][0])["description"] temp = uc.temperatureconverter(weatherinfo["main"]["temp"]) # degrees pressure = weatherinfo["main"]["pressure"] # hPa humidity = weatherinfo["main"]["humidity"] # % windspeed = int(weatherinfo["wind"]["speed"]) # m / s winddir = uc.winddirection(weatherinfo["wind"]["deg"]) sunrise = uc.datetimeconverter(weatherinfo["sys"]["sunrise"]) sunset = uc.datetimeconverter(weatherinfo["sys"]["sunset"]) return [time, city, wd, temp, windspeed, winddir, sunrise, sunset, pressure, humidity]