def cache_icon(self, icon_name): icon_path = f'cache/{icon_name}.png' if not path.exists(icon_path): image_data = easy_requests.get( f'http://openweathermap.org/img/wn/{icon_name}@2x.png', 'image/png') with open(icon_path, 'wb') as file: file.write(image_data)
def make_api_call(self, api): try: return easy_requests.get( f'https://api.openweathermap.org/data/2.5/{api}&units=imperial&APPID={cfg.get("weather-api-key")}' ) except HTTPError as err: if err.getcode() == 401: print( 'Weather API key was rejected. It\'s either invalid or it hasn\'t been activated yet. Verify it ' 'is correct or try again later.') sys.exit(-1)
def make_alerts_call(self, dt): lat = self.coords['lat'] lon = self.coords['lon'] alerts = easy_requests.get( f'https://api.weather.gov/alerts/active?point={lat}%2C{lon}') self.active_alerts = [] for alert in list(alert['properties'] for alert in alerts['features']): self.active_alerts.append({ 'headline': alert['headline'], 'description': alert['description'] })
def toggle(self, light_id): # since IDs are numbers as strings, it's easy to forget to pass a string, ensure we're dealing with a string light_id = str(light_id) light_name = next(light['name'] for light in self.lights if light['id'] == light_id) self.log(f'toggling {light_id} ({light_name})') res = easy_requests.get(f'{self.overseer_url}lights/toggle/{light_id}') if 'error' in res: self.log(f'Error toggling lights: {res["error"]}') else: self.refresh()
def refresh(self, data=None): try: if not data: data = easy_requests.get(f'{self.overseer_url}lights/info') if 'error' in data: self.log( 'error retrieving lights information, does overseer trust this device?' ) sys.exit(-1) self.lights = data except URLError as err: self.log('error reaching overseer') self.log(err)