xulh
commited on
Commit
·
58ca197
1
Parent(s):
d9cf6d3
代码初始化
Browse files- utils/__init__.py +0 -0
- utils/utils.py +28 -0
- video/iqiyi.py +45 -2
utils/__init__.py
ADDED
File without changes
|
utils/utils.py
ADDED
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import hmac
|
2 |
+
import hashlib
|
3 |
+
import json
|
4 |
+
|
5 |
+
SECRET_KEY = "OArmXWpMeX8n" # 小程序中使用的 key
|
6 |
+
|
7 |
+
|
8 |
+
def generate_signature(data: dict, key: str = SECRET_KEY) -> str:
|
9 |
+
"""
|
10 |
+
生成签名
|
11 |
+
:param data: 请求数据 (dict)
|
12 |
+
:param key: 签名密钥
|
13 |
+
:return: 签名字符串
|
14 |
+
"""
|
15 |
+
data_str = json.dumps(data, separators=(',', ':'), ensure_ascii=False)
|
16 |
+
return hmac.new(key.encode(), data_str.encode(), hashlib.sha256).hexdigest()
|
17 |
+
|
18 |
+
|
19 |
+
def verify_signature(data: dict, client_signature: str, key: str = SECRET_KEY) -> bool:
|
20 |
+
"""
|
21 |
+
校验签名
|
22 |
+
:param data: 请求数据 (dict)
|
23 |
+
:param client_signature: 客户端提供的签名
|
24 |
+
:param key: 签名密钥
|
25 |
+
:return: 签名是否有效
|
26 |
+
"""
|
27 |
+
server_signature = generate_signature(data, key)
|
28 |
+
return hmac.compare_digest(server_signature, client_signature)
|
video/iqiyi.py
CHANGED
@@ -1,8 +1,9 @@
|
|
1 |
from fastapi import APIRouter, Query, HTTPException
|
2 |
import requests
|
|
|
|
|
3 |
|
4 |
routerVideo = APIRouter()
|
5 |
-
|
6 |
# 第三方API地址
|
7 |
EXTERNAL_API_URL = "https://api.suyanw.cn/api/Iqiyi_video_search.php"
|
8 |
|
@@ -38,7 +39,7 @@ async def video_search(
|
|
38 |
|
39 |
|
40 |
@routerVideo.get("/video-recommend/")
|
41 |
-
def video_search(
|
42 |
source: str = Query(..., description="资源类型"),
|
43 |
search_type: str = Query(..., description="返回格式,默认text可选json")
|
44 |
):
|
@@ -66,3 +67,45 @@ def video_search(
|
|
66 |
except requests.exceptions.RequestException as e:
|
67 |
# 处理请求错误
|
68 |
raise HTTPException(status_code=500, detail=f"请求第三方API出错: {str(e)}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
from fastapi import APIRouter, Query, HTTPException
|
2 |
import requests
|
3 |
+
from ..utils.utils import generate_signature
|
4 |
+
import time
|
5 |
|
6 |
routerVideo = APIRouter()
|
|
|
7 |
# 第三方API地址
|
8 |
EXTERNAL_API_URL = "https://api.suyanw.cn/api/Iqiyi_video_search.php"
|
9 |
|
|
|
39 |
|
40 |
|
41 |
@routerVideo.get("/video-recommend/")
|
42 |
+
async def video_search(
|
43 |
source: str = Query(..., description="资源类型"),
|
44 |
search_type: str = Query(..., description="返回格式,默认text可选json")
|
45 |
):
|
|
|
67 |
except requests.exceptions.RequestException as e:
|
68 |
# 处理请求错误
|
69 |
raise HTTPException(status_code=500, detail=f"请求第三方API出错: {str(e)}")
|
70 |
+
|
71 |
+
|
72 |
+
@routerVideo.post("/video/search")
|
73 |
+
def search_videos(date: str = Query(..., description="时间"),
|
74 |
+
videoName: str = Query(..., description="视频名称")):
|
75 |
+
"""
|
76 |
+
搜索视频接口
|
77 |
+
:param date: 时间
|
78 |
+
:param videoName: 视频名称
|
79 |
+
:return: 符合条件的视频列表
|
80 |
+
"""
|
81 |
+
api_url = "https://api.q168.vip/api/search"
|
82 |
+
params = {
|
83 |
+
date: date,
|
84 |
+
videoName: videoName
|
85 |
+
}
|
86 |
+
# 签名
|
87 |
+
sign = generate_signature(params)
|
88 |
+
|
89 |
+
# 设置请求头
|
90 |
+
headers = {
|
91 |
+
"Sign": sign,
|
92 |
+
"Accept": "application/json, text/plain, */*",
|
93 |
+
"origin": "https://wantwatch.me"
|
94 |
+
}
|
95 |
+
|
96 |
+
try:
|
97 |
+
# 发送请求到第三方API
|
98 |
+
response = requests.post(api_url, data=params, headers=headers)
|
99 |
+
response.raise_for_status() # 确保HTTP请求成功
|
100 |
+
|
101 |
+
# 返回第三方API的结果
|
102 |
+
return response.json()
|
103 |
+
|
104 |
+
except requests.exceptions.RequestException as e:
|
105 |
+
# 处理请求错误
|
106 |
+
raise HTTPException(status_code=500, detail=f"请求第三方API出错: {str(e)}")
|
107 |
+
|
108 |
+
|
109 |
+
if __name__ == '__main__':
|
110 |
+
timestamp_ms = int(time.time() * 1000)
|
111 |
+
search_videos(timestamp_ms, "我是刑警")
|