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());
}
}
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);
}
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());
}
}
Aggregations