Skip to content

Commit

Permalink
Search torbox torrents and send results (doesn't work yet)
Browse files Browse the repository at this point in the history
  • Loading branch information
theotherp committed Jan 5, 2025
1 parent 04a99b5 commit 6b19985
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ public AddNzbsResponse addBySearchResultIds(List<AddFilesRequest.SearchResult> s
if (addingType == NzbAddingType.UPLOAD && accessTypeForIndexer == FileDownloadAccessType.PROXY) {
DownloadResult result = fileHandler.getFileByResult(FileDownloadAccessType.PROXY, SearchSource.INTERNAL, optionalResult.get()); //Uploading NZBs can only be done via proxying
if (result.isSuccessful()) {
String externalId = addNzb(result.getContent(), result.getTitle(), categoryToSend);
String externalId = addContent(result.getContent(), result.getTitle(), searchResult.getDownloadType(), categoryToSend);
result.getDownloadEntity().setExternalId(externalId);
fileHandler.updateStatusByEntity(result.getDownloadEntity(), FileDownloadStatus.NZB_ADDED);
addedNzbs.add(guid);
Expand All @@ -158,7 +158,7 @@ public AddNzbsResponse addBySearchResultIds(List<AddFilesRequest.SearchResult> s
}
} else {
String link = downloadUrlBuilder.getDownloadLinkForSendingToDownloader(searchResult, false);
String externalId = addLink(link, searchResultTitle, categoryToSend);
String externalId = addLink(link, searchResultTitle, searchResult.getDownloadType(), categoryToSend);
guidExternalIds.put(guid, externalId);
addedNzbs.add(guid);
}
Expand Down Expand Up @@ -203,22 +203,24 @@ protected NzbAddingType getNzbAddingType(DownloadType downloadType) {
public abstract List<String> getCategories();

/**
* @param link Link to the NZB
* @param title Title to tell the downloader
* @param category Category to file under
* @param link Link to the NZB
* @param title Title to tell the downloader
* @param downloadType
* @param category Category to file under
* @return ID returned by the downloader
* @throws DownloaderException Error while downloading
*/
public abstract String addLink(String link, String title, String category) throws DownloaderException;
public abstract String addLink(String link, String title, DownloadType downloadType, String category) throws DownloaderException;

/**
* @param content NZB content to upload
* @param title Title to tell the downloader
* @param category Category to file under
* @param content NZB content to upload
* @param title Title to tell the downloader
* @param downloadType
* @param category Category to file under
* @return ID returned by the downloader
* @throws DownloaderException Error while downloading
*/
public abstract String addNzb(byte[] content, String title, String category) throws DownloaderException;
public abstract String addContent(byte[] content, String title, DownloadType downloadType, String category) throws DownloaderException;

public abstract DownloaderStatus getStatus() throws DownloaderException;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.googlecode.jsonrpc4j.JsonRpcHttpClient;
import org.nzbhydra.GenericResponse;
import org.nzbhydra.config.ConfigProvider;
import org.nzbhydra.config.downloading.DownloadType;
import org.nzbhydra.config.downloading.DownloaderConfig;
import org.nzbhydra.downloading.FileDownloadStatus;
import org.nzbhydra.downloading.FileHandler;
Expand Down Expand Up @@ -151,7 +152,7 @@ public List<String> getCategories() {
}

@Override
public String addLink(String link, String title, String category) throws DownloaderException {
public String addLink(String link, String title, DownloadType downloadType, String category) throws DownloaderException {
logger.debug("Adding link for {} to NZB with category {}", title, category);
try {
return callAppend(link, title, category);
Expand All @@ -165,7 +166,7 @@ public String addLink(String link, String title, String category) throws Downloa
}

@Override
public String addNzb(byte[] content, String title, String category) throws DownloaderException {
public String addContent(byte[] content, String title, DownloadType downloadType, String category) throws DownloaderException {
logger.debug("Adding NZB for {} to NZB with category {}", title, category);
try {
return callAppend(BaseEncoding.base64().encode(content), title, category);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.nzbhydra.GenericResponse;
import org.nzbhydra.Jackson;
import org.nzbhydra.config.ConfigProvider;
import org.nzbhydra.config.downloading.DownloadType;
import org.nzbhydra.downloading.DownloaderType;
import org.nzbhydra.downloading.FileDownloadStatus;
import org.nzbhydra.downloading.FileHandler;
Expand Down Expand Up @@ -99,7 +100,7 @@ private UriComponentsBuilder getBaseUrl() {
}

@Override
public String addLink(String url, String title, String category) throws DownloaderException {
public String addLink(String url, String title, DownloadType downloadType, String category) throws DownloaderException {
logger.debug("Sending link for NZB {} to sabnzbd", title);
title = suffixNzbToTitle(title);
UriComponentsBuilder urlBuilder = getBaseUrl();
Expand Down Expand Up @@ -133,7 +134,7 @@ private String sendAddNzbLinkCommand(UriComponentsBuilder urlBuilder, HttpEntity
}

@Override
public String addNzb(byte[] fileContent, String title, String category) throws DownloaderException {
public String addContent(byte[] fileContent, String title, DownloadType downloadType, String category) throws DownloaderException {
//Using OKHTTP here because RestTemplate wouldn't work
logger.debug("Uploading NZB {} to sabnzbd", title);
UriComponentsBuilder urlBuilder = getBaseUrl();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@
@Component("torboxdownloader")
public class Torbox extends Downloader {

private enum ResultType {
TORBOX_TORRENT,
TORRENT,
MAGNET,
TORBOX_USENET,
NZB
}

// TODO sist 14.12.2024: Try and parse error responses
// TODO sist 14.12.2024: Exclude nzbs.in
// TODO sist 14.12.2024: Check if bypass_cache for status check is OK and which limit should be used
Expand Down Expand Up @@ -116,23 +124,31 @@ public List<String> getCategories() {
}

@Override
public String addLink(String link, String title, String category) throws DownloaderException {
return sendAddRequest(link.getBytes(StandardCharsets.UTF_8), title, "link", "link");
public String addLink(String link, String title, DownloadType downloadType, String category) throws DownloaderException {
return sendAddRequest(link.getBytes(StandardCharsets.UTF_8), title, "link", "link", downloadType);
}

@Override
public String addNzb(byte[] content, String title, String category) throws DownloaderException {
return sendAddRequest(content, title, "file", "data");
public String addContent(byte[] content, String title, DownloadType downloadType, String category) throws DownloaderException {
return sendAddRequest(content, title, "file", "data", downloadType);
}

private String sendAddRequest(byte[] value, String title, String addType, String descInLog) throws DownloaderException {
log.debug("Sending {} for NZB {} to torbox", descInLog, title);
UriComponentsBuilder url = getBaseUrl().path("/usenet/createusenetdownload");
private String sendAddRequest(byte[] value, String title, String addType, String descInLog, DownloadType downloadType) throws DownloaderException {
log.debug("Sending {} for \"{}\" to torbox", descInLog, title);
UriComponentsBuilder url;
// We have no way of knowing if the original search result was a torrent or usenet result. Kinda hacky, I know...
final ResultType resultType = determineResultType(value, title, downloadType);

switch (resultType) {
case TORBOX_USENET, NZB -> url = getBaseUrl().path("/usenet/createusenetdownload");
case TORBOX_TORRENT, TORRENT, MAGNET -> url = getBaseUrl().path("/torrents/createtorrent");
default -> throw new IllegalStateException("Unexpected result type " + resultType);
}

HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
if (addType.equals("file")) {
if (addType.equals("file") && resultType != ResultType.MAGNET) {
ByteArrayResource fileResource = new ByteArrayResource(value) {
@Override
public String getFilename() {
Expand All @@ -141,24 +157,48 @@ public String getFilename() {
};
map.add("file", fileResource);
} else {
map.add(addType, value);
if (resultType == ResultType.MAGNET) {
map.add("magnet", value);
} else {
map.add(addType, value);
}
map.add("name", title);
}
HttpEntity<MultiValueMap<String, Object>> request = new HttpEntity<>(map, headers);
try {
ResponseEntity<AddUDlResponse> entity = restTemplate.postForEntity(url.toUriString(), request, AddUDlResponse.class);
AddUDlResponse dlResponse = entity.getBody();
if (dlResponse.isSuccess()) {
log.info("Successfully added \"{}\" to torbox", title);
return dlResponse.getData().getUsenetdownload_id();
}
log.error("Error adding {} for NZB {} to torbox. Error: {}\nDetail:{}", descInLog, title, dlResponse.getError(), dlResponse.getDetail());
throw new DownloaderException("Torbox returned error: " + dlResponse.getError());

} catch (Exception e) {
log.error("Unexpected response when sending add request for {} to torbox", title, e);
throw new DownloaderException("Error sending " + descInLog + " to torbox", e);
}
}

private static ResultType determineResultType(byte[] value, String title, DownloadType downloadType) throws DownloaderException {
String valueString = new String(value);
final ResultType resultType;

if (downloadType == DownloadType.TORRENT) {
resultType = ResultType.TORRENT;
} else if (downloadType == DownloadType.NZB) {
resultType = ResultType.NZB;
} else if (valueString.contains("usenet/download")) {
resultType = ResultType.TORBOX_USENET;
} else if (valueString.startsWith("magnet:")) {
resultType = ResultType.MAGNET;
} else {
throw new DownloaderException("Unable to determine type of download for " + title + " from type " + downloadType + " and content " + valueString);
}
return resultType;
}

@Override
public DownloaderStatus getStatus() throws DownloaderException {
List<TorboxDownload> downloadingEntries = getLastTorboxDownloads().stream().filter(x -> x.getDownloadState().equals("downloading")).toList();
Expand Down Expand Up @@ -269,6 +309,7 @@ protected FileDownloadStatus getDownloadStatusFromDownloaderEntry(DownloaderEntr

}


private UriComponentsBuilder getBaseUrl() {
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(BASE_API_URL);
return builder;
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/org/nzbhydra/indexers/Anizb.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import java.util.ArrayList;
import java.util.List;

@Component("aninzb")
@Component("anizb")
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class Anizb extends Indexer<NewznabXmlRoot> {

Expand Down
Loading

0 comments on commit 6b19985

Please sign in to comment.