mirror of
				https://github.com/cdryzun/tg_bot_collections.git
				synced 2025-10-30 21:36:43 +08:00 
			
		
		
		
	add image upload function to TelegraphAPI
This commit is contained in:
		| @ -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__ = [ | ||||||
|  | |||||||
		Reference in New Issue
	
	Block a user