修复 bug
This commit is contained in:
parent
83ebd9d0ff
commit
09a367e22f
11
urls/main.py
11
urls/main.py
@ -9,19 +9,22 @@ def natural_sort_key(s):
|
|||||||
"""为了自然排序的辅助函数,将字符串中的数字部分转换成整数"""
|
"""为了自然排序的辅助函数,将字符串中的数字部分转换成整数"""
|
||||||
return [int(text) if text.isdigit() else text.lower() for text in re.split('([0-9]+)', 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, min_size):
|
def generate_urls(file_paths, base_url, sub_directory, min_size):
|
||||||
"""根据文件路径、基础URL和最小文件大小生成URL链接"""
|
"""根据文件路径、基础URL、子目录和最小文件大小生成URL链接"""
|
||||||
urls = {}
|
urls = {}
|
||||||
if not base_url.endswith('/'):
|
if not base_url.endswith('/'):
|
||||||
base_url += '/'
|
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):
|
for path in sorted(file_paths, key=natural_sort_key):
|
||||||
file_size_bytes = os.path.getsize(path)
|
file_size_bytes = os.path.getsize(path)
|
||||||
if file_size_bytes < min_size:
|
if file_size_bytes < min_size:
|
||||||
continue
|
continue
|
||||||
relative_path = os.path.relpath(path, start='.')
|
relative_path = os.path.relpath(path, start='.')
|
||||||
|
relative_path = relative_path.replace(os.sep, '/') # 将路径分隔符替换为正斜杠
|
||||||
encoded_path = urllib.parse.quote(relative_path)
|
encoded_path = urllib.parse.quote(relative_path)
|
||||||
url = f"{base_url}{encoded_path}"
|
url = f"{base_url}{sub_directory}{encoded_path}"
|
||||||
dir_name = os.path.dirname(relative_path)
|
dir_name = os.path.dirname(relative_path)
|
||||||
if dir_name not in urls:
|
if dir_name not in urls:
|
||||||
urls[dir_name] = []
|
urls[dir_name] = []
|
||||||
@ -67,7 +70,7 @@ def main():
|
|||||||
current_script = os.path.basename(__file__)
|
current_script = os.path.basename(__file__)
|
||||||
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.min_size)
|
urls = generate_urls(file_paths, args.base_url, args.dir, args.min_size)
|
||||||
save_urls(urls, args.output, args.rf)
|
save_urls(urls, args.output, args.rf)
|
||||||
print(f"URL链接已保存到{args.output}")
|
print(f"URL链接已保存到{args.output}")
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user