async def get_weather(future, location): start = time.time() url = get_base_aws_uri() + weather_url + '/' + location info = None LOGGER.info(url) try: async with ClientSession() as session: html = await fetch(session, url) LOGGER.info( f'weather content fetch in {time.time() - start} secs.') parse_start = time.time() info = parse_weather(html) LOGGER.info( f'weather parsing took {time.time() - parse_start} secs.') except ClientConnectorError as ex: LOGGER.error(f'Unable to parse Weather API : {repr(ex)}') LOGGER.error(ex) info = WeatherInfo.WeatherInfo('0', "0", "0", "00:00", "", "", "") info.set_error(ex) except BaseException as ex: LOGGER.error(f'Unable to parse Weather API :- {repr(ex)}') LOGGER.error(ex) info = WeatherInfo.WeatherInfo('0', "0", "0", "00:00", "", "", "") info.set_error(ex) future.set_result(info)
async def get_weather(future, location): start = time.time() if location is not None: url = weather_url + location else: url = weather_url + DEFAULT_LOC_UUID info = None LOGGER.info(url) try: async with ClientSession(connector=TCPConnector(ssl=False)) as session: html = await fetch(session, url) LOGGER.info( f'weather content fetch in {time.time() - start} secs.') parse_start = time.time() info = await parse_weather(html) LOGGER.info( f'weather parsing took {time.time() - parse_start} secs.') except ClientConnectorError as ex: LOGGER.error(f'Unable to connect Weather API : {repr(ex)}') LOGGER.error(ex) info = WeatherInfo.WeatherInfo('0', "0", "0", "00:00", "", "", "") info.set_error(ex) except BaseException as ex: LOGGER.error(f'Unable to connect Weather API :- {repr(ex)}') LOGGER.error(ex) info = WeatherInfo.WeatherInfo('0', "0", "0", "00:00", "", "", "") info.set_error(ex) LOGGER.info(f'Weather Time Taken {time.time() - start}') future.set_result(info)
def parse_weather(weather_json): print(weather_json) x = json.loads(weather_json, object_hook=lambda d: namedtuple('x', d.keys()) (*d.values())) weather_info = WeatherInfo.WeatherInfo(x.temperature, x.low, x.high, x.asof, x.condition, x.location, x.preciption) weather_info.set_humidity(x.humidity) return weather_info
async def parse_google_weather(page_content): soup = BeautifulSoup(page_content, 'lxml') seg_temp = soup.find_all('div', {'id': 'wob_wc'})[0] # print(seg_temp.text) # seg_temp = soup.find_all('div#wob_wc') # seg_temp = soup.find('div', class_='vk_c card-section') # print (seg_temp) span = seg_temp.select('span')[0] # First Segment element = span.select('div#wob_loc')[0] location = element.text as_of = '' # element = span.select('div#wob_dts')[0] # as_of = element.text element = span.select('span#wob_dc')[0] condition = element.text div_sub = seg_temp.select('div#wob_d')[0] # Second Segment element = div_sub.select('span#wob_tm')[0] temp = element.text div_sub = seg_temp.find_all('div', class_='vk_gy vk_sh')[1] # Second Sub element = div_sub.select('span#wob_hm')[0] humidity = element.text element = div_sub.select('span#wob_pp')[0] precipitation = element.text idx = util.index_of(precipitation, '%') if idx > 0: precipitation = precipitation + ' chance of rain until' # fetch low and high temperature for the day high = low = temp # div_forecast = seg_temp.find('div', class_='wob_df wob_ds') # Third Segment # div_sub = div_forecast.find('div', class_='vk_gy') # span = div_sub.select('span')[0] # high = span.text # # div_sub = div_forecast.find_all('div')[4] # span = div_sub.select('span')[0] # low = span.text weatherInfo = WeatherInfo.WeatherInfo(temp, low, high, as_of, condition, location, precipitation) weatherInfo.set_humidity(humidity) weatherInfo.set_error(None) LOGGER.info(str(weatherInfo)) return weatherInfo
def get_weather_sync(location): url = get_base_aws_uri() + weather_url + '/' + location print(url) weather_response = requests.get(url) print(weather_response) weather_info = WeatherInfo.WeatherInfo(0, 0, 0, "00:00", "", "", "") if weather_response.status_code == 200: weather_json = weather_response.text print(weather_json) weather_info = parse_weather(weather_json) else: print('WeatherInfo API returned an error.') return weather_info
async def parse_weather(page_content): soup = BeautifulSoup(page_content, 'lxml') # html.parser # lxml # html5lib seg_temp = soup.find_all('div', { 'id': 'WxuCurrentConditions-main-b3094163-ef75-4558-8d9a-e35e6b9b1034' })[0] # print(seg_temp.text) seg_temp = seg_temp.find_all('div', recursive=False)[0] seg_temp = seg_temp.find_all('section', recursive=False)[0] seg_temp = seg_temp.find_all('div', recursive=False)[0] div_ele = seg_temp.find_all('div', recursive=False)[0] # print(div_ele) location = div_ele.find('h1') location = location.text idx = util.index_of(location, ' Weather') if idx > 0: location = location[0:idx] as_of = div_ele.find('div') as_of = as_of.text idx = util.index_of(as_of, "as of ") idx_ist = util.index_of(as_of, " IST") if idx > 0: as_of = as_of[idx + 6:idx_ist] div_ele = seg_temp.find_all('div', recursive=False)[1] # print(div_ele) div_cond = div_ele.find_all('div', recursive=False)[0] # print(div_cond) element = div_cond.find('span') temp = element.text if len(temp) == 3: temp = temp[0:2] element = div_cond.find('div') condition = element.text div_cond = div_ele.find_all('div', recursive=False)[1] div_cond = div_cond.find_all('div', recursive=False)[0] element = div_cond.find_all('span')[0] high = element.text element = div_cond.find_all('span')[1] low = element.text if high == '--': high = temp if len(high) == 3: high = high[0:2] if len(low) == 3: low = low[0:2] preciption = seg_temp.find_all('div', recursive=False)[2] # print(preciption) if preciption is not None: preciption = preciption.text else: preciption = '' seg_temp = soup.find_all('div', {'id': 'todayDetails'}) # seg_temp = soup.find_all('div', {'id': 'WxuTodayDetails-main-fd88de85-7aa1-455f-832a-eacb037c140a'}) if seg_temp is not None and len(seg_temp) > 0: seg_temp = seg_temp[0] # print(seg_temp) div_ele = seg_temp.find('section') div_ele = div_ele.find_all('div', recursive=False)[1] div_node = div_ele.find_all('div', recursive=False)[2] # div_ele = div_node.find_all('div', recursive=False)[0] # print(div_ele.text) div_ele = div_node.find_all('div', recursive=False)[1] # print(div_ele) humidity = div_ele.text else: humidity = '0' weatherInfo = WeatherInfo.WeatherInfo(temp, low, high, as_of, condition, location, preciption) weatherInfo.set_humidity(humidity) weatherInfo.set_error(None) LOGGER.info(str(weatherInfo)) return weatherInfo
def callback_weather_init(future): global weather weather = WeatherInfo.WeatherInfo('32', "18", "22", "00:00", "Foggy", "Thalambur", "75%") weather.set_humidity("95%")
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 bc.mailbox("temperature:" + current.temperature + "," + weathers[0].high + "," + weathers[0].low) bc.mailbox("humidity:" + getWeatherByAddressResults.get_Humidity()) weather_info = wi.get_or_create bc.mailbox("temp_image:/root/images/medium/" + (wi.get_or_create(code = current.code, body= current.text))[0].image_path) print (wi.get_or_create(code=current.code, body = current.text))[0].image_path bc.mailbox("future:"+ weathers[1].high+","+weathers[2].high+","+weathers[3].high)
async def parse_accu_weather(page_content): soup = BeautifulSoup(page_content, 'html.parser') seg_temp = soup.find_all('div', class_='template-root')[0] location = seg_temp.find('div', class_='header-inner') location = location.find('h1', class_='header-loc') print(location.text) temp = seg_temp.find('span', class_='header-temp') print(temp.text) seg_temp = soup.find_all('div', class_='two-column-page-content')[0] # print(seg_temp.text) seg_temp = seg_temp.find('div', class_='page-column-1') content_module = seg_temp.find('div', class_='content-module') curr_weather_card = content_module.find('div', class_='current-weather-card') card_header = curr_weather_card.find('div', class_='card-header') # print(card_header) as_of = card_header.find('p') print(as_of.text) card_content = curr_weather_card.find('div', class_='card-content') # print(card_content.text) disp_temp = card_content.find('div', class_='display-temp') print(disp_temp.text) condition = curr_weather_card.find('div', class_='phrase') print(condition.text) curr_weather_details = curr_weather_card.find( 'div', class_='current-weather-details') left = curr_weather_details.find('div', class_='left') item_content = left.find_all('div', class_='detail-item')[2] humidity = item_content.find_all('div')[1] print(humidity.text) high = low = temp half_day_card = content_module.find('div', class_='half-day-card') # half_day_card_header = half_day_card.find('div', class_='half-day-card-header') # real_feel = half_day_card_header.find('div', class_='real-feel') # high = real_feel.find_all('div')[0] # # print(high.text) # # high = util.get_str_after(high, 'RealFeel') # low = real_feel.find_all('div')[1] # # print(high.text + '; ' + low.text) half_day_card_content = half_day_card.find('div', class_='half-day-card-content') print(half_day_card_content) left = half_day_card_content.find('div', class_='left') item_content = left.find_all('p')[2] precipitation = item_content.find('span') print(precipitation.text) weatherInfo = WeatherInfo.WeatherInfo(temp.text, low.text, high.text, as_of.text, condition.text, location.text, precipitation.text) weatherInfo.set_humidity(humidity.text) weatherInfo.set_error(None) LOGGER.info(str(weatherInfo)) return weatherInfo