feat: 添加视频状态检查API以判断视频是否已下架
This commit is contained in:
parent
d4449cd41a
commit
dbfc2502d7
51
is_offline_api.py
Normal file
51
is_offline_api.py
Normal file
@ -0,0 +1,51 @@
|
||||
#!/usr/bin/env python3
|
||||
# app.py
|
||||
import requests
|
||||
from flask import Flask, jsonify, abort
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
|
||||
def check_video_removed(video_id):
|
||||
"""
|
||||
调用 Dailymotion API 判断视频是否已下架/删除
|
||||
返回:
|
||||
1 → 已被删除 / 不存在 / 已下架 / 设为私有
|
||||
0 → 正常公开中
|
||||
"""
|
||||
url = f"https://api.dailymotion.com/video/{video_id}"
|
||||
params = {"fields": "published,private,status"}
|
||||
try:
|
||||
resp = requests.get(url, params=params, timeout=10)
|
||||
except requests.RequestException as exc:
|
||||
# 网络错误时返回 503,让上游知道需要重试
|
||||
abort(503, description=f"Upstream request failed: {exc}")
|
||||
|
||||
# 404 → 不存在或已被删除
|
||||
if resp.status_code == 404:
|
||||
return 1
|
||||
|
||||
# 其他非 2xx 状态码 → 直接透传给客户端
|
||||
if resp.status_code // 100 != 2:
|
||||
abort(resp.status_code, description=resp.text)
|
||||
|
||||
data = resp.json()
|
||||
# published=False 或 private=True 都视作“已下架”
|
||||
if not data.get("published", False) or data.get("private", False):
|
||||
return 1
|
||||
|
||||
return 0
|
||||
|
||||
|
||||
@app.route("/video/<video_id>/status", methods=["GET"])
|
||||
def video_status(video_id):
|
||||
removed = check_video_removed(video_id)
|
||||
return jsonify({"video_id": video_id, "removed": removed})
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
# 支持通过环境变量覆盖监听地址和端口
|
||||
import os
|
||||
host = os.getenv("HOST", "0.0.0.0")
|
||||
port = int(os.getenv("PORT", "5100"))
|
||||
app.run(host=host, port=port, debug=False)
|
Loading…
x
Reference in New Issue
Block a user