fix: screenshot not showing properly if the website has slow loading time

This commit is contained in:
Norielle Cruz
2023-08-05 06:41:18 +08:00
committed by GitHub
parent 2c36118037
commit 72da699ac7

View File

@@ -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:
return {
'direct_result': {
'kind': 'photo',
'format': 'url',
'value': f'https://image.thum.io/get/maxAge/12/width/720/{kwargs["url"]}'
}
}
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': 'path',
'value': image_file_path
}
}
else:
return {'result': 'Unable to screenshot website'}
except:
return {'result': 'Unable to screenshot website'}