From cf10846d8e36f6afcb3656b9eb44b7d42f375c38 Mon Sep 17 00:00:00 2001 From: zhengshunjin <2667579369@qq.com> Date: Sun, 26 Apr 2026 07:19:38 +0800 Subject: [PATCH] =?UTF-8?q?refactor(api):=20=E9=87=8D=E6=9E=84API=E8=93=9D?= =?UTF-8?q?=E5=9B=BE=E7=BB=93=E6=9E=84=E6=8C=89=E5=8A=9F=E8=83=BD=E6=A8=A1?= =?UTF-8?q?=E5=9D=97=E6=8B=86=E5=88=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 将原有的单一api蓝图拆分为多个独立蓝图(torrent、book、music、file、video、image) - 每个蓝图负责对应的功能模块,实现更好的代码组织和维护性 - 更新应用初始化文件中的蓝图注册方式,使用新的模块化结构 - 修改前端API调用路径,将/files/rename调整为/file/rename - 在torrent服务中新增按名称查询功能和相关处理逻辑 --- app/__init__.py | 11 ++-- app/blueprints/api.py | 91 -------------------------------- app/blueprints/book_bp.py | 17 ++++++ app/blueprints/file_bp.py | 17 ++++++ app/blueprints/image_bp.py | 21 ++++++++ app/blueprints/music_bp.py | 11 ++++ app/blueprints/torrent_bp.py | 23 ++++++++ app/blueprints/video_bp.py | 34 ++++++++++++ app/services/torrent_service.py | 49 +++++++++++++++-- flask-ui/src/api/index.ts | 2 +- instance/flask.db | Bin 36864 -> 36864 bytes 11 files changed, 177 insertions(+), 99 deletions(-) delete mode 100644 app/blueprints/api.py create mode 100644 app/blueprints/book_bp.py create mode 100644 app/blueprints/file_bp.py create mode 100644 app/blueprints/image_bp.py create mode 100644 app/blueprints/music_bp.py create mode 100644 app/blueprints/torrent_bp.py create mode 100644 app/blueprints/video_bp.py diff --git a/app/__init__.py b/app/__init__.py index 9e273e6..46da7bb 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -2,6 +2,7 @@ from flask import Flask from flask_cors import CORS from .extensions import db, migrate from .config import DevelopmentConfig, ProductionConfig, TestingConfig +from .blueprints import torrent_bp, book_bp, music_bp, file_bp, video_bp, image_bp def create_app(config_name="development"): @@ -20,7 +21,11 @@ def create_app(config_name="development"): db.init_app(app) migrate.init_app(app, db) - from .blueprints.api import bp as api_bp - app.register_blueprint(api_bp, url_prefix='/api') + app.register_blueprint(torrent_bp.bp, url_prefix='/api/torrent') + app.register_blueprint(book_bp.bp, url_prefix='/api/book') + app.register_blueprint(music_bp.bp, url_prefix='/api/music') + app.register_blueprint(file_bp.bp, url_prefix='/api/file') + app.register_blueprint(video_bp.bp, url_prefix='/api/video') + app.register_blueprint(image_bp.bp, url_prefix='/api/image') - return app \ No newline at end of file + return app diff --git a/app/blueprints/api.py b/app/blueprints/api.py deleted file mode 100644 index 9fb89ed..0000000 --- a/app/blueprints/api.py +++ /dev/null @@ -1,91 +0,0 @@ -from typing import List -from flask import Blueprint, jsonify, Response, current_app -from app.services import FileService, VideoService, TorrentService, BookService, ImageService, MusicService - -bp = Blueprint("api", __name__) - - -@bp.route("/book/download") -def book_download(): - book_object = BookService() - app = current_app._get_current_object() - - def generate(): - with app.app_context(): - yield from book_object.book_download() - - return Response(generate(), mimetype='text/event-stream') - -@bp.route("/video/findDeduplication") -def video_find_deduplication(): - video_object = VideoService(paths=[ - "C:\\迅雷下载\\0_done", - "D:\\", - "E:\\", - "F:\\" - ]) - - return jsonify({"code": 200, "message": video_object.find_deduplication()}) - - -@bp.route('/video/deduplication') -def video_deduplication(): - video_object = VideoService(paths=[ - "C:\\迅雷下载\\0_done", - "D:\\", - "E:\\", - "F:\\" - ]) - - return jsonify({"code": 200, "message": video_object.deduplication()}) - - -@bp.route("/video/hasTorrent") -def video_has_torrent(): - torrent_obj = TorrentService(["E:\\"]) - return jsonify({"code": 200, "message": torrent_obj.has_torrent()}) - - -@bp.route('/files/rename') -def files_rename(): - paths: List[str] = [ - "C:\\迅雷下载\\0_done", - "C:\\Users\\Localhost\\Desktop\\BT", - ] - - file_object = FileService(paths) - return jsonify({"code": 200, "message": file_object.rename()}) - - -@bp.route('/torrent/deduplication') -def torrent_deduplication(): - torrent_object = TorrentService(["C:\\Users\\Localhost\\Desktop\\BT"]) - return jsonify({"code": 200, "message": torrent_object.deduplication()}) - - -@bp.route("/image/export") -def image_export(): - image_object = ImageService() - - def generate(): - yield from image_object.export() - - return Response(generate(), mimetype='text/event-stream') - - -@bp.route("/image/hasTorrent") -def image_has_torrent(): - torrent_obj = TorrentService(["C:\\Users\\Localhost\\Desktop\\Images"]) - return jsonify({"code": 200, "message": torrent_obj.has_torrent()}) - - -@bp.route("/music/rename") -def music_rename(): - music_object = MusicService() - return jsonify({"code": 200, "message": music_object.rename()}) - -@bp.route("/torrent/statistics") -def torrent_statistics(): - torrent_object = TorrentService(["C:\\Users\\Localhost\\Desktop\\BT"]) - return jsonify({"code": 200, "message": torrent_object.statistics()}) - diff --git a/app/blueprints/book_bp.py b/app/blueprints/book_bp.py new file mode 100644 index 0000000..20f2ace --- /dev/null +++ b/app/blueprints/book_bp.py @@ -0,0 +1,17 @@ +from flask import Blueprint, current_app, Response + +from app.services import BookService + +bp = Blueprint("book", __name__) + + +@bp.route("/download") +def book_download(): + book_object = BookService() + app = current_app._get_current_object() + + def generate(): + with app.app_context(): + yield from book_object.book_download() + + return Response(generate(), mimetype='text/event-stream') diff --git a/app/blueprints/file_bp.py b/app/blueprints/file_bp.py new file mode 100644 index 0000000..78d3911 --- /dev/null +++ b/app/blueprints/file_bp.py @@ -0,0 +1,17 @@ +from typing import List +from flask import Blueprint, jsonify + +from app.services import FileService + +bp = Blueprint("file", __name__) + + +@bp.route('/rename') +def files_rename(): + paths: List[str] = [ + "C:\\迅雷下载\\0_done", + "C:\\Users\\Localhost\\Desktop\\BT", + ] + + file_object = FileService(paths) + return jsonify({"code": 200, "message": file_object.rename()}) diff --git a/app/blueprints/image_bp.py b/app/blueprints/image_bp.py new file mode 100644 index 0000000..efa5693 --- /dev/null +++ b/app/blueprints/image_bp.py @@ -0,0 +1,21 @@ +from flask import Blueprint, Response, jsonify + +from app.services import ImageService, TorrentService + +bp = Blueprint("image", __name__) + + +@bp.route("/export") +def image_export(): + image_object = ImageService() + + def generate(): + yield from image_object.export() + + return Response(generate(), mimetype='text/event-stream') + + +@bp.route("/hasTorrent") +def image_has_torrent(): + torrent_obj = TorrentService(["C:\\Users\\Localhost\\Desktop\\Images"]) + return jsonify({"code": 200, "message": torrent_obj.has_torrent()}) diff --git a/app/blueprints/music_bp.py b/app/blueprints/music_bp.py new file mode 100644 index 0000000..5f35e3c --- /dev/null +++ b/app/blueprints/music_bp.py @@ -0,0 +1,11 @@ +from flask import Blueprint, jsonify + +from app.services import MusicService + +bp = Blueprint("music", __name__) + + +@bp.route("/rename") +def music_rename(): + music_object = MusicService() + return jsonify({"code": 200, "message": music_object.rename()}) diff --git a/app/blueprints/torrent_bp.py b/app/blueprints/torrent_bp.py new file mode 100644 index 0000000..8839d0e --- /dev/null +++ b/app/blueprints/torrent_bp.py @@ -0,0 +1,23 @@ +from flask import Blueprint, jsonify + +from app.services import TorrentService + +bp = Blueprint("torrent", __name__) + + +@bp.route('/deduplication') +def torrent_deduplication(): + torrent_object = TorrentService(["C:\\Users\\Localhost\\Desktop\\BT"]) + return jsonify({"code": 200, "message": torrent_object.deduplication()}) + + +@bp.route("/statistics") +def torrent_statistics(): + torrent_object = TorrentService(["C:\\Users\\Localhost\\Desktop\\BT"]) + return jsonify({"code": 200, "message": torrent_object.statistics()}) + + +@bp.route("/s/") +def torrent_by_name(name): + torrent_object = TorrentService(["C:\\Users\\Localhost\\Desktop\\BT"]) + return jsonify({"code": 200, "message": torrent_object.get_by_name(name)}) \ No newline at end of file diff --git a/app/blueprints/video_bp.py b/app/blueprints/video_bp.py new file mode 100644 index 0000000..af6052d --- /dev/null +++ b/app/blueprints/video_bp.py @@ -0,0 +1,34 @@ +from flask import Blueprint, jsonify + +from app.services import VideoService, TorrentService + +bp = Blueprint("video", __name__) + +@bp.route("/findDeduplication") +def video_find_deduplication(): + video_object = VideoService(paths=[ + "C:\\迅雷下载\\0_done", + "D:\\", + "E:\\", + "F:\\" + ]) + + return jsonify({"code": 200, "message": video_object.find_deduplication()}) + + +@bp.route('/deduplication') +def video_deduplication(): + video_object = VideoService(paths=[ + "C:\\迅雷下载\\0_done", + "D:\\", + "E:\\", + "F:\\" + ]) + + return jsonify({"code": 200, "message": video_object.deduplication()}) + + +@bp.route("/hasTorrent") +def video_has_torrent(): + torrent_obj = TorrentService(["E:\\"]) + return jsonify({"code": 200, "message": torrent_obj.has_torrent()}) diff --git a/app/services/torrent_service.py b/app/services/torrent_service.py index 5c91762..fac2835 100644 --- a/app/services/torrent_service.py +++ b/app/services/torrent_service.py @@ -1,9 +1,9 @@ import os import re from collections import defaultdict -from typing import List +from typing import List, Optional -from app.constants import IGNORE_DIRS +from app.constants import IGNORE_DIRS, SEPARATORS, SPECIAL_PREFIXES, SPECIAL_DIRS from app.services import FolderService from app.utils import to_json_serializable @@ -17,6 +17,7 @@ class TorrentService: self.list_of_duplicate_torrent: List[str] = [] self.result_list = [] self.torrent_name = defaultdict(lambda: defaultdict(set)) + self.search_name = "" def process_torrent_file(self, entry: os.DirEntry[str]) -> None: temp_name = entry.name.replace("-4K", "") @@ -75,7 +76,47 @@ class TorrentService: print(f"有奇怪的东西混入2{path_arr}") def statistics(self): - FolderService(paths=self.paths, - ignore_dirs=IGNORE_DIRS, + FolderService(paths=self.paths, ignore_dirs=IGNORE_DIRS, file_callback=self.statistics_file_callback).process_folder() return to_json_serializable(self.torrent_name) + + def process_file_name(self, filename: str) -> Optional[str]: + name, ext = os.path.splitext(filename) + ext = ext.lower() + name = name.upper() + name = name.replace("-", "") + # 处理特殊前缀 + for prefix in SPECIAL_PREFIXES: + if name.startswith(prefix): + # name = name.replace(prefix, "") + return f"{prefix}-{name[len(prefix):]}{ext}" + return name + + def get_by_name_file_callback(self, entry: os.DirEntry): + path_arr = entry.path.split(os.sep) + torrent_sub_type = {"0_多人", "FWAY", "0_女同", "_temp"} + torrent_type = {"4k2", "4K原版", "456k", "1024", "2048", "done", "FC2", "hhd800", "other", "高清中文字幕", + "三级写真", "无码流出", "亚洲有码原创"} + if path_arr[-2] not in torrent_type and path_arr[-2] not in torrent_sub_type: + if path_arr[-3] in torrent_sub_type: + # print(path_arr[-3]) + if path_arr[-4] in torrent_type: + # print(path_arr[-4]) + pass + else: + print(f"有奇怪的东西混入{path_arr}") + pass + elif path_arr[-3] in torrent_type: + if self.search_name in path_arr[-2]: + print(entry.name) + # self.result_list.insert(0, entry.path) + self.torrent_name[path_arr[-2]][path_arr[-3]].add(entry.name) + pass + else: + print(f"有奇怪的东西混入2{path_arr}") + + def get_by_name(self, name): + self.search_name = name + FolderService(paths=self.paths, ignore_dirs=IGNORE_DIRS, + file_callback=self.get_by_name_file_callback).process_folder() + return to_json_serializable(self.torrent_name) diff --git a/flask-ui/src/api/index.ts b/flask-ui/src/api/index.ts index d20997b..ea94024 100644 --- a/flask-ui/src/api/index.ts +++ b/flask-ui/src/api/index.ts @@ -26,7 +26,7 @@ export function video_hasTorrent() { export function files_rename() { return request({ - url: "/files/rename", + url: "/file/rename", }) } diff --git a/instance/flask.db b/instance/flask.db index f2da06177f2e28cd2829790c718a592e929a3703..e76288249f28ae1f3306a6070e45b10aebf47b57 100644 GIT binary patch delta 31 ncmZozz|^pSX@WFk=R_H2#?FliGyU0&Oqjnhf8YGypV0vTvNj8j delta 31 ncmZozz|^pSX@WFk@kAMC#^Q|$GyT~NEto$uf7$%spV0vTuFnf!