Search in sources :

Example 6 with IndexerConfig

use of org.nzbhydra.config.IndexerConfig in project nzbhydra2 by theotherp.

the class SearchModuleProvider method loadIndexers.

/**
 * Must be called by <tt>{@link SearchModuleConfigProvider}</tt> when config is loaded.
 */
@Transactional
public void loadIndexers(List<IndexerConfig> indexers) {
    if (indexers == null) {
        logger.error("Indexers not set. Check your configuration");
        return;
    }
    logger.info("Loading indexers");
    searchModuleInstances.clear();
    for (IndexerConfig config : indexers) {
        try {
            Optional<IndexerHandlingStrategy> optionalStrategy = indexerHandlingStrategies.stream().filter(x -> x.handlesIndexerConfig(config)).findFirst();
            if (!optionalStrategy.isPresent()) {
                logger.error("Unable to find implementation for indexer type {} and host {}", config.getSearchModuleType(), config.getHost());
                continue;
            }
            Indexer searchModule = beanFactory.createBean(optionalStrategy.get().getIndexerClass());
            logger.info("Initializing indexer {}", config.getName());
            IndexerEntity indexerEntity = indexerRepository.findByName(config.getName());
            if (indexerEntity == null) {
                logger.info("Indexer with name {} not yet in database. Adding it", config.getName());
                indexerEntity = new IndexerEntity();
                indexerEntity.setName(config.getName());
                indexerEntity = indexerRepository.save(indexerEntity);
                logger.info("Now {} indexers in database", indexerRepository.count());
            }
            searchModule.initialize(config, indexerEntity);
            searchModuleInstances.put(config.getName(), searchModule);
        } catch (Exception e) {
            logger.error("Unable to instantiate indexer with name {} and type {}", config.getName(), config.getSearchModuleType(), e);
        }
    }
    List<String> indexerNames = indexers.stream().map(IndexerConfig::getName).collect(Collectors.toList());
    Collection<IndexerEntity> byNameNotIn = indexerRepository.findByNameNotIn(indexerNames);
    if (!byNameNotIn.isEmpty()) {
        logger.info("Found {} indexers in database which are not configured. Will delete them and any related database entries. This may take some time", byNameNotIn.size());
        indexerRepository.delete(byNameNotIn);
    }
    if (searchModuleInstances.isEmpty()) {
        logger.warn("No indexers configured");
    }
}
Also used : Component(org.springframework.stereotype.Component) java.util(java.util) Logger(org.slf4j.Logger) IndexerConfig(org.nzbhydra.config.IndexerConfig) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) AutowireCapableBeanFactory(org.springframework.beans.factory.config.AutowireCapableBeanFactory) org.nzbhydra.indexers(org.nzbhydra.indexers) Collectors(java.util.stream.Collectors) Transactional(org.springframework.transaction.annotation.Transactional) IndexerConfig(org.nzbhydra.config.IndexerConfig) Transactional(org.springframework.transaction.annotation.Transactional)

Example 7 with IndexerConfig

use of org.nzbhydra.config.IndexerConfig in project nzbhydra2 by theotherp.

the class Experiments method createSimpleYaml.

@Test
@Ignore
public void createSimpleYaml() throws IOException, InterruptedException {
    ObjectMapper objectMapper = new ObjectMapper(new YAMLFactory());
    objectMapper.registerModule(new Jdk8Module());
    IndexerConfig indexerConfig = new IndexerConfig();
    indexerConfig.setCategoryMapping(new IndexerCategoryConfig());
    BaseConfig baseConfig = new BaseConfig();
    baseConfig.setIndexers(Arrays.asList(indexerConfig));
    String s = objectMapper.writeValueAsString(baseConfig);
    System.out.println(s);
    objectMapper.readValue(s, BaseConfig.class);
}
Also used : Jdk8Module(com.fasterxml.jackson.datatype.jdk8.Jdk8Module) IndexerConfig(org.nzbhydra.config.IndexerConfig) YAMLFactory(com.fasterxml.jackson.dataformat.yaml.YAMLFactory) IndexerCategoryConfig(org.nzbhydra.config.IndexerCategoryConfig) BaseConfig(org.nzbhydra.config.BaseConfig) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 8 with IndexerConfig

use of org.nzbhydra.config.IndexerConfig in project nzbhydra2 by theotherp.

the class IndexerForSearchSelector method checkIndexerHitLimit.

protected boolean checkIndexerHitLimit(Indexer indexer) {
    Stopwatch stopwatch = Stopwatch.createStarted();
    IndexerConfig indexerConfig = indexer.getConfig();
    if (!indexerConfig.getHitLimit().isPresent() && !indexerConfig.getDownloadLimit().isPresent()) {
        return true;
    }
    LocalDateTime comparisonTime;
    LocalDateTime now = LocalDateTime.now(clock);
    if (indexerConfig.getHitLimitResetTime().isPresent()) {
        comparisonTime = now.with(ChronoField.HOUR_OF_DAY, indexerConfig.getHitLimitResetTime().get());
        if (comparisonTime.isAfter(now)) {
            comparisonTime = comparisonTime.minus(1, ChronoUnit.DAYS);
        }
    } else {
        comparisonTime = now.minus(1, ChronoUnit.DAYS);
    }
    if (indexerConfig.getHitLimit().isPresent()) {
        Query query = entityManager.createNativeQuery("SELECT x.TIME FROM INDEXERAPIACCESS_SHORT x WHERE x.INDEXER_ID = (:indexerId) ORDER BY TIME DESC LIMIT (:hitLimit)");
        query.setParameter("indexerId", indexer.getIndexerEntity().getId());
        query.setParameter("hitLimit", indexerConfig.getHitLimit().get());
        List resultList = query.getResultList();
        if (resultList.size() == indexerConfig.getHitLimit().get()) {
            // Found as many as we want, so now we must check if they're all in the time window
            Instant earliestAccess = ((Timestamp) Iterables.getLast(resultList)).toInstant();
            if (earliestAccess.isAfter(comparisonTime.toInstant(ZoneOffset.UTC))) {
                LocalDateTime nextPossibleHit = calculateNextPossibleHit(indexerConfig, earliestAccess);
                String message = String.format("Not using %s because all %d allowed API hits were already made. The next API hit should be possible at %s", indexerConfig.getName(), indexerConfig.getHitLimit().get(), nextPossibleHit);
                logger.debug(LoggingMarkers.PERFORMANCE, "Detection of API limit reached took {}ms", stopwatch.elapsed(TimeUnit.MILLISECONDS));
                return handleIndexerNotSelected(indexer, message, "API hit limit reached");
            }
        }
    }
    if (indexerConfig.getDownloadLimit().isPresent()) {
        Page<FileDownloadEntity> page = nzbDownloadRepository.findBySearchResultIndexerOrderByTimeDesc(indexer.getIndexerEntity(), new PageRequest(0, indexerConfig.getDownloadLimit().get()));
        if (page.getContent().size() == indexerConfig.getDownloadLimit().get() && Iterables.getLast(page.getContent()).getTime().isAfter(comparisonTime.toInstant(ZoneOffset.UTC))) {
            LocalDateTime nextPossibleHit = calculateNextPossibleHit(indexerConfig, page.getContent().get(page.getContent().size() - 1).getTime());
            String message = String.format("Not using %s because all %d allowed download were already made. The next download should be possible at %s", indexerConfig.getName(), indexerConfig.getDownloadLimit().get(), nextPossibleHit);
            logger.debug(LoggingMarkers.PERFORMANCE, "Detection of download limit reached took {}ms", stopwatch.elapsed(TimeUnit.MILLISECONDS));
            return handleIndexerNotSelected(indexer, message, "Download limit reached");
        }
    }
    logger.debug(LoggingMarkers.PERFORMANCE, "Detection if hit limits were reached for indexer {} took {}ms", indexer.getName(), stopwatch.elapsed(TimeUnit.MILLISECONDS));
    return true;
}
Also used : PageRequest(org.springframework.data.domain.PageRequest) Query(javax.persistence.Query) IndexerConfig(org.nzbhydra.config.IndexerConfig) Stopwatch(com.google.common.base.Stopwatch) Timestamp(java.sql.Timestamp) FileDownloadEntity(org.nzbhydra.downloading.FileDownloadEntity)

Example 9 with IndexerConfig

use of org.nzbhydra.config.IndexerConfig in project nzbhydra2 by theotherp.

the class SslTest method shouldBeAbleToAccessSSL.

@Test
public void shouldBeAbleToAccessSSL() throws Exception {
    configProvider.getBaseConfig().getMain().setVerifySsl(false);
    IndexerConfig config = new IndexerConfig();
    config.setTimeout(99999);
    binsearch.initialize(config, new IndexerEntity());
    // Would not work with SNI disabled
    binsearch.callInderWebAccess(URI.create("https://binsearch.info"), String.class);
    nzbGeek.initialize(config, new IndexerEntity());
    // Would not work with SNI enabled
    nzbGeek.callInderWebAccess(URI.create("https://nzbgeek.info/"), String.class);
}
Also used : IndexerConfig(org.nzbhydra.config.IndexerConfig) DataJpaTest(org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) Test(org.junit.Test)

Example 10 with IndexerConfig

use of org.nzbhydra.config.IndexerConfig in project nzbhydra2 by theotherp.

the class AnizbTest method setUp.

@Before
public void setUp() {
    MockitoAnnotations.initMocks(this);
    when(configProviderMock.getBaseConfig()).thenReturn(baseConfig);
    testee.config = new IndexerConfig();
    testee.config.setName("anizb");
    testee.config.setHost("https://anizb.org");
}
Also used : IndexerConfig(org.nzbhydra.config.IndexerConfig) Before(org.junit.Before)

Aggregations

IndexerConfig (org.nzbhydra.config.IndexerConfig)11 Before (org.junit.Before)4 java.util (java.util)2 Collectors (java.util.stream.Collectors)2 Test (org.junit.Test)2 IndexerCategoryConfig (org.nzbhydra.config.IndexerCategoryConfig)2 Newznab (org.nzbhydra.indexers.Newznab)2 Logger (org.slf4j.Logger)2 LoggerFactory (org.slf4j.LoggerFactory)2 Autowired (org.springframework.beans.factory.annotation.Autowired)2 Component (org.springframework.stereotype.Component)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 YAMLFactory (com.fasterxml.jackson.dataformat.yaml.YAMLFactory)1 Jdk8Module (com.fasterxml.jackson.datatype.jdk8.Jdk8Module)1 Stopwatch (com.google.common.base.Stopwatch)1 Strings (com.google.common.base.Strings)1 URI (java.net.URI)1 Timestamp (java.sql.Timestamp)1 java.util.concurrent (java.util.concurrent)1 Query (javax.persistence.Query)1