4 Commits

Author SHA1 Message Date
ctwj
236051f6c4 Merge pull request #20 from ctwj/feat_xunlei_opt
Feat xunlei opt
2025-11-11 23:35:42 +08:00
ctwj
01bc8f0450 update: ui 2025-11-11 23:01:49 +08:00
ctwj
0e88374905 Merge branch 'main' of https://github.com/ctwj/urldb 2025-11-11 01:37:45 +08:00
ctwj
6b8d2b3cf0 update: 优化推送策略 2025-11-07 23:21:04 +08:00
2 changed files with 67 additions and 32 deletions

View File

@@ -279,6 +279,20 @@ func (r *ResourceRepositoryImpl) SearchWithFilters(params map[string]interface{}
db = db.Where("pan_id = ?", panEntity.ID)
}
}
case "exclude_ids": // 添加exclude_ids参数支持
if excludeIDs, ok := value.([]uint); ok && len(excludeIDs) > 0 {
// 限制排除ID的数量避免SQL语句过长
maxExcludeIDs := 5000 // 限制排除ID数量避免SQL语句过长
if len(excludeIDs) > maxExcludeIDs {
// 只取最近的maxExcludeIDs个ID进行排除
startIndex := len(excludeIDs) - maxExcludeIDs
truncatedExcludeIDs := excludeIDs[startIndex:]
db = db.Where("id NOT IN ?", truncatedExcludeIDs)
utils.Debug("SearchWithFilters: 排除ID数量过多截取最近%d个ID", len(truncatedExcludeIDs))
} else {
db = db.Where("id NOT IN ?", excludeIDs)
}
}
}
}

View File

@@ -167,11 +167,18 @@ func (s *TelegramBotServiceImpl) loadConfig() error {
// Start 启动机器人服务
func (s *TelegramBotServiceImpl) Start() error {
if s.isRunning {
// 确保机器人完全停止状态
if s.isRunning && s.bot != nil {
utils.Info("[TELEGRAM:SERVICE] Telegram Bot 服务已经在运行中")
return nil
}
// 如果isRunning为true但bot为nil说明状态不一致需要清理
if s.isRunning && s.bot == nil {
utils.Info("[TELEGRAM:SERVICE] 检测到不一致状态,清理残留资源")
s.isRunning = false
}
// 加载配置
if err := s.loadConfig(); err != nil {
return fmt.Errorf("加载配置失败: %v", err)
@@ -289,6 +296,8 @@ func (s *TelegramBotServiceImpl) Stop() error {
return nil
}
utils.Info("[TELEGRAM:SERVICE] 开始停止 Telegram Bot 服务")
s.isRunning = false
// 安全地发送停止信号给消息循环
@@ -304,6 +313,9 @@ func (s *TelegramBotServiceImpl) Stop() error {
s.cronScheduler.Stop()
}
// 清理机器人实例以避免冲突
s.bot = nil
utils.Info("[TELEGRAM:SERVICE] Telegram Bot 服务已停止")
return nil
}
@@ -524,6 +536,12 @@ func (s *TelegramBotServiceImpl) setupWebhook() error {
func (s *TelegramBotServiceImpl) messageLoop() {
utils.Info("[TELEGRAM:MESSAGE] 开始监听 Telegram 消息更新...")
// 确保机器人实例存在
if s.bot == nil {
utils.Error("[TELEGRAM:MESSAGE] 机器人实例为空,无法启动消息监听循环")
return
}
u := tgbotapi.NewUpdate(0)
u.Timeout = 60
@@ -541,6 +559,11 @@ func (s *TelegramBotServiceImpl) messageLoop() {
utils.Info("[TELEGRAM:MESSAGE] updates channel 已关闭,退出消息监听循环")
return
}
// 在处理消息前检查机器人是否仍在运行
if !s.isRunning || s.bot == nil {
utils.Info("[TELEGRAM:MESSAGE] 机器人已停止,忽略接收到的消息")
return
}
if update.Message != nil {
utils.Info("[TELEGRAM:MESSAGE] 接收到新消息更新")
s.handleMessage(update.Message)
@@ -1049,16 +1072,16 @@ func (s *TelegramBotServiceImpl) findResourcesForChannel(channel entity.Telegram
func (s *TelegramBotServiceImpl) findLatestResources(channel entity.TelegramChannel, excludeResourceIDs []uint) []interface{} {
params := s.buildFilterParams(channel)
// 在数据库查询中排除已推送的资源
if len(excludeResourceIDs) > 0 {
params["exclude_ids"] = excludeResourceIDs
}
// 使用现有的搜索功能,按更新时间倒序获取最新资源
resources, _, err := s.resourceRepo.SearchWithFilters(params)
if err != nil {
utils.Error("[TELEGRAM:PUSH] 获取最新资源失败: %v", err)
return []interface{}{}
}
// 排除最近推送过的资源
if len(excludeResourceIDs) > 0 {
resources = s.excludePushedResources(resources, excludeResourceIDs)
return s.findRandomResources(channel, excludeResourceIDs) // 回退到随机策略
}
// 应用时间限制
@@ -1067,8 +1090,8 @@ func (s *TelegramBotServiceImpl) findLatestResources(channel entity.TelegramChan
}
if len(resources) == 0 {
utils.Info("[TELEGRAM:PUSH] 没有找到符合条件的最新资源")
return []interface{}{}
utils.Info("[TELEGRAM:PUSH] 没有找到符合条件的最新资源,尝试获取随机资源")
return s.findRandomResources(channel, excludeResourceIDs) // 回退到随机策略
}
// 返回最新资源(第一条)
@@ -1083,6 +1106,11 @@ func (s *TelegramBotServiceImpl) findTransferredResources(channel entity.Telegra
// 添加转存链接条件
params["has_save_url"] = true
// 在数据库查询中排除已推送的资源
if len(excludeResourceIDs) > 0 {
params["exclude_ids"] = excludeResourceIDs
}
// 优先获取有转存链接的资源
resources, _, err := s.resourceRepo.SearchWithFilters(params)
if err != nil {
@@ -1090,11 +1118,6 @@ func (s *TelegramBotServiceImpl) findTransferredResources(channel entity.Telegra
return []interface{}{}
}
// 排除最近推送过的资源
if len(excludeResourceIDs) > 0 {
resources = s.excludePushedResources(resources, excludeResourceIDs)
}
// 应用时间限制
if channel.TimeLimit != "none" && len(resources) > 0 {
resources = s.applyTimeFilter(resources, channel.TimeLimit)
@@ -1118,23 +1141,19 @@ func (s *TelegramBotServiceImpl) findRandomResources(channel entity.TelegramChan
// 如果是已转存优先策略但没有找到转存资源,这里会回退到随机策略
// 此时不需要额外的转存链接条件,让随机函数处理
// 先尝试获取候选资源列表,然后从中排除已推送的资源
var candidateResources []entity.Resource
var err error
// 在数据库查询中排除已推送的资源
if len(excludeResourceIDs) > 0 {
params["exclude_ids"] = excludeResourceIDs
}
// 使用搜索功能获取候选资源,然后过滤
params["limit"] = 100 // 获取更多候选资源
candidateResources, _, err = s.resourceRepo.SearchWithFilters(params)
candidateResources, _, err := s.resourceRepo.SearchWithFilters(params)
if err != nil {
utils.Error("[TELEGRAM:PUSH] 获取候选资源失败: %v", err)
return []interface{}{}
}
// 排除最近推送过的资源
if len(excludeResourceIDs) > 0 {
candidateResources = s.excludePushedResources(candidateResources, excludeResourceIDs)
}
// 应用时间限制
if channel.TimeLimit != "none" && len(candidateResources) > 0 {
candidateResources = s.applyTimeFilter(candidateResources, channel.TimeLimit)
@@ -1809,11 +1828,12 @@ func (s *TelegramBotServiceImpl) addPushedResourceID(chatID int64, resourceID ui
history = []uint{}
}
// 检查是否已经超过100条记录
if len(history) >= 10000 {
// 清空历史记录,重新开始
history = []uint{}
utils.Info("[TELEGRAM:PUSH] 频道 %d 推送历史记录已满(10000条),清空重置", chatID)
// 检查是否已经超过5000条记录
if len(history) >= 5000 {
// 移除旧的2500条记录保留最新的2500条记录
startIndex := len(history) - 2500
history = history[startIndex:]
utils.Info("[TELEGRAM:PUSH] 频道 %d 推送历史记录已满(5000条)移除旧的2500条记录保留最新的2500条", chatID)
}
// 添加新的资源ID到历史记录
@@ -1896,10 +1916,11 @@ func (s *TelegramBotServiceImpl) loadPushHistory() error {
resourceIDs = append(resourceIDs, uint(resourceID))
}
// 只保留最多100条记录
if len(resourceIDs) > 100 {
// 保留最新的100条记录
resourceIDs = resourceIDs[len(resourceIDs)-100:]
// 只保留最多5000条记录
if len(resourceIDs) > 5000 {
// 保留最新的5000条记录
startIndex := len(resourceIDs) - 5000
resourceIDs = resourceIDs[startIndex:]
}
s.pushHistory[chatID] = resourceIDs