getapi commited on
Commit
873da01
1 Parent(s): a43201f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -13
app.py CHANGED
@@ -132,7 +132,24 @@ async def upload_image(file_path: Path | str, file_type: str = 'png') -> str | N
132
  return await upload_image_to_imgbb(file_path, file_type)
133
 
134
 
135
- async def optimize_and_upload(images_urls: list[str] | str, convert: bool = False) -> list[str]:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
136
  images_urls = [images_urls] if isinstance(images_urls, str) else images_urls
137
  print(f'принятые ссылки в обработку ({len(images_urls)}): {images_urls}')
138
 
@@ -143,20 +160,12 @@ async def optimize_and_upload(images_urls: list[str] | str, convert: bool = Fals
143
  new_images_urls = []
144
  images_paths = images_paths if not convert else await convert_to_jpegs(images_paths)
145
 
 
146
  for old_url, image_path in zip(images_urls, images_paths):
147
- new_url = await upload_image(image_path, 'png' if not convert else 'jpeg')
148
- if new_url:
149
- new_images_urls.append(new_url)
150
- print(f'загружено изображение {image_path} в {new_url}')
151
- else:
152
- new_images_urls.append(old_url)
153
- print(f'не удалось загрузить изображение {image_path}, оставим старую ссылку: {old_url}')
154
-
155
- try:
156
- image_path.unlink()
157
- except Exception as e:
158
- print(f'не удалось удалить файл {image_path}: {e}')
159
 
 
 
160
  print(f'новые ссылки: ({len(new_images_urls)}): {new_images_urls}')
161
 
162
  try:
@@ -183,6 +192,8 @@ async def optimize_and_upload(images_urls: list[str] | str, convert: bool = Fals
183
  return new_images_urls
184
 
185
 
 
 
186
  app = FastAPI()
187
 
188
 
 
132
  return await upload_image_to_imgbb(file_path, file_type)
133
 
134
 
135
+
136
+ async def process_image(old_url: str, image_path: Path, convert: bool) -> tuple[str, Path]:
137
+ new_url = await upload_image(image_path, 'png' if not convert else 'jpeg')
138
+ if new_url:
139
+ print(f'загружено изображение {image_path} в {new_url}')
140
+ else:
141
+ new_url = old_url
142
+ print(f'не удалось загрузить изображение {image_path}, оставим старую ссылку: {old_url}')
143
+
144
+ try:
145
+ image_path.unlink()
146
+ except Exception as e:
147
+ print(f'не удалось удалить файл {image_path}: {e}')
148
+
149
+ return new_url, image_path
150
+
151
+
152
+ async def optimize_and_upload(images_urls: List[str] | str, convert: bool = False) -> List[str]:
153
  images_urls = [images_urls] if isinstance(images_urls, str) else images_urls
154
  print(f'принятые ссылки в обработку ({len(images_urls)}): {images_urls}')
155
 
 
160
  new_images_urls = []
161
  images_paths = images_paths if not convert else await convert_to_jpegs(images_paths)
162
 
163
+ tasks = []
164
  for old_url, image_path in zip(images_urls, images_paths):
165
+ tasks.append(process_image(old_url, image_path, convert))
 
 
 
 
 
 
 
 
 
 
 
166
 
167
+ results = await gather(*tasks)
168
+ new_images_urls = [result[0] for result in results]
169
  print(f'новые ссылки: ({len(new_images_urls)}): {new_images_urls}')
170
 
171
  try:
 
192
  return new_images_urls
193
 
194
 
195
+
196
+
197
  app = FastAPI()
198
 
199