From 3a04b4a5043068b0a289358a4f267f48d6aae92a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B2=9B=E9=A3=8E?= Date: Thu, 27 Jun 2024 00:27:16 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dbug=20=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E5=AF=B9=E5=AD=90=E6=96=87=E4=BB=B6=E5=A4=B9=E8=AF=BB=E5=8F=96?= =?UTF-8?q?=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- urls/main.py | 55 +++++++++++++++++++++++++--------------------------- 1 file changed, 26 insertions(+), 29 deletions(-) diff --git a/urls/main.py b/urls/main.py index e4fd9d9..67556ad 100644 --- a/urls/main.py +++ b/urls/main.py @@ -8,21 +8,19 @@ def natural_sort_key(s): """为了自然排序的辅助函数,将字符串中的数字部分转换成整数""" return [int(text) if text.isdigit() else text.lower() for text in re.split('([0-9]+)', s)] -def generate_urls(file_names, base_url, sub_directory): - """根据文件名、基础URL和子目录生成URL链接""" +def generate_urls(file_paths, base_url, sub_directory): + """根据文件路径、基础URL和子目录生成URL链接""" urls = [] - # 确保base_url和sub_directory以斜线结尾(如果sub_directory不为空) if not base_url.endswith('/'): base_url += '/' if sub_directory and not sub_directory.endswith('/'): sub_directory += '/' - current_timestamp = int(time.time()) # 获取当前时间的时间戳,转换为整数 - for name in sorted(file_names, key=natural_sort_key): # 使用自定义排序 - file_path = os.path.join('.', name) - file_size_bytes = os.path.getsize(file_path) # 获取文件大小(字节),为整数 - # 对文件名进行URL编码 - encoded_name = urllib.parse.quote(name) - url = f"{base_url}{sub_directory}{encoded_name}" + current_timestamp = int(time.time()) + for path in sorted(file_paths, key=natural_sort_key): + file_size_bytes = os.path.getsize(path) + relative_path = os.path.relpath(path, start='.') + encoded_path = urllib.parse.quote(relative_path) + url = f"{base_url}{sub_directory}{encoded_path}" urls.append(f"{file_size_bytes}:{current_timestamp}:{url}") return urls @@ -36,27 +34,26 @@ def parse_arguments(): """解析命令行参数""" parser = argparse.ArgumentParser(description='Generate URLs from file names.') parser.add_argument('--dir', type=str, default='', help='Sub-directory for generating file URLs (optional)') + parser.add_argument('--output', type=str, default='urls.txt', help='Output file name (default: urls.txt)') + parser.add_argument('--base-url', type=str, default='https://link.kite.kim/feng', help='Base URL for generating file URLs (default: https://link.kite.kim/feng)') return parser.parse_args() -def main(): - # 解析命令行参数 - args = parse_arguments() - - # 固定的base_url值 - base_url = 'https://link.kite.kim/feng' - - # 获取当前目录下的所有文件名,排除此脚本文件和隐藏文件 - current_script = os.path.basename(__file__) - file_names = [f for f in os.listdir('.') if os.path.isfile(f) and f != current_script and not f.startswith('.')] - - # 生成URL链接 - urls = generate_urls(file_names, base_url, args.dir) - - # 保存URL链接到文本文件 - output_file = 'urls.txt' - save_urls(urls, output_file) +def list_files_recursive(start_path='.'): + """递归列出目录及其子目录中的所有文件""" + file_paths = [] + for root, dirs, files in os.walk(start_path): + for file in files: + file_paths.append(os.path.join(root, file)) + return file_paths - print(f"URL链接已保存到{output_file}") +def main(): + args = parse_arguments() + current_script = os.path.basename(__file__) + file_paths = list_files_recursive('.') + file_paths = [f for f in file_paths if os.path.isfile(f) and os.path.basename(f) != current_script and not os.path.basename(f).startswith('.')] + urls = generate_urls(file_paths, args.base_url, args.dir) + save_urls(urls, args.output) + print(f"URL链接已保存到{args.output}") if __name__ == '__main__': - main() + main() \ No newline at end of file From fcc54a66858089cb204753923c0c958ffa453383 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B2=9B=E9=A3=8E?= Date: Thu, 27 Jun 2024 00:32:52 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=E4=BC=98=E5=8C=96=E7=A8=8B=E5=BA=8F?= =?UTF-8?q?=EF=BC=8C=E5=89=94=E9=99=A4128k=E4=BB=A5=E4=B8=8B=E6=96=87?= =?UTF-8?q?=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- urls/main.py | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/urls/main.py b/urls/main.py index 67556ad..ccc6159 100644 --- a/urls/main.py +++ b/urls/main.py @@ -5,19 +5,21 @@ import re import time def natural_sort_key(s): - """为了自然排序的辅助函数,将字符串中的数字部分转换成整数""" + """为了自然排序的辅助函数,将字符串中的数字部分转换成整数""" return [int(text) if text.isdigit() else text.lower() for text in re.split('([0-9]+)', s)] -def generate_urls(file_paths, base_url, sub_directory): - """根据文件路径、基础URL和子目录生成URL链接""" +def generate_urls(file_paths, base_url, sub_directory, min_size): + """根据文件路径、基础URL、子目录和最小文件大小生成URL链接""" urls = [] if not base_url.endswith('/'): base_url += '/' if sub_directory and not sub_directory.endswith('/'): sub_directory += '/' - current_timestamp = int(time.time()) + current_timestamp = int(time.time()) # 移到循环外 for path in sorted(file_paths, key=natural_sort_key): file_size_bytes = os.path.getsize(path) + if file_size_bytes < min_size: + continue relative_path = os.path.relpath(path, start='.') encoded_path = urllib.parse.quote(relative_path) url = f"{base_url}{sub_directory}{encoded_path}" @@ -36,22 +38,26 @@ def parse_arguments(): parser.add_argument('--dir', type=str, default='', help='Sub-directory for generating file URLs (optional)') parser.add_argument('--output', type=str, default='urls.txt', help='Output file name (default: urls.txt)') parser.add_argument('--base-url', type=str, default='https://link.kite.kim/feng', help='Base URL for generating file URLs (default: https://link.kite.kim/feng)') + parser.add_argument('--min-size', type=int, default=128*1024, help='Minimum file size in bytes (default: 128KB)') return parser.parse_args() -def list_files_recursive(start_path='.'): - """递归列出目录及其子目录中的所有文件""" +def list_files_recursive(start_path='.', exclude_files=None): + """递归列出目录及其子目录中的所有文件,排除指定的文件""" + if exclude_files is None: + exclude_files = set() file_paths = [] for root, dirs, files in os.walk(start_path): for file in files: - file_paths.append(os.path.join(root, file)) + if file not in exclude_files: + file_paths.append(os.path.join(root, file)) return file_paths def main(): args = parse_arguments() current_script = os.path.basename(__file__) - file_paths = list_files_recursive('.') - file_paths = [f for f in file_paths if os.path.isfile(f) and os.path.basename(f) != current_script and not os.path.basename(f).startswith('.')] - urls = generate_urls(file_paths, args.base_url, args.dir) + exclude_files = {current_script} # 排除当前脚本文件 + file_paths = list_files_recursive('.', exclude_files) + urls = generate_urls(file_paths, args.base_url, args.dir, args.min_size) save_urls(urls, args.output) print(f"URL链接已保存到{args.output}") From 112ab8e56ecde65dc5afdcfe01b64ca303c1258f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B2=9B=E9=A3=8E?= Date: Thu, 27 Jun 2024 00:58:09 +0800 Subject: [PATCH 3/4] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E9=80=92=E5=BD=92?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E5=90=8D=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- urls/main.py | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/urls/main.py b/urls/main.py index ccc6159..9dfaea1 100644 --- a/urls/main.py +++ b/urls/main.py @@ -5,12 +5,12 @@ import re import time def natural_sort_key(s): - """为了自然排序的辅助函数,将字符串中的数字部分转换成整数""" + """为了自然排序的辅助函数,将字符串中的数字部分转换成整数""" return [int(text) if text.isdigit() else text.lower() for text in re.split('([0-9]+)', s)] def generate_urls(file_paths, base_url, sub_directory, min_size): """根据文件路径、基础URL、子目录和最小文件大小生成URL链接""" - urls = [] + urls = {} if not base_url.endswith('/'): base_url += '/' if sub_directory and not sub_directory.endswith('/'): @@ -23,14 +23,23 @@ def generate_urls(file_paths, base_url, sub_directory, min_size): relative_path = os.path.relpath(path, start='.') encoded_path = urllib.parse.quote(relative_path) url = f"{base_url}{sub_directory}{encoded_path}" - urls.append(f"{file_size_bytes}:{current_timestamp}:{url}") + dir_name = os.path.dirname(relative_path) + if dir_name not in urls: + urls[dir_name] = [] + urls[dir_name].append(f"{file_size_bytes}:{current_timestamp}:{url}") return urls def save_urls(urls, output_file): """将URL链接保存到文本文件中""" with open(output_file, 'w', encoding='utf-8') as f: - for url in urls: - f.write(url + '\n') + for dir_name, files in urls.items(): + if dir_name == '.': + for file in files: + f.write(f"{file}\n") + else: + f.write(f"{dir_name}:\n") + for file in files: + f.write(f" {file}\n") def parse_arguments(): """解析命令行参数""" @@ -42,7 +51,7 @@ def parse_arguments(): return parser.parse_args() def list_files_recursive(start_path='.', exclude_files=None): - """递归列出目录及其子目录中的所有文件,排除指定的文件""" + """递归列出目录及其子目录中的所有文件,排除指定的文件""" if exclude_files is None: exclude_files = set() file_paths = [] From a463548731a1fcc95706192f3872dc38f4aac339 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B2=9B=E9=A3=8E?= Date: Thu, 27 Jun 2024 01:17:57 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=AE=9A=E5=88=B6?= =?UTF-8?q?=E8=BE=93=E5=87=BA=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- urls/main.py | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/urls/main.py b/urls/main.py index 9dfaea1..2b1c9aa 100644 --- a/urls/main.py +++ b/urls/main.py @@ -3,6 +3,7 @@ import urllib.parse import argparse import re import time +import yaml def natural_sort_key(s): """为了自然排序的辅助函数,将字符串中的数字部分转换成整数""" @@ -29,25 +30,27 @@ def generate_urls(file_paths, base_url, sub_directory, min_size): urls[dir_name].append(f"{file_size_bytes}:{current_timestamp}:{url}") return urls -def save_urls(urls, output_file): - """将URL链接保存到文本文件中""" +def save_urls(urls, output_file, root_folder): + """将URL链接保存到YAML文件中""" with open(output_file, 'w', encoding='utf-8') as f: + f.write(f"{root_folder}:\n") for dir_name, files in urls.items(): if dir_name == '.': - for file in files: - f.write(f"{file}\n") - else: - f.write(f"{dir_name}:\n") for file in files: f.write(f" {file}\n") + else: + f.write(f" {dir_name}:\n") + for file in files: + f.write(f" {file}\n") def parse_arguments(): """解析命令行参数""" parser = argparse.ArgumentParser(description='Generate URLs from file names.') parser.add_argument('--dir', type=str, default='', help='Sub-directory for generating file URLs (optional)') - parser.add_argument('--output', type=str, default='urls.txt', help='Output file name (default: urls.txt)') + parser.add_argument('--output', type=str, default='urls.yaml', help='Output file name (default: urls.yaml)') parser.add_argument('--base-url', type=str, default='https://link.kite.kim/feng', help='Base URL for generating file URLs (default: https://link.kite.kim/feng)') parser.add_argument('--min-size', type=int, default=128*1024, help='Minimum file size in bytes (default: 128KB)') + parser.add_argument('--rf', type=str, required=True, help='Root folder name for YAML output') return parser.parse_args() def list_files_recursive(start_path='.', exclude_files=None): @@ -67,7 +70,7 @@ def main(): exclude_files = {current_script} # 排除当前脚本文件 file_paths = list_files_recursive('.', exclude_files) urls = generate_urls(file_paths, args.base_url, args.dir, args.min_size) - save_urls(urls, args.output) + save_urls(urls, args.output, args.rf) print(f"URL链接已保存到{args.output}") if __name__ == '__main__':