Search in sources :

Example 6 with IndexerAccessException

use of org.nzbhydra.indexers.exceptions.IndexerAccessException in project nzbhydra2 by theotherp.

the class NzbIndex method getNfo.

@Override
public NfoResult getNfo(String guid) {
    URI nfoUri = getBaseUri().pathSegment("nfo", guid).build().toUri();
    try {
        String html = getAndStoreResultToDatabase(nfoUri, String.class, IndexerApiAccessType.NFO);
        Matcher matcher = NFO_PATTERN.matcher(html);
        if (!matcher.find()) {
            return NfoResult.withoutNfo();
        }
        return NfoResult.withNfo(matcher.group(1));
    } catch (IndexerAccessException e) {
        return NfoResult.unsuccessful(e.getMessage());
    }
}
Also used : Matcher(java.util.regex.Matcher) IndexerAccessException(org.nzbhydra.indexers.exceptions.IndexerAccessException) URI(java.net.URI)

Example 7 with IndexerAccessException

use of org.nzbhydra.indexers.exceptions.IndexerAccessException in project nzbhydra2 by theotherp.

the class NewznabChecker method checkConnection.

public GenericResponse checkConnection(IndexerConfig indexerConfig) {
    Xml xmlResponse;
    try {
        URI uri = getBaseUri(indexerConfig).queryParam("t", "search").build().toUri();
        xmlResponse = indexerWebAccess.get(uri, indexerConfig);
        logger.debug("Checking connection to indexer {} using URI {}", indexerConfig.getName(), uri);
        if (xmlResponse instanceof NewznabXmlError) {
            logger.warn("Connection check with indexer {} failed with message: {}", indexerConfig.getName(), ((NewznabXmlError) xmlResponse).getDescription());
            return GenericResponse.notOk("Indexer returned message: " + ((NewznabXmlError) xmlResponse).getDescription());
        }
        NewznabXmlRoot rssRoot = (NewznabXmlRoot) xmlResponse;
        if (!rssRoot.getRssChannel().getItems().isEmpty()) {
            logger.info("Connection to indexer {} successful", indexerConfig.getName());
            return GenericResponse.ok();
        } else {
            logger.warn("Connection to indexer {} successful but search did not return any results", indexerConfig.getName());
            return GenericResponse.notOk("Indexer did not return any results");
        }
    } catch (IndexerAccessException e) {
        logger.warn("Connection check with indexer {} failed with message: {}", indexerConfig.getName(), e.getMessage());
        return GenericResponse.notOk(e.getMessage());
    }
}
Also used : NewznabXmlError(org.nzbhydra.mapping.newznab.xml.NewznabXmlError) Xml(org.nzbhydra.mapping.newznab.xml.Xml) NewznabXmlRoot(org.nzbhydra.mapping.newznab.xml.NewznabXmlRoot) IndexerAccessException(org.nzbhydra.indexers.exceptions.IndexerAccessException) URI(java.net.URI)

Example 8 with IndexerAccessException

use of org.nzbhydra.indexers.exceptions.IndexerAccessException 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

IndexerAccessException (org.nzbhydra.indexers.exceptions.IndexerAccessException)8 URI (java.net.URI)5 Matcher (java.util.regex.Matcher)2 Test (org.junit.Test)2 BackendType (org.nzbhydra.indexers.Indexer.BackendType)2 NewznabXmlError (org.nzbhydra.mapping.newznab.xml.NewznabXmlError)2 NewznabXmlRoot (org.nzbhydra.mapping.newznab.xml.NewznabXmlRoot)2 Xml (org.nzbhydra.mapping.newznab.xml.Xml)2 IdType (org.nzbhydra.mediainfo.InfoProvider.IdType)2 Strings (com.google.common.base.Strings)1 StringReader (java.io.StringReader)1 SocketTimeoutException (java.net.SocketTimeoutException)1 java.util (java.util)1 HashMap (java.util.HashMap)1 java.util.concurrent (java.util.concurrent)1 Collectors (java.util.stream.Collectors)1 StreamSource (javax.xml.transform.stream.StreamSource)1 AllArgsConstructor (lombok.AllArgsConstructor)1 Data (lombok.Data)1 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)1