def write_influx_data(self, json_data): """ Writes the provided JSON to the database :param json_data: :return: """ log.debug(json_data) try: self.influx_client.write_points(json_data) except (InfluxDBClientError, ConnectionError, InfluxDBServerError) as e: if hasattr(e, 'code') and e.code == 404: log.error('Database {} Does Not Exist. Attempting To Create') self.influx_client.create_database(config.influx_database) self.influx_client.write_points(json_data) return log.error('Failed to write data to InfluxDB') log.debug('Written To Influx: {}'.format(json_data))
def get_auth_token(self, username, password): """ Make a reqest to plex.tv to get an authentication token for future requests :param username: Plex Username :param password: Plex Password :return: str """ log.info('Getting Auth Token For User {}'.format(username)) auth_string = '{}:{}'.format(username, password) base_auth = base64.encodebytes(bytes(auth_string, 'utf-8')) req = Request('https://plex.tv/users/sign_in.json') req = self._set_default_headers(req) req.add_header('Authorization', 'Basic {}'.format(base_auth[:-1].decode('utf-8'))) try: result = urlopen(req, data=b'').read() except HTTPError as e: print('Failed To Get Authentication Token') if e.code == 401: log.error('Failed to get token due to bad username/password') else: print('Maybe this will help:') print(e) log.error('Failed to get authentication token. No idea why') sys.exit(1) output = json.loads(result.decode('utf-8')) # Make sure we actually got a token back if 'authToken' in output['user']: log.debug('Successfully Retrieved Auth Token') return output['user']['authToken'] else: print( 'Something Broke \n We got a valid response but for some reason there\'s no auth token' ) sys.exit(1)