Update app.py
Browse files
app.py
CHANGED
|
@@ -11,7 +11,7 @@ from fastapi import FastAPI, HTTPException
|
|
| 11 |
from fastapi.responses import PlainTextResponse
|
| 12 |
from httpx import AsyncClient, HTTPStatusError, RequestError
|
| 13 |
from pydantic import BaseModel, HttpUrl
|
| 14 |
-
|
| 15 |
from aiorentry.client import Client as RentryClient
|
| 16 |
from uvicorn import run as uvicorn_run
|
| 17 |
|
|
@@ -168,17 +168,23 @@ async def process_image(old_url: str, image_path: Path, convert: bool) -> tuple[
|
|
| 168 |
async def optimize_and_upload(images_urls: List[str] | str, convert: bool = False) -> List[str]:
|
| 169 |
images_urls = [images_urls] if isinstance(images_urls, str) else images_urls
|
| 170 |
if convert:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 171 |
async with RentryClient('https://rentry.org') as client:
|
| 172 |
-
page = await client.new_page('
|
| 173 |
page_id, code = page.url, page.edit_code
|
| 174 |
-
continue_task = create_task(continue_optimizing_and_uploading(images_urls, page_id, code))
|
| 175 |
print(f'https://rentry.co/{page_id}', f'https://rentry.org/{page_id}')
|
| 176 |
-
return [f'https://rentry.co/{page_id}', f'https://rentry.org/{page_id}']
|
| 177 |
else:
|
| 178 |
return await continue_optimizing_and_uploading(images_urls)
|
| 179 |
|
| 180 |
|
| 181 |
-
async def continue_optimizing_and_uploading(images_urls: list[str], page_id: str = None, code: str = None) -> list[str]:
|
| 182 |
images_paths = await download_pngs(images_urls)
|
| 183 |
|
| 184 |
if not page_id: # convert=False
|
|
@@ -200,9 +206,14 @@ async def continue_optimizing_and_uploading(images_urls: list[str], page_id: str
|
|
| 200 |
except Exception as e:
|
| 201 |
print(f'не удалось удалить файл {images_paths[0].parent}: {e}')
|
| 202 |
|
| 203 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 204 |
if page_id and code:
|
| 205 |
-
content = '\n\n'.join([f'' for url in new_images_urls])
|
| 206 |
try:
|
| 207 |
async with RentryClient('https://rentry.org') as client:
|
| 208 |
await client.edit_page(
|
|
|
|
| 11 |
from fastapi.responses import PlainTextResponse
|
| 12 |
from httpx import AsyncClient, HTTPStatusError, RequestError
|
| 13 |
from pydantic import BaseModel, HttpUrl
|
| 14 |
+
from ytelegraph import TelegraphAPI
|
| 15 |
from aiorentry.client import Client as RentryClient
|
| 16 |
from uvicorn import run as uvicorn_run
|
| 17 |
|
|
|
|
| 168 |
async def optimize_and_upload(images_urls: List[str] | str, convert: bool = False) -> List[str]:
|
| 169 |
images_urls = [images_urls] if isinstance(images_urls, str) else images_urls
|
| 170 |
if convert:
|
| 171 |
+
try:
|
| 172 |
+
ph = TelegraphAPI()
|
| 173 |
+
ph_link = ph.create_page_md('DAll-E v3', 'изображения скоро появятся, обнови страницу...')
|
| 174 |
+
except Exception as e:
|
| 175 |
+
print(f'не получилось создать страницу на телеграфе: {e}')
|
| 176 |
+
ph_link = None
|
| 177 |
async with RentryClient('https://rentry.org') as client:
|
| 178 |
+
page = await client.new_page('изображения скоро появятся, обнови страницу...')
|
| 179 |
page_id, code = page.url, page.edit_code
|
| 180 |
+
continue_task = create_task(continue_optimizing_and_uploading(images_urls, page_id, code, ph_link))
|
| 181 |
print(f'https://rentry.co/{page_id}', f'https://rentry.org/{page_id}')
|
| 182 |
+
return [ph_link, f'https://rentry.co/{page_id}', f'https://rentry.org/{page_id}']
|
| 183 |
else:
|
| 184 |
return await continue_optimizing_and_uploading(images_urls)
|
| 185 |
|
| 186 |
|
| 187 |
+
async def continue_optimizing_and_uploading(images_urls: list[str], page_id: str = None, code: str = None, ph_link: str = None) -> list[str]:
|
| 188 |
images_paths = await download_pngs(images_urls)
|
| 189 |
|
| 190 |
if not page_id: # convert=False
|
|
|
|
| 206 |
except Exception as e:
|
| 207 |
print(f'не удалось удалить файл {images_paths[0].parent}: {e}')
|
| 208 |
|
| 209 |
+
content = '\n\n'.join([f'' for url in new_images_urls])
|
| 210 |
+
if ph_link:
|
| 211 |
+
try:
|
| 212 |
+
ph = TelegraphAPI()
|
| 213 |
+
ph.edit_page_md(ph_link, content)
|
| 214 |
+
except Exception as e:
|
| 215 |
+
print(f'не удалось создать страницу на телеграфе: {e}')
|
| 216 |
if page_id and code:
|
|
|
|
| 217 |
try:
|
| 218 |
async with RentryClient('https://rentry.org') as client:
|
| 219 |
await client.edit_page(
|