Skip to content

Commit

Permalink
处理 imgtg 维护时上传图片的错误
Browse files Browse the repository at this point in the history
  • Loading branch information
thep0y committed Aug 5, 2023
1 parent 923c138 commit c5bc75d
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 11 deletions.
7 changes: 0 additions & 7 deletions up2b/up2b_lib/custom_types.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
# @Author: thepoy
# @Email: thepoy@163.com
# @File Name: custom_types.py
# @Created At: 2021-02-09 11:27:21
# @Modified At: 2023-02-21 12:41:21
# @Modified By: thepoy

from enum import IntEnum
from typing import Any, Dict, List, Optional, Union
Expand Down
9 changes: 6 additions & 3 deletions up2b/up2b_lib/up2b_api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ def _clear_cache(self):
shutil.rmtree(CACHE_PATH)
os.mkdir(CACHE_PATH)

logger.info("cache folder has been cleared", cache_path=CACHE_PATH)
logger.debug("cache folder has been cleared", cache_path=CACHE_PATH)

def _check_cache(self, image: Path) -> Tuple[str, str, bool]:
url, md5, ok = self.cache.check_cache_of_image_bed(
Expand All @@ -306,7 +306,7 @@ def _check_cache(self, image: Path) -> Tuple[str, str, bool]:

return (url, md5, ok)

logger.debug("缓存中未找到此图片链接")
logger.info("缓存中未找到此图片链接,开始上传")
return (url, md5, ok)

def upload_images(
Expand All @@ -331,7 +331,10 @@ def upload_images(

if to_console:
for iu in image_urls:
print(iu)
if isinstance(iu, UploadErrorResponse):
logger.error("上传出错", status_code=iu.status_code, error=iu.error)
else:
print(iu)

self._clear_cache()
return image_urls
Expand Down
9 changes: 8 additions & 1 deletion up2b/up2b_lib/up2b_api/imgtg.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,13 @@ def _update_auth_token(self):
self.auth_info["token"] = self.token = auth_token
self._save_auth_info(self.auth_info)

@staticmethod
def _is_in_maintenance(status_code: int, text: str, image: ImageType):
if "<h1>网站正在维护中</h1>" in text:
return UploadErrorResponse(500, "网站正在维护", str(image))

return UploadErrorResponse(status_code, text, str(image))

def __upload(
self, image: ImageType, retries: int = 0
) -> Union[str, UploadErrorResponse]:
Expand Down Expand Up @@ -203,7 +210,7 @@ def __upload(
try:
json_resp = resp.json()
except json.decoder.JSONDecodeError:
return UploadErrorResponse(resp.status_code, resp.text, str(image))
return self._is_in_maintenance(resp.status_code, resp.text, image)

try:
uploaded_url: str = json_resp["image"]["image"]["url"]
Expand Down

0 comments on commit c5bc75d

Please sign in to comment.