Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
print(f'загрузка изображения: {url}')
|
38 |
for attempt in range(retries):
|
39 |
try:
|
40 |
response = await client.get(url)
|
@@ -52,7 +52,7 @@ 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 |
-
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):
|
@@ -64,7 +64,7 @@ async def download_pngs(urls: str | list[str]) -> list[Any]:
|
|
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)
|
@@ -82,7 +82,7 @@ async def optimize_png(image_path: Path, retries: int = 3) -> None:
|
|
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')
|
@@ -96,14 +96,14 @@ async def convert_to_jpeg(image_path: Path) -> Path:
|
|
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 |
|
@@ -151,7 +151,7 @@ async def process_image(old_url: str, image_path: Path, convert: bool) -> tuple[
|
|
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 |
|
156 |
images_paths = await download_pngs(images_urls)
|
157 |
if not convert:
|
@@ -186,6 +186,7 @@ async def optimize_and_upload(images_urls: List[str] | str, convert: bool = Fals
|
|
186 |
async with RentryClient('https://rentry.org') as client:
|
187 |
page = await client.new_page(content)
|
188 |
page_id = page.url
|
|
|
189 |
return [f'https://rentry.co/{page_id}', f'https://rentry.org/{page_id}']
|
190 |
except Exception as e:
|
191 |
print(f'не удалось создать страницу в rentry: {e}')
|
|
|
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):
|
|
|
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)
|
|
|
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')
|
|
|
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 |
|
|
|
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 |
|
156 |
images_paths = await download_pngs(images_urls)
|
157 |
if not convert:
|
|
|
186 |
async with RentryClient('https://rentry.org') as client:
|
187 |
page = await client.new_page(content)
|
188 |
page_id = page.url
|
189 |
+
print(f'https://rentry.co/{page_id}', f'https://rentry.org/{page_id}')
|
190 |
return [f'https://rentry.co/{page_id}', f'https://rentry.org/{page_id}']
|
191 |
except Exception as e:
|
192 |
print(f'не удалось создать страницу в rentry: {e}')
|