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