Spaces:
Sleeping
Sleeping
File size: 1,977 Bytes
4c93c89 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
#批量上传
import os
import requests
# 补零函数,将数字部分补齐为指定长度,从而顺序上传
def zero_pad(s, length):
return s.zfill(length)
# ->void
def upload_manga_to_space(baseUrl:str,manga_path:str,fake_headers:dict=None):
# 创建一个Session对象
session = requests.Session()
if fake_headers is not None:
session.headers = fake_headers
# 先删除原来的后上传
url = baseUrl +"/deleteFiles"
directory_clear_list = ["manga", "manga1", "manga12", "output", "mp3_out", "mp4_out", "cover", "cache"]
for directory in directory_clear_list:
response = session.delete(url, params={"directory": directory})
if response.status_code == 200:
print(response.text)
else:
print("请求失败,状态码:", response.status_code)
print("请求失败,状态码:", response.text)
url = baseUrl+'/getOriginalMangaList'
# 获取当前目录的下的全部图片用于上传
img_path = manga_path
subdir_path = os.path.join(os.getcwd(), img_path)
image_files = []
for root, dirs, files in os.walk(subdir_path):
for file in files:
if file.endswith(".jpg") or file.endswith(".png"):
image_files.append(os.path.relpath(os.path.join(root, file)))
# 对对话框文件名中的数字部分进行补零操作-这样顺序会正常 #排序上传列表中的顺序
image_files.sort(
key=lambda x: zero_pad(''.join(filter(str.isdigit, os.path.splitext(os.path.basename(x))[0])), 3))
# 转换为上传格式并上传
upload_files = []
for image_path in image_files:
upload_files.append(("images", (image_path, open(image_path, "rb"), "image/jpeg")))
response = session.post(url, files=upload_files)
print(response.text)
if __name__ == '__main__':
upload_manga_to_space(baseUrl='https://rogerxavier-moviepy-with-manga-test.hf.space',manga_path='manga')
|