前言

订阅了挺多的网盘影视tg频道,但是打开tg还是觉得麻烦,于是就简单让cursor糊了个实时更新订阅的几个网盘资源频道消息。

项目地址:https://github.com/DEKVIW/tgmonitor

demo:https://pan.pkll.de

下面是详细的使用教程

电报api获取步骤
  • 打开 Telegram 官方网站:https://my.telegram.org

  • 使用你的 Telegram 账户登录(扫码或手机验证码登录)

  • 登录成功后点击:

    API Development Tools

  • 填写基本信息(随便填也可以):

    • App title: 如 newquark
    • Short name: 如 quarkbot
    • 其他选填
  • 点击“Create application”

  • 创建完成后页面会显示你的:

    • api_id
    • api_hash

保存好这两个信息,后面要用。

安装python

1
2
3
4
5
6
7
8
9
10
11
12
13
14
sudo apt update                                      # 更新软件包列表
sudo apt install python3 python3-pip python3-venv -y # 安装 Python3、pip 和 venv 工具

cd /root/data/python/tg-monitor # 进入你的项目目录

python3 -m venv venv # 创建名为 venv 的虚拟环境

source venv/bin/activate # 激活虚拟环境

pip install -r requirements.txt # 安装项目依赖

# ...在虚拟环境中开发或运行你的项目...

deactivate # 退出虚拟环境

配置数据库

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
sudo apt update
# 更新软件包列表

sudo apt install postgresql postgresql-contrib -y
# 安装 PostgreSQL 数据库及其常用扩展

sudo -u postgres psql
# 以 postgres 管理员身份进入 PostgreSQL 命令行

CREATE DATABASE tg_monitor;
-- 创建名为 tg_monitor 的数据库

CREATE USER tg_user WITH PASSWORD 'your_password';
-- 创建名为 tg_user 的数据库用户,并设置密码(your_password请替换为你自己的密码)

GRANT ALL PRIVILEGES ON DATABASE tg_monitor TO tg_user;
-- 授予 tg_user 用户对 tg_monitor 数据库的所有权限

\q
-- 退出 psql 命令行

手动启动服务

拉取项目代码

虚拟环境激活

1
2
cd /root/data/python/tg-monitor
source venv/bin/activate

手动启动/停止服务

  • 启动监控服务
1
python monitor.py
  • 启动 Web 服务
1
streamlit run web.py

默认端口 8501,可通过 —server.port 端口号 修改

可使用如下命令后台启动监控/Web 服务

1
2
nohup python monitor.py > data/monitor.log 2>&1 &
nohup streamlit run web.py --server.port 8501 --server.address 0.0.0.0 > data/web.log 2>&1 &
  • 停止后台进程(如用 nohup 启动)
1
2
3
ps aux | grep monitor.py
ps aux | grep streamlit
kill 进程号

管理和维护命令

1
2
3
4
5
6
python manage.py --list-channels                # 查看当前所有监听频道列表
python manage.py --add-channel 频道名 # 添加新频道(频道名不加@)支持多个一起增加(空格分隔)
python manage.py --del-channel 频道名 # 删除指定频道
python manage.py --edit-channel 旧频道名 新频道名 # 修改频道名(将旧频道名改为新频道名)
python manage.py --fix-tags # 修复数据库中消息的tags字段脏数据
python manage.py --dedup-links # 网盘链接去重,只保留每个链接最新的消息

systemd 服务管理

为了方便启动和停止项目,可以使用systemd。也可不做,用手动也行。

监控服务

创建文件:/etc/systemd/system/tg-monitor.service
内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
[Unit]
Description=TG Monitor Service
After=network.target

[Service]
User=root
WorkingDirectory=/root/data/python/tg-monitor
Environment="PATH=/root/data/python/tg-monitor/venv/bin"
ExecStart=/root/data/python/tg-monitor/venv/bin/python monitor.py
Restart=always

[Install]
WantedBy=multi-user.target

Web服务

创建文件:/etc/systemd/system/tg-monitor-web.service
内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
[Unit]
Description=TG Monitor Web Service
After=network.target

[Service]
User=root
WorkingDirectory=/root/data/python/tg-monitor
Environment="PATH=/root/data/python/tg-monitor/venv/bin"
ExecStart=/root/data/python/tg-monitor/venv/bin/streamlit run web.py --server.port 8501 --server.address 0.0.0.0
Restart=always

[Install]
WantedBy=multi-user.target

启动和管理服务

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# 重新加载 systemd 配置
sudo systemctl daemon-reload

# 启用服务(开机自启)
sudo systemctl enable tg-monitor
sudo systemctl enable tg-monitor-web

# 启动服务
sudo systemctl start tg-monitor
sudo systemctl start tg-monitor-web

# 重启服务
sudo systemctl restart tg-monitor
sudo systemctl restart tg-monitor-web

# 查看服务状态
sudo systemctl status tg-monitor
sudo systemctl status tg-monitor-web

# 查看日志(推荐用 journalctl)
sudo journalctl -u tg-monitor -f
sudo journalctl -u tg-monitor-web -f

# 停止服务
sudo systemctl stop tg-monitor
sudo systemctl stop tg-monitor-web

条目去重

相同的网盘链接只保存最新更新的条目信息,遇到更新间隔不超过1分钟保留网盘标签最多的条目。

systemd 定时去重和crontab二选一即可

systemd 定时去重

新建服务文件 /etc/systemd/system/tg-dedup.service

1
2
3
4
5
6
7
[Unit]
Description=TG Monitor Dedup Service

[Service]
Type=oneshot
WorkingDirectory=/root/data/python/tg-monitor
ExecStart=/root/data/python/tg-monitor/venv/bin/python manage.py --dedup-links

新建定时器 /etc/systemd/system/tg-dedup.timer

1
2
3
4
5
6
7
8
9
[Unit]
Description=Run TG Monitor Dedup daily

[Timer]
OnCalendar=*-*-* 02:00:00
Persistent=true

[Install]
WantedBy=timers.target

启用并启动定时器

1
2
3
4
sudo systemctl daemon-reload
sudo systemctl enable tg-dedup.timer
sudo systemctl start tg-dedup.timer
sudo systemctl status tg-dedup.timer

crontab

1
(crontab -l 2>/dev/null; echo "0 * * * * cd /root/data/python/tg-monitor && /root/data/python/tg-monitor/venv/bin/python manage.py --dedup-links-fast >> /root/data/python/tg-monitor/dedup.log 2>&1") | crontab -

数据库备份和恢复

备份

创建backup_db.sh文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#!/bin/bash

# 设置变量
BACKUP_DIR="/root/data/python/tg-monitor/backup"
DATE=$(date +%Y%m%d_%H%M%S)
BACKUP_FILE="$BACKUP_DIR/tg_monitor_$DATE.sql.gz"

# 设置数据库密码环境变量
export PGPASSWORD='你的数据库密码'

# 创建备份目录
mkdir -p $BACKUP_DIR

# 执行备份
pg_dump -U tg_user -h localhost -d tg_monitor | gzip > $BACKUP_FILE

# 清除密码环境变量
unset PGPASSWORD

# 只保留最新的3份备份
ls -t $BACKUP_DIR/tg_monitor_*.sql.gz | tail -n +4 | xargs -r rm

# 输出备份信息
echo "备份完成: $BACKUP_FILE"
echo "当前备份文件列表:"
ls -lh $BACKUP_DIR/tg_monitor_*.sql.gz

添加定时任务:

1
(crontab -l 2>/dev/null; echo "0 3 * * * /root/data/python/tg-monitor/backup_db.sh >> /root/data/python/tg-monitor/backup/backup.log 2>&1") | crontab -

恢复

恢复脚本:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#!/bin/bash

# 检查是否提供了备份文件参数
if [ -z "$1" ]; then
echo "请指定要恢复的备份文件"
echo "使用方法: ./restore_db.sh 备份文件名"
echo "例如: ./restore_db.sh tg_monitor_20240607_091557.sql.gz"
exit 1
fi

BACKUP_FILE="$1"

# 检查备份文件是否存在
if [ ! -f "$BACKUP_FILE" ]; then
echo "错误: 备份文件 $BACKUP_FILE 不存在"
exit 1
fi

# 设置数据库密码环境变量
export PGPASSWORD='数据库密码'

echo "开始恢复数据库..."

# 如果是压缩文件,先解压再恢复
if [[ "$BACKUP_FILE" == *.gz ]]; then
gunzip -c "$BACKUP_FILE" | psql -U tg_user -h localhost -d tg_monitor
else
psql -U tg_user -h localhost -d tg_monitor < "$BACKUP_FILE"
fi

# 清除密码环境变量
unset PGPASSWORD

echo "数据库恢复完成!"