添加定制输出功能

This commit is contained in:
岛风 2024-06-27 01:17:57 +08:00
parent 112ab8e56e
commit a463548731

View File

@ -3,6 +3,7 @@ import urllib.parse
import argparse
import re
import time
import yaml
def natural_sort_key(s):
"""为了自然排序的辅助函数,将字符串中的数字部分转换成整数"""
@ -29,15 +30,16 @@ 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")
f.write(f" {file}\n")
else:
f.write(f"{dir_name}:\n")
f.write(f" {dir_name}:\n")
for file in files:
f.write(f" {file}\n")
@ -45,9 +47,10 @@ 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__':