From d7a31b41048f82d88d1ea412ac58c38eb733bcb6 Mon Sep 17 00:00:00 2001 From: Alex Ivanov Date: Wed, 16 Oct 2024 17:32:23 +0300 Subject: [PATCH] Improve code style in HentaiBot class --- src/main/java/org/hentaibot/HentaiBot.java | 195 +++++------------- .../org/hentaibot/constants/Constants.java | 96 +++++++++ 2 files changed, 152 insertions(+), 139 deletions(-) create mode 100644 src/main/java/org/hentaibot/constants/Constants.java diff --git a/src/main/java/org/hentaibot/HentaiBot.java b/src/main/java/org/hentaibot/HentaiBot.java index aa8d4f1..34926b3 100644 --- a/src/main/java/org/hentaibot/HentaiBot.java +++ b/src/main/java/org/hentaibot/HentaiBot.java @@ -1,12 +1,13 @@ package org.hentaibot; import org.apache.log4j.Logger; +import org.hentaibot.constants.Constants; import org.hentaibot.dtos.Rule34Dto; import org.hentaibot.dtos.WaifuDto; -import org.hentaibot.network.AnimeWaifuQueries; -import org.hentaibot.network.Client; -import org.hentaibot.network.ClientSFW; -import org.hentaibot.network.HentaiQueries; +import org.hentaibot.network.waifu.AnimeQueries; +import org.hentaibot.network.rule34.Rule34ApiClient; +import org.hentaibot.network.rule34.HentaiQueries; +import org.hentaibot.network.waifu.WaifuApiClient; import org.telegram.telegrambots.bots.TelegramLongPollingBot; import org.telegram.telegrambots.meta.api.methods.send.SendAnimation; import org.telegram.telegrambots.meta.api.methods.send.SendMessage; @@ -22,28 +23,17 @@ import javax.validation.constraints.NotNull; import java.io.IOException; import java.net.URI; -import java.util.Arrays; import java.util.List; +import java.util.Optional; import java.util.Random; public class HentaiBot extends TelegramLongPollingBot { private static final Random random = new Random(); - private static final List picExtensions = - Arrays.asList(".jpg", ".jpeg", ".png", ".webp", ".avif", ".jfif"); - - private static final List tags = - Arrays.asList("ass","anal","cum","creampie","hentai","masturbation", - "public","orgy","elf","yuri","pussy","glasses","blowjob", - "handjob","footjob","boobs","thighs","ahe_gao","uniform","gangbang","tentacles","genshin_impact", "naruto", "bleach"); - private static final List tags_artist = - Arrays.asList("shexyo", "theobrobine", "cutesexyrobutts", "sakimichan", "krabby_(artist)", "lexaiduer", "miraihikari", - "LumiNyu", "lime_(purple_haze", "nepcill", "sciamano240", "neoartcore", "dandon_fuga", "zaphn", "flou", - "prywinko", "alexander_dinh", "kittew", "supullim", "tofuubear", "azto_dio", "derpixon", "kinkymation", "ggc", "afrobull"); - private static final Logger logger = Logger.getLogger(HentaiBot.class.getName()); - private static final HentaiQueries hentaiClient = Client.getNsfwClient(); - private static final AnimeWaifuQueries animeClient = ClientSFW.getSfwClient(); + private static final HentaiQueries hentaiClient = Rule34ApiClient.getNsfwClient(); + private static final AnimeQueries animeClient = WaifuApiClient.getSfwClient(); + private final String botName; public HentaiBot(String botName, String botToken) { @@ -51,127 +41,57 @@ public HentaiBot(String botName, String botToken) { this.botName = botName; } - // received message + @Override + public String getBotUsername() { + return botName; + } + @Override public void onUpdateReceived(Update update) { String chatId = update.getMessage().getChatId().toString(); if (!update.hasMessage() || !update.getMessage().hasText()) { - respondWithText(chatId, "I don't understand you. " + - "For list of all supported commands, try /help"); + respondWithText(chatId, Constants.MESSAGES.get("UNRECOGNIZED_INPUT")); return; } - String userMsg = update.getMessage().getText(), respText; + String userMsg = update.getMessage().getText(); - switch (userMsg) { - case "/start" -> respText = """ - Hi! I'm HentaiBot. I can send you some spicy anime pics. - Use me if you're 18+. - For list of all supported commands, try /help - """; - case "/help" -> respText = """ - List of commands available for now: - /help - /hentai - /anime - /help_tags - """; - case "/help_tags" -> respText = """ - List of tags available for now: - /tags_anal - /tags_cum - /tags_creampie - /tags_hentai - /tags_masturbation - /tags_public - /tags_orgy - /tags_elf - /tags_yuri - /tags_pussy - /tags_glasses - /tags_blowjob - /tags_handjob - /tags_footjob - /tags_boobs - /tags_thighs - /tags_ahe_gao - /tags_uniform - /tags_gangbang - /tags_tentacles - /tags_genshin_impact - /tags_naruto - /tags_bleach - /tags_shexyo - /tags_theobrobine - /tags_cutesexyrobutts - /tags_sakimichan - /tags_krabby_(artist) - /tags_lexaiduer - /tags_miraihikari - /tags_LumiNyu - /tags_lime_(purple_haze) - /tags_nepcill - /tags_sciamano240 - /tags_neoartcore - /tags_dandon_fuga - /tags_zaphn - /tags_flou - /tags_prywinko - /tags_alexander_dinh - /tags_kittew - /tags_supullim - /tags_tofuubear - /tags_azto_dio - /tags_derpixon - /tags_kinkymation - /tags_ggc - /tags_afrobull - """; - case "/anime", "/sfw" -> { - respText = null; - respondSfw(chatId); - } - case "/hentai", "/nsfw" -> { - respText = null; - respondNsfw(chatId, "female", 200000); - } - default -> - { - if (userMsg.startsWith("/tags_")) - { - respText = null; - if(tags.contains(userMsg.substring(6))) - { - respondNsfw(chatId, userMsg.substring(6) , 50000); - } - else if(tags_artist.contains(userMsg.substring(6))) - { - respondNsfw(chatId, userMsg.substring(6), 500); - } - else - { - respText = "Invalid command. For list of all supported commands, try /help_tags"; - } - } - else - { - respText = "Invalid command. For list of all supported commands, try /help"; - } - - } - } - - respondWithText(chatId, respText); + if (userMsg.startsWith("/tags_")) + respondOnTags(chatId, userMsg); + else + respondOnCommands(chatId, userMsg); } - @Override - public String getBotUsername() { - return botName; + public void respondOnTags(String chatId, String userMsg) { + if (!userMsg.startsWith("/tags_")) return; + + String tag = userMsg.substring(6); + Optional randomUpperBound = + Constants.TAGS.contains(tag) + ? Optional.of(50000) + : Constants.ARTISTS.contains(tag) + ? Optional.of(500) + : Optional.empty(); + + if (randomUpperBound.isPresent()) + respondNsfw(chatId, tag, randomUpperBound.get()); + else + respondWithText(chatId, Constants.MESSAGES.get("UNRECOGNIZED_INPUT")); } - public void respondNsfw(String chatId, String tag, int rand_number) { - int pageId = random.nextInt(1, rand_number); + public void respondOnCommands(String chatId, String userMsg) { + switch (userMsg) { + case "/start" -> respondWithText(chatId, Constants.MESSAGES.get("START")); + case "/help" -> respondWithText(chatId, Constants.MESSAGES.get("HELP")); + case "/help_tags" -> respondWithText(chatId, Constants.MESSAGES.get("HELP_TAGS")); + case "/anime", "/sfw" -> respondSfw(chatId); + case "/hentai", "/nsfw" -> respondNsfw(chatId, "female", 200000); + default -> respondWithText(chatId, Constants.MESSAGES.get("UNRECOGNIZED_INPUT")); + } + } + public void respondNsfw(String chatId, String tag, int randomUpperBound) { + int pageId = random.nextInt(1, randomUpperBound); try { hentaiClient .getNsfw(tag, pageId) @@ -201,31 +121,29 @@ public void onFailure(Call> call, Throwable throwable) { } } ); - - } catch (Exception ex) { logger.error(ex.getMessage()); } } public void respondSfw(String chatId) { - final String[] categories = new String[]{"waifu", "neko", "shinobu", "megumin", "hug", "awoo", "kiss", "lick", "smug", "blush", "smile", "wave", "nom", "glomp", "slap", "kill", "happy", "wink", "poke", "dance", "cringe"}; - String urlCategory = categories[random.nextInt(0, categories.length)]; + int categoryIndex = random.nextInt(Constants.SFW_CATEGORIES.size()); + String category = Constants.SFW_CATEGORIES.get(categoryIndex); + animeClient - .getSFW(urlCategory) + .getSFW(category) .enqueue( new Callback<>() { @Override public void onResponse(Call call, Response response) { - var picSDto = response.body(); - logger.info("\nResponse:\n" + picSDto.getUrl() + "\n"); - - if (picSDto == null) { + var picDto = response.body(); + if (picDto == null) { onFailure(call, new IOException("Response body is null")); return; } - var path = picSDto.getUrl(); + var path = picDto.getUrl(); + logger.info("\nResponse:\n" + path + "\n"); respondWithContent(chatId, path); } @@ -236,7 +154,6 @@ public void onFailure(Call call, Throwable throwable) { } } ); - } public void respondWithContent(String chatId, @NotNull String pathToSource) { @@ -247,7 +164,7 @@ public void respondWithContent(String chatId, @NotNull String pathToSource) { if (pathToSource.endsWith(".gif")) respondWithGif(chatId, file); - else if (picExtensions.stream().anyMatch(pathToSource::endsWith)) + else if (Constants.PICTURE_EXTENSIONS.stream().anyMatch(pathToSource::endsWith)) respondWithPicture(chatId, file); else respondWithVideo(chatId, file); } catch (IOException ex) { diff --git a/src/main/java/org/hentaibot/constants/Constants.java b/src/main/java/org/hentaibot/constants/Constants.java new file mode 100644 index 0000000..9aad303 --- /dev/null +++ b/src/main/java/org/hentaibot/constants/Constants.java @@ -0,0 +1,96 @@ +package org.hentaibot.constants; + +import java.util.Arrays; +import java.util.List; +import java.util.Map; + +public class Constants { + public static final List TAGS = Arrays.asList( + "ass", "anal", "cum", "creampie", "hentai", "masturbation", + "public", "orgy", "elf", "yuri", "pussy", "glasses", "blowjob", + "handjob", "footjob", "boobs", "thighs", "ahe_gao", "uniform", "gangbang", "tentacles", "genshin_impact", "naruto", "bleach" + ); + + public static final List ARTISTS = Arrays.asList( + "shexyo", "theobrobine", "cutesexyrobutts", "sakimichan", "krabby_(artist)", "lexaiduer", "miraihikari", + "LumiNyu", "lime_(purple_haze", "nepcill", "sciamano240", "neoartcore", "dandon_fuga", "zaphn", "flou", + "prywinko", "alexander_dinh", "kittew", "supullim", "tofuubear", "azto_dio", "derpixon", "kinkymation", "ggc", "afrobull" + ); + + public static final List SFW_CATEGORIES = Arrays.asList( + "waifu", "neko", "shinobu", "megumin", "hug", "awoo", "kiss", "lick", "smug", "blush", "smile", "wave", "nom", "glomp", "slap", "kill", "happy", "wink", "poke", "dance", "cringe" + ); + + public static final List PICTURE_EXTENSIONS = + Arrays.asList(".jpg", ".jpeg", ".png", ".webp", ".avif", ".jfif"); + + public static final Map MESSAGES = Map.of( + "UNRECOGNIZED_INPUT", "I don't understand you. For list of all supported commands, try /help", + "START", """ + Hi! I'm HentaiBot. I can send you some spicy anime pics. + Use me if you're 18+. + For list of all supported commands, try /help + """, + "HELP", """ + List of commands available for now: + /help + /hentai + /anime + /help_tags + """, + "HELP_TAGS", """ + List of tags available for now: + /tags_anal + /tags_cum + /tags_creampie + /tags_hentai + /tags_masturbation + /tags_public + /tags_orgy + /tags_elf + /tags_yuri + /tags_pussy + /tags_glasses + /tags_blowjob + /tags_handjob + /tags_footjob + /tags_boobs + /tags_thighs + /tags_ahe_gao + /tags_uniform + /tags_gangbang + /tags_tentacles + /tags_genshin_impact + /tags_naruto + /tags_bleach + /tags_shexyo + /tags_theobrobine + /tags_cutesexyrobutts + /tags_sakimichan + /tags_krabby_(artist) + /tags_lexaiduer + /tags_miraihikari + /tags_LumiNyu + /tags_lime_(purple_haze) + /tags_nepcill + /tags_sciamano240 + /tags_neoartcore + /tags_dandon_fuga + /tags_zaphn + /tags_flou + /tags_prywinko + /tags_alexander_dinh + /tags_kittew + /tags_supullim + /tags_tofuubear + /tags_azto_dio + /tags_derpixon + /tags_kinkymation + /tags_ggc + /tags_afrobull + """, + "INVALID_COMMAND", "Invalid command. For list of all supported commands, try /help", + "INVALID_TAG", "Invalid tag. For list of all supported tags, try /help_tags", + "NO_PICS", "No pics found for this tag. Try another one." + ); +}