Search in sources :

Example 1 with IdType

use of org.nzbhydra.mediainfo.InfoProvider.IdType in project nzbhydra2 by theotherp.

the class Newznab method extendQueryUrlWithSearchIds.

protected UriComponentsBuilder extendQueryUrlWithSearchIds(SearchRequest searchRequest, UriComponentsBuilder componentsBuilder) throws IndexerSearchAbortedException {
    if (!searchRequest.getIdentifiers().isEmpty()) {
        Map<IdType, String> params = new HashMap<>();
        boolean indexerSupportsAnyOfTheProvidedIds = searchRequest.getIdentifiers().keySet().stream().anyMatch(x -> config.getSupportedSearchIds().contains(x));
        if (!indexerSupportsAnyOfTheProvidedIds) {
            boolean canConvertAnyId = infoProvider.canConvertAny(searchRequest.getIdentifiers().keySet(), new HashSet<>(config.getSupportedSearchIds()));
            if (canConvertAnyId) {
                try {
                    MediaInfo info = infoProvider.convert(searchRequest.getIdentifiers());
                    if (info.getImdbId().isPresent()) {
                        params.put(IdType.IMDB, info.getImdbId().get().replace("tt", ""));
                    }
                    if (info.getTmdbId().isPresent()) {
                        params.put(IdType.TMDB, info.getTmdbId().get());
                    }
                    if (info.getTvRageId().isPresent()) {
                        params.put(IdType.TVRAGE, info.getTvRageId().get());
                    }
                    if (info.getTvMazeId().isPresent()) {
                        params.put(IdType.TVMAZE, info.getTvMazeId().get());
                    }
                    if (info.getTvDbId().isPresent()) {
                        params.put(IdType.TVDB, info.getTvDbId().get());
                    }
                } catch (InfoProviderException e) {
                    error("Error while converting search ID", e);
                }
            }
        }
        searchRequest.getIdentifiers().putAll(params);
        for (Map.Entry<IdType, String> entry : searchRequest.getIdentifiers().entrySet()) {
            // We just add all IDs that we have. Some indexers support more than they say or will find results under one ID but not the other
            if (entry.getValue() == null) {
                continue;
            }
            componentsBuilder.queryParam(idTypeToParamValueMap.get(entry.getKey()), entry.getValue().replace("tt", ""));
        }
    }
    return componentsBuilder;
}
Also used : MediaInfo(org.nzbhydra.mediainfo.MediaInfo) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) InfoProviderException(org.nzbhydra.mediainfo.InfoProviderException) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) IdType(org.nzbhydra.mediainfo.InfoProvider.IdType)

Example 2 with IdType

use of org.nzbhydra.mediainfo.InfoProvider.IdType in project nzbhydra2 by theotherp.

the class IndexerTest method shouldUseFallback.

@Test
public void shouldUseFallback() throws Exception {
    SearchRequest searchRequest = new SearchRequest(SearchSource.INTERNAL, SearchType.SEARCH, 0, 100);
    Map<IdType, String> identifiers = new HashMap<>();
    identifiers.put(IdType.IMDB, "123");
    searchRequest.setIdentifiers(identifiers);
    SearchResultItem item = new SearchResultItem();
    item.setIndexer(indexerMock);
    item.setIndexerGuid("indexerGuid");
    // searchResultItemsToReturn = Arrays.asList(item);
    IndexerSearchResult search = testee.search(searchRequest, 0, 100);
    verify(testee, times(2)).searchInternal(searchRequestCaptor.capture(), anyInt(), anyInt());
}
Also used : SearchRequest(org.nzbhydra.searching.searchrequests.SearchRequest) HashMap(java.util.HashMap) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) IdType(org.nzbhydra.mediainfo.InfoProvider.IdType) Test(org.junit.Test)

Example 3 with IdType

use of org.nzbhydra.mediainfo.InfoProvider.IdType in project nzbhydra2 by theotherp.

the class IndexerTest method shouldGenerateQuery.

@Test
public void shouldGenerateQuery() throws IndexerSearchAbortedException {
    baseConfig.getSearching().setGenerateQueries(SearchSourceRestriction.BOTH);
    SearchRequest searchRequest = new SearchRequest(SearchSource.INTERNAL, SearchType.SEARCH, 0, 100);
    Map<IdType, String> identifiers = new HashMap<>();
    identifiers.put(IdType.IMDB, "123");
    searchRequest.setIdentifiers(identifiers);
    String query = testee.generateQueryIfApplicable(searchRequest, "query");
    assertThat(query, is("title"));
}
Also used : SearchRequest(org.nzbhydra.searching.searchrequests.SearchRequest) HashMap(java.util.HashMap) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) IdType(org.nzbhydra.mediainfo.InfoProvider.IdType) Test(org.junit.Test)

Example 4 with IdType

use of org.nzbhydra.mediainfo.InfoProvider.IdType in project nzbhydra2 by theotherp.

the class IndexerTest method shouldSanitizeQuery.

@Test
public void shouldSanitizeQuery() throws IndexerSearchAbortedException, InfoProviderException {
    when(infoProviderMock.convert(anyString(), any())).thenReturn(new MediaInfo(new TvInfo("tvdbid", "tvrageid", "tvmazeid", "title()':", 2017, "")));
    baseConfig.getSearching().setGenerateQueries(SearchSourceRestriction.BOTH);
    SearchRequest searchRequest = new SearchRequest(SearchSource.INTERNAL, SearchType.SEARCH, 0, 100);
    Map<IdType, String> identifiers = new HashMap<>();
    identifiers.put(IdType.IMDB, "123");
    searchRequest.setIdentifiers(identifiers);
    String query = testee.generateQueryIfApplicable(searchRequest, "query");
    assertThat(query, is("title"));
}
Also used : SearchRequest(org.nzbhydra.searching.searchrequests.SearchRequest) MediaInfo(org.nzbhydra.mediainfo.MediaInfo) HashMap(java.util.HashMap) TvInfo(org.nzbhydra.mediainfo.TvInfo) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) IdType(org.nzbhydra.mediainfo.InfoProvider.IdType) Test(org.junit.Test)

Example 5 with IdType

use of org.nzbhydra.mediainfo.InfoProvider.IdType in project nzbhydra2 by theotherp.

the class SqliteMigration method migrateSearches.

protected Map<Integer, SearchEntity> migrateSearches() throws SQLException {
    Map<Integer, SearchEntity> oldIdToNewEntity = new HashMap<>();
    Map<String, String> categoryMap = categoryProvider.getCategories().stream().map(Category::getName).collect(Collectors.toMap(x -> x.replace(" ", "").toLowerCase(), Function.identity()));
    Map<String, SearchType> oldTypeToNewMap = new HashMap<>();
    oldTypeToNewMap.put("general", SearchType.SEARCH);
    oldTypeToNewMap.put("book", SearchType.BOOK);
    oldTypeToNewMap.put("movie", SearchType.MOVIE);
    oldTypeToNewMap.put("tv", SearchType.TVSEARCH);
    oldTypeToNewMap.put("audio", SearchType.MUSIC);
    Map<String, IdType> oldIdTypeToNewMap = new HashMap<>();
    oldIdTypeToNewMap.put("rid", IdType.TVRAGE);
    oldIdTypeToNewMap.put("tvdbid", IdType.TVDB);
    oldIdTypeToNewMap.put("imdbid", IdType.IMDB);
    oldIdTypeToNewMap.put("tmdbid", IdType.TMDB);
    Statement statement = connection.createStatement();
    int searches = getCount(statement, "search");
    logger.info("Migrating {} searches from old database", searches);
    eventPublisher.publishEvent(new MigrationMessageEvent("Migrating " + searches + " search entries"));
    ResultSet oldSearches = statement.executeQuery("SELECT * FROM search");
    boolean hasAuthor = hasColumn(oldSearches, "author");
    boolean hasTitle = hasColumn(oldSearches, "title");
    int skippedSearches = 0;
    while (oldSearches.next()) {
        try {
            SearchEntity entity = new SearchEntity();
            String oldCategory = oldSearches.getString("category");
            String newCategory = (!Strings.isNullOrEmpty(oldCategory) && categoryMap.containsKey(oldCategory.toLowerCase())) ? categoryMap.get(oldCategory.toLowerCase()) : "All";
            entity.setCategoryName(newCategory);
            entity.setUsername(oldSearches.getString("username"));
            entity.setSeason(oldSearches.getObject("season") != null ? oldSearches.getInt("season") : null);
            entity.setEpisode(oldSearches.getString("episode"));
            entity.setQuery(oldSearches.getString("query"));
            if (hasAuthor) {
                entity.setAuthor(oldSearches.getString("author"));
            }
            if (hasTitle) {
                entity.setTitle(oldSearches.getString("title"));
            }
            entity.setSearchType(oldTypeToNewMap.getOrDefault(oldSearches.getString("type"), SearchType.SEARCH));
            if (oldSearches.getString("identifier_key") != null && oldSearches.getString("identifier_value") != null && oldIdTypeToNewMap.containsKey(oldSearches.getString("identifier_key"))) {
                String identifierKey = oldIdTypeToNewMap.get(oldSearches.getString("identifier_key")).name();
                IdentifierKeyValuePair keyValuePair = new IdentifierKeyValuePair(identifierKey, oldSearches.getString("identifier_value"));
                entity.setIdentifiers(Sets.newHashSet(keyValuePair));
            }
            entity.setSource((oldSearches.getBoolean("internal")) ? SearchSource.INTERNAL : SearchSource.API);
            Instant time;
            String timeString = oldSearches.getString("time");
            time = timestampToInstant(timeString);
            entity.setTime(time);
            oldIdToNewEntity.put(oldSearches.getInt("id"), entity);
        } catch (SQLException e) {
            logger.error("Problem while migrating search", e);
            skippedSearches++;
        }
    }
    logger.info("Saving search entities to database");
    searchRepository.save(oldIdToNewEntity.values());
    if (skippedSearches > 0) {
        String message = "Skipped " + skippedSearches + " of " + (skippedSearches + oldIdToNewEntity.size()) + " searches because the database entries could not be read";
        logger.warn(message);
        eventPublisher.publishEvent(new MigrationMessageEvent(message));
    } else {
        logger.info("Successfully migrated searches from old database");
        eventPublisher.publishEvent(new MigrationMessageEvent("Successfully migrated searches from old database"));
    }
    return oldIdToNewEntity;
}
Also used : java.sql(java.sql) java.util(java.util) org.nzbhydra.searching(org.nzbhydra.searching) Strings(joptsimple.internal.Strings) LoggerFactory(org.slf4j.LoggerFactory) LocalDateTime(java.time.LocalDateTime) Autowired(org.springframework.beans.factory.annotation.Autowired) Function(java.util.function.Function) FileDownloadEntity(org.nzbhydra.downloading.FileDownloadEntity) SearchSource(org.nzbhydra.searching.searchrequests.SearchRequest.SearchSource) FileDownloadStatus(org.nzbhydra.downloading.FileDownloadStatus) ApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher) ZoneOffset(java.time.ZoneOffset) TypeReference(com.fasterxml.jackson.core.type.TypeReference) Category(org.nzbhydra.config.Category) Logger(org.slf4j.Logger) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) IdType(org.nzbhydra.mediainfo.InfoProvider.IdType) IOException(java.io.IOException) EntityManager(javax.persistence.EntityManager) PersistenceContext(javax.persistence.PersistenceContext) org.nzbhydra.indexers(org.nzbhydra.indexers) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) TimeUnit(java.util.concurrent.TimeUnit) FileDownloadAccessType(org.nzbhydra.config.FileDownloadAccessType) Component(org.springframework.stereotype.Component) ChronoUnit(java.time.temporal.ChronoUnit) ProgressLogger(org.nzbhydra.logging.ProgressLogger) DateTimeFormatter(java.time.format.DateTimeFormatter) MigrationMessageEvent(org.nzbhydra.migration.FromPythonMigration.MigrationMessageEvent) FileDownloadRepository(org.nzbhydra.downloading.FileDownloadRepository) Transactional(org.springframework.transaction.annotation.Transactional) Instant(java.time.Instant) MigrationMessageEvent(org.nzbhydra.migration.FromPythonMigration.MigrationMessageEvent) IdType(org.nzbhydra.mediainfo.InfoProvider.IdType)

Aggregations

IdType (org.nzbhydra.mediainfo.InfoProvider.IdType)7 HashMap (java.util.HashMap)3 Test (org.junit.Test)3 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)3 SearchRequest (org.nzbhydra.searching.searchrequests.SearchRequest)3 MediaInfo (org.nzbhydra.mediainfo.MediaInfo)2 TypeReference (com.fasterxml.jackson.core.type.TypeReference)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Sets (com.google.common.collect.Sets)1 IOException (java.io.IOException)1 URI (java.net.URI)1 java.sql (java.sql)1 Instant (java.time.Instant)1 LocalDateTime (java.time.LocalDateTime)1 ZoneOffset (java.time.ZoneOffset)1 DateTimeFormatter (java.time.format.DateTimeFormatter)1 ChronoUnit (java.time.temporal.ChronoUnit)1 java.util (java.util)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 TimeUnit (java.util.concurrent.TimeUnit)1