diff --git a/Dockerfile b/Dockerfile index 33ea741..adf071d 100644 --- a/Dockerfile +++ b/Dockerfile @@ -11,5 +11,7 @@ ENV excludeList="" ENV threadPoolNum="99" ENV syncUrl="" ENV syncDir="" +ENV tgToken="" +ENV tgUserId="" ENV JAVA_OPTS="-Xms512m -Xmx512m" ENTRYPOINT ["sh","-c","java -jar $JAVA_OPTS -XX:+OptimizeStringConcat -XX:+PrintGCDetails -Xloggc:/log/gc.log -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/log /application.jar"] \ No newline at end of file diff --git a/README.md b/README.md index 8a5caee..dbe948f 100644 --- a/README.md +++ b/README.md @@ -53,6 +53,7 @@ PikPak/ 20240524 修改日志打印、每日任务只同步每日更新、新增任务每7天同步一次全量数据、修改更新文件逻辑,文件时间戳改成网页上面的,而不去head请求网站,减少服务器压力,未设置同步网站的情况下随机从网站池中获取同步网站 20240526 每日任务增加启动任务前的一分钟内随机等待时间、修改同步全量文件的频次为每三天 20240529 保留两天日志、增加启动内存参数设置、完成时间打印优化 +20240604 增加tg消息推送文件同步情况、机器人命令手动执行任务,新增参数tgToken、tgUserId ``` @@ -61,12 +62,16 @@ PikPak/ ``` 部署前参数需要修改 +必要参数 /volume1/docker-data/xiaoya/xiaoya修改成媒体库路径 +可选参数 runAfterStartup 启动是否立即执行同步任务 默认不启用0,启用填1 excludeList 排除列表 默认为空 设置不进行同步及删除的目录例如每日更新/动漫/.*,每日更新/动漫剧场版/.* syncUrl 同步网站 默认从网站池中随机选一个 可选https://icyou.eu.org/或者https://lanyuewan.cn/ syncDir 同步路径 指定同步路径 默认空 同步全站,可填入 每日更新/电影/ 或者 每日更新/ 等具体的网站路径 threadPoolNum 设置线程数默认99 不建议修改 +tgToken tg机器人token,通过t.me/BotFather机器人创建bot获取token +tgUserId tg用户id,通过t.me/userinfobot机器人获取userId ``` 一键命令部署 diff --git a/pom.xml b/pom.xml index 334c792..039ca3f 100644 --- a/pom.xml +++ b/pom.xml @@ -64,20 +64,14 @@ org.telegram - telegrambots-longpolling - 7.2.1 - - - - org.telegram - telegrambots-client - 7.2.1 + telegrambots + 6.1.0 org.telegram telegrambots-abilities - 7.2.1 + 6.1.0 diff --git a/src/main/java/cn/jackding/xiaoyasync/SyncService.java b/src/main/java/cn/jackding/xiaoyasync/SyncService.java index 6055c8a..a390d7d 100644 --- a/src/main/java/cn/jackding/xiaoyasync/SyncService.java +++ b/src/main/java/cn/jackding/xiaoyasync/SyncService.java @@ -82,10 +82,12 @@ public void syncFiles(String syncDir) { init(); try { log.info("媒体库同步任务开始"); + Util.sendTgMsg("媒体库同步任务开始"); log.info("排除列表:{}", excludeList); syncFilesRecursively(useBaseUrl + Util.encode(syncDir), localDir + syncDir.replace("/", File.separator).replaceAll("[:*?\"<>|]", "_"), syncDir); } catch (Exception e) { log.warn("媒体库同步任务失败"); + Util.sendTgMsg("媒体库同步任务失败"); log.error("", e); } @@ -405,14 +407,17 @@ public void checkThreadPoolStatus() { } log.info("以上是下载的文件"); log.info("共下载{}个文件", downloadFiles.size()); + Util.sendTgMsg("共下载" + downloadFiles.size() + "个文件"); } else { log.info("没有新的内容更新"); + Util.sendTgMsg("没有新的内容更新"); } long milliseconds = (System.currentTimeMillis() - currentTimeMillis) < 10000 ? (System.currentTimeMillis() - currentTimeMillis) : (System.currentTimeMillis() - currentTimeMillis - 10000); long hours = TimeUnit.MILLISECONDS.toHours(milliseconds); long minutes = TimeUnit.MILLISECONDS.toMinutes(milliseconds) - TimeUnit.HOURS.toMinutes(hours); long seconds = TimeUnit.MILLISECONDS.toSeconds(milliseconds) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(milliseconds)); log.info("媒体库同步任务全部完成耗时:{}小时{}分钟{}秒", hours, minutes, seconds); + Util.sendTgMsg("媒体库同步任务全部完成耗时:" + hours + "小时" + minutes + "分钟" + seconds + "秒"); currentTimeMillis = 0; downloadFiles = null; executorService = null; diff --git a/src/main/java/cn/jackding/xiaoyasync/Util.java b/src/main/java/cn/jackding/xiaoyasync/Util.java index 282a00c..9482808 100644 --- a/src/main/java/cn/jackding/xiaoyasync/Util.java +++ b/src/main/java/cn/jackding/xiaoyasync/Util.java @@ -1,6 +1,14 @@ package cn.jackding.xiaoyasync; +import cn.jackding.xiaoyasync.config.Config; +import cn.jackding.xiaoyasync.tgbot.SyncBot; +import cn.jackding.xiaoyasync.tgbot.TgSendMsg; import lombok.extern.log4j.Log4j2; +import org.apache.commons.lang3.StringUtils; +import org.telegram.telegrambots.bots.DefaultBotOptions; +import org.telegram.telegrambots.meta.TelegramBotsApi; +import org.telegram.telegrambots.meta.exceptions.TelegramApiException; +import org.telegram.telegrambots.updatesreceivers.DefaultBotSession; import java.io.UnsupportedEncodingException; import java.net.URLDecoder; @@ -68,4 +76,31 @@ public static void randomSleep() { } } + public static void sendTgMsg(String msg) { + new TgSendMsg().sendMsg(msg); + } + + public static void initBot() { + if (StringUtils.isBlank(Config.tgUserId) || StringUtils.isBlank(Config.tgToken)) { + return; + } + TelegramBotsApi telegramBotsApi; + try { + telegramBotsApi = new TelegramBotsApi(DefaultBotSession.class); + } catch (TelegramApiException e) { + log.error("", e); + return; + } + DefaultBotOptions botOptions = new DefaultBotOptions(); +// botOptions.setProxyHost(Config.telegramBotProxyHost); +// botOptions.setProxyPort(Config.telegramBotProxyPort); +// botOptions.setProxyType(DefaultBotOptions.ProxyType.HTTP); + //使用AbilityBot创建的事件响应机器人 + try { + telegramBotsApi.registerBot(new SyncBot(botOptions)); + } catch (TelegramApiException e) { + log.error("", e); + } + } + } diff --git a/src/main/java/cn/jackding/xiaoyasync/XiaoyaSyncApplication.java b/src/main/java/cn/jackding/xiaoyasync/XiaoyaSyncApplication.java index 66ca96d..8e6916c 100644 --- a/src/main/java/cn/jackding/xiaoyasync/XiaoyaSyncApplication.java +++ b/src/main/java/cn/jackding/xiaoyasync/XiaoyaSyncApplication.java @@ -33,6 +33,7 @@ public static void main(String[] args) { @Bean CommandLineRunner run(SyncService syncService) { return args -> { + Util.initBot(); if ("1".equals(runAfterStartup)) { if(StringUtil.isBlank(syncDir)){ syncDir="每日更新/"; diff --git a/src/main/java/cn/jackding/xiaoyasync/config/Config.java b/src/main/java/cn/jackding/xiaoyasync/config/Config.java new file mode 100644 index 0000000..8914815 --- /dev/null +++ b/src/main/java/cn/jackding/xiaoyasync/config/Config.java @@ -0,0 +1,28 @@ +package cn.jackding.xiaoyasync.config; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; + +/** + * @Author Jack + * @Date 2022/8/2 21:04 + * @Version 1.0.0 + */ +@Component +public class Config { + + public static String tgToken; + + public static String tgUserId; + + @Value("${tgToken}") + public void setTgToken(String tgToken) { + Config.tgToken = tgToken; + } + + @Value("${tgUserId}") + public void setTgUserId(String tgUserId) { + Config.tgUserId = tgUserId; + } + +} diff --git a/src/main/java/cn/jackding/xiaoyasync/tgbot/SyncBot.java b/src/main/java/cn/jackding/xiaoyasync/tgbot/SyncBot.java index d0a0b00..3aaec59 100644 --- a/src/main/java/cn/jackding/xiaoyasync/tgbot/SyncBot.java +++ b/src/main/java/cn/jackding/xiaoyasync/tgbot/SyncBot.java @@ -1,21 +1,69 @@ package cn.jackding.xiaoyasync.tgbot; -import org.telegram.telegrambots.client.okhttp.OkHttpTelegramClient; -import org.telegram.telegrambots.longpolling.util.LongPollingSingleThreadUpdateConsumer; -import org.telegram.telegrambots.meta.api.objects.Update; -import org.telegram.telegrambots.meta.generics.TelegramClient; +import cn.jackding.xiaoyasync.SyncService; +import cn.jackding.xiaoyasync.config.Config; +import org.apache.commons.lang3.StringUtils; +import org.telegram.abilitybots.api.bot.AbilityBot; +import org.telegram.abilitybots.api.objects.Ability; +import org.telegram.telegrambots.bots.DefaultBotOptions; + +import static org.telegram.abilitybots.api.objects.Locality.USER; +import static org.telegram.abilitybots.api.objects.Privacy.CREATOR; /** * @Author Jack - * @Date 2024/6/4 17:17 + * @Date 2024/6/4 21:33 * @Version 1.0.0 */ -public class SyncBot implements LongPollingSingleThreadUpdateConsumer { - private TelegramClient telegramClient = new OkHttpTelegramClient(""); +public class SyncBot extends AbilityBot { + + + public SyncBot() { + super(Config.tgToken, ""); + } + + public SyncBot(DefaultBotOptions options) { + super(Config.tgToken, "", options); + } @Override - public void consume(Update update) { - // TODO + public long creatorId() { + return Long.parseLong(Config.tgUserId); + } + + public Ability sync() { + return Ability.builder() + .name("sync") + .info("同步媒体库") + .privacy(CREATOR) + .locality(USER) + .input(0) + .action(ctx -> { + silent.send("开始执行同步任务", ctx.chatId()); + new SyncService().syncFiles("每日更新/"); + silent.send("同步任务执行完成", ctx.chatId()); + }) + .build(); + } + + public Ability syncDir() { + return Ability.builder() + .name("syncDir") + .info("同步指定媒体库路径") + .privacy(CREATOR) + .locality(USER) + .input(0) + .action(ctx -> { + String parameter = ctx.firstArg(); + if (StringUtils.isBlank(parameter)) { + silent.send("请加上路径参数", ctx.chatId()); + return; + } + silent.send("开始执行同步指定路径任务", ctx.chatId()); + new SyncService().syncFiles(parameter); + silent.send("同步指定路径任务执行完成", ctx.chatId()); + }) + .build(); } } diff --git a/src/main/java/cn/jackding/xiaoyasync/tgbot/Telegram.java b/src/main/java/cn/jackding/xiaoyasync/tgbot/Telegram.java deleted file mode 100644 index 88e2fec..0000000 --- a/src/main/java/cn/jackding/xiaoyasync/tgbot/Telegram.java +++ /dev/null @@ -1,34 +0,0 @@ -package cn.jackding.xiaoyasync.tgbot; - -import org.telegram.telegrambots.longpolling.TelegramBotsLongPollingApplication; -import org.telegram.telegrambots.meta.exceptions.TelegramApiException; - -/** - * 机器人注册中心 - * - * @Author Jack - * @Date 2022/9/2 13:54 - * @Version 1.0.0 - */ -public class Telegram { - - public static void registerBot() { - String botToken = "12345:YOUR_TOKEN"; - try { - TelegramBotsLongPollingApplication botsApplication = new TelegramBotsLongPollingApplication(); - botsApplication.registerBot(botToken, new SyncBot()); - } catch (TelegramApiException e) { - throw new RuntimeException(e); - } -// TelegramBotsApi telegramBotsApi = new TelegramBotsApi(DefaultBotSession.class); -// // Set up Http proxy -// DefaultBotOptions botOptions = new DefaultBotOptions(); -// -// botOptions.setProxyHost(Config.telegramBotProxyHost); -// botOptions.setProxyPort(Config.telegramBotProxyPort); -// botOptions.setProxyType(DefaultBotOptions.ProxyType.HTTP); -// //使用AbilityBot创建的事件响应机器人 -// telegramBotsApi.registerBot(new MovieAbilityBot(Config.telegramBotToken, Config.telegramBotName, botOptions)); - } - -} diff --git a/src/main/java/cn/jackding/xiaoyasync/tgbot/TgSendMsg.java b/src/main/java/cn/jackding/xiaoyasync/tgbot/TgSendMsg.java new file mode 100644 index 0000000..9f93fbe --- /dev/null +++ b/src/main/java/cn/jackding/xiaoyasync/tgbot/TgSendMsg.java @@ -0,0 +1,48 @@ +package cn.jackding.xiaoyasync.tgbot; + +import cn.jackding.xiaoyasync.config.Config; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.StringUtils; +import org.telegram.telegrambots.bots.TelegramLongPollingBot; +import org.telegram.telegrambots.meta.api.methods.send.SendMessage; +import org.telegram.telegrambots.meta.api.objects.Update; +import org.telegram.telegrambots.meta.exceptions.TelegramApiException; + +/** + * @Author Jack + * @Date 2024/6/4 17:17 + * @Version 1.0.0 + */ +@Slf4j +public class TgSendMsg extends TelegramLongPollingBot { + + public void sendMsg(String msg) { + if (StringUtils.isBlank(Config.tgUserId) || StringUtils.isBlank(Config.tgToken)) { + return; + } + SendMessage message = new SendMessage(); + message.setChatId(Config.tgUserId); + message.setText(msg); + + try { + execute(message); + } catch (TelegramApiException e) { + log.error("", e); + } + } + + @Override + public String getBotUsername() { + return ""; + } + + @Override + public String getBotToken() { + return Config.tgToken; + } + + @Override + public void onUpdateReceived(Update update) { + + } +}