{ "cells": [ { "cell_type": "code", "id": "initial_id", "metadata": { "collapsed": true, "ExecuteTime": { "end_time": "2025-02-07T09:41:38.049465Z", "start_time": "2025-02-07T09:41:38.045253Z" } }, "source": "root_path = \"/Users/lanyuanxiaoyao/Documents/comic/待处理/调教开关\"", "outputs": [], "execution_count": 81 }, { "metadata": { "ExecuteTime": { "end_time": "2025-02-07T09:41:38.071893Z", "start_time": "2025-02-07T09:41:38.059949Z" } }, "cell_type": "code", "source": [ "import os\n", "\n", "# CBZ解压\n", "for path in list(\n", " filter(lambda x: x.endswith('.cbz'), map(lambda x: os.path.join(root_path, x), os.listdir(root_path)))\n", "):\n", " os.rename(path, path.replace('.cbz', '.zip'))\n", " # print(f'{root_path}/{os.path.basename(path).replace(\".cbz\", \"zip\")}')" ], "id": "ff77e3324c9a90d6", "outputs": [], "execution_count": 82 }, { "metadata": { "ExecuteTime": { "end_time": "2025-02-07T09:41:40.793331Z", "start_time": "2025-02-07T09:41:38.088816Z" } }, "cell_type": "code", "source": [ "import zipfile\n", "\n", "# ZIP解压\n", "for path in list(\n", " filter(lambda x: x.endswith('.zip'), map(lambda x: os.path.join(root_path, x), os.listdir(root_path)))\n", "):\n", " folder_name = os.path.splitext(path)[0]\n", " os.makedirs(folder_name, exist_ok=True)\n", " with zipfile.ZipFile(path, 'r') as zip_ref:\n", " zip_ref.extractall(folder_name)" ], "id": "bfd49eed033959fb", "outputs": [], "execution_count": 83 }, { "metadata": { "ExecuteTime": { "end_time": "2025-02-07T09:41:40.882038Z", "start_time": "2025-02-07T09:41:40.804754Z" } }, "cell_type": "code", "source": [ "# 删除ZIP\n", "for path in list(\n", " filter(lambda x: x.endswith('.zip'), map(lambda x: os.path.join(root_path, x), os.listdir(root_path)))\n", "):\n", " os.remove(path)" ], "id": "64f9b9d169f9e416", "outputs": [], "execution_count": 84 }, { "metadata": { "ExecuteTime": { "end_time": "2025-02-07T09:41:40.912970Z", "start_time": "2025-02-07T09:41:40.891555Z" } }, "cell_type": "code", "source": [ "# 删除ComicInfo\n", "for root, dirs, files in os.walk(root_path):\n", " for file in files:\n", " if file.endswith('.xml'):\n", " os.remove(os.path.join(root, file))" ], "id": "200531eb9a12439b", "outputs": [], "execution_count": 85 }, { "metadata": { "ExecuteTime": { "end_time": "2025-02-07T09:41:41.318381Z", "start_time": "2025-02-07T09:41:40.922687Z" } }, "cell_type": "code", "source": [ "# 按序重命名文件夹\n", "import re\n", "\n", "\n", "def natural_sort_key(s):\n", " \"\"\"\n", " 将字符串转换为自然排序的键。\n", " \"\"\"\n", " return [int(text) if text.isdigit() else text for text in re.split('([0-9]+)', s)]\n", "\n", "\n", "def mac_like_sorted(file_list, reverse=False):\n", " \"\"\"\n", " 按照类似 macOS Finder 的逻辑对文件列表进行排序。\n", " \"\"\"\n", " return sorted(file_list, key=lambda x: natural_sort_key(x), reverse=reverse)\n", "\n", "\n", "for index, path in enumerate(\n", " mac_like_sorted(\n", " filter(lambda x: os.path.isdir(x), map(lambda x: os.path.join(root_path, x), os.listdir(root_path)))\n", " )\n", "):\n", " for index2, path2 in enumerate(\n", " mac_like_sorted(\n", " filter(lambda x: os.path.isfile(x), map(lambda x: os.path.join(path, x), os.listdir(path)))\n", " )\n", " ):\n", " os.rename(path2, f'{os.path.dirname(path2)}/{str(index2 + 1).zfill(3)}{os.path.splitext(path2)[1]}')\n", " os.rename(path, f'{os.path.dirname(path)}/第{str(index + 1).zfill(2)}话')" ], "id": "e5b7bced98c094f9", "outputs": [], "execution_count": 86 }, { "metadata": { "ExecuteTime": { "end_time": "2025-02-07T09:41:41.357835Z", "start_time": "2025-02-07T09:41:41.338196Z" } }, "cell_type": "code", "source": [ "import xml.etree.ElementTree as ET\n", "\n", "for index, path in enumerate(\n", " mac_like_sorted(\n", " filter(lambda x: os.path.isdir(x), map(lambda x: os.path.join(root_path, x), os.listdir(root_path)))\n", " )\n", "):\n", " root = ET.Element('ComicInfo')\n", " ET.SubElement(root, 'Title').text = os.path.basename(path)\n", " ET.SubElement(root, 'Series').text = '调教开关'\n", " ET.SubElement(root, 'Number').text = str(index + 1)\n", " ET.SubElement(\n", " root, 'Summary'\n", " ).text = '平时把我当奴隶使唤的学姐偶然被我抓到了把柄学姐~ 这次换我来调教你了!'\n", " ET.SubElement(root, 'Writer').text = 'KMH/外殼'\n", " ET.SubElement(root, 'Genre').text = '控制, 調教, 韩漫, 跳蛋'\n", " tree = ET.ElementTree(root)\n", " tree.write(f'{path}/ComicInfo.xml', encoding='utf-8', xml_declaration=True)" ], "id": "17c6388c9a60cd94", "outputs": [], "execution_count": 87 }, { "metadata": { "ExecuteTime": { "end_time": "2025-02-07T10:18:39.559242Z", "start_time": "2025-02-07T10:18:30.819510Z" } }, "cell_type": "code", "source": [ "# 先运行上面的,然后压缩图片,在运行下面的\n", "import shutil\n", "\n", "for index, path in enumerate(\n", " mac_like_sorted(\n", " filter(lambda x: os.path.isdir(x), map(lambda x: os.path.join(root_path, x), os.listdir(root_path)))\n", " )\n", "):\n", " with zipfile.ZipFile(f'{path}.zip', 'w', zipfile.ZIP_DEFLATED, compresslevel=9) as zip_ref:\n", " for index2, path2 in enumerate(\n", " mac_like_sorted(\n", " filter(lambda x: os.path.isfile(x), map(lambda x: os.path.join(path, x), os.listdir(path)))\n", " )\n", " ):\n", " zip_ref.write(path2, arcname=os.path.relpath(path2, path))\n", " shutil.rmtree(path)\n", " os.rename(f'{path}.zip', f'{path}.cbz')" ], "id": "fbc823de11a39ba2", "outputs": [], "execution_count": 88 } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.6" } }, "nbformat": 4, "nbformat_minor": 5 }