Skip to content

Commit

Permalink
Alpha (#39)
Browse files Browse the repository at this point in the history
Add vagrant file to the examples
Add examples
Add lombok logs
Add jakson
Add java 20
Move torrent client to the separate service
Make output more readable
Change CommandsCollection
Move Void actions to the separate classes
Refactoring
Add start and pause all command
Change CommandsCollection
Move Void actions to the separate classes
Refactoring
Fix percentage for file list
  • Loading branch information
halushko authored Nov 5, 2023
1 parent 87f91e9 commit 69e18f0
Show file tree
Hide file tree
Showing 48 changed files with 691 additions and 498 deletions.
3 changes: 2 additions & 1 deletion bot/src/main/java/com/halushko/kinocat/bot/KoTorrentBot.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.halushko.kinocat.bot.handlers.telegram.MyPingHandler;
import com.halushko.kinocat.bot.handlers.telegram.TextHandler;
import com.halushko.kinocat.bot.handlers.telegram.TorrentFileHandler;
import com.halushko.kinocat.core.handlers.input.InputMessageHandler;
import com.halushko.kinocat.core.handlers.telegram.UserMessageHandler;
import lombok.extern.slf4j.Slf4j;
import org.telegram.telegrambots.bots.TelegramLongPollingBot;
Expand Down Expand Up @@ -88,7 +89,7 @@ public static void sendText(long chatId, String str) {
log.debug("[BOT.sendText] Send text chatId:{}, text:{}", chatId, str);
BOT.execute(new SendMessage() {{
setChatId(chatId);
setText(str);
setText(str.replace(InputMessageHandler.OUTPUT_SEPARATOR, "\n"));
}}
);
} catch (TelegramApiException ex) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.halushko.kinocat.bot.handlers.input;

import com.halushko.kinocat.core.cli.Constants;
import com.halushko.kinocat.core.commands.Constants;
import com.halushko.kinocat.core.handlers.input.InputMessageHandler;
import com.halushko.kinocat.core.rabbit.SmartJson;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -10,11 +10,12 @@
@Slf4j
public class SendTextMessageToUser extends InputMessageHandler {
@Override
protected void getDeliverCallbackPrivate(SmartJson message) {
protected String getDeliverCallbackPrivate(SmartJson message) {
long chatId = message.getUserId();
String text = message.getText();
log.debug("[SendTextMessageToUser] Send text chatId:{}, text:{}", chatId, text);
sendText(chatId, text);
return text;
}
@Override
protected String getQueue() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.halushko.kinocat.bot.handlers.telegram;

import com.halushko.kinocat.core.cli.Constants;
import com.halushko.kinocat.core.commands.Constants;
import com.halushko.kinocat.core.handlers.telegram.UserMessageHandler;
import com.halushko.kinocat.core.rabbit.RabbitUtils;
import com.halushko.kinocat.core.rabbit.SmartJson;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.halushko.kinocat.bot.handlers.telegram;

import com.halushko.kinocat.bot.KoTorrentBot;
import com.halushko.kinocat.core.cli.Constants;
import com.halushko.kinocat.core.commands.Constants;
import com.halushko.kinocat.core.handlers.telegram.UserMessageHandler;
import com.halushko.kinocat.core.rabbit.SmartJson;
import com.halushko.kinocat.core.rabbit.RabbitUtils;
Expand Down Expand Up @@ -34,11 +34,11 @@ protected void readMessagePrivate(Update update) {
try {
SmartJson rm = new SmartJson(chatId).
addValue(FILE_PATH, KoTorrentBot.BOT.execute(uploadedFile).getFilePath()).
addValue("FILE_ID", update.getMessage().getDocument().getFileId()).
addValue(FILE_ID, update.getMessage().getDocument().getFileId()).
addValue(TEXT, message).
addValue("CAPTION", caption).
addValue("SIZE", String.valueOf(fileSize)).
addValue("MIME_TYPE", mimeType);
addValue(CAPTION, caption).
addValue(SIZE, String.valueOf(fileSize)).
addValue(MIME_TYPE, mimeType);

RabbitUtils.postMessage(rm, Constants.Queues.Telegram.TELEGRAM_INPUT_FILE);
} catch (TelegramApiException e) {
Expand Down
71 changes: 0 additions & 71 deletions core/src/main/java/com/halushko/kinocat/core/cli/Command.java

This file was deleted.

37 changes: 0 additions & 37 deletions core/src/main/java/com/halushko/kinocat/core/cli/Script.java

This file was deleted.

29 changes: 29 additions & 0 deletions core/src/main/java/com/halushko/kinocat/core/commands/Command.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.halushko.kinocat.core.commands;

import com.halushko.kinocat.core.rabbit.SmartJson;
import lombok.Getter;

import java.util.LinkedHashMap;
import java.util.Map;

@Getter
public class Command {
private final String command;
private final String queue;
private final SmartJson arguments;

Command(){
command = "";
queue = Constants.Queues.Telegram.TELEGRAM_OUTPUT_TEXT;
arguments = new SmartJson(SmartJson.KEYS.TEXT, "Такої команди не знайдено");
}

Command(String command, String queue, SmartJson arguments) {
this.command = command == null ? "" : command;
this.queue = queue == null ? "" : queue;
this.arguments = arguments == null ? new SmartJson("") : arguments;
}
Command(String command, String queue, Map<String, String> arguments) {
this(command, queue, arguments == null ? new SmartJson("") : new SmartJson(new LinkedHashMap<>(arguments)));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.halushko.kinocat.core.commands;

import com.halushko.kinocat.core.rabbit.SmartJson;
import lombok.extern.slf4j.Slf4j;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

@Slf4j
class CommandChecker {
private final String fullText;
private Command command = new Command();

private CommandChecker(String str) {
this.fullText = str;
}

static Command getCommand(String inputText, Collection<Command> commandsToCheck) {
CommandChecker checker = new CommandChecker(inputText);
commandsToCheck.forEach(checker::validate);
return checker.command;
}

private void validate(Command candidate) {
if (candidate == null) return;
String fullCommand = fullText.split(" ")[0];
if (!fullCommand.endsWith("_")) {
if (candidate.getCommand().equals(fullCommand)) {
setCommand(candidate);
}
}
if (fullText.startsWith(candidate.getCommand()) && this.command.getCommand().length() < candidate.getCommand().length()) {
setCommand(candidate);
}
}

private void setCommand(Command candidate) {
log.debug("[setCommandText] Command={}, fullText={}, Queue={}, Arguments={}", candidate.getCommand(), fullText, candidate.getQueue(), candidate.getArguments());
String delimiter = candidate.getCommand().endsWith("_") ? "_" : "\\s+";
Map<String, Object> arguments = new HashMap<>() {{
put(SmartJson.KEYS.COMMAND_ARGUMENTS.name(),
fullText.replace(candidate.getCommand(), "")
.trim()
.split(delimiter)
);
}};
command = new Command(candidate.getCommand(), candidate.getQueue(), new SmartJson(arguments));
}
}
Original file line number Diff line number Diff line change
@@ -1,37 +1,42 @@
package com.halushko.kinocat.core.cli;
package com.halushko.kinocat.core.commands;

import lombok.extern.slf4j.Slf4j;
import lombok.val;

import java.util.*;

@Slf4j
public class ScriptsCollection {
private Map<String, Script> allCommands;
private final List<Script> values = new ArrayList<>();
public class CommandsCollection {
private Map<String, Command> allCommands;
private final List<Command> values = new ArrayList<>();

public void addValue(String command, String script, String queue, String... args) {
values.add(new Script(command, script, queue, args));
@SuppressWarnings("unused")
public void addValue(String command, String queue) {
values.add(new Command(command, queue, new LinkedHashMap<>()));
}
@SuppressWarnings("unused")
public void addValue(String command, String queue, Map<String, String> params) {
values.add(new Command(command, queue, params));
}

@SuppressWarnings("unused")
public Command getCommand(String text) {
log.debug("Try to get command from text [{}]", text);
if (text == null) return new Command("");

Command tmp = new Command(text);
getCommandList().forEach(tmp::tryToSetScript);
log.debug("Command is command=%s, script={}, queue={}", tmp.getFinalCommand(), tmp.getScript(), tmp.getQueue());
return tmp;
log.debug("[getCommand] Try to get command from text [{}]", text);
if (text == null || text.isEmpty()) return new Command();
val result = CommandChecker.getCommand(text, getCommandList());
log.debug("[getCommand] Command is command={}, queue={}", result.getCommand(), result.getQueue());
return result;
}

private Collection<Script> getCommandList() {
private Collection<Command> getCommandList() {
init();
return allCommands.values();
}

private void init() {
if (allCommands == null) {
allCommands = new LinkedHashMap<>();
Map<String, Script> allCommandsTemp = new LinkedHashMap<>();
Map<String, Command> allCommandsTemp = new LinkedHashMap<>();
values.sort(Comparator.comparingInt(s -> s.getCommand().length()));
values.forEach(s -> allCommandsTemp.put(s.getCommand(), s));
allCommandsTemp.forEach((key, value) -> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.halushko.kinocat.core.cli;
package com.halushko.kinocat.core.commands;

@SuppressWarnings("unused")
public interface Constants {
Expand All @@ -10,11 +10,17 @@ interface Telegram {
}

interface Torrent {
String EXECUTE_VOID_TORRENT_COMMAND = "EXECUTE_TORRENT_COMMAND_QUEUE";
String EXECUTE_TORRENT_COMMAND_LIST = "EXECUTE_TORRENT_COMMAND_LIST";
String EXECUTE_TORRENT_COMMAND_LIST_FILES = "EXECUTE_TORRENT_COMMAND_LIST_FILES";
String EXECUTE_TORRENT_COMMAND_INFO = "EXECUTE_TORRENT_COMMAND_INFO";
String EXECUTE_TORRENT_COMMAND_COMMANDS = "EXECUTE_TORRENT_COMMAND_FILE_COMMANDS";
String TORRENTS_LIST = "EXECUTE_TORRENT_COMMAND_LIST";
String FILES_LIST = "EXECUTE_TORRENT_COMMAND_LIST_FILES";
String TORRENT_INFO = "EXECUTE_TORRENT_COMMAND_INFO";
String TORRENT_COMMANDS = "EXECUTE_TORRENT_COMMAND_SHOW_COMMANDS";
String RESUME_TORRENT = "EXECUTE_TORRENT_COMMAND_RESUME_TORRENT";
String PAUSE_TORRENT = "EXECUTE_TORRENT_COMMAND_PAUSE_TORRENT";
String RESUME_ALL = "EXECUTE_TORRENT_COMMAND_RESUME_ALL_TORRENTS";
String PAUSE_ALL = "EXECUTE_TORRENT_COMMAND_PAUSE_ALL_TORRENTS";
String DELETE = "EXECUTE_TORRENT_COMMAND_DELETE";
String DELETE_ONLY_TORRENT = "EXECUTE_TORRENT_COMMAND_DELETE_ONLY_TORRENT";
String DELETE_WITH_FILES = "EXECUTE_TORRENT_COMMAND_DELETE_WITH_FILES";

}

Expand All @@ -26,20 +32,22 @@ interface MediaServer {
interface Commands {
interface Torrent {
String LIST_TORRENTS = "/list";
String START_TORRENT_FILE = "/start_torrent";
String LIST_TORRENT_COMMANDS = "/more_";
String RESUME = "/resume_";
String PAUSE = "/pause_";
String RESUME_ALL = "/resume_all";
String PAUSE_ALL = "/pause_all";
String TORRENT_INFO = "/full_info_";
String REMOVE_WITH_FILES = "/approve_with_files_";
String REMOVE_JUST_TORRENT = "/approve_just_torrent_";
String LIST_FILES = "/files_";
String REMOVE_COMMAND = "/remove_";

}

interface Text {
String SEND_TEXT_TO_USER = "#$SEND_TEXT_TO_USER$#";
String SEPARATOR = "#$SEPARATOR$#";
String REMOVE_COMMAND = "/remove_";
String REMOVE_WARN_TEXT_FUNC = "askRemoveTorrent";
}
}
Expand Down
Loading

0 comments on commit 69e18f0

Please sign in to comment.