Skip to content

Commit

Permalink
增加tg消息推送文件同步情况、机器人命令手动执行任务
Browse files Browse the repository at this point in the history
  • Loading branch information
907739769 committed Jun 4, 2024
1 parent 116d8e0 commit 999b4e0
Show file tree
Hide file tree
Showing 10 changed files with 184 additions and 52 deletions.
2 changes: 2 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ PikPak/
20240524 修改日志打印、每日任务只同步每日更新、新增任务每7天同步一次全量数据、修改更新文件逻辑,文件时间戳改成网页上面的,而不去head请求网站,减少服务器压力,未设置同步网站的情况下随机从网站池中获取同步网站
20240526 每日任务增加启动任务前的一分钟内随机等待时间、修改同步全量文件的频次为每三天
20240529 保留两天日志、增加启动内存参数设置、完成时间打印优化
20240604 增加tg消息推送文件同步情况、机器人命令手动执行任务,新增参数tgToken、tgUserId
```

Expand All @@ -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
```

一键命令部署
Expand Down
12 changes: 3 additions & 9 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,20 +64,14 @@

<dependency>
<groupId>org.telegram</groupId>
<artifactId>telegrambots-longpolling</artifactId>
<version>7.2.1</version>
</dependency>

<dependency>
<groupId>org.telegram</groupId>
<artifactId>telegrambots-client</artifactId>
<version>7.2.1</version>
<artifactId>telegrambots</artifactId>
<version>6.1.0</version>
</dependency>

<dependency>
<groupId>org.telegram</groupId>
<artifactId>telegrambots-abilities</artifactId>
<version>7.2.1</version>
<version>6.1.0</version>
</dependency>
</dependencies>

Expand Down
5 changes: 5 additions & 0 deletions src/main/java/cn/jackding/xiaoyasync/SyncService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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;
Expand Down
35 changes: 35 additions & 0 deletions src/main/java/cn/jackding/xiaoyasync/Util.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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="每日更新/";
Expand Down
28 changes: 28 additions & 0 deletions src/main/java/cn/jackding/xiaoyasync/config/Config.java
Original file line number Diff line number Diff line change
@@ -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;
}

}
66 changes: 57 additions & 9 deletions src/main/java/cn/jackding/xiaoyasync/tgbot/SyncBot.java
Original file line number Diff line number Diff line change
@@ -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();
}

}
34 changes: 0 additions & 34 deletions src/main/java/cn/jackding/xiaoyasync/tgbot/Telegram.java

This file was deleted.

48 changes: 48 additions & 0 deletions src/main/java/cn/jackding/xiaoyasync/tgbot/TgSendMsg.java
Original file line number Diff line number Diff line change
@@ -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) {

}
}

0 comments on commit 999b4e0

Please sign in to comment.