def _request(self, method, endpoint, **params): url = TVDBRequest.BASE_URL + endpoint language = params.pop('language', 'en') headers = {'Authorization': f'Bearer {self.get_auth_token()}', 'Accept-Language': language} data = params.pop('data', None) result = requests.request( method, url, params=params, headers=headers, raise_status=False, json=data ) if result.status_code == 401: logger.debug('Auth token expired, refreshing') headers['Authorization'] = f'Bearer {self.get_auth_token(refresh=True)}' result = requests.request( method, url, params=params, headers=headers, raise_status=False, json=data ) result.raise_for_status() result = result.json() if result.get('errors'): logger.debug('Result contains errors: {}', result['errors']) # a hack to make sure it doesn't raise exception on a simple invalidLanguage. This is because tvdb # has a tendency to contain bad data and randomly return this error for no reason if len(result['errors']) > 1 or 'invalidLanguage' not in result['errors']: raise LookupError(f'Error processing request on tvdb: {result["errors"]}') return result
def _request(self, method, endpoint, **params): url = TVDBRequest.BASE_URL + endpoint headers = {'Authorization': 'Bearer %s' % self.get_auth_token()} data = params.pop('data', None) result = requests.request(method, url, params=params, headers=headers, raise_status=False, json=data) if result.status_code == 401: log.debug('Auth token expired, refreshing') headers['Authorization'] = 'Bearer %s' % self.get_auth_token( refresh=True) result = requests.request(method, url, params=params, headers=headers, raise_status=False, json=data) result.raise_for_status() result = result.json() if result.get('errors'): raise LookupError('Error processing request on tvdb: %s' % result.get('errors')) return result
def _request(self, method, endpoint, **params): url = TVDBRequest.BASE_URL + endpoint language = params.pop('language', 'en') headers = { 'Authorization': 'Bearer %s' % self.get_auth_token(), 'Accept-Language': language, } data = params.pop('data', None) result = requests.request( method, url, params=params, headers=headers, raise_status=False, json=data ) if result.status_code == 401: log.debug('Auth token expired, refreshing') headers['Authorization'] = 'Bearer %s' % self.get_auth_token(refresh=True) result = requests.request( method, url, params=params, headers=headers, raise_status=False, json=data ) result.raise_for_status() result = result.json() if result.get('errors'): log.debug('Result contains errors: %s', result['errors']) # a hack to make sure it doesn't raise exception on a simple invalidLanguage. This is because tvdb # has a tendency to contain bad data and randomly return this error for no reason if len(result['errors']) > 1 or 'invalidLanguage' not in result['errors']: raise LookupError('Error processing request on tvdb: %s' % result['errors']) return result
def _request(self, method, endpoint, **params): url = TVDBRequest.BASE_URL + endpoint headers = {'Authorization': 'Bearer %s' % self.get_auth_token()} data = params.pop('data', None) result = requests.request(method, url, params=params, headers=headers, raise_status=False, json=data) if result.status_code == 401: log.debug('Auth token expired, refreshing') headers['Authorization'] = 'Bearer %s' % self.get_auth_token(refresh=True) result = requests.request(method, url, params=params, headers=headers, raise_status=False, json=data) result.raise_for_status() result = result.json() if result.get('errors'): raise LookupError('Error processing request on tvdb: %s' % result.get('errors')) return result
def query_api(url, method, post=None): try: response = requests.request( 'post' if post is not None else 'get', url.rstrip("/") + "/" + method.strip("/"), data=post) response.raise_for_status() return response except RequestException as e: if e.response.status_code == 500: raise plugin.PluginError('Internal API Error: <%s> <%s> <%s>' % (method, url, post), log) raise
def query_api(url, method, post=None): try: response = requests.request('post' if post is not None else 'get', url.rstrip("/") + "/" + method.strip("/"), data=post) response.raise_for_status() return response except RequestException as e: if e.response.status_code == 500: raise plugin.PluginError( 'Internal API Error: <%s> <%s> <%s>' % (method, url, post), log) raise
def on_task_output(self, task, config): for entry in task.accepted: item = {} for key in config['fields']: if type(key) is dict: item[key.keys()[0]] = render_from_entry(key.values()[0], entry) elif entry.get(key): item[key] = entry[key] try: path = [] if config['path'] == "" else [render_from_entry(config['path'], entry)] if config['grouping']: group = entry['title'].replace(".", " ") group = group.replace("_", " ") group = group.split(" - ")[0] group = group.split(",")[0] group = group.split("#")[0] group = group.split(":")[0] group = group.split(" (")[0] group = re.compile("[\d]+").split(group)[0] group = group.strip() item['group'] = group path.append(group) if config['key']: path.append(render_from_entry(config['key'], entry)) method = "put" else: method = "post" path = urllib.quote("/".join(path)) url = config['storage'] + "/" + path + ".json" if config['auth']: url += "?auth=" + config['auth'] log.verbose("Requesting Firebase: " + method.upper() + " " + url) log.verbose(item) response = requests.request(method, url, data=jsonpickle.encode(item, unpicklable=False)) log.verbose('Firebase response: ' + response.text) if not response.status_code == 200: entry.fail('http error') except Exception as e: entry.fail("exception: %s" % e)
def _sonarr_request(self, endpoint, term=None, method='get', data=None): base_url = self.config['base_url'] port = self.config['port'] url = '{}:{}/api/{}'.format(base_url, port, endpoint) headers = {'X-Api-Key': self.config['api_key']} if term: url += '?term={}'.format(term) try: rsp = requests.request(method, url, headers=headers, json=data) data = rsp.json() log.trace('sonarr response: %s', data) return data except RequestException as e: base_msg = 'Sonarr returned an error. {}' if e.response is not None: error = e.response.json()[0] error = '{}: {} \'{}\''.format(error['errorMessage'], error['propertyName'], error['attemptedValue']) else: error = str(e) raise plugin.PluginError(base_msg.format(error))
def _sonarr_request(self, endpoint, term=None, method='get', data=None): base_url = self.config['base_url'] port = self.config['port'] base_path = self.config['base_path'] url = '{}:{}{}/api/{}'.format(base_url, port, base_path, endpoint) headers = {'X-Api-Key': self.config['api_key']} if term: url += '?term={}'.format(term) try: rsp = requests.request(method, url, headers=headers, json=data) data = rsp.json() log.trace('sonarr response: %s', data) return data except RequestException as e: base_msg = 'Sonarr returned an error. {}' if e.response is not None: error = e.response.json()[0] error = '{}: {} \'{}\''.format(error['errorMessage'], error['propertyName'], error['attemptedValue']) else: error = str(e) raise plugin.PluginError(base_msg.format(error))