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