mirror of
https://github.com/aljazceru/chatgpt-telegram-bot.git
synced 2025-12-22 23:25:41 +01:00
fix: screenshot not showing properly if the website has slow loading time
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import os, requests, random, string
|
||||
from typing import Dict
|
||||
from .plugin import Plugin
|
||||
|
||||
@@ -11,21 +12,41 @@ class WebshotPlugin(Plugin):
|
||||
def get_spec(self) -> [Dict]:
|
||||
return [{
|
||||
"name": "screenshot_website",
|
||||
"description": "Show screenshot/image of a website from a given url or domain name",
|
||||
"description": "Show screenshot/image of a website from a given url or domain name.",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"url": {"type": "string", "description": "Website url or domain name"}
|
||||
"url": {"type": "string", "description": "Website url or domain name. Correctly formatted url is required. Example: https://www.google.com"}
|
||||
},
|
||||
"required": ["url"],
|
||||
},
|
||||
}]
|
||||
|
||||
def generate_random_string(self, length):
|
||||
characters = string.ascii_letters + string.digits
|
||||
return ''.join(random.choice(characters) for _ in range(length))
|
||||
|
||||
async def execute(self, function_name, **kwargs) -> Dict:
|
||||
try:
|
||||
image_url = f'https://image.thum.io/get/maxAge/12/width/720/{kwargs["url"]}'
|
||||
response = requests.get(image_url, timeout=30)
|
||||
|
||||
if response.status_code == 200:
|
||||
if not os.path.exists("uploads/webshot"):
|
||||
os.makedirs("uploads/webshot")
|
||||
|
||||
image_file_path = os.path.join("uploads/webshot", f"{self.generate_random_string(15)}.png")
|
||||
with open(image_file_path, "wb") as f:
|
||||
f.write(response.content)
|
||||
|
||||
return {
|
||||
'direct_result': {
|
||||
'kind': 'photo',
|
||||
'format': 'url',
|
||||
'value': f'https://image.thum.io/get/maxAge/12/width/720/{kwargs["url"]}'
|
||||
'format': 'path',
|
||||
'value': image_file_path
|
||||
}
|
||||
}
|
||||
else:
|
||||
return {'result': 'Unable to screenshot website'}
|
||||
except:
|
||||
return {'result': 'Unable to screenshot website'}
|
||||
|
||||
Reference in New Issue
Block a user