Update xhs.py
Browse files
xhs.py
CHANGED
@@ -1,9 +1,29 @@
|
|
1 |
from flask import Flask, request, jsonify
|
2 |
import re
|
3 |
import requests
|
|
|
|
|
4 |
|
5 |
app = Flask(__name__)
|
6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
def extract_url(text):
|
8 |
"""从文本中提取小红书链接(短链接或完整链接)"""
|
9 |
# 匹配形如 http://xhslink.com/xxxx 的短链接
|
@@ -69,6 +89,7 @@ def convert_links(text):
|
|
69 |
return {"status": "success", "message": "链接转换成功", "results": results}
|
70 |
|
71 |
@app.route('/api/convert', methods=['POST'])
|
|
|
72 |
def api_convert():
|
73 |
"""API端点,接收POST请求并转换链接"""
|
74 |
if not request.is_json:
|
@@ -85,6 +106,7 @@ def api_convert():
|
|
85 |
return jsonify(result)
|
86 |
|
87 |
@app.route('/api/health', methods=['GET'])
|
|
|
88 |
def health_check():
|
89 |
"""健康检查端点"""
|
90 |
return jsonify({"status": "ok", "message": "服务正常运行"})
|
@@ -108,6 +130,13 @@ def home():
|
|
108 |
<p>这是一个将小红书分享链接转换为真实可访问链接的API服务。</p>
|
109 |
|
110 |
<h2>API使用说明</h2>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
111 |
<h3>转换链接</h3>
|
112 |
<p><strong>端点:</strong> <code>/api/convert</code></p>
|
113 |
<p><strong>方法:</strong> POST</p>
|
@@ -148,4 +177,4 @@ def home():
|
|
148 |
|
149 |
if __name__ == '__main__':
|
150 |
# 默认运行在7860端口,适配Hugging Face Spaces
|
151 |
-
app.run(host='0.0.0.0', port=7860, debug=False,threaded=True)
|
|
|
1 |
from flask import Flask, request, jsonify
|
2 |
import re
|
3 |
import requests
|
4 |
+
import os
|
5 |
+
from functools import wraps
|
6 |
|
7 |
app = Flask(__name__)
|
8 |
|
9 |
+
# 从环境变量获取API密钥,如果不存在则使用默认值
|
10 |
+
API_KEY = os.environ.get("XHS_API_KEY", "default_secret_key")
|
11 |
+
|
12 |
+
# API密钥验证装饰器
|
13 |
+
def require_api_key(f):
|
14 |
+
@wraps(f)
|
15 |
+
def decorated_function(*args, **kwargs):
|
16 |
+
# 从请求头获取API密钥
|
17 |
+
api_key = request.headers.get('x-api-key')
|
18 |
+
|
19 |
+
# 验证API密钥
|
20 |
+
if api_key and api_key == API_KEY:
|
21 |
+
return f(*args, **kwargs)
|
22 |
+
else:
|
23 |
+
return jsonify({"status": "error", "message": "无效的API密钥"}), 401
|
24 |
+
|
25 |
+
return decorated_function
|
26 |
+
|
27 |
def extract_url(text):
|
28 |
"""从文本中提取小红书链接(短链接或完整链接)"""
|
29 |
# 匹配形如 http://xhslink.com/xxxx 的短链接
|
|
|
89 |
return {"status": "success", "message": "链接转换成功", "results": results}
|
90 |
|
91 |
@app.route('/api/convert', methods=['POST'])
|
92 |
+
@require_api_key
|
93 |
def api_convert():
|
94 |
"""API端点,接收POST请求并转换链接"""
|
95 |
if not request.is_json:
|
|
|
106 |
return jsonify(result)
|
107 |
|
108 |
@app.route('/api/health', methods=['GET'])
|
109 |
+
@require_api_key
|
110 |
def health_check():
|
111 |
"""健康检查端点"""
|
112 |
return jsonify({"status": "ok", "message": "服务正常运行"})
|
|
|
130 |
<p>这是一个将小红书分享链接转换为真实可访问链接的API服务。</p>
|
131 |
|
132 |
<h2>API使用说明</h2>
|
133 |
+
<h3>认证</h3>
|
134 |
+
<p>所有API请求都需要在请求头中包含有效的API密钥:</p>
|
135 |
+
<pre>
|
136 |
+
x-api-key: [您的API密钥]
|
137 |
+
</pre>
|
138 |
+
<p>API密钥可以通过环境变量 <code>XHS_API_KEY</code> 设置</p>
|
139 |
+
|
140 |
<h3>转换链接</h3>
|
141 |
<p><strong>端点:</strong> <code>/api/convert</code></p>
|
142 |
<p><strong>方法:</strong> POST</p>
|
|
|
177 |
|
178 |
if __name__ == '__main__':
|
179 |
# 默认运行在7860端口,适配Hugging Face Spaces
|
180 |
+
app.run(host='0.0.0.0', port=7860, debug=False, threaded=True)
|