|
|
@@ -1,8 +1,7 @@
|
|
|
from fastapi import FastAPI, Request
|
|
|
from fastapi.staticfiles import StaticFiles
|
|
|
-from fastapi.responses import FileResponse
|
|
|
from fastapi.templating import Jinja2Templates
|
|
|
-from fastapi.responses import JSONResponse
|
|
|
+from fastapi.responses import JSONResponse, FileResponse
|
|
|
import uvicorn
|
|
|
import glob
|
|
|
import os
|
|
|
@@ -12,7 +11,7 @@ from utils import *
|
|
|
|
|
|
app = FastAPI(title="EH-Downloader", version="1.0.0")
|
|
|
|
|
|
-# 在应用启动时检查并创建data文件夹和targets.txt
|
|
|
+# 在应用启动时检查并创建data文件夹和targets.txt,以及proxy.txt
|
|
|
@app.on_event("startup")
|
|
|
async def startup_event():
|
|
|
# 检查并创建data文件夹
|
|
|
@@ -31,6 +30,15 @@ async def startup_event():
|
|
|
print(f"创建文件: {targets_file}")
|
|
|
else:
|
|
|
print(f"文件已存在: {targets_file}")
|
|
|
+
|
|
|
+ # 检查并创建proxy.txt文件
|
|
|
+ proxy_file = "proxy.txt"
|
|
|
+ if not os.path.exists(proxy_file):
|
|
|
+ with open(proxy_file, 'w', encoding='utf-8') as f:
|
|
|
+ f.write("127.0.0.1:7890\n")
|
|
|
+ print(f"创建文件: {proxy_file}")
|
|
|
+ else:
|
|
|
+ print(f"文件已存在: {proxy_file}")
|
|
|
|
|
|
# 挂载静态文件和模板
|
|
|
app.mount("/static", StaticFiles(directory="static"), name="static")
|
|
|
@@ -44,7 +52,24 @@ async def favicon():
|
|
|
@app.get("/")
|
|
|
async def home(request: Request):
|
|
|
"""主页面"""
|
|
|
- return templates.TemplateResponse("index.html", {"request": request})
|
|
|
+ # 读取proxy.txt中的代理列表
|
|
|
+ proxies = []
|
|
|
+ try:
|
|
|
+ with open("proxy.txt", 'r', encoding='utf-8') as f:
|
|
|
+ proxies = [line.strip() for line in f.readlines() if line.strip()]
|
|
|
+ except Exception as e:
|
|
|
+ print(f"读取proxy.txt失败: {e}")
|
|
|
+ proxies = ["127.0.0.1:7890"]
|
|
|
+
|
|
|
+ # 如果没有代理配置,使用默认值
|
|
|
+ if not proxies:
|
|
|
+ proxies = ["127.0.0.1:7890"]
|
|
|
+
|
|
|
+ return templates.TemplateResponse("index.html", {
|
|
|
+ "request": request,
|
|
|
+ "proxies": proxies,
|
|
|
+ "default_proxy": proxies[0] if proxies else "127.0.0.1:7890"
|
|
|
+ })
|
|
|
|
|
|
@app.post("/load_urls")
|
|
|
async def load_urls():
|
|
|
@@ -89,18 +114,27 @@ async def clear_output():
|
|
|
})
|
|
|
|
|
|
class ProxyRequest(BaseModel):
|
|
|
- ip: str
|
|
|
- port: str
|
|
|
+ proxy: str # 修改为单个proxy字段
|
|
|
|
|
|
@app.post("/download_urls")
|
|
|
async def download_urls(req: ProxyRequest):
|
|
|
- proxy = f"http://{req.ip}:{req.port}"
|
|
|
+ # 解析proxy字符串为ip和port
|
|
|
+ if ":" in req.proxy:
|
|
|
+ ip, port = req.proxy.split(":", 1)
|
|
|
+ proxy = f"http://{ip}:{port}"
|
|
|
+ else:
|
|
|
+ proxy = None
|
|
|
msg = await run_step1(proxy)
|
|
|
return JSONResponse({"success": True, "message": msg})
|
|
|
|
|
|
@app.post("/download_images")
|
|
|
async def download_images(req: ProxyRequest):
|
|
|
- proxy = f"http://{req.ip}:{req.port}"
|
|
|
+ # 解析proxy字符串为ip和port
|
|
|
+ if ":" in req.proxy:
|
|
|
+ ip, port = req.proxy.split(":", 1)
|
|
|
+ proxy = f"http://{ip}:{port}"
|
|
|
+ else:
|
|
|
+ proxy = None
|
|
|
msg = await run_step2(proxy)
|
|
|
return JSONResponse({"success": True, "message": msg})
|
|
|
|