use of org.nzbhydra.mapping.newznab.NewznabResponse in project nzbhydra2 by theotherp.
the class ExternalApi method transformResults.
protected NewznabResponse transformResults(SearchResult searchResult, NewznabParameters params, SearchRequest searchRequest) {
Stopwatch stopwatch = Stopwatch.createStarted();
NewznabResponse response;
int total = searchResult.getNumberOfTotalAvailableResults() - searchResult.getNumberOfRejectedResults() - searchResult.getNumberOfRemovedDuplicates();
if (params.getO() == OutputType.JSON) {
response = newznabJsonTransformer.transformToRoot(searchResult.getSearchResultItems(), params.getOffset(), total, searchRequest);
} else {
response = newznabXmlTransformer.getRssRoot(searchResult.getSearchResultItems(), params.getOffset(), total, searchRequest);
}
logger.debug(LoggingMarkers.PERFORMANCE, "Transforming results took {}ms", stopwatch.elapsed(TimeUnit.MILLISECONDS));
return response;
}
use of org.nzbhydra.mapping.newznab.NewznabResponse in project nzbhydra2 by theotherp.
the class ExternalApi method handleCachingSearch.
protected ResponseEntity<?> handleCachingSearch(NewznabParameters params) {
// Remove old entries
cache.entrySet().removeIf(x -> x.getValue().getLastUpdate().isBefore(clock.instant().minus(MAX_CACHE_AGE_HOURS, ChronoUnit.HOURS)));
CacheEntryValue cacheEntryValue;
if (cache.containsKey(params.cacheKey())) {
cacheEntryValue = cache.get(params.cacheKey());
if (cacheEntryValue.getLastUpdate().isAfter(clock.instant().minus(params.getCachetime(), ChronoUnit.MINUTES))) {
Instant nextUpdate = cacheEntryValue.getLastUpdate().plus(params.getCachetime(), ChronoUnit.MINUTES);
logger.info("Returning cached search result. Next update of search will be done at {}", nextUpdate);
return new ResponseEntity<>(cacheEntryValue.getSearchResult(), HttpStatus.OK);
} else {
logger.info("Updating search because cache time is exceeded");
}
}
// Remove oldest entry when max size is reached
if (cache.size() == MAX_CACHE_SIZE) {
Optional<Entry<Integer, CacheEntryValue>> keyToEvict = cache.entrySet().stream().min(Comparator.comparing(o -> o.getValue().getLastUpdate()));
// Should always be the case anyway
logger.info("Removing oldest entry from cache because its limit of {} is reached", MAX_CACHE_SIZE);
keyToEvict.ifPresent(newznabParametersCacheEntryValueEntry -> cache.remove(newznabParametersCacheEntryValueEntry.getKey()));
}
NewznabResponse searchResult = search(params);
logger.info("Putting search result into cache");
cache.put(params.cacheKey(), new CacheEntryValue(params, clock.instant(), searchResult));
return new ResponseEntity<>(searchResult, HttpStatus.OK);
}
use of org.nzbhydra.mapping.newznab.NewznabResponse in project nzbhydra2 by theotherp.
the class ExternalApi method search.
protected NewznabResponse search(NewznabParameters params) {
Stopwatch stopwatch = Stopwatch.createStarted();
SearchRequest searchRequest = buildBaseSearchRequest(params);
if (isTorznabCall()) {
searchRequest.setDownloadType(org.nzbhydra.searching.DownloadType.TORRENT);
} else {
searchRequest.setDownloadType(org.nzbhydra.searching.DownloadType.NZB);
}
SearchResult searchResult = searcher.search(searchRequest);
NewznabResponse transformedResults = transformResults(searchResult, params, searchRequest);
logger.info("Search took {}ms. Returning {} results", stopwatch.elapsed(TimeUnit.MILLISECONDS), searchResult.getSearchResultItems().size());
return transformedResults;
}
use of org.nzbhydra.mapping.newznab.NewznabResponse in project nzbhydra2 by theotherp.
the class ExternalApi method api.
@RequestMapping(value = { "/api", "/rss", "/torznab/api" }, consumes = MediaType.ALL_VALUE)
public ResponseEntity<? extends Object> api(NewznabParameters params) throws Exception {
logger.info("Received external {}API call: {}", (isTorznabCall() ? "torznab " : ""), params);
if (!noApiKeyNeeded && !Objects.equals(params.getApikey(), configProvider.getBaseConfig().getMain().getApiKey())) {
logger.error("Received API call with wrong API key");
throw new WrongApiKeyException("Wrong api key");
}
if (Stream.of(ActionAttribute.SEARCH, ActionAttribute.BOOK, ActionAttribute.TVSEARCH, ActionAttribute.MOVIE).anyMatch(x -> x == params.getT())) {
if (params.getCachetime() != null) {
return handleCachingSearch(params);
}
NewznabResponse searchResult = search(params);
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.set(HttpHeaders.CONTENT_TYPE, searchResult.getContentHeader());
if (params.getO() != OutputType.JSON) {
searchResult.setSearchType(isTorznabCall() ? "torznab" : "newznab");
}
return new ResponseEntity<>(searchResult, httpHeaders, HttpStatus.OK);
}
if (params.getT() == ActionAttribute.GET) {
return getNzb(params);
}
if (params.getT() == ActionAttribute.CAPS) {
return getCaps();
}
logger.error("Incorrect API request: {}", params);
NewznabXmlError error = new NewznabXmlError("200", "Unknown or incorrect parameter");
return new ResponseEntity<Object>(error, HttpStatus.OK);
}
Aggregations