添加定制输出功能
This commit is contained in:
parent
112ab8e56e
commit
a463548731
15
urls/main.py
15
urls/main.py
@ -3,6 +3,7 @@ import urllib.parse
|
|||||||
import argparse
|
import argparse
|
||||||
import re
|
import re
|
||||||
import time
|
import time
|
||||||
|
import yaml
|
||||||
|
|
||||||
def natural_sort_key(s):
|
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}")
|
urls[dir_name].append(f"{file_size_bytes}:{current_timestamp}:{url}")
|
||||||
return urls
|
return urls
|
||||||
|
|
||||||
def save_urls(urls, output_file):
|
def save_urls(urls, output_file, root_folder):
|
||||||
"""将URL链接保存到文本文件中"""
|
"""将URL链接保存到YAML文件中"""
|
||||||
with open(output_file, 'w', encoding='utf-8') as f:
|
with open(output_file, 'w', encoding='utf-8') as f:
|
||||||
|
f.write(f"{root_folder}:\n")
|
||||||
for dir_name, files in urls.items():
|
for dir_name, files in urls.items():
|
||||||
if dir_name == '.':
|
if dir_name == '.':
|
||||||
for file in files:
|
for file in files:
|
||||||
f.write(f"{file}\n")
|
f.write(f" {file}\n")
|
||||||
else:
|
else:
|
||||||
f.write(f"{dir_name}:\n")
|
f.write(f" {dir_name}:\n")
|
||||||
for file in files:
|
for file in files:
|
||||||
f.write(f" {file}\n")
|
f.write(f" {file}\n")
|
||||||
|
|
||||||
@ -45,9 +47,10 @@ def parse_arguments():
|
|||||||
"""解析命令行参数"""
|
"""解析命令行参数"""
|
||||||
parser = argparse.ArgumentParser(description='Generate URLs from file names.')
|
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('--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('--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('--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()
|
return parser.parse_args()
|
||||||
|
|
||||||
def list_files_recursive(start_path='.', exclude_files=None):
|
def list_files_recursive(start_path='.', exclude_files=None):
|
||||||
@ -67,7 +70,7 @@ def main():
|
|||||||
exclude_files = {current_script} # 排除当前脚本文件
|
exclude_files = {current_script} # 排除当前脚本文件
|
||||||
file_paths = list_files_recursive('.', exclude_files)
|
file_paths = list_files_recursive('.', exclude_files)
|
||||||
urls = generate_urls(file_paths, args.base_url, args.dir, args.min_size)
|
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}")
|
print(f"URL链接已保存到{args.output}")
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
Loading…
Reference in New Issue
Block a user