-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetch_url.py
More file actions
48 lines (44 loc) · 1.87 KB
/
Copy pathfetch_url.py
File metadata and controls
48 lines (44 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
from urllib.parse import urlsplit
TOOL = {
'id': 'fetch_url',
'name': '网页信息提取',
'description': '读取公开网页的标题、描述、正文摘要和主要链接信息;需要了解指定网页内容时使用。',
'parameters': {
'type': 'object',
'properties': {
'url': {'type': 'string', 'description': '公开 HTTP 或 HTTPS 网页地址', 'maxLength': 2000},
},
'required': ['url'],
'additionalProperties': False,
},
}
async def run(arguments, context):
url = str(arguments.get('url') or '').strip()[:2000]
try:
parsed = urlsplit(url)
except ValueError:
parsed = None
if parsed is None or parsed.scheme not in {'http', 'https'} or not parsed.hostname:
return {'ok': False, 'error': '请输入有效的 HTTP 或 HTTPS 网页地址'}
data = await context['http_json'](
'https://api.microlink.io/',
{'url': url, 'audio': 'false', 'video': 'false', 'screenshot': 'false'},
)
if not isinstance(data, dict) or data.get('status') != 'success':
message = data.get('message') if isinstance(data, dict) else None
return {'ok': False, 'error': str(message or '网页信息提取失败')[:300]}
page = data.get('data') if isinstance(data.get('data'), dict) else {}
image = page.get('image') if isinstance(page.get('image'), dict) else {}
logo = page.get('logo') if isinstance(page.get('logo'), dict) else {}
return {
'ok': True,
'url': page.get('url') or url,
'title': page.get('title') or '',
'description': page.get('description') or '',
'publisher': page.get('publisher') or '',
'author': page.get('author') or '',
'language': page.get('lang') or '',
'date': page.get('date') or '',
'image': image.get('url') or '',
'logo': logo.get('url') or '',
}