Search in sources :

Example 6 with IdType

use of org.nzbhydra.mediainfo.InfoProvider.IdType in project nzbhydra2 by theotherp.

the class NewznabChecker method setSupportedSearchTypesAndIndexerCategoryMapping.

public IndexerCategoryConfig setSupportedSearchTypesAndIndexerCategoryMapping(IndexerConfig indexerConfig, int timeout) throws IndexerAccessException {
    List<IdType> supportedSearchIds = indexerConfig.getSupportedSearchIds();
    List<ActionAttribute> supportedSearchTypes = new ArrayList<>();
    if (supportedSearchIds.contains(IdType.TVDB) || supportedSearchIds.contains(IdType.TVRAGE) || supportedSearchIds.contains(IdType.TVMAZE) || supportedSearchIds.contains(IdType.TRAKT)) {
        supportedSearchTypes.add(ActionAttribute.TVSEARCH);
    }
    if (supportedSearchIds.contains(IdType.IMDB) || supportedSearchIds.contains(IdType.TMDB)) {
        supportedSearchTypes.add(ActionAttribute.MOVIE);
    }
    URI uri = getBaseUri(indexerConfig).queryParam("t", "caps").build().toUri();
    CapsRoot capsRoot = indexerWebAccess.get(uri, indexerConfig);
    if (capsRoot.getSearching().getAudioSearch() != null) {
        supportedSearchTypes.add(ActionAttribute.AUDIO);
    }
    if (capsRoot.getSearching().getBookSearch() != null) {
        supportedSearchTypes.add(ActionAttribute.BOOK);
    }
    indexerConfig.setSupportedSearchTypes(supportedSearchTypes);
    IndexerCategoryConfig categoryConfig = new IndexerCategoryConfig();
    List<CapsCategory> categories = readAndConvertCategories(capsRoot, categoryConfig);
    setCategorySpecificMappings(categoryConfig, categories);
    return categoryConfig;
}
Also used : CapsRoot(org.nzbhydra.mapping.newznab.caps.CapsRoot) IndexerCategoryConfig(org.nzbhydra.config.IndexerCategoryConfig) URI(java.net.URI) CapsCategory(org.nzbhydra.mapping.newznab.caps.CapsCategory) ActionAttribute(org.nzbhydra.mapping.newznab.ActionAttribute) IdType(org.nzbhydra.mediainfo.InfoProvider.IdType)

Example 7 with IdType

use of org.nzbhydra.mediainfo.InfoProvider.IdType 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

IdType (org.nzbhydra.mediainfo.InfoProvider.IdType)7 HashMap (java.util.HashMap)3 Test (org.junit.Test)3 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)3 SearchRequest (org.nzbhydra.searching.searchrequests.SearchRequest)3 MediaInfo (org.nzbhydra.mediainfo.MediaInfo)2 TypeReference (com.fasterxml.jackson.core.type.TypeReference)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Sets (com.google.common.collect.Sets)1 IOException (java.io.IOException)1 URI (java.net.URI)1 java.sql (java.sql)1 Instant (java.time.Instant)1 LocalDateTime (java.time.LocalDateTime)1 ZoneOffset (java.time.ZoneOffset)1 DateTimeFormatter (java.time.format.DateTimeFormatter)1 ChronoUnit (java.time.temporal.ChronoUnit)1 java.util (java.util)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 TimeUnit (java.util.concurrent.TimeUnit)1