前言

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

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

demo:https://pan.pkll.de

安装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 服务管理

监控服务

创建文件:/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
# 重新加载 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 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

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
sudo systemctl daemon-reload
sudo systemctl enable tg-dedup.timer
sudo systemctl start tg-dedup.timer