Search in sources :

Example 1 with BackendType

use of org.nzbhydra.indexers.Indexer.BackendType in project nzbhydra2 by theotherp.

the class NewznabChecker method checkCaps.

/**
 * Attempts to determine which search IDs like IMDB or TVDB ID are supported by the indexer specified in the config.
 * <p>
 * Executes a search for each of the known IDs. If enough returned results match the expected title the ID is probably supported.
 */
public CheckCapsResponse checkCaps(IndexerConfig indexerConfig) {
    List<CheckCapsRequest> requests = Arrays.asList(new CheckCapsRequest(indexerConfig, "tvsearch", "tvdbid", "121361", Arrays.asList("Thrones", "GOT")), new CheckCapsRequest(indexerConfig, "tvsearch", "rid", "24493", Arrays.asList("Thrones", "GOT")), new CheckCapsRequest(indexerConfig, "tvsearch", "tvmazeid", "82", Arrays.asList("Thrones", "GOT")), new CheckCapsRequest(indexerConfig, "tvsearch", "traktid", "1390", Arrays.asList("Thrones", "GOT")), new CheckCapsRequest(indexerConfig, "movie", "tmdbid", "1399", Arrays.asList("Avengers", "Vengadores")), new CheckCapsRequest(indexerConfig, "movie", "imdbid", "0848228", Arrays.asList("Avengers", "Vengadores")));
    boolean allChecked = true;
    boolean configComplete = true;
    Integer timeout = indexerConfig.getTimeout().orElse(configProvider.getBaseConfig().getSearching().getTimeout()) + 1;
    List<Callable<SingleCheckCapsResponse>> callables = new ArrayList<>();
    for (int i = 0; i < requests.size(); i++) {
        CheckCapsRequest request = requests.get(i);
        callables.add(() -> {
            // Give indexer some time to breathe
            Thread.sleep(PAUSE_BETWEEN_CALLS);
            return singleCheckCaps(request, indexerConfig);
        });
    }
    Set<SingleCheckCapsResponse> responses = new HashSet<>();
    Set<IdType> supportedIds;
    String backend = null;
    ExecutorService executor = MdcThreadPoolExecutor.newWithInheritedMdc(MAX_CONNECTIONS);
    try {
        logger.info("Will check capabilities of indexer {} using {} concurrent connections", indexerConfig.getName(), MAX_CONNECTIONS);
        List<Future<SingleCheckCapsResponse>> futures = executor.invokeAll(callables);
        for (Future<SingleCheckCapsResponse> future : futures) {
            try {
                SingleCheckCapsResponse response = future.get(timeout, TimeUnit.SECONDS);
                if (response.getBackend() != null) {
                    backend = response.getBackend();
                }
                responses.add(response);
            } catch (ExecutionException e) {
                if (e.getCause() instanceof IndexerAccessException) {
                    logger.error("Error while communicating with indexer: " + e.getMessage());
                } else {
                    logger.error("Unexpected error while checking caps", e);
                }
                allChecked = false;
            } catch (TimeoutException e) {
                logger.error("Indexer {] failed to answer in {} seconds", indexerConfig.getName(), timeout);
                allChecked = false;
            }
        }
        supportedIds = responses.stream().filter(SingleCheckCapsResponse::isSupported).map(x -> Newznab.paramValueToIdMap.get(x.getKey())).collect(Collectors.toSet());
        if (supportedIds.isEmpty()) {
            logger.info("Indexer {} does not support searching by any IDs", indexerConfig.getName());
        } else {
            logger.info("Indexer {} supports searching using the following IDs: {}", indexerConfig.getName(), supportedIds.stream().map(Enum::name).collect(Collectors.joining(", ")));
        }
        indexerConfig.setSupportedSearchIds(new ArrayList<>(supportedIds));
    } catch (InterruptedException e) {
        logger.error("Unexpected error while checking caps", e);
        allChecked = false;
    } finally {
        executor.shutdown();
    }
    try {
        indexerConfig.setCategoryMapping(setSupportedSearchTypesAndIndexerCategoryMapping(indexerConfig, timeout));
        if (indexerConfig.getSupportedSearchTypes().isEmpty()) {
            logger.info("Indexer {} does not support any special search types", indexerConfig.getName());
        } else {
            logger.info("Indexer {} supports the following search types: {}", indexerConfig.getName(), indexerConfig.getSupportedSearchTypes().stream().map(Enum::name).collect(Collectors.joining(", ")));
        }
    } catch (IndexerAccessException e) {
        logger.error("Error while accessing indexer: " + e.getMessage());
        configComplete = false;
    }
    BackendType backendType = BackendType.NEWZNAB;
    if (backend == null && indexerConfig.getSearchModuleType() == SearchModuleType.NEWZNAB) {
        logger.info("Indexer {} didn't provide a backend type. Will use newznab.", indexerConfig.getName());
    } else if (backend != null) {
        try {
            backendType = BackendType.valueOf(backend.toUpperCase());
            logger.info("Indexer {} uses backend type {}", indexerConfig.getName(), backendType);
        } catch (IllegalArgumentException e) {
            logger.warn("Indexer {} reported unknown backend type {}. Will use newznab for now. Please report this.", indexerConfig.getName(), backend);
        }
    }
    indexerConfig.setBackend(backendType);
    indexerConfig.setConfigComplete(configComplete);
    indexerConfig.setAllCapsChecked(allChecked);
    indexerConfig.setState(configComplete ? IndexerConfig.State.ENABLED : IndexerConfig.State.DISABLED_SYSTEM);
    return new CheckCapsResponse(indexerConfig, allChecked, configComplete);
}
Also used : IndexerAccessException(org.nzbhydra.indexers.exceptions.IndexerAccessException) BackendType(org.nzbhydra.indexers.Indexer.BackendType) IdType(org.nzbhydra.mediainfo.InfoProvider.IdType)

Aggregations

BackendType (org.nzbhydra.indexers.Indexer.BackendType)1 IndexerAccessException (org.nzbhydra.indexers.exceptions.IndexerAccessException)1 IdType (org.nzbhydra.mediainfo.InfoProvider.IdType)1