diff --git a/torrent/src/main/java/com/halushko/kinocat/torrent/externalCalls/FilesList.java b/torrent/src/main/java/com/halushko/kinocat/torrent/externalCalls/FilesList.java new file mode 100644 index 0000000..a714d87 --- /dev/null +++ b/torrent/src/main/java/com/halushko/kinocat/torrent/externalCalls/FilesList.java @@ -0,0 +1,51 @@ +package com.halushko.kinocat.torrent.externalCalls; + +import com.halushko.kinocat.core.cli.Constants; +import com.halushko.kinocat.torrent.entities.SubTorrentEntity; +import com.halushko.kinocat.torrent.entities.TorrentEntity; + +import java.util.stream.IntStream; + +public class FilesList extends GetTorrent { + + @Override + protected String generateAnswer(TorrentEntity torrent) { + StringBuilder sb = new StringBuilder(); + torrent.getFiles().forEach(file -> sb.append(getFileInfo(file)).append("\n")); + return sb.toString(); + } + + protected String getFileInfo(SubTorrentEntity file){ + return String.format("%s\n%s\n||%s|| %s", String.join("/", file.getFolders()), file.getName(), getProgressBar(file), getGigabytesLeft(file)); + } + + protected String getGigabytesLeft(SubTorrentEntity torrent) { + long completed = torrent.getBytesCompleted(); + long full = torrent.getLength(); + double percents = (double) completed / full; + + return percents == 1.0 + ? " (done)" + : " % (" + Math.round((full - full * completed) / 1000000.0) / 1000.0 + " Gb left)"; + } + + protected String getProgressBar(SubTorrentEntity torrent) { + int blocks = 10; + long completed = torrent.getBytesCompleted(); + long full = torrent.getLength(); + double percents = (double) completed / full; + + int blackBlocks = (int) (percents * blocks); + StringBuilder line = new StringBuilder(); + + IntStream.range(0, blackBlocks).mapToObj(i -> "█").forEach(line::append); + IntStream.range(blackBlocks, blocks).mapToObj(i -> "░").forEach(line::append); + + return line.toString(); + } + + @Override + protected String getQueue() { + return Constants.Queues.Torrent.EXECUTE_TORRENT_COMMAND_LIST_FILES; + } +} diff --git a/torrent/src/main/java/com/halushko/kinocat/torrent/externalCalls/GetTorrent.java b/torrent/src/main/java/com/halushko/kinocat/torrent/externalCalls/GetTorrent.java new file mode 100644 index 0000000..e78d993 --- /dev/null +++ b/torrent/src/main/java/com/halushko/kinocat/torrent/externalCalls/GetTorrent.java @@ -0,0 +1,25 @@ +package com.halushko.kinocat.torrent.externalCalls; + +import com.halushko.kinocat.core.rabbit.SmartJson; +import com.halushko.kinocat.torrent.entities.TorrentEntity; + +import java.util.ArrayList; +import java.util.Comparator; +import java.util.List; + +public abstract class GetTorrent extends TransmissionWebApiExecutor { + @Override + protected final String executeRequest(SmartJson json) { + List torrents = new ArrayList<>(); + json.getSubMessage("arguments") + .getSubMessage("torrents") + .convertToList() + .forEach(torrentMap -> torrents.add(new TorrentEntity(torrentMap))); + StringBuilder sb = new StringBuilder(); + torrents.sort(Comparator.comparing(TorrentEntity::getName)); + torrents.forEach(torrent -> sb.append(generateAnswer(torrent)).append("\n")); + return sb.toString(); + } + + protected abstract String generateAnswer(TorrentEntity torrent); +} 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 new file mode 100644 index 0000000..a37b0e3 --- /dev/null +++ b/torrent/src/main/java/com/halushko/kinocat/torrent/externalCalls/TransmissionWebApiExecutor.java @@ -0,0 +1,64 @@ +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.ApiResponce; +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 { + private static 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()); + long chatId = message.getUserId(); + if (sessionIdValue == null) { + //new session + log.debug("[executeRequest] Create a new session"); + + val responce = send("", "Content-Type", "application/json"); + TransmissionWebApiExecutor.sessionIdValue = responce.getHeader(sessionIdKey); + } + String requestBodyFormat = ResourceReader.readResourceContent(String.format("transmission_requests/%s", message.getValue("SCRIPT"))); + String requestBody = String.format(requestBodyFormat, (Object[]) message.getValue("ARG").split(" ")); + log.debug("[executeRequest] Request body:\n{}", requestBody); + + ApiResponce responce = send(requestBody, "Content-Type", "application/json", sessionIdKey, sessionIdValue); + String responceBody = responce.getBody(); + if(responceBody.contains("409: Conflict")){ + //expired session + log.debug("[executeRequest] Recreate a session"); + TransmissionWebApiExecutor.sessionIdValue = responce.getHeader(sessionIdKey); + responce = send(requestBody, "Content-Type", "application/json", sessionIdKey, sessionIdValue); + responceBody = responce.getBody(); + } + + log.debug("[executeRequest] Responce body:\n{}", responceBody); + val json = new SmartJson(responceBody); + + String requestResult = json.getValue("result"); + + String output; + if ("success".equalsIgnoreCase(requestResult)) { + output = executeRequest(json); + } else { + output = String.format("result of request is: %s", responceBody); + } + + RabbitUtils.postMessage(chatId, output, Constants.Queues.Telegram.TELEGRAM_OUTPUT_TEXT); + + } + + protected abstract String executeRequest(SmartJson message); +}