rogerxavier commited on
Commit
4c93c89
1 Parent(s): 6b71423

Update task/_1批量顺序上传原始图片到服务器manga.py

Browse files
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
- url = baseUrl +"/deleteFiles"
12
- directory_clear_list = ["manga", "manga1", "manga12", "output", "mp3_out", "mp4_out", "cover", "cache"]
13
- for directory in directory_clear_list:
14
- response = requests.delete(url, params={"directory": directory})
15
-
16
- if response.status_code == 200:
17
- print(response.text)
18
- else:
19
- print("请求失败,状态码:", response.status_code)
20
- print("请求失败,状态码:", response.text)
21
-
22
- url = baseUrl+'/getOriginalMangaList'
23
- # 获取当前目录的下的全部图片用于上传
24
- img_path = manga_path
25
- subdir_path = os.path.join(os.getcwd(), img_path)
26
- image_files = []
27
- for root, dirs, files in os.walk(subdir_path):
28
- for file in files:
29
- if file.endswith(".jpg") or file.endswith(".png"):
30
- image_files.append(os.path.relpath(os.path.join(root, file)))
31
-
32
- # 对对话框文件名中的数字部分进行补零操作-这样顺序会正常 #排序上传列表中的顺序
33
- image_files.sort(
34
- key=lambda x: zero_pad(''.join(filter(str.isdigit, os.path.splitext(os.path.basename(x))[0])), 3))
35
-
36
- # 转换为上传格式并上传
37
- upload_files = []
38
-
39
- for image_path in image_files:
40
- upload_files.append(("images", (image_path, open(image_path, "rb"), "image/jpeg")))
41
-
42
- response = requests.post(url, files=upload_files)
43
- print(response.text)
44
- if __name__ == '__main__':
45
- upload_manga_to_space(baseUrl='https://rogerxavier-moviepy-with-manga-test.hf.space',manga_path='manga')
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
+