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 e71c4ce commit 4291872
Show file tree
Hide file tree
Showing 15 changed files with 101 additions and 111 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ public class UserMessageHandler extends InputMessageHandler {

addValue("/list", "get_torrents_list.json", Constants.Queues.Torrent.EXECUTE_TORRENT_COMMAND_LIST);
addValue(Constants.Commands.Torrent.LIST_TORRENT_COMMANDS, "get_torrents_names.json", Constants.Queues.Torrent.EXECUTE_TORRENT_COMMAND_COMMANDS);
addValue(Constants.Commands.Torrent.RESUME, "resume_torrent.sh", Constants.Queues.Torrent.EXECUTE_VOID_TORRENT_COMMAND);
addValue(Constants.Commands.Torrent.PAUSE, "pause_torrent.sh", Constants.Queues.Torrent.EXECUTE_VOID_TORRENT_COMMAND);
addValue(Constants.Commands.Torrent.RESUME, "resume_torrent.json", Constants.Queues.Torrent.EXECUTE_VOID_TORRENT_COMMAND);
addValue(Constants.Commands.Torrent.PAUSE, "pause_torrent.json", Constants.Queues.Torrent.EXECUTE_VOID_TORRENT_COMMAND);
addValue(Constants.Commands.Torrent.TORRENT_INFO, "info_torrent.sh", Constants.Queues.Torrent.EXECUTE_TORRENT_COMMAND_INFO);
addValue(Constants.Commands.Torrent.REMOVE_WITH_FILES, "remove_with_files.sh", Constants.Queues.Torrent.EXECUTE_VOID_TORRENT_COMMAND);
addValue(Constants.Commands.Torrent.REMOVE_JUST_TORRENT, "remove_only_torrent.sh", Constants.Queues.Torrent.EXECUTE_VOID_TORRENT_COMMAND);
addValue(Constants.Commands.Torrent.REMOVE_WITH_FILES, "remove_with_files.json", Constants.Queues.Torrent.EXECUTE_VOID_TORRENT_COMMAND);
addValue(Constants.Commands.Torrent.REMOVE_JUST_TORRENT, "remove_only_torrent.json", Constants.Queues.Torrent.EXECUTE_VOID_TORRENT_COMMAND);

addValue(Constants.Commands.Text.REMOVE_COMMAND, Constants.Commands.Text.SEND_TEXT_TO_USER, Constants.Queues.Telegram.TELEGRAM_OUTPUT_TEXT, Constants.Commands.Text.REMOVE_WARN_TEXT_FUNC);
}};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public CommonTorrentEntity(Map<String, Object> map){
}

public String generateTorrentCommonInfo(){
return String.format("%s %s\n%s %s /more_%s", status, name, getProgressBar(), getGigabytesLeft(), id);
return String.format("%s %s\n%s %s /more_%s", getStatusIcon(), name, getProgressBar(), getGigabytesLeft(), id);
}

public String generateTorrentCommands(){
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,12 @@
package com.halushko.kinocat.torrent.externalCalls;

import com.halushko.kinocat.core.cli.Constants;
import com.halushko.kinocat.core.files.ResourceReader;
import com.halushko.kinocat.core.handlers.input.ExternalCliCommandExecutor;
import com.halushko.kinocat.core.rabbit.RabbitUtils;
import com.halushko.kinocat.core.rabbit.SmartJson;
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.Arrays;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Slf4j
public class TorrentCommands extends TransmissionWebApiExecutor {
Expand All @@ -24,37 +15,21 @@ public TorrentCommands() {
}

@Override
protected void executeRequest(SmartJson message) {
long chatId = message.getUserId();
String torrentId = message.getValue("ARG");
String requestBodyFormat = ResourceReader.readResourceContent(String.format("transmission_requests/%s", message.getValue("SCRIPT")));
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);

protected String executeRequest(SmartJson json) {
StringBuilder sb = new StringBuilder();
String requestResult = json.getValue("result");
if ("success".equalsIgnoreCase(requestResult)) {
json.getSubMessage("arguments")
.getSubMessage("torrents")
.convertToList()
.forEach(torrentMap -> {
if (torrentMap instanceof Map) {
//noinspection unchecked
val torrent = new CommonTorrentEntity((Map<String, Object>) torrentMap);
sb.append(torrent.generateTorrentCommands()).append("\n");
} else {
throw new RuntimeException(String.format("Can't generate torrent commands for %s", torrentId));
}
});
} else {
sb.append(String.format("result of request is: %s", requestResult));
}
RabbitUtils.postMessage(chatId, sb.toString(), Constants.Queues.Telegram.TELEGRAM_OUTPUT_TEXT);
json.getSubMessage("arguments")
.getSubMessage("torrents")
.convertToList()
.forEach(torrentMap -> {
if (torrentMap instanceof Map) {
//noinspection unchecked
val torrent = new CommonTorrentEntity((Map<String, Object>) torrentMap);
sb.append(torrent.generateTorrentCommands()).append("\n");
} else {
throw new RuntimeException("Can't generate torrent commands for torrent");
}
});
return sb.toString();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.halushko.kinocat.torrent.externalCalls;

import com.halushko.kinocat.core.cli.Constants;
import com.halushko.kinocat.core.files.ResourceReader;
import com.halushko.kinocat.core.rabbit.RabbitUtils;
import com.halushko.kinocat.core.rabbit.SmartJson;
import com.halushko.kinocat.torrent.entities.CommonTorrentEntity;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -17,33 +15,21 @@ public TorrentsList() {
}

@Override
protected void executeRequest(SmartJson message) {
long chatId = message.getUserId();
String requestBody = ResourceReader.readResourceContent(String.format("transmission_requests/%s", message.getText()));
val responce = send(requestBody, "Content-Type", "application/json", sessionIdKey, sessionIdValue);
String bodyJson = responce.getBody();
val json = new SmartJson(bodyJson);

protected String executeRequest(SmartJson json) {
StringBuilder sb = new StringBuilder();
String requestResult = json.getValue("result");
if ("success".equalsIgnoreCase(requestResult)) {
json.getSubMessage("arguments")
.getSubMessage("torrents")
.convertToList()
.forEach(torrentMap -> {
if (torrentMap instanceof Map) {
//noinspection unchecked
val torrent = new CommonTorrentEntity((Map<String, Object>) torrentMap);
sb.append(torrent.generateTorrentCommonInfo()).append("\n");
} else {
throw new RuntimeException("Can't parse torrents list");
}
});
} else {
sb.append(String.format("result of request is: %s", requestResult));
}

RabbitUtils.postMessage(chatId, sb.toString(), Constants.Queues.Telegram.TELEGRAM_OUTPUT_TEXT);
json.getSubMessage("arguments")
.getSubMessage("torrents")
.convertToList()
.forEach(torrentMap -> {
if (torrentMap instanceof Map) {
//noinspection unchecked
val torrent = new CommonTorrentEntity((Map<String, Object>) torrentMap);
sb.append(torrent.generateTorrentCommonInfo()).append("\n");
} else {
throw new RuntimeException("Can't parse torrents list");
}
});
return sb.toString();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.halushko.kinocat.torrent.externalCalls;

import com.halushko.kinocat.core.cli.Constants;
import com.halushko.kinocat.core.files.ResourceReader;
import com.halushko.kinocat.core.rabbit.RabbitUtils;
import com.halushko.kinocat.core.rabbit.SmartJson;
import com.halushko.kinocat.core.web.InputMessageHandlerApiRequest;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -11,21 +13,42 @@
public abstract class TransmissionWebApiExecutor extends InputMessageHandlerApiRequest {
protected String sessionIdValue;
protected final static String sessionIdKey = "X-Transmission-Session-Id";

public TransmissionWebApiExecutor() {
super("http", "10.10.255.253", 9091, "transmission/rpc");
}

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

if(sessionIdValue == null) {
long chatId = message.getUserId();
if (sessionIdValue == null) {
val responce = send("", "Content-Type", "application/json");
String sessionIdKey = "X-Transmission-Session-Id";
this.sessionIdValue = responce.getHeader(sessionIdKey);
}
executeRequest(message);
String arguments = message.getValue("ARG");
String requestBodyFormat = ResourceReader.readResourceContent(String.format("transmission_requests/%s", message.getValue("SCRIPT")));
String requestBody = String.format(requestBodyFormat, (Object[]) arguments.split(" "));
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);

String requestResult = json.getValue("result");

String output;
if ("success".equalsIgnoreCase(requestResult)) {
output = executeRequest(json);
} else {
output = String.format("result of request is: %s", requestResult);
}

RabbitUtils.postMessage(chatId, output, Constants.Queues.Telegram.TELEGRAM_OUTPUT_TEXT);

}

protected abstract void executeRequest(SmartJson message);
protected abstract String executeRequest(SmartJson message);
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package com.halushko.kinocat.torrent.externalCalls;

import com.halushko.kinocat.core.cli.Constants;
import com.halushko.kinocat.core.handlers.input.ExternalCliCommandExecutor;
import com.halushko.kinocat.core.rabbit.SmartJson;

public class VoidTorrentOperator extends ExternalCliCommandExecutor {
public class VoidTorrentOperator extends TransmissionWebApiExecutor {
@Override
protected String getQueue() {
return Constants.Queues.Torrent.EXECUTE_VOID_TORRENT_COMMAND;
}

@Override
protected String executeRequest(SmartJson message) {
return "Команда виконана без помилок";
}
}
2 changes: 0 additions & 2 deletions torrent/src/main/resources/commands/pause_torrent.sh

This file was deleted.

2 changes: 0 additions & 2 deletions torrent/src/main/resources/commands/remove_only_torrent.sh

This file was deleted.

25 changes: 0 additions & 25 deletions torrent/src/main/resources/commands/remove_torrent.sh

This file was deleted.

2 changes: 0 additions & 2 deletions torrent/src/main/resources/commands/remove_with_files.sh

This file was deleted.

2 changes: 0 additions & 2 deletions torrent/src/main/resources/commands/resume_torrent.sh

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"arguments": {
"ids": [
%s
]
},
"method": "torrent-stop"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"arguments": {
"delete-local-data": false,
"ids": [
%s
]
},
"method": "torrent-remove"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"arguments": {
"delete-local-data": true,
"ids": [
%s
]
},
"method": "torrent-remove"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"arguments": {
"ids": [
%s
]
},
"method": "torrent-start"
}

0 comments on commit 4291872

Please sign in to comment.