add image upload function to TelegraphAPI

This commit is contained in:
F4ria 2024-07-10 19:29:15 +08:00
parent 8f2ae93ea4
commit f524af9b36

View File

@ -5,6 +5,7 @@ import importlib
import re import re
import traceback import traceback
from functools import update_wrapper from functools import update_wrapper
from mimetypes import guess_type
from pathlib import Path from pathlib import Path
from typing import Any, Callable, TypeVar from typing import Any, Callable, TypeVar
@ -452,6 +453,25 @@ class TelegraphAPI:
return new_dom return new_dom
def upload_image(self, file_name: str) -> str:
base_url = "https://telegra.ph"
upload_url = f"{base_url}/upload"
try:
content_type = guess_type(file_name)[0]
with open(file_name, "rb") as f:
response = requests.post(
upload_url, files={"file": ("blob", f, content_type)}
)
response.raise_for_status()
# [{'src': '/file/xx.jpg'}]
response = response.json()
image_url = f"{base_url}{response[0]['src']}"
return image_url
except Exception as e:
print(f"upload image: {e}")
return "https://telegra.ph/api"
# `import *` will give you these # `import *` will give you these
__all__ = [ __all__ = [