Search in sources :

Example 1 with NewznabMockRequest

use of org.nzbhydra.mapping.newznab.mock.NewznabMockRequest in project nzbhydra2 by theotherp.

the class MockNewznab method api.

@RequestMapping(value = { "/api", "/dognzb/api" }, produces = MediaType.TEXT_XML_VALUE)
public ResponseEntity<? extends Object> api(NewznabParameters params, HttpServletRequest request) throws Exception {
    if (params.getT() == ActionAttribute.CAPS) {
        // throw new RuntimeException("test");
        return new ResponseEntity<Object>(NewznabMockBuilder.getCaps(), HttpStatus.OK);
    }
    if (params.getT() == ActionAttribute.GETNFO) {
        NewznabXmlRoot rssRoot = new NewznabXmlRoot();
        rssRoot.getRssChannel().setNewznabResponse(new NewznabXmlResponse(0, 1));
        NewznabXmlItem item = new NewznabXmlItem();
        item.setDescription("NFO for NZB with ID " + params.getId());
        rssRoot.getRssChannel().getItems().add(item);
        return ResponseEntity.ok(rssRoot);
    }
    String itemTitleBase = params.getApikey();
    if (params.getQ() != null && params.getQ().contains("groups")) {
        itemTitleBase = "";
    }
    if (params.getRid() != null && params.getQ() == null) {
        NewznabXmlRoot rssRoot = NewznabMockBuilder.generateResponse(0, -1, itemTitleBase, false, Collections.emptyList());
        logger.info("Returning no results for rid based search without query");
        return new ResponseEntity<Object>(rssRoot, HttpStatus.OK);
    }
    boolean doGenerateDuplicates = "duplicates".equals(params.getQ());
    if (params.getQ() != null && params.getQ().equals("offsettest")) {
        NewznabXmlRoot rssRoot = new NewznabXmlRoot();
        rssRoot.getRssChannel().setNewznabResponse(new NewznabXmlResponse(0, 0));
        if (params.getOffset() >= 40) {
            return new ResponseEntity<Object>(rssRoot, HttpStatus.OK);
        }
        int start = params.getOffset() == 0 ? 0 : params.getOffset();
        int end = Math.min(start + 10 - 1, 40);
        rssRoot = NewznabMockBuilder.generateResponse(start, end, "offsetTest", doGenerateDuplicates, Collections.emptyList());
        rssRoot.getRssChannel().getNewznabResponse().setTotal(40);
        return new ResponseEntity<Object>(rssRoot, HttpStatus.OK);
    }
    if (params.getQ() != null && params.getQ().equals("offsettest2")) {
        NewznabMockRequest mockRequest = NewznabMockRequest.builder().numberOfResults(100).titleBase("offsettest").offset(params.getOffset()).titleWords(Collections.emptyList()).total(300).build();
        NewznabXmlRoot rssRoot = NewznabMockBuilder.generateResponse(mockRequest);
        return new ResponseEntity<Object>(rssRoot, HttpStatus.OK);
    }
    if (params.getQ() != null && params.getQ().equals("slash")) {
        NewznabMockRequest mockRequest = NewznabMockRequest.builder().numberOfResults(100).titleBase("/").offset(params.getOffset()).titleWords(Collections.emptyList()).total(300).build();
        NewznabXmlRoot rssRoot = NewznabMockBuilder.generateResponse(mockRequest);
        return new ResponseEntity<Object>(rssRoot, HttpStatus.OK);
    }
    if (params.getQ() != null && params.getQ().equals("actualduplicates")) {
        NewznabMockRequest mockRequest = NewznabMockRequest.builder().numberOfResults(10).titleBase("actualduplicates").offset(params.getOffset()).titleWords(Collections.emptyList()).total(10).build();
        NewznabXmlRoot rssRoot = NewznabMockBuilder.generateResponse(mockRequest);
        rssRoot.getRssChannel().getItems().forEach(x -> x.setTitle(rssRoot.getRssChannel().getItems().get(0).getTitle()));
        rssRoot.getRssChannel().getItems().forEach(x -> x.setLink(rssRoot.getRssChannel().getItems().get(0).getLink()));
        rssRoot.getRssChannel().getItems().forEach(x -> x.setRssGuid(rssRoot.getRssChannel().getItems().get(0).getRssGuid()));
        rssRoot.getRssChannel().getItems().forEach(x -> x.setNewznabAttributes(rssRoot.getRssChannel().getItems().get(0).getNewznabAttributes()));
        return new ResponseEntity<Object>(rssRoot, HttpStatus.OK);
    }
    if (params.getQ() != null && params.getQ().equals("oneresult")) {
        NewznabMockRequest mockRequest = NewznabMockRequest.builder().numberOfResults(1).titleBase("oneresult").offset(params.getOffset()).titleWords(Collections.emptyList()).total(1).build();
        NewznabXmlRoot rssRoot = NewznabMockBuilder.generateResponse(mockRequest);
        return new ResponseEntity<Object>(rssRoot, HttpStatus.OK);
    }
    if (params.getQ() != null && params.getQ().equals("uitest")) {
        if (params.getApikey().equals("1")) {
            NewznabXmlItem result1 = RssItemBuilder.builder("indexer1-result1").pubDate(Instant.now().minus(1, ChronoUnit.DAYS)).hasNfo(false).grabs(1).size(mbToBytes(1)).newznabAttributes(new ArrayList<>(Arrays.asList(new NewznabAttribute("category", "5000")))).category("TV").build();
            NewznabXmlItem result2 = RssItemBuilder.builder("indexer1-result2").pubDate(Instant.now().minus(2, ChronoUnit.DAYS)).hasNfo(true).grabs(2).size(mbToBytes(2)).newznabAttributes(new ArrayList<>(Arrays.asList(new NewznabAttribute("category", "5040")))).category("TV SD").build();
            NewznabXmlItem result3 = RssItemBuilder.builder("indexer1-result3").pubDate(Instant.now().minus(3, ChronoUnit.DAYS)).comments("comments").grabs(3).size(mbToBytes(3)).newznabAttributes(new ArrayList<>(Arrays.asList(new NewznabAttribute("category", "5030")))).category("TV HD").build();
            NewznabXmlRoot rssRoot = NewznabMockBuilder.getRssRoot(Arrays.asList(result1, result2, result3), 0, 3);
            return new ResponseEntity<Object>(rssRoot, HttpStatus.OK);
        }
        NewznabXmlItem result4 = RssItemBuilder.builder("indexer2-result1").pubDate(Instant.now().minus(4, ChronoUnit.DAYS)).grabs(4).size(mbToBytes(4)).newznabAttributes(new ArrayList<>(Arrays.asList(new NewznabAttribute("category", "2000")))).category("Movies").build();
        NewznabXmlItem result5 = RssItemBuilder.builder("indexer2-result2").pubDate(Instant.now().minus(5, ChronoUnit.DAYS)).grabs(5).size(mbToBytes(5)).newznabAttributes(new ArrayList<>(Arrays.asList(new NewznabAttribute("category", "2040")))).category("Movies HD").build();
        NewznabXmlRoot rssRoot = NewznabMockBuilder.getRssRoot(Arrays.asList(result4, result5), 0, 2);
        return new ResponseEntity<Object>(rssRoot, HttpStatus.OK);
    }
    if (params.getQ() != null && params.getQ().equals("dognzbtotaltest") && System.getProperty("nomockdognzb") == null) {
        if (params.getOffset() >= 300) {
            NewznabXmlRoot rssRoot = NewznabMockBuilder.generateResponse(0, -1, itemTitleBase, false, Collections.emptyList());
            return new ResponseEntity<Object>(rssRoot, HttpStatus.OK);
        }
        NewznabMockRequest mockRequest = NewznabMockRequest.builder().numberOfResults(100).titleBase("dognzbtotaltest").offset(params.getOffset()).titleWords(Collections.emptyList()).total(300).build();
        NewznabXmlRoot rssRoot = NewznabMockBuilder.generateResponse(mockRequest);
        rssRoot.getRssChannel().getNewznabResponse().setTotal(100);
        return new ResponseEntity<Object>(rssRoot, HttpStatus.OK);
    }
    if ((params.getQ() != null && params.getQ().equals("noresults")) || (params.getTvdbid() != null && params.getTvdbid().equals("329089"))) {
        NewznabXmlRoot rssRoot = NewznabMockBuilder.generateResponse(0, -1, itemTitleBase, false, Collections.emptyList());
        return new ResponseEntity<Object>(rssRoot, HttpStatus.OK);
    }
    if (params.getQ() != null && params.getQ().equals("sleep")) {
        Thread.sleep(new Random().nextInt(5000));
    }
    if (params.getQ() != null && params.getQ().equals("sleep10")) {
        Thread.sleep(5000);
    }
    if (params.getQ() != null && params.getQ().contains("movies")) {
        NewznabXmlRoot rssRoot = NewznabMockBuilder.generateResponse(0, 100, itemTitleBase, false, Arrays.asList("cam", "ts", "blu-ray 2160p", "web-dl 1080p", "bluray 1080p", "3d bluray"));
        rssRoot.getRssChannel().getNewznabResponse().setTotal(100);
        return new ResponseEntity<Object>(rssRoot, HttpStatus.OK);
    }
    if ("oneduplicate".equals(params.getQ())) {
        NewznabMockRequest mockRequest = NewznabMockRequest.builder().numberOfResults(1).titleBase(itemTitleBase).generateOneDuplicate(true).build();
        NewznabXmlRoot rssRoot = NewznabMockBuilder.generateResponse(mockRequest);
        rssRoot.getRssChannel().getNewznabResponse().setTotal(1);
        return new ResponseEntity<Object>(rssRoot, HttpStatus.OK);
    }
    if ("titlegroup".equals(params.getQ())) {
        NewznabMockRequest mockRequest = NewznabMockRequest.builder().numberOfResults(1).titleBase(itemTitleBase).generateOneDuplicate(false).build();
        NewznabXmlRoot rssRoot = NewznabMockBuilder.generateResponse(mockRequest);
        rssRoot.getRssChannel().getNewznabResponse().setTotal(1);
        rssRoot.getRssChannel().getItems().forEach(x -> x.setTitle("titlegroup"));
        return new ResponseEntity<Object>(rssRoot, HttpStatus.OK);
    }
    if (params.getTmdbid() != null) {
        if (itemTitleBase.equals("tmdberror") || "capscheckerror".equals(params.getApikey())) {
            NewznabXmlError rssError = new NewznabXmlError("123", "description");
            return new ResponseEntity<Object>(rssError, HttpStatus.OK);
        }
        NewznabXmlRoot rssRoot = NewznabMockBuilder.generateResponse(0, 10, "avengers", doGenerateDuplicates, Collections.emptyList());
        return new ResponseEntity<Object>(rssRoot, HttpStatus.OK);
    }
    if (params.getImdbid() != null) {
        NewznabXmlRoot rssRoot = NewznabMockBuilder.generateResponse(0, 10, "avengers", doGenerateDuplicates, Collections.emptyList());
        return new ResponseEntity<Object>(rssRoot, HttpStatus.OK);
    }
    int endIndex;
    int key = 0;
    try {
        key = Integer.valueOf(itemTitleBase);
    } catch (NumberFormatException e) {
        endIndex = 0;
    }
    if (apikeyToResultCount.containsKey(key)) {
        endIndex = apikeyToResultCount.get(key);
    } else {
        endIndex = 0;
    }
    if (responsesPerApikey.containsKey(endIndex)) {
        return new ResponseEntity<Object>(responsesPerApikey.get(endIndex), HttpStatus.OK);
    } else {
        if (params.getOffset() != null && params.getLimit() != null) {
            endIndex = Math.min(params.getOffset() + params.getLimit(), endIndex);
        }
        NewznabXmlRoot rssRoot = NewznabMockBuilder.generateResponse(0, endIndex, itemTitleBase, doGenerateDuplicates, Collections.emptyList());
        rssRoot.getRssChannel().getNewznabResponse().setTotal(endIndex);
        return new ResponseEntity<Object>(rssRoot, HttpStatus.OK);
    }
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) NewznabMockRequest(org.nzbhydra.mapping.newznab.mock.NewznabMockRequest) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with NewznabMockRequest

use of org.nzbhydra.mapping.newznab.mock.NewznabMockRequest in project nzbhydra2 by theotherp.

the class DevIndexer method getAndStoreResultToDatabase.

protected Xml getAndStoreResultToDatabase(URI uri, IndexerApiAccessType apiAccessType) throws IndexerAccessException {
    NewznabXmlRoot rssRoot = new NewznabXmlRoot();
    if (uri.toString().contains("oneduplicate")) {
        NewznabMockRequest mockRequest = NewznabMockRequest.builder().numberOfResults(1).titleBase("oneresult").titleWords(Collections.emptyList()).total(1).build();
        rssRoot = NewznabMockBuilder.generateResponse(mockRequest);
        rssRoot.getRssChannel().getNewznabResponse().setTotal(1);
        rssRoot.getRssChannel().getItems().get(0).getEnclosure().setLength(100000L);
        rssRoot.getRssChannel().getItems().get(0).getNewznabAttributes().clear();
    } else if (uri.toString().contains("duplicatesandtitlegroups")) {
        // One duplicate
        NewznabMockRequest mockRequest = NewznabMockRequest.builder().numberOfResults(1).titleBase("oneresult").titleWords(Collections.emptyList()).total(1).build();
        rssRoot = NewznabMockBuilder.generateResponse(mockRequest);
        rssRoot.getRssChannel().getItems().get(0).getEnclosure().setLength(100000L);
        rssRoot.getRssChannel().getItems().get(0).getNewznabAttributes().clear();
        rssRoot.getRssChannel().getItems().get(0).getTorznabAttributes().clear();
        rssRoot.getRssChannel().getItems().get(0).getNewznabAttributes().add(new NewznabAttribute("grabs", "100"));
        // Another duplicate in the same title group
        mockRequest = NewznabMockRequest.builder().numberOfResults(1).titleBase("oneresult").titleWords(Collections.emptyList()).total(1).build();
        NewznabXmlRoot rssRoot3 = NewznabMockBuilder.generateResponse(mockRequest);
        rssRoot3.getRssChannel().getItems().get(0).getEnclosure().setLength(200000L);
        rssRoot3.getRssChannel().getItems().get(0).getNewznabAttributes().clear();
        rssRoot3.getRssChannel().getItems().get(0).getTorznabAttributes().clear();
        rssRoot3.getRssChannel().getItems().get(0).getNewznabAttributes().add(new NewznabAttribute("grabs", "2000"));
        // Otherwise it will result in a unique key exception
        rssRoot3.getRssChannel().getItems().get(0).setLink("anotherlink");
        rssRoot.getRssChannel().getItems().add(rssRoot3.getRssChannel().getItems().get(0));
        // Will be a grouped title but have no duplicates
        mockRequest = NewznabMockRequest.builder().numberOfResults(1).titleBase("anotherresult").titleWords(Collections.emptyList()).total(1).build();
        NewznabXmlRoot rssRoot2 = NewznabMockBuilder.generateResponse(mockRequest);
        rssRoot.getRssChannel().getItems().add(rssRoot2.getRssChannel().getItems().get(0));
        rssRoot.getRssChannel().getNewznabResponse().setTotal(3);
    } else if (uri.toString().contains("duplicates")) {
        NewznabMockRequest mockRequest = NewznabMockRequest.builder().numberOfResults(10).titleBase("duplicates").titleWords(Collections.emptyList()).total(10).build();
        rssRoot = NewznabMockBuilder.generateResponse(mockRequest);
        rssRoot.getRssChannel().getNewznabResponse().setTotal(10);
        for (NewznabXmlItem rssItem : rssRoot.getRssChannel().getItems()) {
            rssItem.getEnclosure().setLength(100000L);
            rssItem.getNewznabAttributes().clear();
            rssItem.setPubDate(Instant.now());
            rssItem.setDescription("Indexer: " + getName() + ", title:" + rssItem.getTitle());
        }
    } else if (uri.toString().contains("tworesults")) {
        rssRoot = NewznabMockBuilder.generateResponse(0, 2, "results", false, Collections.emptyList());
        rssRoot.getRssChannel().getNewznabResponse().setTotal(2);
    } else {
        rssRoot = NewznabMockBuilder.generateResponse(0, 100, "results", false, Collections.emptyList());
        rssRoot.getRssChannel().getNewznabResponse().setTotal(100);
    }
    if (uri.toString().contains("punkte")) {
        rssRoot.getRssChannel().getItems().get(0).setTitle("a a");
        rssRoot.getRssChannel().getItems().get(1).setTitle("ab");
        rssRoot.getRssChannel().getItems().get(2).setTitle("a.c");
    }
    return rssRoot;
}
Also used : NewznabXmlRoot(org.nzbhydra.mapping.newznab.xml.NewznabXmlRoot) NewznabMockRequest(org.nzbhydra.mapping.newznab.mock.NewznabMockRequest) NewznabAttribute(org.nzbhydra.mapping.newznab.xml.NewznabAttribute) NewznabXmlItem(org.nzbhydra.mapping.newznab.xml.NewznabXmlItem)

Aggregations

NewznabMockRequest (org.nzbhydra.mapping.newznab.mock.NewznabMockRequest)2 NewznabAttribute (org.nzbhydra.mapping.newznab.xml.NewznabAttribute)1 NewznabXmlItem (org.nzbhydra.mapping.newznab.xml.NewznabXmlItem)1 NewznabXmlRoot (org.nzbhydra.mapping.newznab.xml.NewznabXmlRoot)1 ResponseEntity (org.springframework.http.ResponseEntity)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1