live-forum/server/crawler/commerce/test_report_server.py
2026-03-24 11:27:37 +08:00

166 lines
5.5 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""
测试数据上报服务器
用于测试数据上报功能是否正常工作
使用方法:
1. 安装依赖: pip install flask
2. 运行服务器: python test_report_server.py
3. 在另一个终端运行爬虫: python main.py fetch-daren
4. 查看本服务器的输出,确认数据是否成功上报
"""
from flask import Flask, request, jsonify
from datetime import datetime
import json
app = Flask(__name__)
# 用于统计接收到的数据
received_count = 0
@app.route('/api/Streamers/ReportStreamerData', methods=['POST'])
def report_streamer_data():
"""接收数据上报"""
global received_count
received_count += 1
# 获取查询参数
category = request.args.get('category', 'unknown')
log_id = request.args.get('logId', 'unknown')
# 获取POST数据
try:
data = request.json
if not data:
return jsonify({
'success': False,
'message': '无效的数据格式'
}), 400
# 打印接收信息
print("\n" + "="*80)
print(f"📥 [{received_count}] 收到数据上报")
print("="*80)
print(f"⏰ 时间: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
print(f"📁 Category: {category}")
print(f"🆔 LogId: {log_id}")
# 打印元数据
if 'metadata' in data:
metadata = data['metadata']
print(f"\n📊 元数据:")
print(f" - 总数: {metadata.get('total', 0)}")
print(f" - 创建时间: {metadata.get('created_at', 'N/A')}")
print(f" - 来源: {metadata.get('source', 'N/A')}")
# 打印数据统计
if 'data' in data:
records = data['data']
print(f"\n📋 数据记录:")
print(f" - 记录数: {len(records)}")
# 显示前3条数据
if len(records) > 0:
print(f"\n📝 前3条数据预览:")
for i, record in enumerate(records[:3], 1):
print(f"\n {i}. {record.get('anchorName', 'Unknown')}")
print(f" Anchor ID: {record.get('anchorID', 'N/A')}")
print(f" 粉丝数: {record.get('fans', '0')}")
print(f" 直播状态: {record.get('liveStatus', 'N/A')}")
print(f" User ID: {record.get('userID', 'N/A')}")
# 统计直播中的达人
live_count = sum(1 for r in records if r.get('liveStatus') == '2')
if live_count > 0:
print(f"\n 🔴 正在直播: {live_count}")
print("\n" + "="*80)
print("✅ 数据上报成功")
print("="*80 + "\n")
# 返回成功响应
return jsonify({
'success': True,
'message': '数据上报成功',
'received_count': received_count,
'timestamp': datetime.now().isoformat()
}), 200
except Exception as e:
print(f"\n❌ 处理数据时发生错误: {e}")
return jsonify({
'success': False,
'message': f'服务器错误: {str(e)}'
}), 500
@app.route('/health', methods=['GET'])
def health():
"""健康检查接口"""
return jsonify({
'status': 'ok',
'message': '测试服务器运行正常',
'received_count': received_count
})
@app.route('/', methods=['GET'])
def index():
"""首页"""
return f"""
<html>
<head>
<title>数据上报测试服务器</title>
<style>
body {{ font-family: Arial, sans-serif; padding: 20px; }}
.status {{ color: green; font-weight: bold; }}
.info {{ background: #f0f0f0; padding: 10px; border-radius: 5px; }}
</style>
</head>
<body>
<h1>📡 数据上报测试服务器</h1>
<p class="status">✅ 服务器运行正常</p>
<div class="info">
<h2>状态信息</h2>
<p><strong>已接收数据:</strong> {received_count} 次</p>
<p><strong>API地址:</strong> /api/Streamers/ReportStreamerData</p>
<p><strong>健康检查:</strong> <a href="/health">/health</a></p>
</div>
<h2>使用方法</h2>
<ol>
<li>确保此服务器正在运行端口8080</li>
<li>配置爬虫的上报地址为: <code>http://localhost:8080/api/Streamers/ReportStreamerData?category=commerce&logId={{logId}}</code></li>
<li>运行爬虫: <code>python main.py fetch-daren</code></li>
<li>在终端查看上报详情</li>
</ol>
<h2>配置示例</h2>
<pre>
# config/settings.py
REPORT_URL = "http://localhost:8080/api/Streamers/ReportStreamerData?category=commerce&logId={{logId}}"
</pre>
</body>
</html>
"""
if __name__ == '__main__':
print("\n" + "="*80)
print("🚀 数据上报测试服务器启动中...")
print("="*80)
print(f"📍 监听地址: http://localhost:8080")
print(f"📡 API地址: http://localhost:8080/api/Streamers/ReportStreamerData")
print(f"💚 健康检查: http://localhost:8080/health")
print(f"🌐 首页: http://localhost:8080/")
print("\n💡 提示:")
print(" 1. 在浏览器打开 http://localhost:8080 查看状态")
print(" 2. 运行爬虫测试数据上报")
print(" 3. 按 Ctrl+C 停止服务器")
print("="*80 + "\n")
app.run(host='0.0.0.0', port=8080, debug=False)