-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__init__.py
More file actions
183 lines (141 loc) · 6.16 KB
/
__init__.py
File metadata and controls
183 lines (141 loc) · 6.16 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
"""
DeepReader Skill for OpenClaw
==============================
An autonomous web content ingestion engine that:
1. Extracts URLs from user messages
2. Routes each URL to the appropriate specialized parser
3. Saves clean Markdown with YAML frontmatter to the agent's memory
Usage (from OpenClaw)::
from deepreader_skill import run
response = run("Check out this article: https://example.com/blog/post")
# → Scrapes the article, saves to memory/inbox/, returns confirmation.
Supported URL types:
- **Generic** (blogs, articles, docs) → via trafilatura
- **Twitter / X** → via Nitter instances
- **YouTube** → via youtube_transcript_api
"""
from __future__ import annotations
import logging
from typing import Any
from .core.router import ParserRouter
from .core.storage import StorageManager
from .core.utils import extract_urls
__version__ = "1.0.0"
__all__ = ["run"]
logger = logging.getLogger("deepreader")
# Configure logging if not already configured
if not logger.handlers:
handler = logging.StreamHandler()
handler.setFormatter(
logging.Formatter(
"%(asctime)s [%(name)s] %(levelname)s: %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
)
)
logger.addHandler(handler)
logger.setLevel(logging.INFO)
# ---------------------------------------------------------------------------
# Singleton instances (lazy init)
# ---------------------------------------------------------------------------
_router: ParserRouter | None = None
_storage: StorageManager | None = None
def _get_router() -> ParserRouter:
global _router
if _router is None:
_router = ParserRouter()
return _router
def _get_storage() -> StorageManager:
global _storage
if _storage is None:
_storage = StorageManager()
return _storage
# ---------------------------------------------------------------------------
# Public API — OpenClaw Entry Point
# ---------------------------------------------------------------------------
def run(text: str, **kwargs: Any) -> str:
"""Main entry point for the DeepReader skill.
Called by OpenClaw when a user message potentially contains URLs.
Args:
text: The raw user message (may contain one or more URLs).
kwargs: Reserved for future OpenClaw context (e.g. user_id,
chat_id, config overrides).
Returns:
A human-readable status message summarizing what was processed.
On failure, returns a graceful error description — never raises.
"""
try:
# Step 1: Extract URLs from the message
urls = extract_urls(text)
if not urls:
return "🔍 No URL detected in your message."
router = _get_router()
storage = _get_storage()
results: list[str] = []
errors: list[str] = []
for url in urls:
logger.info("Processing URL: %s", url)
# Step 2: Route to the correct parser
parse_result = router.route(url)
if not parse_result.success:
error_msg = (
f"❌ Failed to read **{url}**\n"
f" Reason: {parse_result.error}"
)
errors.append(error_msg)
logger.warning("Parse failed for %s: %s", url, parse_result.error)
continue
# Step 3: Save to memory
try:
filepath = storage.save(parse_result)
success_msg = (
f"✅ **{parse_result.title or 'Untitled'}**\n"
f" Source: {url}\n"
f" Saved to: `{filepath}`\n"
f" Content: {len(parse_result.content)} characters"
)
# --- NotebookLM Integration ---
text_lower = text.lower()
use_notebooklm = "notebooklm" in text_lower or "audio" in text_lower or "podcast" in text_lower
generate_audio = "audio" in text_lower or "podcast" in text_lower
if use_notebooklm:
logger.info("NotebookLM integration triggered for %s", filepath)
from .integrations.notebooklm import NotebookLMIntegration
nl_integration = NotebookLMIntegration()
nl_result = nl_integration.run_sync(
filepath=filepath,
title=parse_result.title or "DeepReader Document",
generate_audio=generate_audio
)
if "error" in nl_result:
errors.append(f"❌ NotebookLM upload failed: {nl_result['error']}")
else:
nb_id = nl_result.get("notebook_id")
success_msg += f"\n 📓 Notebook ID: {nb_id}"
if generate_audio and "audio_path" in nl_result:
success_msg += f"\n 🎙️ Audio Output: `{nl_result['audio_path']}`"
results.append(success_msg)
logger.info("Successfully saved %s", filepath)
except OSError as exc:
error_msg = (
f"❌ Parsed **{url}** but failed to save.\n"
f" Error: {exc}"
)
errors.append(error_msg)
logger.error("Storage error for %s: %s", url, exc)
# Step 4: Build the response
response_parts: list[str] = []
if results:
response_parts.append(f"📚 **DeepReader** — Processed {len(results)} URL(s):\n")
response_parts.extend(results)
if errors:
if results:
response_parts.append("\n---\n")
response_parts.append(f"⚠️ {len(errors)} URL(s) had issues:\n")
response_parts.extend(errors)
return "\n\n".join(response_parts)
except Exception as exc: # noqa: BLE001
logger.exception("DeepReader encountered an unexpected error")
return (
f"🚨 DeepReader encountered an unexpected error: {exc}\n"
"The agent remains operational. Please try again or check the logs."
)