Skip to content

Commit

Permalink
优化上传文件不存在时报错,增加上传后是否删除原文件选项
Browse files Browse the repository at this point in the history
  • Loading branch information
holll committed Jun 20, 2023
1 parent 3ab8071 commit 77b9da2
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
12 changes: 11 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,17 @@ async def client_main():
elif select == '3':
channel_id = input('上传到:')
folder_path = input('文件(夹)路径:')
client.loop.run_until_complete(upload_file(client, channel_id, folder_path))
while True:
del_after_upload = input('上传后删除原文件(Y/N):').upper()
if del_after_upload == 'Y':
del_after_upload = True
break
elif del_after_upload == 'N':
del_after_upload = False
break
else:
print("无效的输入,请重新输入")
client.loop.run_until_complete(upload_file(client, channel_id, folder_path,del_after_upload))
elif select == '4':
chat_id = input('请输入频道id:')
client.loop.run_until_complete(print_group(client, chat_id))
Expand Down
7 changes: 6 additions & 1 deletion tools/upload_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from tools.tqdm import TqdmUpTo


async def upload_file(client: TelegramClient, chat_id, path: str):
async def upload_file(client: TelegramClient, chat_id, path: str, del_after_upload: bool):
isId = re.match(r'-?[1-9][0-9]{4,}', chat_id)
if isId:
chat_id = int(chat_id)
Expand All @@ -27,6 +27,9 @@ async def upload_file(client: TelegramClient, chat_id, path: str):
path_list = get_all_files(path)
# 遍历文件夹下的所有文件
for file_path in path_list:
# 如果文件不存在,跳过上传(防呆处理)
if not os.path.exists(file_path):
continue
# 文件预处理,解析信息
file_caption = os.path.basename(file_path)
file_size = os.path.getsize(file_path)
Expand Down Expand Up @@ -58,6 +61,8 @@ async def upload_file(client: TelegramClient, chat_id, path: str):
thumb=thumb_input,
progress_callback=bar.update_to,
attributes=[video_attr])
if del_after_upload:
os.remove(file_path)
except CancelledError:
print("取消上传")
sys.exit()
Expand Down

0 comments on commit 77b9da2

Please sign in to comment.