下面是一段监控指定抖音用户首页作品信息的程序代码,需要优化以下,从本地指定文档读取的访问地址地址原来为:“https://api.cenguigui.cn/api/douyin/user.php?url=https://www.douyin.com/user/MS4wLjABAAAAMsNm8EEeoiSOJ4cu7395eOFV4aomv7PwH_jikyF892eNI_iA0fJw_pqQXagTTRMU”将访问地址修改为:“https://api.cenguigui.cn/api/douyin/user.php?url=”+本地读取到的“https://www.douyin.com/user/MS4wLjABAAAAMsNm8EEeoiSOJ4cu7395eOFV4aomv7PwH_jikyF892eNI_iA0fJw_pqQXagTTRMU”。一个地址是一个用户,表格显示作者在最前面,如果有多个用户,显示数据排列按文档中用户排列顺序显示,发布时间如果日期为当日,则将发布时间设定为第一次发现此视频的详细时间。原代码如下:import requests
import sqlite3
import time
import logging
import smtplib
from email.mime.text import MIMEText
from pathlib import Path
from datetime import datetime
from threading import Thread
from flask import Flask, render_template
import json
# 配置日志
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s',
handlers=[
logging.FileHandler('douyin_monitor.log'),
logging.StreamHandler()
]
)
# 配置类
class Config:
# 邮件配置
SMTP_SERVER = 'smtp.example.com'
SMTP_PORT = 587
EMAIL_USER = 'your_email@example.com'
EMAIL_PASSWORD = 'your_password'
EMAIL_RECEIVER = 'receiver@example.com'
# 通知阈值
LIKE_THRESHOLD = 5
COMMENT_THRESHOLD = 3
# Web配置
WEB_HOST = '0.0.0.0'
WEB_PORT = 5000
class DouyinMonitor:
def __init__(self, config_file='config.txt', db_file='douyin_data.db'):
self.config_file = config_file
self.db_file = db_file
self.init_db()
self.web_app = self.create_web_app()
def init_db(self):
"""初始化数据库"""
with sqlite3.connect(self.db_file) as conn:
cursor = conn.cursor()
# 添加变化量字段用于记录变化
cursor.execute('''
CREATE TABLE IF NOT EXISTS douyin_data (
aweme_id TEXT PRIMARY KEY,
nickname TEXT,
comment INTEGER,
prev_comment INTEGER DEFAULT 0,
comment_change INTEGER DEFAULT 0,
like_count INTEGER,
prev_like INTEGER DEFAULT 0,
like_change INTEGER DEFAULT 0,
time TEXT,
last_update TEXT,
UNIQUE(aweme_id)
)
''')
conn.commit()
def create_web_app(self):
"""创建Flask应用"""
app = Flask(__name__, template_folder='templates') # 显式指定模板目录
@app.route('/')
def index():
data = self.get_all_data()
return render_template('index.html', data=data)
@app.route('/api/data')
def api_data():
data = self.get_all_data()
return json.dumps(data, ensure_ascii=False)
return app
def get_all_data(self):
"""获取所有数据用于Web展示"""
with sqlite3.connect(self.db_file) as conn:
conn.row_factory = sqlite3.Row
cursor = conn.cursor()
cursor.execute('''
SELECT * FROM douyin_data
ORDER BY last_update DESC
''')
return [dict(row) for row in cursor.fetchall()]
def read_urls_from_config(self):
"""从配置文件读取URL"""
try:
with open(self.config_file, 'r') as f:
urls = [line.strip() for line in f if line.strip()]
return urls
except FileNotFoundError:
logging.error(f"配置文件 {self.config_file} 不存在")
return []
def fetch_data(self, url):
"""获取抖音数据"""
try:
response = requests.get(url, timeout=10)
response.raise_for_status()
data = response.json()
if data.get("code") == 200:
return data.get("data", [])
else:
logging.warning(f"API返回错误: {data.get('msg', '未知错误')}")
return []
except Exception as e:
logging.error(f"获取数据失败: {e}")
return []
def process_data(self, data):
"""处理数据并返回结构化结果"""
processed = []
for item in data:
processed.append({
'aweme_id': item.get('aweme_id', ''),
'nickname': item.get('nickname', ''),
'comment': item.get('comment', 0),
'like_count': item.get('like', 0),
'time': item.get('time', ''),
'last_update': datetime.now().strftime('%Y-%m-%d %H:%M:%S')
})
return processed
def send_notification(self, item, changes):
"""发送邮件通知"""
try:
subject = f"抖音数据变化通知 - {item['nickname']}"
content = f"""
检测到抖音数据有显著变化:
视频ID: {item['aweme_id']}
作者: {item['nickname']}
时间: {item['time']}
变化情况:
- 点赞数: {changes['like_change']} (新: {item['like_count']}, 旧: {item['prev_like']})
- 评论数: {changes['comment_change']} (新: {item['comment']}, 旧: {item['prev_comment']})
最后更新时间: {item['last_update']}
"""
msg = MIMEText(content, 'plain', 'utf-8')
msg['Subject'] = subject
msg['From'] = Config.EMAIL_USER
msg['To'] = Config.EMAIL_RECEIVER
with smtplib.SMTP(Config.SMTP_SERVER, Config.SMTP_PORT) as server:
server.starttls()
server.login(Config.EMAIL_USER, Config.EMAIL_PASSWORD)
server.sendmail(Config.EMAIL_USER, [Config.EMAIL_RECEIVER], msg.as_string())
logging.info(f"已发送通知邮件: {subject}")
except Exception as e:
logging.error(f"发送邮件失败: {e}")
def update_db(self, new_data):
"""更新数据库并检测显著变化"""
notifications = []
with sqlite3.connect(self.db_file) as conn:
cursor = conn.cursor()
for item in new_data:
# 获取旧数据
cursor.execute('''
SELECT comment, like_count
FROM douyin_data
WHERE aweme_id = ?
''', (item['aweme_id'],))
old_data = cursor.fetchone()
# 计算变化量
changes = {
'comment_change': 0,
'like_change': 0
}
if old_data:
old_comment, old_like = old_data
changes['comment_change'] = item['comment'] - old_comment
changes['like_change'] = item['like_count'] - old_like
# 检查是否达到通知阈值
if (changes['like_change'] >= Config.LIKE_THRESHOLD or
changes['comment_change'] >= Config.COMMENT_THRESHOLD):
item.update({
'prev_comment': old_comment,
'prev_like': old_like
})
notifications.append((item.copy(), changes.copy()))
# 更新或插入数据
cursor.execute('''
INSERT OR REPLACE INTO douyin_data
(aweme_id, nickname, comment, prev_comment, comment_change,
like_count, prev_like, like_change, time, last_update)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
''', (
item['aweme_id'], item['nickname'],
item['comment'], item.get('prev_comment', 0),
changes['comment_change'],
item['like_count'], item.get('prev_like', 0),
changes['like_change'],
item['time'], item['last_update']
))
conn.commit()
# 发送通知
for item, changes in notifications:
self.send_notification(item, changes)
return len(notifications)
def run_monitor(self, interval=3600):
"""运行监控程序"""
logging.info("抖音数据监控程序启动")
while True:
try:
urls = self.read_urls_from_config()
if not urls:
logging.warning("没有找到有效的URL,请检查配置文件")
time.sleep(60)
continue
for url in urls:
logging.info(f"正在处理URL: {url}")
raw_data = self.fetch_data(url)
if not raw_data:
continue
processed_data = self.process_data(raw_data)
notified = self.update_db(processed_data)
logging.info(
f"数据更新完成 - 处理: {len(processed_data)}条, "
f"触发通知: {notified}条"
)
logging.info(f"等待 {interval//60} 分钟后再次检查...")
time.sleep(interval)
except KeyboardInterrupt:
logging.info("用户中断,监控服务停止")
break
except Exception as e:
logging.error(f"监控出错: {e}", exc_info=True)
time.sleep(300)
def run_web(self):
"""运行Web服务"""
logging.info(f"启动Web服务: http://{Config.WEB_HOST}:{Config.WEB_PORT}")
self.web_app.run(host=Config.WEB_HOST, port=Config.WEB_PORT)
def run(self):
"""启动监控和Web服务"""
# 创建模板文件 (如果不存在)
templates_dir = Path('templates')
try:
if not templates_dir.exists():
templates_dir.mkdir(exist_ok=True)
index_template = templates_dir / 'index.html'
if not index_template.exists():
with open(index_template, 'w', encoding='utf-8') as f:
f.write('''<!DOCTYPE html>
<html>
<head>
<title>抖音数据监控</title>
<meta charset="utf-8">
<style>
body { font-family: Arial, sans-serif; margin: 20px; }
table { border-collapse: collapse; width: 100%; }
th, td { border: 1px solid #ddd; padding: 8px; text-align: left; }
th { background-color: #f2f2f2; }
tr:nth-child(even) { background-color: #f9f9f9; }
.positive { color: green; }
.negative { color: red; }
</style>
</head>
<body>
<h1>抖音数据监控</h1>
<p>最后更新时间: {{ data[0].last_update if data else '无数据' }}</p>
<table>
<thead>
<tr>
<th>ID</th>
<th>作者</th>
<th>评论数</th>
<th>评论变化</th>
<th>点赞数</th>
<th>点赞变化</th>
<th>发布时间</th>
<th>最后更新</th>
</tr>
</thead>
<tbody>
{% for item in data %}
<tr>
<td>{{ item.aweme_id }}</td>
<td>{{ item.nickname }}</td>
<td>{{ item.comment }}</td>
<td class="{% if item.comment_change > 0 %}positive{% elif item.comment_change < 0 %}negative{% endif %}">
{{ item.comment_change }}
</td>
<td>{{ item.like_count }}</td>
<td class="{% if item.like_change > 0 %}positive{% elif item.like_change < 0 %}negative{% endif %}">
{{ item.like_change }}
</td>
<td>{{ item.time }}</td>
<td>{{ item.last_update }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</body>
</html>''')
logging.info("已创建模板文件")
except Exception as e:
logging.error(f"创建模板文件失败: {e}")
# 启动监控线程
monitor_thread = Thread(target=self.run_monitor)
monitor_thread.daemon = True
monitor_thread.start()
# 启动Web服务
self.run_web()
if __name__ == "__main__":
# 创建配置文件示例 (如果不存在)
config_path = Path('config.txt')
if not config_path.exists():
with open(config_path, 'w') as f:
f.write("https://api.cenguigui.cn/api/douyin/user.php?url=https://www.douyin.com/user/MS4wLjABAAAAMsNm8EEeoiSOJ4cu7395eOFV4aomv7PwH_jikyF892eNI_iA0fJw_pqQXagTTRMU\n")
# 运行程序
monitor = DouyinMonitor()
monitor.run()
最新发布