def insert_db_weather(weather_values): """Function for inserting scraped data from Weather API into database""" session = Session() new_data = Weather( coord_lon=weather_values["coord"]["lon"], coord_lat=weather_values["coord"]["lat"], weather_id=weather_values["weather"][0]["id"], weather_main=weather_values["weather"][0]["main"], weather_description=weather_values["weather"][0]["description"], weather_icon=weather_values["weather"][0]["icon"], base=weather_values["base"], main_temp=weather_values["main"]["temp"], main_pressure=weather_values["main"]["pressure"], main_humidity=weather_values["main"]["humidity"], main_temp_min=weather_values["main"]["temp_min"], main_temp_max=weather_values["main"]["temp_max"], visibility=weather_values["visibility"], wind_speed=weather_values["wind"]["speed"], wind_deg=weather_values["wind"]["deg"], clouds_all=weather_values["clouds"]["all"], dt=datetime.fromtimestamp(weather_values["dt"]), sys_type=weather_values["sys"]["type"], sys_id=weather_values["sys"]["id"], sys_message=weather_values["sys"]["message"], sys_country=weather_values["sys"]["country"], sys_sunrise=datetime.fromtimestamp(weather_values["sys"]["sunrise"]), sys_sunset=datetime.fromtimestamp(weather_values["sys"]["sunset"]), city_id=weather_values["id"], city_name=weather_values["name"], cod=weather_values["cod"]) session.add(new_data) session.commit() session.close()
def create_weather_type(weather_type, weather_time): """Creating and returning all weather conditions user selects""" weather = Weather(weather_type=weather_type, weather_time=weather_time) db.session.add(weather) db.session.commit() return weather
def write_weatherdb(): """Write weather database from json from Darsky API >>> forecast = (requests.get("https://api.darksky.net/forecast/" + KEY + "/37.8267,-122.4233,1521010800?exclude=hourly,currently")) >>> forecast = forecast.json() >>> day = forecast['daily'] >>> day_data = day['data'][0] >>> summary = day_data['summary'] >>> print summary Partly cloudy throughout the day. """ # use city and month tables to set up get request below cities_from_table = db.session.query(City).all() times_from_table = db.session.query(Month).all() for city in cities_from_table: try: if ((city.city_id < 265) and (city.city_id > 244)): for time in times_from_table: forecast = ( requests.get("https://api.darksky.net/forecast/" + KEY + "/" + city.city_lat + ", " + city.city_long + ", " + time.date + "?exclude=hourly,currently")) # save the usable dictionary object to variable forecast = forecast.json() #filter out cities without weather information if 'daily' in forecast: # the full json inlcuding lat,long,city full_day = forecast['daily'] # weather info day_data = full_day['data'][0] # the computer/predictible icon description icon = day_data['icon'] # human readable summary, 'Partly sunny all day' summary = day_data['summary'] temp_high = day_data['temperatureHigh'] temp_low = day_data['temperatureLow'] weather = Weather(city_id=city.city_id, month=time.month, temp_high=temp_high, temp_low=temp_low, summary=summary, icon=icon) db.session.add(weather) else: break # leave month loop if there's no data for January except: print city db.session.commit()
def print_everything_from_db(): print('---------------Weather---------------') for weather in Weather.select(): print(weather.date, weather.avgtempC, weather.windspeedKmph, weather.weatherDesc, weather.precipMM, weather.humidity, weather.pressure) print('---------------Image---------------') for image in Image.select(): print(image.date, image.filename) print('-----------------------------------')
def get_weather(weather_list, weather_high_avg, weather_low_avg): """Add weather summary in db.""" new_weather = Weather(weather_summary_id=weather_list[2][1], temperature_high=(weather_high_avg / 3), temperature_low=(weather_low_avg / 3)) db.session.add(new_weather) db.session.commit() return new_weather
def weather_to_db(data): session = Session() weather = json.loads(data) print(weather) print(type(weather), len(weather)) timestamp_dt = weather.get('dt') print(timestamp_dt) time_standard_dt = timestamp_convert(timestamp_dt) timestamp_sunrise = weather.get('sys').get('sunrise') time_standard_surise = timestamp_convert(timestamp_sunrise) timestamp_sunset = weather.get('sys').get('sunset') time_standard_sunset = timestamp_convert(timestamp_sunset) kwargs ={ 'coord_lon':weather.get('coord').get('lon'), 'coord_lat':weather.get('coord').get('lat'), 'weather_id':weather.get('weather')[0]['id'], 'weather_main':weather.get('weather')[0]['main'], 'weather_description': weather.get('weather')[0]['description'], 'weather_icon':weather.get('weather')[0]['icon'], 'base':weather.get('base'), 'main_temp': weather.get('main').get('temp'), 'main_feels_like':weather.get('main').get('feels_like'), 'main_temp_max':weather.get('main').get('temp_max'), 'main_temp_min': weather.get('main').get('temp_min'), 'main_pressure':weather.get('main').get('pressure'), 'main_humidity': weather.get('main').get('humidity'), 'visibility': weather.get('visibility'), 'wind_speed': weather.get('wind').get('speed'), 'wind_deg': weather.get('wind').get('speed'), 'clouds_all': weather.get('clouds').get('all'), 'dt':time_standard_dt, 'sys_type':weather.get('sys').get('type'), 'sys_id': weather.get('sys').get('id'), 'sys_country': weather.get('sys').get('country'), 'sys_sunrise':time_standard_surise, 'sys_sunset':time_standard_sunset, 'timezone':weather.get('timezone'), 'city_id':weather.get('id'), 'name': weather.get('name'), 'cod':weather.get('cod'), } row_weather = Weather(**kwargs) session.add(row_weather) session.commit() session.close() return
def create_fake_weather(): for i in list(range(75)): time = fake.date_time_this_month(before_now=True, after_now=True, tzinfo=None) location = 'Pacifica' sky_condition = fake.random_element(elements=('rain', 'clouds', 'clear')) temp = fake.random_int(min=45, max=70, step=1) user_id = 1 weather = Weather(time=time, location=location, sky_condition=sky_condition, temp=temp, user_id=user_id) db.session.add(weather) db.session.commit()
def parse_weather(weather_info): """Takes in jsonified weather info and parses it for entry into the database. Returns the data to be inserted.""" timestamp = weather_info['dt'] time = datetime.datetime.fromtimestamp(timestamp) location = weather_info['name'] sky_condition = weather_info['weather'][0]['description'] temp_kelvin = weather_info['main']['temp'] temp_farenheit = (temp_kelvin - 273.15) * 9 / 5 + 32 temp_int = int(temp_farenheit) ins = Weather(time=time, location=location, sky_condition=sky_condition, temp=temp_int, user_id=PLACEHOLDER) return ins
def parse(self, response): """ Parse through Whistlers weather page """ # Extract the current weather report daily_rept = Selector( response=response).xpath('//script/text()').extract()[8] snowfall_rept = json.loads(daily_rept.split("=")[2].split(";")[0]) # Extract the forcast weather report forcast_rept = Selector( response=response).xpath('//script/text()').extract()[11] forcast = json.loads(forcast_rept.split("=")[2].split(";")[0]) # Calculate and convert snow to inches daily_snow_inches = snowfall_rept['TwentyFourHourSnowfall']['Inches'] daily_snow_centimeters = snowfall_rept['TwentyFourHourSnowfall'][ 'Centimeters'] daily_snowfall = (int(daily_snow_centimeters) * 0.39) + int(daily_snow_inches) # extract overnight snowfall information overnight_snowfall_inches = snowfall_rept['OvernightSnowfall'][ 'Inches'] overnight_snowfall_centimeters = snowfall_rept['OvernightSnowfall'][ 'Centimeters'] overnight_snowfall = (int(overnight_snowfall_centimeters) * 0.39) + int(overnight_snowfall_inches) # extract forcast information long_forcast = forcast[0]['ForecastData'][0]['WeatherLongDescription'] wind_forcast = forcast[0]['Wind'] forcast_icon = forcast[0]['WeatherIconStatus'] # Instansiate a weather object weather = Weather(wind_forcast=wind_forcast, daily_snowfall=daily_snowfall, overnight_snowfall=overnight_snowfall, snow_forcast=long_forcast, forcast_icon=forcast_icon) db.session.add(weather) db.session.commit()
def create_fake_weather(): """Creates fake weather entries for use in the graph""" for i in list(range(200)): time = fake.date_time_this_month(before_now=True, after_now=True, tzinfo=None) location = 'Pacifica' sky_condition = fake.random_element(elements=('broken clouds', 'scattered clouds', 'clear sky', 'moderate rain')) temp = fake.random_int(min=45, max=70, step=1) user_id = 1 weather = Weather(time=time, location=location, sky_condition=sky_condition, temp=temp, user_id=user_id) db.session.add(weather) db.session.commit()
def parse_weather(weather_info): """Take in jsonified weather info and parse it for entry into the database. Return the data to be inserted.""" # Used local time due to API response being cached when a the same # payload is send without restarting the browser session time_stamp = datetime.datetime.now() # Parse the API response. Temp and location to be used in future iteration # of app. location = weather_info['name'] sky_condition = weather_info['weather'][0]['description'] temp_kelvin = weather_info['main']['temp'] temp_farenheit = (temp_kelvin - 273.15) * 9/5 + 32 temp_int = int(temp_farenheit) # Create the insert for the database ins = Weather(time=time_stamp, location=location, sky_condition=sky_condition, temp=temp_int, user_id=PLACEHOLDER) return ins
def __init__(self): self.dates_in_db = set() database_proxy.initialize(database) Weather.create_table() Image.create_table()
def get_weather_from_duration(first_date_from_duration, second_date_from_duration): return Weather.select().where( (Weather.date >= first_date_from_duration) & (Weather.date <= second_date_from_duration))
def add_weather(): to_add = Weather() # TODO db.session.add(to_add) db.session.commit() pass
def get_weather(station_id): """run a query to get today but wet.""" wetDay = Weather.findWetWeatherDays(db_session, datetime.today().weekday()) # pass that day into the get bikes db and return the average bike usage for that day. return jsonify(UsageData.get_bikes_for_wetday(db_session, wetDay, station_id))
def get_weather_or_none_from_duration(first_date_from_duration, second_date_from_duration): return Weather.get_or_none((Weather.date >= first_date_from_duration) & (Weather.date <= second_date_from_duration))
def get_dates_from_db(self): for the_weather in Weather.select(): self.dates_in_db.add(the_weather.date) return self.dates_in_db
def save_dict_to_db(downloaded_result): Weather.insert_many(downloaded_result).execute()
from datetime import datetime session = TembooSession("marsland", "SmartClock", "bkOW4wZpfNVuXC2A6f52z7Ja4oAukXZL") getWeatherByAddressChoreo = GetWeatherByAddress(session) getWeatherByAddressInputs = getWeatherByAddressChoreo.new_input_set() getWeatherByAddressInputs.set_ResponseFormat("json") getWeatherByAddressInputs.set_Address("1 beenleigh ave 5087 sa") getWeatherByAddressInputs.set_Day("4") getWeatherByAddressInputs.set_Units("c") getWeatherByAddressResults = getWeatherByAddressChoreo.execute_with_results(getWeatherByAddressInputs) # print("Response: " + getWeatherByAddressResults.get_Response()) res = json.loads(getWeatherByAddressResults.get_Response()) forecasts = res["channel"]["item"]["yweather:forecast"] current= w.create(code = getWeatherByAddressResults.get_ForecastCode(), temperature = getWeatherByAddressResults.get_Temperature(),text= getWeatherByAddressResults.get_ForecastText(), humidity = getWeatherByAddressResults.get_Humidity()) weathers = {} counter = 0 for forecast in forecasts: weathers[counter] = f.create(code= forecast["@code"], high= forecast["@high"], low = forecast["@low"], text = forecast["@text"] ) wi.create_or_get(code = forecast["@code"], body = forecast["@text"]) counter += 1 counter = 0 bc = bridgeclient() bc.begin() print "bc.begin" print "code = " + str(current.code) + "text = " + current.text