Skip to content

Commit

Permalink
Move torrent client to the separate service
Browse files Browse the repository at this point in the history
  • Loading branch information
galushko committed Oct 18, 2023
1 parent f6454f7 commit 9a55ebc
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,49 +8,31 @@
import com.halushko.kinocat.core.web.InputMessageHandlerApiRequest;
import com.halushko.kinocat.torrent.entities.CommonTorrentEntity;
import com.halushko.kinocat.torrent.internalScripts.ViewTorrentInfo;
import lombok.extern.slf4j.Slf4j;
import lombok.val;

import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Slf4j
public class TorrentCommands extends TransmissionWebApiExecutor {
public TorrentCommands() {
super();
}
// @Override
// protected String getResultString(List<String> lines, SmartJson rabbitMessage) {
// if (lines == null || lines.isEmpty()) return "Something went wrong with EXECUTE_TORRENT_COMMAND_FILE_COMMANDS";
// String arg = rabbitMessage.getValue("ARG");
//
// Matcher matcher;
// String name = "";
// for (String info : ViewTorrentInfo.getInfo(arg)) {
// matcher = PATTERN_GET_NAME.matcher(info);
// if (matcher.find()) {
// name = matcher.group(1);
// break;
// }
// }
//
// return String.format("%s\n%s%s\n%s%s\n%s%s\n%s%s", name,
// Constants.Commands.Torrent.PAUSE, arg,
// Constants.Commands.Torrent.RESUME, arg,
// Constants.Commands.Torrent.TORRENT_INFO, arg,
// Constants.Commands.Text.REMOVE_COMMAND, arg
// );
// }

@Override
protected void executeRequest(SmartJson message) {
long chatId = message.getUserId();
String requestBodyFormat = ResourceReader.readResourceContent(String.format("transmission_requests/%s", message.getText()));
String torrentId = message.getValue("ARG");
String requestBody = String.format(requestBodyFormat, torrentId);
log.debug("[executeRequest] Request body:\n{}", requestBody);

val responce = send(requestBody, "Content-Type", "application/json", sessionIdKey, sessionIdValue);
String bodyJson = responce.getBody();
log.debug("[executeRequest] Responce body:\n{}", bodyJson);
val json = new SmartJson(bodyJson);

StringBuilder sb = new StringBuilder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
import com.halushko.kinocat.core.files.ResourceReader;
import com.halushko.kinocat.core.rabbit.SmartJson;
import com.halushko.kinocat.core.web.InputMessageHandlerApiRequest;
import lombok.extern.slf4j.Slf4j;
import lombok.val;

// https://github.com/transmission/transmission/blob/main/docs/rpc-spec.md
@Slf4j
public abstract class TransmissionWebApiExecutor extends InputMessageHandlerApiRequest {
protected String sessionIdValue;
protected final static String sessionIdKey = "X-Transmission-Session-Id";
Expand All @@ -15,8 +17,9 @@ public TransmissionWebApiExecutor() {

@Override
protected final void getDeliverCallbackPrivate(SmartJson message) {
log.debug("[executeRequest] Message:\n{}", message.getRabbitMessageText());

if(sessionIdValue == null) {
String requestBody = ResourceReader.readResourceContent("transmission_requests/get_torrents_list.json");
val responce = send("", "Content-Type", "application/json");
String sessionIdKey = "X-Transmission-Session-Id";
this.sessionIdValue = responce.getHeader(sessionIdKey);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
"id",
"name"
],
"format": "table",
"ids": [
%s
]
Expand Down

0 comments on commit 9a55ebc

Please sign in to comment.