Search in sources :

Example 1 with SearchResultEntity

use of org.nzbhydra.searching.SearchResultEntity 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)

Example 2 with SearchResultEntity

use of org.nzbhydra.searching.SearchResultEntity in project nzbhydra2 by theotherp.

the class TorrentFileHandler method getTorrentByGuid.

public DownloadResult getTorrentByGuid(long guid, FileDownloadAccessType accessType, SearchRequest.SearchSource accessSource) throws InvalidSearchResultIdException {
    // Get result. if link contains magnet: return redirect to magnet URI. otherwise return file
    SearchResultEntity result = searchResultRepository.findOne(guid);
    if (result == null) {
        logger.error("Download request with invalid/outdated GUID {}", guid);
        throw new InvalidSearchResultIdException(guid, accessSource == SearchRequest.SearchSource.INTERNAL);
    }
    logger.info("Download request for \"{}\" from indexer {}", result.getTitle(), result.getIndexer().getName());
    if (result.getLink().contains("magnet:") || accessType == FileDownloadAccessType.REDIRECT) {
        return fileHandler.handleRedirect(accessSource, result);
    } else {
        return fileHandler.handleContentDownload(accessSource, result, "torrent");
    }
}
Also used : InvalidSearchResultIdException(org.nzbhydra.downloading.InvalidSearchResultIdException) SearchResultEntity(org.nzbhydra.searching.SearchResultEntity)

Example 3 with SearchResultEntity

use of org.nzbhydra.searching.SearchResultEntity in project nzbhydra2 by theotherp.

the class DownloadResultTest method shouldBuildFilenameCorrectly.

@Test
public void shouldBuildFilenameCorrectly() {
    SearchResultEntity searchResultEntity = new SearchResultEntity();
    searchResultEntity.setDownloadType(SearchResultItem.DownloadType.NZB);
    FileDownloadEntity nzbDownloadEntity = new FileDownloadEntity();
    nzbDownloadEntity.setSearchResult(searchResultEntity);
    DownloadResult testee = DownloadResult.createSuccessfulDownloadResult("title", "content".getBytes(), nzbDownloadEntity);
    assertThat(testee.getAsResponseEntity().getHeaders().get(HttpHeaders.CONTENT_DISPOSITION)).containsExactly("attachment; filename=title.nzb");
    searchResultEntity.setDownloadType(SearchResultItem.DownloadType.TORRENT);
    testee = DownloadResult.createSuccessfulDownloadResult("title", "content".getBytes(), nzbDownloadEntity);
    assertThat(testee.getAsResponseEntity().getHeaders().get(HttpHeaders.CONTENT_DISPOSITION)).containsExactly("attachment; filename=title.torrent");
}
Also used : SearchResultEntity(org.nzbhydra.searching.SearchResultEntity) Test(org.junit.Test)

Example 4 with SearchResultEntity

use of org.nzbhydra.searching.SearchResultEntity in project nzbhydra2 by theotherp.

the class SearchResultIdCalculatorTest method shouldCalculateSameSearchResultId.

@Test
public void shouldCalculateSameSearchResultId() throws Exception {
    SearchResultEntity searchResultEntity = new SearchResultEntity();
    IndexerEntity indexerEntity = new IndexerEntity();
    indexerEntity.setName("indexerName");
    indexerEntity = indexerRepository.save(indexerEntity);
    searchResultEntity.setIndexer(indexerEntity);
    searchResultEntity.setIndexerGuid("indexerGuid");
    searchResultEntity.setTitle("title");
    searchResultEntity = searchResultRepository.save(searchResultEntity);
    assertEquals(-2991137394797183212L, SearchResultIdCalculator.calculateSearchResultId(searchResultEntity));
    assertEquals(-2991137394797183212L, searchResultEntity.getId());
    assertEquals(searchResultEntity, searchResultRepository.findOne(-2991137394797183212L));
}
Also used : IndexerEntity(org.nzbhydra.indexers.IndexerEntity) SearchResultEntity(org.nzbhydra.searching.SearchResultEntity) Test(org.junit.Test) DataJpaTest(org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 5 with SearchResultEntity

use of org.nzbhydra.searching.SearchResultEntity in project nzbhydra2 by theotherp.

the class NzbDownloadingTests method setup.

@Before
public void setup() throws Exception {
    System.setProperty("nzbhydra.dev.noApiKey", "true");
    System.setProperty("server.host", "127.0.0.1");
    mockServer = startClientAndServer(7070);
    proxy = startClientAndProxy(7072);
    mvc = MockMvcBuilders.webAppContextSetup(context).apply(SecurityMockMvcConfigurers.springSecurity()).build();
    IndexerEntity indexer = new IndexerEntity();
    indexer.setName("indexer");
    indexerRepository.save(indexer);
    searchResultRepository.deleteAll();
    SearchResultEntity searchResult = new SearchResultEntity();
    searchResult.setIndexer(indexer);
    searchResult.setIndexerGuid("someNzbd");
    searchResult.setLink("http://127.0.0.1:7070/getnzb?id=123");
    searchResult.setTitle("someNzb");
    searchResult.setPubDate(Instant.now());
    searchResult.setFirstFound(Instant.now());
    searchResult = searchResultRepository.save(searchResult);
    searchResultId = searchResult.getId();
    IndexerConfig indexerConfig = new IndexerConfig();
    indexerConfig.setName("indexer");
    indexerConfig.setHost("http://127.0.0.1:7070");
    baseConfig.getIndexers().add(indexerConfig);
    DownloaderConfig downloaderConfig = new DownloaderConfig();
    downloaderConfig.setDownloaderType(DownloaderType.SABNZBD);
    downloaderConfig.setName("sabnzbd");
    downloaderConfig.setUrl("http://127.0.0.1:7070/sabnzbd/");
    downloaderConfig.setNzbAddingType(NzbAddingType.SEND_LINK);
    downloaderConfig.setApiKey("apikey");
    baseConfig.getDownloading().getDownloaders().clear();
    baseConfig.getDownloading().getDownloaders().add(downloaderConfig);
    baseConfig.getSearching().setNzbAccessType(FileDownloadAccessType.REDIRECT);
    downloaderProvider.handleNewConfig(new ConfigChangedEvent(this, new BaseConfig(), baseConfig));
}
Also used : IndexerEntity(org.nzbhydra.indexers.IndexerEntity) SearchResultEntity(org.nzbhydra.searching.SearchResultEntity) Before(org.junit.Before)

Aggregations

SearchResultEntity (org.nzbhydra.searching.SearchResultEntity)7 Test (org.junit.Test)2 IndexerEntity (org.nzbhydra.indexers.IndexerEntity)2 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 Before (org.junit.Before)1 GenericResponse (org.nzbhydra.GenericResponse)1 DownloaderConfig (org.nzbhydra.config.DownloaderConfig)1 FileDownloadAccessType (org.nzbhydra.config.FileDownloadAccessType)1