getapi commited on
Commit
dae6ebe
1 Parent(s): b3a61ea

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -14
app.py CHANGED
@@ -34,7 +34,7 @@ tokens = [
34
 
35
 
36
  async def download_png(url: str, folder: str, client: AsyncClient, retries: int = 5) -> Path:
37
- logger.info(f'загрузка изображения: {url}')
38
  for attempt in range(retries):
39
  try:
40
  response = await client.get(url)
@@ -52,12 +52,11 @@ async def download_png(url: str, folder: str, client: AsyncClient, retries: int
52
 
53
  async def download_pngs(urls: str | list[str]) -> list[Any]:
54
  urls = [urls] if isinstance(urls, str) else urls
55
- logger.info(f'скачивается список список из {len(urls)}: {urls}')
56
  print(f'скачивается список список из {len(urls)}: {urls}')
57
  # бот coze имеет баг, и из воркфлоу прибавляет предыдущий ответ к ссылкам, если включен контекст чата:
58
  valid_urls = [url for url in urls if url and '\n' not in url and '\\n' not in url and url.strip() != '']
59
  if len(valid_urls) != len(urls):
60
- logger.warning(f'некорректные ссылки удалены из списка: {set(urls) - set(valid_urls)}')
61
  async with AsyncClient(verify=False, follow_redirects=True, timeout=30.0) as client:
62
  tasks = [download_png(url, str(uuid4()), client) for url in valid_urls]
63
  return list(await gather(*tasks))
@@ -65,7 +64,7 @@ async def download_pngs(urls: str | list[str]) -> list[Any]:
65
 
66
  async def optimize_png(image_path: Path, retries: int = 3) -> None:
67
  command = f'{oxipng_bin.resolve()} --opt 2 --strip safe --out {image_path} {image_path}'
68
- logger.info(f'оптимизация картинки {image_path}')
69
  for attempt in range(retries):
70
  try:
71
  process = await create_subprocess_shell(command, stdout=PIPE, stderr=PIPE)
@@ -75,7 +74,7 @@ async def optimize_png(image_path: Path, retries: int = 3) -> None:
75
  else:
76
  raise CalledProcessError(process.returncode, command, output=stdout, stderr=stderr)
77
  except CalledProcessError as e:
78
- logger.error(f'ошибка при оптимизации {image_path}')
79
  if attempt < retries - 1:
80
  await sleep(2 ** attempt)
81
  else:
@@ -83,7 +82,7 @@ async def optimize_png(image_path: Path, retries: int = 3) -> None:
83
 
84
 
85
  async def convert_to_jpeg(image_path: Path) -> Path:
86
- logger.info(f'конвертируется {image_path}')
87
  try:
88
  image = Image.open(image_path)
89
  output_path = image_path.with_suffix('.jpg')
@@ -91,20 +90,20 @@ async def convert_to_jpeg(image_path: Path) -> Path:
91
  image_path.unlink(missing_ok=True)
92
  return output_path
93
  except:
94
- logger.error(f'ошибка при конвертации {image_path}')
95
  return image_path
96
 
97
 
98
  async def convert_to_jpegs(image_paths: list[str | Path] | str | Path) -> tuple[Path]:
99
  image_paths = [Path(image_file) for image_file in ([image_paths] if not isinstance(image_paths, list) else image_paths)]
100
- logger.info(f'оптимизируется список список из {len(image_paths)}: {image_paths}')
101
  tasks = [convert_to_jpeg(image_path) for image_path in image_paths]
102
  return await gather(*tasks)
103
 
104
 
105
  async def optimize_pngs(image_paths: list[str | Path] | str | Path) -> None:
106
  image_paths = [Path(image_file) for image_file in ([image_paths] if not isinstance(image_paths, list) else image_paths)]
107
- logger.info(f'оптимизируется список список из {len(image_paths)}: {image_paths}')
108
  tasks = [optimize_png(image_path) for image_path in image_paths]
109
  await gather(*tasks)
110
 
@@ -132,7 +131,7 @@ async def upload_image(file_path: Path | str, file_type: str = 'png') -> str | N
132
 
133
  async def optimize_and_upload(images_urls: list[str] | str, convert: bool = False) -> list[str]:
134
  images_urls = [images_urls] if isinstance(images_urls, str) else images_urls
135
- logger.info(f'принятые ссылки в обработку ({len(images_urls)}): {images_urls}')
136
  images_paths = await download_pngs(images_urls)
137
  if not convert:
138
  await optimize_pngs(images_paths)
@@ -142,16 +141,16 @@ async def optimize_and_upload(images_urls: list[str] | str, convert: bool = Fals
142
  new_url = await upload_image(image_path, 'png' if not convert else 'jpeg')
143
  if new_url:
144
  new_images_urls.append(new_url)
145
- logger.info(f'загружено изображение {image_path} в {new_url}')
146
  try:
147
  image_path.unlink()
148
  except Exception as e:
149
- logger.error(f'не удалось удалить файл {image_path}: {e}')
150
- logger.info(f'новые ссылки: ({len(new_images_urls)}): {new_images_urls}')
151
  try:
152
  rmtree(images_paths[0].parent)
153
  except Exception as e:
154
- logger.error(f'не удалось удалить файл {images_paths[0].parent}: {e}')
155
  return new_images_urls
156
 
157
 
 
34
 
35
 
36
  async def download_png(url: str, folder: str, client: AsyncClient, retries: int = 5) -> Path:
37
+ print(f'загрузка изображения: {url}')
38
  for attempt in range(retries):
39
  try:
40
  response = await client.get(url)
 
52
 
53
  async def download_pngs(urls: str | list[str]) -> list[Any]:
54
  urls = [urls] if isinstance(urls, str) else urls
 
55
  print(f'скачивается список список из {len(urls)}: {urls}')
56
  # бот coze имеет баг, и из воркфлоу прибавляет предыдущий ответ к ссылкам, если включен контекст чата:
57
  valid_urls = [url for url in urls if url and '\n' not in url and '\\n' not in url and url.strip() != '']
58
  if len(valid_urls) != len(urls):
59
+ print(f'некорректные ссылки удалены из списка: {set(urls) - set(valid_urls)}')
60
  async with AsyncClient(verify=False, follow_redirects=True, timeout=30.0) as client:
61
  tasks = [download_png(url, str(uuid4()), client) for url in valid_urls]
62
  return list(await gather(*tasks))
 
64
 
65
  async def optimize_png(image_path: Path, retries: int = 3) -> None:
66
  command = f'{oxipng_bin.resolve()} --opt 2 --strip safe --out {image_path} {image_path}'
67
+ print(f'оптимизация картинки {image_path}')
68
  for attempt in range(retries):
69
  try:
70
  process = await create_subprocess_shell(command, stdout=PIPE, stderr=PIPE)
 
74
  else:
75
  raise CalledProcessError(process.returncode, command, output=stdout, stderr=stderr)
76
  except CalledProcessError as e:
77
+ print(f'ошибка при оптимизации {image_path}')
78
  if attempt < retries - 1:
79
  await sleep(2 ** attempt)
80
  else:
 
82
 
83
 
84
  async def convert_to_jpeg(image_path: Path) -> Path:
85
+ print(f'конвертируется {image_path}')
86
  try:
87
  image = Image.open(image_path)
88
  output_path = image_path.with_suffix('.jpg')
 
90
  image_path.unlink(missing_ok=True)
91
  return output_path
92
  except:
93
+ print(f'ошибка при конвертации {image_path}')
94
  return image_path
95
 
96
 
97
  async def convert_to_jpegs(image_paths: list[str | Path] | str | Path) -> tuple[Path]:
98
  image_paths = [Path(image_file) for image_file in ([image_paths] if not isinstance(image_paths, list) else image_paths)]
99
+ print(f'оптимизируется список список из {len(image_paths)}: {image_paths}')
100
  tasks = [convert_to_jpeg(image_path) for image_path in image_paths]
101
  return await gather(*tasks)
102
 
103
 
104
  async def optimize_pngs(image_paths: list[str | Path] | str | Path) -> None:
105
  image_paths = [Path(image_file) for image_file in ([image_paths] if not isinstance(image_paths, list) else image_paths)]
106
+ print(f'оптимизируется список список из {len(image_paths)}: {image_paths}')
107
  tasks = [optimize_png(image_path) for image_path in image_paths]
108
  await gather(*tasks)
109
 
 
131
 
132
  async def optimize_and_upload(images_urls: list[str] | str, convert: bool = False) -> list[str]:
133
  images_urls = [images_urls] if isinstance(images_urls, str) else images_urls
134
+ print(f'принятые ссылки в обработку ({len(images_urls)}): {images_urls}')
135
  images_paths = await download_pngs(images_urls)
136
  if not convert:
137
  await optimize_pngs(images_paths)
 
141
  new_url = await upload_image(image_path, 'png' if not convert else 'jpeg')
142
  if new_url:
143
  new_images_urls.append(new_url)
144
+ print(f'загружено изображение {image_path} в {new_url}')
145
  try:
146
  image_path.unlink()
147
  except Exception as e:
148
+ print(f'не удалось удалить файл {image_path}: {e}')
149
+ print(f'новые ссылки: ({len(new_images_urls)}): {new_images_urls}')
150
  try:
151
  rmtree(images_paths[0].parent)
152
  except Exception as e:
153
+ print(f'не удалось удалить файл {images_paths[0].parent}: {e}')
154
  return new_images_urls
155
 
156