From 4291872d110b52ff984f0a7827013f7a37d48e30 Mon Sep 17 00:00:00 2001 From: galushko Date: Thu, 19 Oct 2023 02:05:50 +0300 Subject: [PATCH] Move torrent client to the separate service --- .../kinocat/text/UserMessageHandler.java | 8 +-- .../torrent/entities/CommonTorrentEntity.java | 2 +- .../externalCalls/TorrentCommands.java | 53 +++++-------------- .../torrent/externalCalls/TorrentsList.java | 42 +++++---------- .../TransmissionWebApiExecutor.java | 31 +++++++++-- .../externalCalls/VoidTorrentOperator.java | 9 +++- .../main/resources/commands/pause_torrent.sh | 2 - .../resources/commands/remove_only_torrent.sh | 2 - .../main/resources/commands/remove_torrent.sh | 25 --------- .../resources/commands/remove_with_files.sh | 2 - .../main/resources/commands/resume_torrent.sh | 2 - .../transmission_requests/pause_torrent.json | 8 +++ .../remove_only_torrent.json | 9 ++++ .../remove_with_files.json | 9 ++++ .../transmission_requests/resume_torrent.json | 8 +++ 15 files changed, 101 insertions(+), 111 deletions(-) delete mode 100644 torrent/src/main/resources/commands/pause_torrent.sh delete mode 100644 torrent/src/main/resources/commands/remove_only_torrent.sh delete mode 100644 torrent/src/main/resources/commands/remove_torrent.sh delete mode 100644 torrent/src/main/resources/commands/remove_with_files.sh delete mode 100644 torrent/src/main/resources/commands/resume_torrent.sh create mode 100644 torrent/src/main/resources/transmission_requests/pause_torrent.json create mode 100644 torrent/src/main/resources/transmission_requests/remove_only_torrent.json create mode 100644 torrent/src/main/resources/transmission_requests/remove_with_files.json create mode 100644 torrent/src/main/resources/transmission_requests/resume_torrent.json diff --git a/text/src/main/java/com/halushko/kinocat/text/UserMessageHandler.java b/text/src/main/java/com/halushko/kinocat/text/UserMessageHandler.java index 3e15ed04..8b6527a5 100644 --- a/text/src/main/java/com/halushko/kinocat/text/UserMessageHandler.java +++ b/text/src/main/java/com/halushko/kinocat/text/UserMessageHandler.java @@ -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); }}; diff --git a/torrent/src/main/java/com/halushko/kinocat/torrent/entities/CommonTorrentEntity.java b/torrent/src/main/java/com/halushko/kinocat/torrent/entities/CommonTorrentEntity.java index 115cd663..70d756a8 100644 --- a/torrent/src/main/java/com/halushko/kinocat/torrent/entities/CommonTorrentEntity.java +++ b/torrent/src/main/java/com/halushko/kinocat/torrent/entities/CommonTorrentEntity.java @@ -29,7 +29,7 @@ public CommonTorrentEntity(Map 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(){ diff --git a/torrent/src/main/java/com/halushko/kinocat/torrent/externalCalls/TorrentCommands.java b/torrent/src/main/java/com/halushko/kinocat/torrent/externalCalls/TorrentCommands.java index bc86608f..e05a5cd7 100644 --- a/torrent/src/main/java/com/halushko/kinocat/torrent/externalCalls/TorrentCommands.java +++ b/torrent/src/main/java/com/halushko/kinocat/torrent/externalCalls/TorrentCommands.java @@ -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 { @@ -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) 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) torrentMap); + sb.append(torrent.generateTorrentCommands()).append("\n"); + } else { + throw new RuntimeException("Can't generate torrent commands for torrent"); + } + }); + return sb.toString(); } @Override diff --git a/torrent/src/main/java/com/halushko/kinocat/torrent/externalCalls/TorrentsList.java b/torrent/src/main/java/com/halushko/kinocat/torrent/externalCalls/TorrentsList.java index 0704efe6..1d44b271 100644 --- a/torrent/src/main/java/com/halushko/kinocat/torrent/externalCalls/TorrentsList.java +++ b/torrent/src/main/java/com/halushko/kinocat/torrent/externalCalls/TorrentsList.java @@ -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; @@ -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) 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) torrentMap); + sb.append(torrent.generateTorrentCommonInfo()).append("\n"); + } else { + throw new RuntimeException("Can't parse torrents list"); + } + }); + return sb.toString(); } @Override diff --git a/torrent/src/main/java/com/halushko/kinocat/torrent/externalCalls/TransmissionWebApiExecutor.java b/torrent/src/main/java/com/halushko/kinocat/torrent/externalCalls/TransmissionWebApiExecutor.java index a112b1c4..17d119a1 100644 --- a/torrent/src/main/java/com/halushko/kinocat/torrent/externalCalls/TransmissionWebApiExecutor.java +++ b/torrent/src/main/java/com/halushko/kinocat/torrent/externalCalls/TransmissionWebApiExecutor.java @@ -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; @@ -11,6 +13,7 @@ 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"); } @@ -18,14 +21,34 @@ public TransmissionWebApiExecutor() { @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); } diff --git a/torrent/src/main/java/com/halushko/kinocat/torrent/externalCalls/VoidTorrentOperator.java b/torrent/src/main/java/com/halushko/kinocat/torrent/externalCalls/VoidTorrentOperator.java index 143172e1..1ed5d411 100644 --- a/torrent/src/main/java/com/halushko/kinocat/torrent/externalCalls/VoidTorrentOperator.java +++ b/torrent/src/main/java/com/halushko/kinocat/torrent/externalCalls/VoidTorrentOperator.java @@ -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 "Команда виконана без помилок"; + } } diff --git a/torrent/src/main/resources/commands/pause_torrent.sh b/torrent/src/main/resources/commands/pause_torrent.sh deleted file mode 100644 index 6a685ee6..00000000 --- a/torrent/src/main/resources/commands/pause_torrent.sh +++ /dev/null @@ -1,2 +0,0 @@ -#!/bin/bash -transmission-remote -t $1 -S \ No newline at end of file diff --git a/torrent/src/main/resources/commands/remove_only_torrent.sh b/torrent/src/main/resources/commands/remove_only_torrent.sh deleted file mode 100644 index a1af7b5b..00000000 --- a/torrent/src/main/resources/commands/remove_only_torrent.sh +++ /dev/null @@ -1,2 +0,0 @@ -#!/bin/bash -transmission-remote -t $1 -r \ No newline at end of file diff --git a/torrent/src/main/resources/commands/remove_torrent.sh b/torrent/src/main/resources/commands/remove_torrent.sh deleted file mode 100644 index 6ed27d87..00000000 --- a/torrent/src/main/resources/commands/remove_torrent.sh +++ /dev/null @@ -1,25 +0,0 @@ -#!/bin/bash -V_REMOVE_FILES=false - -if [ $# -eq 0 ]; then - V_REMOVE_FILES=false -else - while [ "$1" != "" ]; do - case $1 in - --full) - V_REMOVE_FILES=true - ;; - *) - ;; - esac - shift - done -fi - -if [[ $V_REMOVE_FILES == true ]]; then - transmission-remote -t $1 -rad -else - transmission-remote -t $1 -r -fi - -exit 1 diff --git a/torrent/src/main/resources/commands/remove_with_files.sh b/torrent/src/main/resources/commands/remove_with_files.sh deleted file mode 100644 index 5a19c7ae..00000000 --- a/torrent/src/main/resources/commands/remove_with_files.sh +++ /dev/null @@ -1,2 +0,0 @@ -#!/bin/bash -transmission-remote -t $1 -rad \ No newline at end of file diff --git a/torrent/src/main/resources/commands/resume_torrent.sh b/torrent/src/main/resources/commands/resume_torrent.sh deleted file mode 100644 index c044bfb6..00000000 --- a/torrent/src/main/resources/commands/resume_torrent.sh +++ /dev/null @@ -1,2 +0,0 @@ -#!/bin/bash -transmission-remote -t $1 -s \ No newline at end of file diff --git a/torrent/src/main/resources/transmission_requests/pause_torrent.json b/torrent/src/main/resources/transmission_requests/pause_torrent.json new file mode 100644 index 00000000..3f2789a4 --- /dev/null +++ b/torrent/src/main/resources/transmission_requests/pause_torrent.json @@ -0,0 +1,8 @@ +{ + "arguments": { + "ids": [ + %s + ] + }, + "method": "torrent-stop" +} diff --git a/torrent/src/main/resources/transmission_requests/remove_only_torrent.json b/torrent/src/main/resources/transmission_requests/remove_only_torrent.json new file mode 100644 index 00000000..ed093ffd --- /dev/null +++ b/torrent/src/main/resources/transmission_requests/remove_only_torrent.json @@ -0,0 +1,9 @@ +{ + "arguments": { + "delete-local-data": false, + "ids": [ + %s + ] + }, + "method": "torrent-remove" +} diff --git a/torrent/src/main/resources/transmission_requests/remove_with_files.json b/torrent/src/main/resources/transmission_requests/remove_with_files.json new file mode 100644 index 00000000..2c95de77 --- /dev/null +++ b/torrent/src/main/resources/transmission_requests/remove_with_files.json @@ -0,0 +1,9 @@ +{ + "arguments": { + "delete-local-data": true, + "ids": [ + %s + ] + }, + "method": "torrent-remove" +} diff --git a/torrent/src/main/resources/transmission_requests/resume_torrent.json b/torrent/src/main/resources/transmission_requests/resume_torrent.json new file mode 100644 index 00000000..77bc6db8 --- /dev/null +++ b/torrent/src/main/resources/transmission_requests/resume_torrent.json @@ -0,0 +1,8 @@ +{ + "arguments": { + "ids": [ + %s + ] + }, + "method": "torrent-start" +}