93 lines
3.0 KiB
Python
93 lines
3.0 KiB
Python
from flask import Flask
|
|
from flask_cors import CORS
|
|
import logging
|
|
import os
|
|
|
|
app = Flask(__name__)
|
|
CORS(app) # 允许跨域访问
|
|
|
|
# 配置日志
|
|
# 确保logs目录存在
|
|
logs_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'handlers', 'Rankings', 'logs')
|
|
os.makedirs(logs_dir, exist_ok=True)
|
|
|
|
logging.basicConfig(
|
|
level=logging.INFO,
|
|
format='%(asctime)s - %(levelname)s - %(message)s',
|
|
handlers=[
|
|
logging.FileHandler(os.path.join(logs_dir, 'app.log'), encoding='utf-8'),
|
|
logging.StreamHandler()
|
|
]
|
|
)
|
|
|
|
# 导入路由
|
|
from routers.rank_api_routes import api
|
|
|
|
# 注册路由
|
|
@app.route('/')
|
|
def index():
|
|
"""API首页"""
|
|
from flask import jsonify
|
|
return jsonify({
|
|
"name": "抖音播放量数据API服务",
|
|
"version": "2.0",
|
|
"description": "主程序服务 - 整合小程序API功能",
|
|
"endpoints": {
|
|
"/api/videos": "获取视频列表 (支持分页和排序)",
|
|
"/api/top": "获取热门视频榜单",
|
|
"/api/search": "搜索视频",
|
|
"/api/detail": "获取视频详情",
|
|
"/api/stats": "获取统计信息",
|
|
"/api/health": "健康检查"
|
|
},
|
|
"features": [
|
|
"分页支持",
|
|
"多种排序方式",
|
|
"搜索功能",
|
|
"详情查看",
|
|
"统计分析",
|
|
"小程序优化"
|
|
]
|
|
})
|
|
|
|
# 注册小程序API路由
|
|
@app.route('/api/videos')
|
|
def get_videos():
|
|
return api.get_videos()
|
|
|
|
@app.route('/api/top')
|
|
def get_top():
|
|
return api.get_top()
|
|
|
|
@app.route('/api/search')
|
|
def search():
|
|
return api.search()
|
|
|
|
@app.route('/api/detail')
|
|
def get_detail():
|
|
return api.get_detail()
|
|
|
|
@app.route('/api/stats')
|
|
def get_stats():
|
|
return api.get_stats()
|
|
|
|
@app.route('/api/health')
|
|
def health_check():
|
|
return api.health_check()
|
|
|
|
if __name__ == '__main__':
|
|
print("启动主程序服务...")
|
|
print("服务地址: http://localhost:5000")
|
|
print("API接口列表:")
|
|
print(" - GET / 显示API信息")
|
|
print(" - GET /api/videos?page=1&limit=20&sort=playcount 获取视频列表(总播放量排序)")
|
|
print(" - GET /api/videos?page=1&limit=20&sort=growth 获取视频列表(增长排序,默认昨天到今天的差值)")
|
|
print(" - GET /api/videos?page=1&limit=20&sort=growth&start_date=2025-10-16&end_date=2025-10-17 获取视频列表(自定义日期范围增长排序)")
|
|
print(" - GET /api/top?limit=10 获取热门榜单")
|
|
print(" - GET /api/search?q=关键词&page=1&limit=10 搜索视频")
|
|
print(" - GET /api/detail?id=视频ID 获取视频详情")
|
|
print(" - GET /api/stats 获取统计信息")
|
|
print(" - GET /api/health 健康检查")
|
|
print("专为小程序优化:分页、搜索、详情、统计、增长排序、自定义日期范围")
|
|
|
|
app.run(host='0.0.0.0', port=5000, debug=True) |