def resource_search_indexers_for_show_name(self): """ Search indexers for show name. Query parameters: :param query: Search term :param indexerId: Indexer to search, defined by ID. '0' for all indexers. :param language: 2-letter language code to search the indexer(s) in """ query = self.get_argument('query', '').strip() indexer_id = self.get_argument('indexerId', '0').strip() language = self.get_argument('language', '').strip() if not query: return self._bad_request('No search term provided.') enabled_indexers = indexerApi().indexers indexer_id = try_int(indexer_id) if indexer_id > 0 and indexer_id not in enabled_indexers: return self._bad_request('Invalid indexer id.') if not language or language == 'null': language = app.INDEXER_DEFAULT_LANGUAGE search_terms = [query] # If search term ends with what looks like a year, enclose it in () matches = re.match(r'^(.+ |)([12][0-9]{3})$', query) if matches: search_terms.append('{0}({1})'.format(matches.group(1), matches.group(2))) for search_term in search_terms: # If search term begins with an article, let's also search for it without matches = re.match(r'^(?:a|an|the) (.+)$', search_term, re.I) if matches: search_terms.append(matches.group(1)) results = {} final_results = [] # Query indexers for each search term and build the list of results for indexer in enabled_indexers if indexer_id == 0 else [indexer_id]: indexer_instance = indexerApi(indexer) custom_api_params = indexer_instance.api_params.copy() custom_api_params['language'] = language custom_api_params['custom_ui'] = classes.AllShowsListUI try: indexer_api = indexer_instance.indexer(**custom_api_params) except IndexerUnavailable as msg: log.info('Could not initialize indexer {indexer}: {error}', { 'indexer': indexer_instance.name, 'error': msg }) continue log.debug( 'Searching for show with search term(s): {terms} on indexer: {indexer}', { 'terms': search_terms, 'indexer': indexer_api.name }) for search_term in search_terms: try: indexer_results = indexer_api[search_term] # add search results results.setdefault(indexer, []).extend(indexer_results) except IndexerException as error: log.info( 'Error searching for show. term(s): {terms} indexer: {indexer} error: {error}', { 'terms': search_terms, 'indexer': indexer_api.name, 'error': error }) except Exception as error: log.error( 'Internal Error searching for show. term(s): {terms} indexer: {indexer} error: {error}', { 'terms': search_terms, 'indexer': indexer_api.name, 'error': error }) # Get all possible show ids all_show_ids = {} for show in app.showList: for ex_indexer_name, ex_show_id in iteritems(show.externals): ex_indexer_id = reverse_mappings.get(ex_indexer_name) if not ex_indexer_id: continue all_show_ids[(ex_indexer_id, ex_show_id)] = (show.indexer_name, show.series_id) for indexer, shows in iteritems(results): indexer_api = indexerApi(indexer) indexer_results_set = set() for show in shows: show_id = int(show['id']) indexer_results_set.add( (indexer_api.name, indexer, indexer_api.config['show_url'], show_id, show['seriesname'], show['firstaired'] or 'N/A', show.get('network', '') or 'N/A', sanitize_filename(show['seriesname']), all_show_ids.get((indexer, show_id), False))) final_results.extend(indexer_results_set) language_id = indexerApi().config['langabbv_to_id'][language] data = {'results': final_results, 'languageId': language_id} return self._ok(data=data)
def resource_search_indexers_for_show_name(self): """ Search indexers for show name. Query parameters: :param query: Search term :param indexerId: Indexer to search, defined by ID. '0' for all indexers. :param language: 2-letter language code to search the indexer(s) in """ query = self.get_argument('query', '').strip() indexer_id = self.get_argument('indexerId', '0').strip() language = self.get_argument('language', '').strip() if not query: return self._bad_request('No search term provided.') enabled_indexers = indexerApi().indexers indexer_id = try_int(indexer_id) if indexer_id > 0 and indexer_id not in enabled_indexers: return self._bad_request('Invalid indexer id.') if not language or language == 'null': language = app.INDEXER_DEFAULT_LANGUAGE search_terms = [query] # If search term ends with what looks like a year, enclose it in () matches = re.match(r'^(.+ |)([12][0-9]{3})$', query) if matches: search_terms.append('{0}({1})'.format(matches.group(1), matches.group(2))) for search_term in search_terms: # If search term begins with an article, let's also search for it without matches = re.match(r'^(?:a|an|the) (.+)$', search_term, re.I) if matches: search_terms.append(matches.group(1)) results = {} final_results = [] # Query indexers for each search term and build the list of results for indexer in enabled_indexers if indexer_id == 0 else [indexer_id]: indexer_instance = indexerApi(indexer) custom_api_params = indexer_instance.api_params.copy() custom_api_params['language'] = language custom_api_params['custom_ui'] = classes.AllShowsListUI try: indexer_api = indexer_instance.indexer(**custom_api_params) except IndexerUnavailable as msg: log.info('Could not initialize indexer {indexer}: {error}', {'indexer': indexer_instance.name, 'error': msg}) continue log.debug('Searching for show with search term(s): {terms} on indexer: {indexer}', {'terms': search_terms, 'indexer': indexer_api.name}) for search_term in search_terms: try: indexer_results = indexer_api[search_term] # add search results results.setdefault(indexer, []).extend(indexer_results) except IndexerException as error: log.info('Error searching for show. term(s): {terms} indexer: {indexer} error: {error}', {'terms': search_terms, 'indexer': indexer_api.name, 'error': error}) except Exception as error: log.error('Internal Error searching for show. term(s): {terms} indexer: {indexer} error: {error}', {'terms': search_terms, 'indexer': indexer_api.name, 'error': error}) # Get all possible show ids all_show_ids = {} for show in app.showList: for ex_indexer_name, ex_show_id in iteritems(show.externals): ex_indexer_id = reverse_mappings.get(ex_indexer_name) if not ex_indexer_id: continue all_show_ids[(ex_indexer_id, ex_show_id)] = (show.indexer_name, show.series_id) for indexer, shows in iteritems(results): indexer_api = indexerApi(indexer) indexer_results_set = set() for show in shows: show_id = int(show['id']) indexer_results_set.add( ( indexer_api.name, indexer, indexer_api.config['show_url'], show_id, show['seriesname'], show['firstaired'] or 'N/A', show.get('network', '') or 'N/A', sanitize_filename(show['seriesname']), all_show_ids.get((indexer, show_id), False) ) ) final_results.extend(indexer_results_set) language_id = indexerApi().config['langabbv_to_id'][language] data = { 'results': final_results, 'languageId': language_id } return self._ok(data=data)