use of org.nzbhydra.indexers.exceptions.IndexerAccessException 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.indexers.exceptions.IndexerAccessException in project nzbhydra2 by theotherp.
the class Binsearch method getNfo.
@Override
public NfoResult getNfo(String guid) {
URI nfoUri = UriComponentsBuilder.fromHttpUrl(config.getHost()).pathSegment("viewNFO.php").queryParam("oid", 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("nfo"));
} catch (IndexerAccessException e) {
return NfoResult.unsuccessful(e.getMessage());
}
}
use of org.nzbhydra.indexers.exceptions.IndexerAccessException in project nzbhydra2 by theotherp.
the class NewznabTest method shouldCatchException.
@Test
public void shouldCatchException() throws Exception {
doReturn("rawnfo").when(testee).getAndStoreResultToDatabase(any(), eq(String.class), eq(IndexerApiAccessType.NFO));
doThrow(new IndexerAccessException("message")).when(testee).getAndStoreResultToDatabase(any(), eq(String.class), eq(IndexerApiAccessType.NFO));
NfoResult nfo = testee.getNfo("guid");
assertThat(nfo.getContent(), is("message"));
assertThat(nfo.isSuccessful(), is(false));
assertThat(nfo.isHasNfo(), is(false));
}
use of org.nzbhydra.indexers.exceptions.IndexerAccessException in project nzbhydra2 by theotherp.
the class NewznabCheckerTest method shouldSaySoIfNotAllWereChecked.
@Test
public void shouldSaySoIfNotAllWereChecked() throws Exception {
NewznabResponseBuilder builder = new NewznabResponseBuilder();
when(indexerWebAccess.get(new URI("http://127.0.0.1:1234/api?apikey=apikey&t=tvsearch&tvdbid=121361"), indexerConfig)).thenReturn(builder.getTestResult(1, 100, "Thrones", 0, 100));
when(indexerWebAccess.get(new URI("http://127.0.0.1:1234/api?apikey=apikey&t=tvsearch&rid=24493"), indexerConfig)).thenReturn(builder.getTestResult(1, 100, "Thrones", 0, 100));
when(indexerWebAccess.get(new URI("http://127.0.0.1:1234/api?apikey=apikey&t=tvsearch&tvmazeid=82"), indexerConfig)).thenReturn(builder.getTestResult(1, 100, "Thrones", 0, 100));
when(indexerWebAccess.get(new URI("http://127.0.0.1:1234/api?apikey=apikey&t=tvsearch&traktid=1390"), indexerConfig)).thenReturn(builder.getTestResult(1, 100, "Thrones", 0, 100));
when(indexerWebAccess.get(new URI("http://127.0.0.1:1234/api?apikey=apikey&t=movie&tmdbid=1399"), indexerConfig)).thenReturn(builder.getTestResult(1, 100, "Avengers", 0, 100));
when(indexerWebAccess.get(new URI("http://127.0.0.1:1234/api?apikey=apikey&t=movie&imdbid=0848228"), indexerConfig)).thenThrow(new IndexerAccessException("some error"));
CheckCapsResponse checkCapsRespone = testee.checkCaps(indexerConfig);
assertEquals(5, checkCapsRespone.getIndexerConfig().getSupportedSearchIds().size());
assertFalse(checkCapsRespone.isAllCapsChecked());
verify(indexerWebAccess, times(7)).get(any(), eq(indexerConfig));
}
use of org.nzbhydra.indexers.exceptions.IndexerAccessException in project nzbhydra2 by theotherp.
the class IndexerWebAccess method get.
public <T> T get(URI uri, IndexerConfig indexerConfig, Class responseType) throws IndexerAccessException {
int timeout = indexerConfig.getTimeout().orElse(configProvider.getBaseConfig().getSearching().getTimeout());
String userAgent = indexerConfig.getUserAgent().orElse(configProvider.getBaseConfig().getSearching().getUserAgent().orElse("NZBHydra2"));
Map<String, String> headers = new HashMap<>();
headers.put("User-Agent", userAgent);
if (indexerConfig.getUsername().isPresent() && indexerConfig.getPassword().isPresent()) {
headers.put("Authorization", "Basic " + BaseEncoding.base64().encode((indexerConfig.getUsername().get() + ":" + indexerConfig.getPassword().get()).getBytes()));
}
Future<T> future;
ExecutorService executorService = Executors.newSingleThreadExecutor();
try {
future = executorService.submit(() -> {
String response = webAccess.callUrl(uri.toString(), headers, timeout);
if (responseType == String.class) {
return (T) response;
}
return (T) unmarshaller.unmarshal(new StreamSource(new StringReader(response)));
});
} catch (RejectedExecutionException e) {
logger.error("Unexpected execution exception while executing call for indexer " + indexerConfig.getName() + ". This will hopefully be fixed soon", e);
throw new IndexerProgramErrorException("Unexpected error in hydra code. Sorry...");
} finally {
executorService.shutdown();
}
try {
// Give it one second more than the actual timeout
return future.get(timeout + 1, TimeUnit.SECONDS);
} catch (ExecutionException e) {
if (e.getCause() instanceof SocketTimeoutException) {
throw new IndexerUnreachableException("Connection with indexer timed out with a time out of " + timeout + " seconds: " + e.getCause().getMessage());
}
throw new IndexerUnreachableException("Error while communicating with indexer " + indexerConfig.getName() + ". Server returned: " + e.getMessage(), e.getCause());
} catch (TimeoutException e) {
throw new IndexerAccessException("Indexer did not complete request within " + timeout + " seconds");
} catch (Exception e) {
throw new RuntimeException("Unexpected error while accessing indexer", e);
}
}
Aggregations