rogerxavier commited on
Commit
523d3b6
1 Parent(s): 33cdb47

Update api.py

Browse files
Files changed (1) hide show
  1. api.py +26 -20
api.py CHANGED
@@ -157,26 +157,27 @@ def returnRandomSubscribeUrl(background_tasks: BackgroundTasks)->str:
157
 
158
 
159
  async def upload_video(meta:dict):
160
- credential = Credential(sessdata=sessdata,
161
- bili_jct=bili_jct,
162
- buvid3=buvid3)
163
-
164
- # 读取本地cover/0.jpg文件为字节数据作为cover
165
- with open("cover/0.jpg", "rb") as f:
166
- cover_data = f.read()
167
- cover = bilibili_api.utils.picture.Picture.from_content(cover_data,"jpg")#直接读取本地图片content
168
-
169
-
170
-
171
- page = video_uploader.VideoUploaderPage(path='mp4_out/output_video.mp4', title=meta['title'], description=meta['desc'])
 
 
 
172
 
173
- uploader = video_uploader.VideoUploader([page], meta, credential, cover=cover)
174
-
175
- @uploader.on("__ALL__")
176
- async def ev(data):
177
- print(data)
178
 
179
- await uploader.start()
 
 
 
180
 
181
 
182
 
@@ -204,8 +205,13 @@ def zero_pad(s, length):
204
 
205
  #如果默认的cover不能过审,那么随机选取无水印的manga1下面的随便一个作为cover重新上传
206
  @app.get("/upload_again")
207
- async def upload_again(bili_meta_data):
208
  bili_meta_data = json.loads(bili_meta_data)
 
 
 
 
 
209
  meta = bili_meta_data
210
  credential = Credential(sessdata=sessdata,
211
  bili_jct=bili_jct,
@@ -225,7 +231,7 @@ async def upload_again(bili_meta_data):
225
  image_files.sort(
226
  key=lambda x: zero_pad(''.join(filter(str.isdigit, os.path.splitext(os.path.basename(x))[0])), 3))
227
 
228
- # 获取随机一张图片图片存入cover/0.jpg作为cover图片
229
  random_image = Image.open(random.choice(image_files))
230
  # 定义要保存的文件路径
231
  save_path = os.path.join("cover", "0.jpg")
 
157
 
158
 
159
  async def upload_video(meta:dict):
160
+ #假设最多重新上传5次,上传cover超时那么选取随机图片重新作为cover上传
161
+ max_attempts = 5
162
+ attempts = 0
163
+ while attempts < max_attempts:
164
+ try:
165
+ await upload_with_random_cover(meta);
166
+ except Exception as e:
167
+ print("上传video遇到错误,可能是cover不符合超时,下面将调用upload_with_random_cover函数")
168
+ print(f"Attempt 第{attempts+1}次重新上传: 本次错误原因是Error : {e}")
169
+ #重新上传会执行try的部分,所以except部分不用写upload again
170
+ attempts += 1
171
+ else:
172
+ print("No errors occurred")
173
+ break
174
+
175
 
 
 
 
 
 
176
 
177
+
178
+
179
+
180
+
181
 
182
 
183
 
 
205
 
206
  #如果默认的cover不能过审,那么随机选取无水印的manga1下面的随便一个作为cover重新上传
207
  @app.get("/upload_again")
208
+ async def upload_again(bili_meta_data:str):
209
  bili_meta_data = json.loads(bili_meta_data)
210
+ await upload_with_random_cover(bili_meta_data)
211
+
212
+
213
+ #这个函数单独拆开,因为手动和自动重新上传都需要调用
214
+ async def upload_with_random_cover(bili_meta_data:dict):
215
  meta = bili_meta_data
216
  credential = Credential(sessdata=sessdata,
217
  bili_jct=bili_jct,
 
231
  image_files.sort(
232
  key=lambda x: zero_pad(''.join(filter(str.isdigit, os.path.splitext(os.path.basename(x))[0])), 3))
233
 
234
+ # 获取随机一张图片图片存入cover/0.jpg作为cover图片,至于保存为cover/0.jpg而不是直接用是因为能在出错的时候看效果,通过/cover.jpg接口
235
  random_image = Image.open(random.choice(image_files))
236
  # 定义要保存的文件路径
237
  save_path = os.path.join("cover", "0.jpg")