Search in sources :

Example 1 with NewznabXmlError

use of org.nzbhydra.mapping.newznab.xml.NewznabXmlError in project nzbhydra2 by theotherp.

the class NewznabChecker method singleCheckCaps.

private SingleCheckCapsResponse singleCheckCaps(CheckCapsRequest request, IndexerConfig indexerConfig) throws IndexerAccessException {
    URI uri = getBaseUri(request.getIndexerConfig()).queryParam("t", request.getTMode()).queryParam(request.getKey(), request.getValue()).build().toUri();
    logger.debug("Calling URL {}", uri);
    Xml response = indexerWebAccess.get(uri, indexerConfig);
    if (response instanceof NewznabXmlError) {
        String errorDescription = ((NewznabXmlError) response).getDescription();
        if (errorDescription.toLowerCase().contains("function not available")) {
            logger.error("Indexer {} reports that it doesn't support the ID type {}", request.indexerConfig.getName(), request.getKey());
            eventPublisher.publishEvent(new CheckerEvent(indexerConfig.getName(), "Doesn't support " + request.getKey()));
            return new SingleCheckCapsResponse(request.getKey(), false, null);
        }
        logger.debug("RSS error from indexer {}: {}", request.indexerConfig.getName(), errorDescription);
        eventPublisher.publishEvent(new CheckerEvent(indexerConfig.getName(), "RSS error from indexer: " + errorDescription));
        throw new IndexerAccessException("RSS error from indexer: " + errorDescription);
    }
    NewznabXmlRoot rssRoot = (NewznabXmlRoot) response;
    if (rssRoot.getRssChannel().getItems().isEmpty()) {
        logger.info("Indexer {} probably doesn't support the ID type {}. It returned no results.", request.indexerConfig.getName(), request.getKey());
        eventPublisher.publishEvent(new CheckerEvent(indexerConfig.getName(), "Probably doesn't support " + request.getKey()));
        return new SingleCheckCapsResponse(request.getKey(), false, rssRoot.getRssChannel().getGenerator());
    }
    long countCorrectResults = rssRoot.getRssChannel().getItems().stream().filter(x -> request.getTitleExpectedToContain().stream().anyMatch(y -> x.getTitle().toLowerCase().contains(y.toLowerCase()))).count();
    float percentCorrect = (100 * countCorrectResults) / rssRoot.getRssChannel().getItems().size();
    boolean supported = percentCorrect >= ID_THRESHOLD_PERCENT;
    if (supported) {
        logger.info("Indexer {} probably supports the ID type {}. {}% of results were correct.", request.indexerConfig.getName(), request.getKey(), percentCorrect);
        eventPublisher.publishEvent(new CheckerEvent(indexerConfig.getName(), "Probably supports " + request.getKey()));
        return new SingleCheckCapsResponse(request.getKey(), true, rssRoot.getRssChannel().getGenerator());
    } else {
        logger.info("Indexer {} probably doesn't support the ID type {}. {}% of results were correct.", request.indexerConfig.getName(), request.getKey(), percentCorrect);
        eventPublisher.publishEvent(new CheckerEvent(indexerConfig.getName(), "Probably doesn't support " + request.getKey()));
        return new SingleCheckCapsResponse(request.getKey(), false, rssRoot.getRssChannel().getGenerator());
    }
}
Also used : UriComponentsBuilder(org.springframework.web.util.UriComponentsBuilder) java.util(java.util) NewznabXmlRoot(org.nzbhydra.mapping.newznab.xml.NewznabXmlRoot) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) SearchModuleType(org.nzbhydra.config.SearchModuleType) MainCategory(org.nzbhydra.config.IndexerCategoryConfig.MainCategory) Strings(com.google.common.base.Strings) GenericResponse(org.nzbhydra.GenericResponse) Newznab(org.nzbhydra.indexers.Newznab) ConfigProvider(org.nzbhydra.config.ConfigProvider) BackendType(org.nzbhydra.indexers.Indexer.BackendType) Xml(org.nzbhydra.mapping.newznab.xml.Xml) ApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher) SubCategory(org.nzbhydra.config.IndexerCategoryConfig.SubCategory) CheckType(org.nzbhydra.indexers.capscheck.CapsCheckRequest.CheckType) CapsRoot(org.nzbhydra.mapping.newznab.caps.CapsRoot) URI(java.net.URI) IndexerWebAccess(org.nzbhydra.indexers.IndexerWebAccess) Logger(org.slf4j.Logger) IndexerConfig(org.nzbhydra.config.IndexerConfig) CapsCategory(org.nzbhydra.mapping.newznab.caps.CapsCategory) NewznabXmlError(org.nzbhydra.mapping.newznab.xml.NewznabXmlError) java.util.concurrent(java.util.concurrent) IdType(org.nzbhydra.mediainfo.InfoProvider.IdType) IndexerCategoryConfig(org.nzbhydra.config.IndexerCategoryConfig) Collectors(java.util.stream.Collectors) ActionAttribute(org.nzbhydra.mapping.newznab.ActionAttribute) Component(org.springframework.stereotype.Component) Data(lombok.Data) MdcThreadPoolExecutor(org.nzbhydra.logging.MdcThreadPoolExecutor) AllArgsConstructor(lombok.AllArgsConstructor) IndexerAccessException(org.nzbhydra.indexers.exceptions.IndexerAccessException) 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 2 with NewznabXmlError

use of org.nzbhydra.mapping.newznab.xml.NewznabXmlError in project nzbhydra2 by theotherp.

the class ExternalApi method api.

@RequestMapping(value = { "/api", "/rss", "/torznab/api" }, consumes = MediaType.ALL_VALUE)
public ResponseEntity<? extends Object> api(NewznabParameters params) throws Exception {
    logger.info("Received external {}API call: {}", (isTorznabCall() ? "torznab " : ""), params);
    if (!noApiKeyNeeded && !Objects.equals(params.getApikey(), configProvider.getBaseConfig().getMain().getApiKey())) {
        logger.error("Received API call with wrong API key");
        throw new WrongApiKeyException("Wrong api key");
    }
    if (Stream.of(ActionAttribute.SEARCH, ActionAttribute.BOOK, ActionAttribute.TVSEARCH, ActionAttribute.MOVIE).anyMatch(x -> x == params.getT())) {
        if (params.getCachetime() != null) {
            return handleCachingSearch(params);
        }
        NewznabResponse searchResult = search(params);
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.set(HttpHeaders.CONTENT_TYPE, searchResult.getContentHeader());
        if (params.getO() != OutputType.JSON) {
            searchResult.setSearchType(isTorznabCall() ? "torznab" : "newznab");
        }
        return new ResponseEntity<>(searchResult, httpHeaders, HttpStatus.OK);
    }
    if (params.getT() == ActionAttribute.GET) {
        return getNzb(params);
    }
    if (params.getT() == ActionAttribute.CAPS) {
        return getCaps();
    }
    logger.error("Incorrect API request: {}", params);
    NewznabXmlError error = new NewznabXmlError("200", "Unknown or incorrect parameter");
    return new ResponseEntity<Object>(error, HttpStatus.OK);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) ResponseEntity(org.springframework.http.ResponseEntity) NewznabXmlError(org.nzbhydra.mapping.newznab.xml.NewznabXmlError) NewznabResponse(org.nzbhydra.mapping.newznab.NewznabResponse) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 3 with NewznabXmlError

use of org.nzbhydra.mapping.newznab.xml.NewznabXmlError 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)

Aggregations

NewznabXmlError (org.nzbhydra.mapping.newznab.xml.NewznabXmlError)3 URI (java.net.URI)2 IndexerAccessException (org.nzbhydra.indexers.exceptions.IndexerAccessException)2 NewznabXmlRoot (org.nzbhydra.mapping.newznab.xml.NewznabXmlRoot)2 Xml (org.nzbhydra.mapping.newznab.xml.Xml)2 Strings (com.google.common.base.Strings)1 java.util (java.util)1 java.util.concurrent (java.util.concurrent)1 Collectors (java.util.stream.Collectors)1 AllArgsConstructor (lombok.AllArgsConstructor)1 Data (lombok.Data)1 GenericResponse (org.nzbhydra.GenericResponse)1 ConfigProvider (org.nzbhydra.config.ConfigProvider)1 IndexerCategoryConfig (org.nzbhydra.config.IndexerCategoryConfig)1 MainCategory (org.nzbhydra.config.IndexerCategoryConfig.MainCategory)1 SubCategory (org.nzbhydra.config.IndexerCategoryConfig.SubCategory)1 IndexerConfig (org.nzbhydra.config.IndexerConfig)1 SearchModuleType (org.nzbhydra.config.SearchModuleType)1 BackendType (org.nzbhydra.indexers.Indexer.BackendType)1 IndexerWebAccess (org.nzbhydra.indexers.IndexerWebAccess)1