main.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. from fastapi import FastAPI, Request, Form
  2. from fastapi.staticfiles import StaticFiles
  3. from fastapi.templating import Jinja2Templates
  4. from fastapi.responses import JSONResponse
  5. import uvicorn
  6. import os
  7. from pydantic import BaseModel
  8. from utils import *
  9. app = FastAPI(title="下载工具", version="1.0.0")
  10. # 在应用启动时检查并创建data文件夹和targets.txt
  11. @app.on_event("startup")
  12. async def startup_event():
  13. # 检查并创建data文件夹
  14. data_dir = "data"
  15. if not os.path.exists(data_dir):
  16. os.makedirs(data_dir)
  17. print(f"创建目录: {data_dir}")
  18. # 检查并创建targets.txt文件
  19. targets_file = os.path.join(data_dir, "targets.txt")
  20. if not os.path.exists(targets_file):
  21. with open(targets_file, 'w', encoding='utf-8') as f:
  22. f.write("# 在这里添加目标URL,每行一个\n")
  23. f.write("# 示例:\n")
  24. f.write("# https://example.com/file1.zip\n")
  25. f.write("# https://example.com/image.jpg\n")
  26. print(f"创建文件: {targets_file}")
  27. else:
  28. print(f"文件已存在: {targets_file}")
  29. # 挂载静态文件和模板
  30. app.mount("/static", StaticFiles(directory="static"), name="static")
  31. templates = Jinja2Templates(directory="templates")
  32. @app.get("/")
  33. async def home(request: Request):
  34. """主页面"""
  35. return templates.TemplateResponse("index.html", {"request": request})
  36. @app.post("/load_urls")
  37. async def load_urls():
  38. """读取 targets.txt 文件中的URL"""
  39. try:
  40. file_path = "data/targets.txt"
  41. # 读取文件内容
  42. with open(file_path, 'r', encoding='utf-8') as f:
  43. urls = [line.strip() for line in f.readlines() if line.strip()]
  44. # 过滤掉空行和注释行(以#开头的行)
  45. urls = [url for url in urls if url and not url.startswith('#')]
  46. if not urls:
  47. return JSONResponse({
  48. "success": True,
  49. "message": "targets.txt 文件为空,请在data/targets.txt中添加URL",
  50. "urls": []
  51. })
  52. return JSONResponse({
  53. "success": True,
  54. "message": f"成功读取 {len(urls)} 个URL",
  55. "urls": urls
  56. })
  57. except Exception as e:
  58. return JSONResponse({
  59. "success": False,
  60. "message": f"读取文件时出错: {str(e)}",
  61. "urls": []
  62. })
  63. @app.post("/clear")
  64. async def clear_output():
  65. """清除输出"""
  66. return JSONResponse({
  67. "success": True,
  68. "message": "输出已清除",
  69. "output": ""
  70. })
  71. class ProxyRequest(BaseModel):
  72. ip: str
  73. port: str
  74. @app.post("/download_urls")
  75. async def download_urls(req: ProxyRequest):
  76. proxy = f"http://{req.ip}:{req.port}"
  77. msg = await run_step1(proxy)
  78. return JSONResponse({"success": True, "message": msg})
  79. @app.post("/download_images")
  80. async def download_images(req: ProxyRequest):
  81. proxy = f"http://{req.ip}:{req.port}"
  82. msg = await run_step2(proxy)
  83. return JSONResponse({"success": True, "message": msg})
  84. if __name__ == "__main__":
  85. uvicorn.run("main:app", host="0.0.0.0", port=8000, reload=True)