0f9cfe66ec
- WebChannel.read(): reads any URL via Jina Reader (r.jina.ai), returns Markdown - _install_skill(): add fallback from importlib.resources to Path(__file__) for editable installs - Explicit UTF-8 encoding on all file read/write operations Inspired by PR #215, implemented correctly (Jina instead of raw requests). Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
33 lines
1.0 KiB
Python
33 lines
1.0 KiB
Python
# -*- coding: utf-8 -*-
|
||
"""Web — any URL via Jina Reader. Always available."""
|
||
|
||
import urllib.request
|
||
from .base import Channel
|
||
|
||
_UA = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36"
|
||
|
||
|
||
class WebChannel(Channel):
|
||
name = "web"
|
||
description = "任意网页"
|
||
backends = ["Jina Reader"]
|
||
tier = 0
|
||
|
||
def can_handle(self, url: str) -> bool:
|
||
return True # Fallback — handles any URL
|
||
|
||
def check(self, config=None):
|
||
return "ok", "通过 Jina Reader 读取任意网页(curl https://r.jina.ai/URL)"
|
||
|
||
def read(self, url: str) -> str:
|
||
"""通过 Jina Reader 读取网页,返回 Markdown 全文。"""
|
||
if not url.startswith(("http://", "https://")):
|
||
url = "https://" + url
|
||
jina_url = f"https://r.jina.ai/{url}"
|
||
req = urllib.request.Request(
|
||
jina_url,
|
||
headers={"User-Agent": _UA, "Accept": "text/plain"},
|
||
)
|
||
with urllib.request.urlopen(req, timeout=30) as resp:
|
||
return resp.read().decode("utf-8")
|