53 lines
1.6 KiB
Python
53 lines
1.6 KiB
Python
|
import os
|
||
|
import urllib.parse
|
||
|
import argparse
|
||
|
|
||
|
def generate_urls(file_names, base_url, sub_directory):
|
||
|
"""根据文件名、基础URL和子目录生成URL链接"""
|
||
|
urls = []
|
||
|
# 确保base_url和sub_directory以斜线结尾
|
||
|
if not base_url.endswith('/'):
|
||
|
base_url += '/'
|
||
|
if not sub_directory.endswith('/'):
|
||
|
sub_directory += '/'
|
||
|
for name in file_names:
|
||
|
# 对文件名进行URL编码
|
||
|
encoded_name = urllib.parse.quote(name)
|
||
|
urls.append(f"{base_url}{sub_directory}{encoded_name}")
|
||
|
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')
|
||
|
|
||
|
def parse_arguments():
|
||
|
"""解析命令行参数"""
|
||
|
parser = argparse.ArgumentParser(description='Generate URLs from file names.')
|
||
|
parser.add_argument('--dir', type=str, help='Sub-directory for generating file URLs', required=True)
|
||
|
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]
|
||
|
|
||
|
# 生成URL链接
|
||
|
urls = generate_urls(file_names, base_url, args.dir)
|
||
|
|
||
|
# 保存URL链接到文本文件
|
||
|
output_file = 'urls.txt'
|
||
|
save_urls(urls, output_file)
|
||
|
|
||
|
print(f"URL链接已保存到{output_file}")
|
||
|
|
||
|
if __name__ == '__main__':
|
||
|
main()
|