-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
# Conflicts: # bot/src/main/java/com/halushko/kinocat/bot/KoTorrentBot.java # bot/src/main/java/com/halushko/kinocat/bot/handlers/input/SendTextMessageToUser.java # bot/src/main/java/com/halushko/kinocat/bot/handlers/telegram/TorrentFileHandler.java # core/src/main/java/com/halushko/kinocat/core/cli/ExecuteBash.java # core/src/main/java/com/halushko/kinocat/core/cli/ScriptsCollection.java # core/src/main/java/com/halushko/kinocat/core/commands/Constants.java # core/src/main/java/com/halushko/kinocat/core/handlers/input/ExternalCliCommandExecutor.java # core/src/main/java/com/halushko/kinocat/core/handlers/input/InputMessageHandler.java # core/src/main/java/com/halushko/kinocat/core/handlers/input/PrivateCliCommandExecutor.java # core/src/main/java/com/halushko/kinocat/core/rabbit/SmartJson.java # core/src/main/java/com/halushko/kinocat/core/web/InputMessageHandlerApiRequest.java # file/src/main/java/com/halushko/kinocat/file/UserMessageHandler.java # text/src/main/java/com/halushko/kinocat/text/TextGenerators.java # text/src/main/java/com/halushko/kinocat/text/UserMessageHandler.java # torrent/src/main/java/com/halushko/kinocat/torrent/Main.java # torrent/src/main/java/com/halushko/kinocat/torrent/externalCalls/TorrentCommands.java # torrent/src/main/java/com/halushko/kinocat/torrent/externalCalls/TorrentsList.java # torrent/src/main/java/com/halushko/kinocat/torrent/externalCalls/VoidTorrentOperator.java # torrent/src/main/java/com/halushko/kinocat/torrent/internalScripts/ViewTorrentInfo.java # torrent/src/main/resources/transmission_requests/file_list.json # torrent/src/main/resources/transmission_requests/get_torrents_names.json # torrent/src/main/resources/transmission_requests/pause_torrent.json # torrent/src/main/resources/transmission_requests/remove_only_torrent.json # torrent/src/main/resources/transmission_requests/remove_with_files.json # torrent/src/main/resources/transmission_requests/resume_torrent.json
- Loading branch information
Showing
3 changed files
with
140 additions
and
0 deletions.
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
torrent/src/main/java/com/halushko/kinocat/torrent/externalCalls/FilesList.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
torrent/src/main/java/com/halushko/kinocat/torrent/externalCalls/GetTorrent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<TorrentEntity> 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); | ||
} |
64 changes: 64 additions & 0 deletions
64
.../src/main/java/com/halushko/kinocat/torrent/externalCalls/TransmissionWebApiExecutor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} |