Search in sources :

Example 1 with NzbAddingType

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

the class Downloader method addBySearchResultIds.

@Transactional
public AddNzbsResponse addBySearchResultIds(List<AddFilesRequest.SearchResult> searchResults, String category) {
    NzbAddingType addingType = downloaderConfig.getNzbAddingType();
    List<Long> addedNzbs = new ArrayList<>();
    try {
        for (AddFilesRequest.SearchResult entry : searchResults) {
            Long guid = Long.valueOf(entry.getSearchResultId());
            String categoryToSend;
            if (Strings.isNullOrEmpty(category) && !"N/A".equals(entry.getOriginalCategory())) {
                categoryToSend = entry.getOriginalCategory();
            } else {
                categoryToSend = category;
            }
            if (addingType == NzbAddingType.UPLOAD) {
                // Uploading NZBs can only be done via proxying
                DownloadResult result = nzbHandler.getFileByGuid(guid, FileDownloadAccessType.PROXY, SearchSource.INTERNAL);
                String externalId = addNzb(result.getContent(), result.getTitle(), categoryToSend);
                result.getDownloadEntity().setExternalId(externalId);
                nzbHandler.updateStatusByEntity(result.getDownloadEntity(), FileDownloadStatus.NZB_ADDED);
            } else {
                SearchResultEntity searchResultEntity = searchResultRepository.getOne(guid);
                addLink(nzbHandler.getDownloadLink(guid, false, DownloadType.NZB), searchResultEntity.getTitle(), categoryToSend);
            }
            addedNzbs.add(guid);
        }
    } catch (InvalidSearchResultIdException | DownloaderException | EntityNotFoundException e) {
        String message;
        if (e instanceof DownloaderException) {
            message = "Error while adding NZB(s) to downloader: " + e.getMessage();
        } else if (e instanceof EntityNotFoundException) {
            message = "Unable to find the search result in the database. Unable to download";
        } else {
            message = e.getMessage();
        }
        logger.error(message);
        if (!addedNzbs.isEmpty()) {
            message += ".\n" + addedNzbs.size() + " were added successfully";
        }
        Set<Long> searchResultIds = Sets.newHashSet(searchResults.stream().map(x -> Long.valueOf(x.getSearchResultId())).collect(Collectors.toSet()));
        searchResultIds.removeAll(addedNzbs);
        return new AddNzbsResponse(false, message, addedNzbs, searchResultIds);
    }
    return new AddNzbsResponse(true, null, addedNzbs, Collections.emptyList());
}
Also used : Iterables(com.google.common.collect.Iterables) java.util(java.util) Stopwatch(com.google.common.base.Stopwatch) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) DownloaderException(org.nzbhydra.downloading.exceptions.DownloaderException) Strings(com.google.common.base.Strings) GenericResponse(org.nzbhydra.GenericResponse) SearchSource(org.nzbhydra.searching.searchrequests.SearchRequest.SearchSource) EntityNotFoundException(javax.persistence.EntityNotFoundException) NzbAddingType(org.nzbhydra.config.NzbAddingType) SearchResultEntity(org.nzbhydra.searching.SearchResultEntity) Logger(org.slf4j.Logger) SearchResultRepository(org.nzbhydra.searching.SearchResultRepository) MoreObjects(com.google.common.base.MoreObjects) Instant(java.time.Instant) DownloadType(org.nzbhydra.searching.SearchResultItem.DownloadType) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) TimeUnit(java.util.concurrent.TimeUnit) FileDownloadAccessType(org.nzbhydra.config.FileDownloadAccessType) org.nzbhydra.downloading(org.nzbhydra.downloading) Component(org.springframework.stereotype.Component) Data(lombok.Data) DownloaderConfig(org.nzbhydra.config.DownloaderConfig) AllArgsConstructor(lombok.AllArgsConstructor) LoggingMarkers(org.nzbhydra.logging.LoggingMarkers) NoArgsConstructor(lombok.NoArgsConstructor) Transactional(org.springframework.transaction.annotation.Transactional) DownloaderException(org.nzbhydra.downloading.exceptions.DownloaderException) EntityNotFoundException(javax.persistence.EntityNotFoundException) SearchResultEntity(org.nzbhydra.searching.SearchResultEntity) NzbAddingType(org.nzbhydra.config.NzbAddingType) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

MoreObjects (com.google.common.base.MoreObjects)1 Stopwatch (com.google.common.base.Stopwatch)1 Strings (com.google.common.base.Strings)1 Iterables (com.google.common.collect.Iterables)1 Sets (com.google.common.collect.Sets)1 Instant (java.time.Instant)1 java.util (java.util)1 TimeUnit (java.util.concurrent.TimeUnit)1 Collectors (java.util.stream.Collectors)1 EntityNotFoundException (javax.persistence.EntityNotFoundException)1 AllArgsConstructor (lombok.AllArgsConstructor)1 Data (lombok.Data)1 NoArgsConstructor (lombok.NoArgsConstructor)1 GenericResponse (org.nzbhydra.GenericResponse)1 DownloaderConfig (org.nzbhydra.config.DownloaderConfig)1 FileDownloadAccessType (org.nzbhydra.config.FileDownloadAccessType)1 NzbAddingType (org.nzbhydra.config.NzbAddingType)1 org.nzbhydra.downloading (org.nzbhydra.downloading)1 DownloaderException (org.nzbhydra.downloading.exceptions.DownloaderException)1 LoggingMarkers (org.nzbhydra.logging.LoggingMarkers)1