Update app.py
Browse files
app.py
CHANGED
@@ -1,10 +1,11 @@
|
|
1 |
import os
|
|
|
2 |
import urllib.parse
|
3 |
-
from flask import Flask, render_template, request, redirect, url_for, flash, jsonify
|
4 |
from flask_login import LoginManager, UserMixin, login_user, logout_user, login_required, current_user
|
5 |
from werkzeug.security import check_password_hash, generate_password_hash
|
6 |
from dotenv import load_dotenv
|
7 |
-
|
8 |
|
9 |
# 加载环境变量
|
10 |
load_dotenv()
|
@@ -37,6 +38,25 @@ users = {
|
|
37 |
)
|
38 |
}
|
39 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
@login_manager.user_loader
|
41 |
def load_user(user_id):
|
42 |
return users.get(user_id)
|
@@ -78,69 +98,60 @@ def convert():
|
|
78 |
if not original_url:
|
79 |
return jsonify({"status": "error", "message": "订阅链接不能为空"})
|
80 |
|
81 |
-
# 使用自定义转换器处理(处理整个转换逻辑)
|
82 |
try:
|
83 |
-
#
|
84 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
85 |
|
86 |
-
# 如果转换失败
|
87 |
-
if result.get('status') == 'error':
|
88 |
-
return jsonify(result)
|
89 |
-
|
90 |
-
# 如果返回的是字符串,说明是配置内容,我们需要将它放入临时文件并提供下载链接
|
91 |
-
# 但是在HF Space中,我们可以直接通过API返回内容
|
92 |
-
if isinstance(result.get('result'), str):
|
93 |
-
# 这里我们直接返回配置文件内容的下载链接
|
94 |
-
# 实际部署时,我们会添加一个下载路由
|
95 |
-
response_data = {
|
96 |
-
"status": "success",
|
97 |
-
"message": "转换成功",
|
98 |
-
"result": f"/download?target={target}&token={_generate_download_token(original_url)}"
|
99 |
-
}
|
100 |
-
# 保存转换结果到会话中,用于下载
|
101 |
-
if not hasattr(current_user, 'conversion_results'):
|
102 |
-
current_user.conversion_results = {}
|
103 |
-
current_user.conversion_results[_generate_download_token(original_url)] = {
|
104 |
-
'content': result.get('result'),
|
105 |
-
'target': target
|
106 |
-
}
|
107 |
-
return jsonify(response_data)
|
108 |
-
|
109 |
-
# 否则返回结果
|
110 |
-
return jsonify(result)
|
111 |
-
|
112 |
except Exception as e:
|
113 |
return jsonify({"status": "error", "message": f"处理失败: {str(e)}"})
|
114 |
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
@app.route('/download')
|
123 |
-
@login_required
|
124 |
-
def download():
|
125 |
-
target = request.args.get('target', 'clash')
|
126 |
-
token = request.args.get('token', '')
|
127 |
-
|
128 |
-
# 获取保存的转换结果
|
129 |
-
if not hasattr(current_user, 'conversion_results') or token not in current_user.conversion_results:
|
130 |
-
return "配置文件不存在或已过期", 404
|
131 |
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
|
145 |
# 不要在这里调用 app.run()
|
146 |
# 让 Hugging Face 的 WSGI 服务器来运行应用
|
|
|
1 |
import os
|
2 |
+
import requests
|
3 |
import urllib.parse
|
4 |
+
from flask import Flask, render_template, request, redirect, url_for, flash, jsonify, Response
|
5 |
from flask_login import LoginManager, UserMixin, login_user, logout_user, login_required, current_user
|
6 |
from werkzeug.security import check_password_hash, generate_password_hash
|
7 |
from dotenv import load_dotenv
|
8 |
+
import time
|
9 |
|
10 |
# 加载环境变量
|
11 |
load_dotenv()
|
|
|
38 |
)
|
39 |
}
|
40 |
|
41 |
+
# 检查 subconverter 是否启动
|
42 |
+
def check_subconverter():
|
43 |
+
max_retries = 10
|
44 |
+
for i in range(max_retries):
|
45 |
+
try:
|
46 |
+
response = requests.get('http://localhost:25500/version', timeout=2)
|
47 |
+
if response.status_code == 200:
|
48 |
+
print(f"Subconverter 服务已启动: {response.text}")
|
49 |
+
return True
|
50 |
+
except:
|
51 |
+
pass
|
52 |
+
print(f"等待 Subconverter 服务启动... ({i+1}/{max_retries})")
|
53 |
+
time.sleep(2)
|
54 |
+
print("警告: Subconverter 服务可能未正常启动")
|
55 |
+
return False
|
56 |
+
|
57 |
+
# 应用启动时检查 subconverter 服务
|
58 |
+
check_subconverter()
|
59 |
+
|
60 |
@login_manager.user_loader
|
61 |
def load_user(user_id):
|
62 |
return users.get(user_id)
|
|
|
98 |
if not original_url:
|
99 |
return jsonify({"status": "error", "message": "订阅链接不能为空"})
|
100 |
|
|
|
101 |
try:
|
102 |
+
# 检查subconverter服务是否可用
|
103 |
+
try:
|
104 |
+
version_check = requests.get('http://localhost:25500/version', timeout=2)
|
105 |
+
if version_check.status_code != 200:
|
106 |
+
return jsonify({"status": "error", "message": "订阅转换服务暂时不可用,请稍后再试"})
|
107 |
+
except:
|
108 |
+
return jsonify({"status": "error", "message": "订阅转换服务暂时不可用,请稍后再试"})
|
109 |
+
|
110 |
+
# 构建转换请求参数
|
111 |
+
params = {
|
112 |
+
'target': target,
|
113 |
+
'url': original_url,
|
114 |
+
'insert': 'false',
|
115 |
+
'config': backend_url,
|
116 |
+
'emoji': 'true',
|
117 |
+
'list': 'false',
|
118 |
+
'xudp': 'false',
|
119 |
+
'udp': 'false',
|
120 |
+
'tfo': 'false',
|
121 |
+
'expand': 'true',
|
122 |
+
'scv': 'false',
|
123 |
+
'fdn': 'false',
|
124 |
+
'new_name': 'true'
|
125 |
+
}
|
126 |
+
|
127 |
+
# 通过URL参数构建转换链接
|
128 |
+
base_url = request.host_url.rstrip('/')
|
129 |
+
convert_url = f"{base_url}/api/sub?{urllib.parse.urlencode(params)}"
|
130 |
+
|
131 |
+
return jsonify({"status": "success", "result": convert_url})
|
132 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
133 |
except Exception as e:
|
134 |
return jsonify({"status": "error", "message": f"处理失败: {str(e)}"})
|
135 |
|
136 |
+
@app.route('/api/sub')
|
137 |
+
def subscribe_api():
|
138 |
+
"""代理 subconverter 的转换请求"""
|
139 |
+
try:
|
140 |
+
# 获取所有URL参数
|
141 |
+
params = request.args.to_dict()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
142 |
|
143 |
+
# 调用本地 subconverter 服务
|
144 |
+
sub_url = f"http://localhost:25500/sub?{urllib.parse.urlencode(params)}"
|
145 |
+
response = requests.get(sub_url, timeout=30)
|
146 |
+
|
147 |
+
# 返回原始响应
|
148 |
+
return Response(
|
149 |
+
response.content,
|
150 |
+
status=response.status_code,
|
151 |
+
content_type=response.headers.get('Content-Type', 'text/plain')
|
152 |
+
)
|
153 |
+
except Exception as e:
|
154 |
+
return f"转换失败: {str(e)}", 500
|
155 |
|
156 |
# 不要在这里调用 app.run()
|
157 |
# 让 Hugging Face 的 WSGI 服务器来运行应用
|