Send request for updated weather data
(endpoint)
| 148 | |
| 149 | |
| 150 | def request_weather_data(endpoint): |
| 151 | """ Send request for updated weather data """ |
| 152 | global APP_DATA |
| 153 | |
| 154 | if endpoint is None: |
| 155 | sg.popup_error('Could not connect to api. endpoint is None', keep_on_top=True, location=win_location) |
| 156 | return |
| 157 | else: |
| 158 | try: |
| 159 | response = request.urlopen(endpoint) |
| 160 | except request.HTTPError: |
| 161 | sg.popup_error('ERROR Obtaining Weather Data', |
| 162 | 'Is your API Key set correctly?', |
| 163 | API_KEY, keep_on_top=True, location=win_location) |
| 164 | return |
| 165 | if APP_DATA['Units'] == 'metric': |
| 166 | temp_units, speed_units = '°C', 'm/sec' |
| 167 | else: |
| 168 | temp_units, speed_units = '°F', 'miles/hr' |
| 169 | if response.reason == 'OK': |
| 170 | weather = json.loads(response.read()) |
| 171 | APP_DATA['City'] = weather['name'].title() |
| 172 | APP_DATA['Description'] = weather['weather'][0]['description'] |
| 173 | APP_DATA['Temp'] = "{:,.0f}{}".format(weather['main']['temp'], temp_units) |
| 174 | APP_DATA['Humidity'] = "{:,d}%".format(weather['main']['humidity']) |
| 175 | APP_DATA['Pressure'] = "{:,d} hPa".format(weather['main']['pressure']) |
| 176 | APP_DATA['Feels Like'] = "{:,.0f}{}".format(weather['main']['feels_like'], temp_units) |
| 177 | APP_DATA['Wind'] = "{:,.1f}{}".format(weather['wind']['speed'], speed_units) |
| 178 | APP_DATA['Precip 1hr'] = None if not weather.get('rain') else "{:2} mm".format(weather['rain']['1h']) |
| 179 | APP_DATA['Updated'] = 'Updated: ' + datetime.datetime.now().strftime("%B %d %I:%M:%S %p") |
| 180 | APP_DATA['Lon'] = weather['coord']['lon'] |
| 181 | APP_DATA['Lat'] = weather['coord']['lat'] |
| 182 | |
| 183 | icon_url = "http://openweathermap.org/img/wn/{}@2x.png".format(weather['weather'][0]['icon']) |
| 184 | APP_DATA['Icon'] = base64.b64encode(request.urlopen(icon_url).read()) |
| 185 | |
| 186 | |
| 187 | def metric_row(metric): |
no test coverage detected