getapi commited on
Commit
6fcad5b
1 Parent(s): dae6ebe

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -13
app.py CHANGED
@@ -109,19 +109,22 @@ async def optimize_pngs(image_paths: list[str | Path] | str | Path) -> None:
109
 
110
 
111
  async def upload_image_to_imgbb(file_path: Path, file_type: str = 'png') -> str | None:
112
- url = f'https://api.imgbb.com/1/upload?key={choice(tokens)}'
113
- try:
114
- with file_path.open('rb') as file:
115
- files = {'image': (file_path.name, file, f'image/{file_type}')}
116
- data = {}
117
- async with AsyncClient() as client:
118
- response = await client.post(url, files=files, data=data, timeout=30)
119
- response.raise_for_status()
120
- json = response.json()
121
- if json.get('success'):
122
- return json['data']['url']
123
- except:
124
- return None
 
 
 
125
 
126
 
127
  async def upload_image(file_path: Path | str, file_type: str = 'png') -> str | None:
 
109
 
110
 
111
  async def upload_image_to_imgbb(file_path: Path, file_type: str = 'png') -> str | None:
112
+ for token in tokens:
113
+ url = f'https://api.imgbb.com/1/upload?key={token}'
114
+ try:
115
+ with file_path.open('rb') as file:
116
+ files = {'image': (file_path.name, file, f'image/{file_type}')}
117
+ data = {}
118
+ async with AsyncClient() as client:
119
+ response = await client.post(url, files=files, data=data, timeout=30)
120
+ response.raise_for_status()
121
+ json = response.json()
122
+ if json.get('success'):
123
+ return json['data']['url']
124
+ except Exception as e:
125
+ print(f"ошибка при загрузке с {token}: {e}")
126
+ continue
127
+ return None
128
 
129
 
130
  async def upload_image(file_path: Path | str, file_type: str = 'png') -> str | None: