Search in sources :

Example 1 with IndexerAccessException

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

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());
    }
}
Also used : Matcher(java.util.regex.Matcher) IndexerAccessException(org.nzbhydra.indexers.exceptions.IndexerAccessException) URI(java.net.URI)

Example 3 with IndexerAccessException

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));
}
Also used : CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) IndexerAccessException(org.nzbhydra.indexers.exceptions.IndexerAccessException) Test(org.junit.Test)

Example 4 with IndexerAccessException

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));
}
Also used : NewznabResponseBuilder(org.nzbhydra.fortests.NewznabResponseBuilder) IndexerAccessException(org.nzbhydra.indexers.exceptions.IndexerAccessException) URI(java.net.URI) BinsearchTest(org.nzbhydra.indexers.BinsearchTest) Test(org.junit.Test)

Example 5 with IndexerAccessException

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);
    }
}
Also used : HashMap(java.util.HashMap) IndexerUnreachableException(org.nzbhydra.indexers.exceptions.IndexerUnreachableException) StreamSource(javax.xml.transform.stream.StreamSource) IndexerAccessException(org.nzbhydra.indexers.exceptions.IndexerAccessException) IndexerProgramErrorException(org.nzbhydra.indexers.exceptions.IndexerProgramErrorException) IndexerUnreachableException(org.nzbhydra.indexers.exceptions.IndexerUnreachableException) SocketTimeoutException(java.net.SocketTimeoutException) IndexerAccessException(org.nzbhydra.indexers.exceptions.IndexerAccessException) IndexerProgramErrorException(org.nzbhydra.indexers.exceptions.IndexerProgramErrorException) SocketTimeoutException(java.net.SocketTimeoutException) StringReader(java.io.StringReader) SocketTimeoutException(java.net.SocketTimeoutException)

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