前言

最近折腾了一个小工具,解决了我长期以来的一个痛点——Telegram 频道资源自动转存到夸克网盘

很多人喜欢在 Telegram 上关注各种资源分享频道,但 Telegram 本身不提供网盘转存功能,每次看到想要的资源都要手动复制链接、打开夸克、粘贴、转存,非常麻烦。于是我写了这个 telegram-quark-monitor 程序,实现全自动监听 + 转存。

项目已开源在 GitHub:wenxixixi-miao/telegram-quark-monitor


它能做什么

简单来说,这个程序会:

  1. 实时监听你关注的 Telegram 频道新消息

  2. 自动识别消息中的夸克网盘链接(pan.quark.cn/s/xxx

  3. 按关键字过滤,只转存你感兴趣的资源(比如”斗破苍穹”、”完美世界”)

  4. 自动转存到你指定的夸克网盘目录

  5. 智能去重,同一个资源 N 天内不会重复转存


功能亮点

  • 📡 实时监听 - Telegram 频道新消息秒级响应

  • 🔑 关键字过滤 - 只转存匹配的资源,不浪费空间

  • 🔗 自动提取链接 - 智能识别夸克网盘分享链接

  • ⏭️ 同名去重 - 同一关键字 5 天内已转存过则跳过

  • 🔧 后缀修正 - 大文件自动重命名为 .mp4

  • 🔍 历史搜索 - 支持搜索频道历史消息

  • 📢 通知推送 - 转存结果可推送通知(对接 Hermes Agent)

  • 🛡️ 失败重试 - 转存失败的消息不会标记为已处理


快速上手

1. 安装

1
2
3
4
5
6
7

git clone https://github.com/wenxixixi-miao/telegram-quark-monitor.git

cd telegram-quark-monitor

pip install telethon

2. 配置

复制配置模板并编辑:

1
2
3

cp monitor_config.example.json monitor_config.json

配置文件长这样:

1
2
3

{

“api_id”: 你的Telegram_API_ID,

1
2


“api_hash”: “你的Telegram_API_HASH”,

1
2


“phone”: “+86你的手机号”,

1
2


“channels”: [“Quark_Movies”],

1
2


“save_path”: “/自动转存”,

1
2


“keywords”: [“斗破苍穹”, “完美世界”],

1
2


“dedup_days”: 5

1
2
3
4


}

3. 获取 Telegram API 凭据

  1. 访问 https://my.telegram.org

  2. 登录你的 Telegram 账号

  3. 进入 “API development tools”

  4. 创建应用,获取 api_idapi_hash

4. 运行

1
2
3

python telegram_monitor.py

首次运行需要输入 Telegram 验证码,之后 session 会自动保存,下次启动无需重复验证。


核心代码解析

整个项目的核心其实不复杂,主要分几个模块:

配置加载

1
2
3

def load_config():

“””加载配置文件,缺失时给出提示”””

1
2


if not os.path.exists(CONFIG_FILE):

1
2


print(f”❌ 配置文件不存在: {CONFIG_FILE}”)

1
2


sys.exit(1)

1
2


with open(CONFIG_FILE, “r”, encoding=”utf-8”) as f:

1
2


return json.load(f)

1
2


夸克链接提取

用正则表达式匹配所有夸克网盘链接:

1
2
3

QUARK_URL_PATTERN = re.compile(r'https?://pan\.quark\.cn/s/[a-zA-Z0-9]+')

关键字去重

防止同一个资源短时间内重复转存:

1
2
3

def is_keyword_dedup(keyword, dedup_days=5):

“””检查关键字是否在去重期内”””

1
2


读取 keyword_dedup.json,比较时间戳

1
2


通知系统

转存结果写入队列,可对接外部推送:

1
2
3

def push_notify(title, detail=""):

“””将通知写入 notify_queue.json”””

1
2


notifications.append({

1
2


“time”: datetime.now().strftime(“%m-%d %H:%M”),

1
2


“title”: title,

1
2


“detail”: detail,

1
2


“sent”: False,

1
2


})

1
2



对接 Hermes Agent

这个程序还支持和我的 Hermes Agent 联动。通过 check_notify.py 脚本,可以定时检查转存结果并推送到微信或 Telegram:

1
2
3
4
5
6
7
8
9
10
11

# Hermes cron 配置

- name: quark-monitor-notify

schedule: "every 2m"

script: check_notify.py

no_agent: true

这样每当有新资源转存成功,Hermes Agent 会自动把结果推送给我,非常方便。


文件说明

| 文件 | 说明 |

|——|——|

| telegram_monitor.py | 主监听脚本 |

| monitor_config.json | 配置文件(不提交到 git) |

| quark_monitor.session | Telegram 登录 session |

| processed_messages.json | 已处理消息记录 |

| keyword_dedup.json | 关键字去重时间戳 |

| notify_queue.json | 通知队列 |

| monitor.log | 运行日志 |


总结

这个小工具虽然代码量不大,但解决了一个实际问题。现在我只需要配置好关键字,程序就会 24 小时自动帮我监听 Telegram 频道,看到匹配的资源就自动转存到夸克网盘,省去了大量手动操作的时间。

如果你也有类似的需求,欢迎去 GitHub 点个 Star 或者提 Issue。

项目地址:https://github.com/wenxixixi-miao/telegram-quark-monitor

基于 Cp0204/quark-auto-save 扩展开发,原项目协议 AGPL-3.0,本项目 MIT。