html-reader-llm 0.0.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,39 @@
1
+ """html-reader-llm — HTML simplification and intelligent extraction for LLM.
2
+
3
+ Quick start (pip install):
4
+ pip install selectolax
5
+ from html_reader_llm import simplify_html
6
+ result = simplify_html(html_string)
7
+
8
+ Copy usage (no pip):
9
+ Copy settings.py and simplify.py into your project,
10
+ change `from html_reader_llm import settings` to `from . import settings` (or
11
+ adjust the import path), then use:
12
+ from simplify import simplify_html
13
+ result = simplify_html(html_string)
14
+ """
15
+
16
+ from html_reader_llm.browser import (
17
+ BrowserNotFoundError,
18
+ detect_browser_paths,
19
+ get_browser_path,
20
+ validate_browser_path,
21
+ )
22
+ from html_reader_llm.llm import AnalysisResult, analyze_page
23
+ from html_reader_llm.render_detect import RenderDetectResult, detect_render
24
+ from html_reader_llm.selector_validator import SelectorValidator, validate_url_selectors
25
+ from html_reader_llm.simplify import simplify_html
26
+
27
+ __all__ = [
28
+ "AnalysisResult",
29
+ "BrowserNotFoundError",
30
+ "RenderDetectResult",
31
+ "SelectorValidator",
32
+ "analyze_page",
33
+ "detect_browser_paths",
34
+ "detect_render",
35
+ "get_browser_path",
36
+ "simplify_html",
37
+ "validate_browser_path",
38
+ "validate_url_selectors",
39
+ ]
@@ -0,0 +1,161 @@
1
+ """Chrome/Edge browser path detection — platform-aware discovery and validation.
2
+
3
+ Three usage modes:
4
+ - detect_browser_paths() → list all found paths
5
+ - get_browser_path() → auto-select first (or None), checks ENV first
6
+ - validate_browser_path(path) → validate user path, raise if invalid
7
+
8
+ ENV override: HTMLREADER_CHROME_PATH
9
+ """
10
+
11
+ from __future__ import annotations
12
+
13
+ import logging
14
+ import os
15
+ import platform
16
+ import shutil
17
+ import subprocess
18
+
19
+ logger = logging.getLogger(__name__)
20
+
21
+ # --- Platform-specific known paths ---
22
+
23
+ WIN32_PATHS: list[str] = [
24
+ "C:/Program Files (x86)/Google/Chrome/Application/chrome.exe",
25
+ "C:/Program Files/Google/Chrome/Application/chrome.exe",
26
+ "C:/Program Files (x86)/Microsoft/Edge/Application/msedge.exe",
27
+ "C:/Program Files/Microsoft/Edge/Application/msedge.exe",
28
+ ]
29
+
30
+ # Add %USERPROFILE% based paths at runtime
31
+ _WIN32_USERPROFILE_PATHS: list[str] = [
32
+ "AppData/Local/Google/Chrome/Application/chrome.exe",
33
+ "AppData/Local/Microsoft/Edge/Application/msedge.exe",
34
+ ]
35
+
36
+ LINUX_PATHS: list[str] = [
37
+ "google-chrome",
38
+ "google-chrome-stable",
39
+ "google-chrome-beta",
40
+ "google-chrome-dev",
41
+ "microsoft-edge-stable",
42
+ ]
43
+
44
+ DARWIN_PATHS: list[str] = [
45
+ "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome",
46
+ "/Applications/Microsoft Edge.app/Contents/MacOS/Microsoft Edge",
47
+ ]
48
+
49
+ _VERSION_PREFIXES = (b"Google Chrome ", b"Microsoft Edge")
50
+
51
+
52
+ class BrowserNotFoundError(Exception):
53
+ """Raised when no valid Chrome/Edge executable is found."""
54
+
55
+
56
+ def _iter_browser_paths() -> list[str]:
57
+ """Detect installed Chrome/Edge paths on the current platform.
58
+
59
+ Returns a list of absolute paths, sorted by mtime descending (newest first).
60
+ """
61
+ current = platform.system()
62
+ found: set[str] = set()
63
+
64
+ if current == "Windows":
65
+ # Fixed known paths
66
+ for p in WIN32_PATHS:
67
+ if os.path.isfile(p):
68
+ found.add(os.path.abspath(p))
69
+ # USERPROFILE-based paths
70
+ userprofile = os.environ.get("USERPROFILE", "")
71
+ if userprofile:
72
+ for rel in _WIN32_USERPROFILE_PATHS:
73
+ full = os.path.join(userprofile, rel)
74
+ if os.path.isfile(full):
75
+ found.add(os.path.abspath(full))
76
+
77
+ elif current == "Linux":
78
+ for cmd in LINUX_PATHS:
79
+ resolved = shutil.which(cmd)
80
+ if resolved and _verify_browser(resolved):
81
+ found.add(os.path.abspath(resolved))
82
+
83
+ elif current == "Darwin":
84
+ for p in DARWIN_PATHS:
85
+ if os.path.isfile(p):
86
+ found.add(os.path.abspath(p))
87
+
88
+ else:
89
+ logger.warning("Unsupported platform: %s", current)
90
+
91
+ result = sorted(found, key=_mtime_key, reverse=True)
92
+ return result
93
+
94
+
95
+ def _verify_browser(path: str) -> bool:
96
+ """Check if a binary is actually Chrome/Edge by running --version."""
97
+ try:
98
+ out = subprocess.check_output(
99
+ [path, "--version"], timeout=2, stderr=subprocess.DEVNULL
100
+ )
101
+ return any(out.startswith(prefix) for prefix in _VERSION_PREFIXES)
102
+ except (FileNotFoundError, subprocess.TimeoutExpired, OSError):
103
+ return False
104
+
105
+
106
+ def _mtime_key(path: str) -> float:
107
+ """Get mtime for sorting, 0 if file doesn't exist."""
108
+ try:
109
+ return os.path.getmtime(path)
110
+ except OSError:
111
+ return 0.0
112
+
113
+
114
+ def detect_browser_paths() -> list[str]:
115
+ """List all detected Chrome/Edge browser paths on this platform.
116
+
117
+ Returns absolute paths sorted by modification time (newest first).
118
+ """
119
+ return _iter_browser_paths()
120
+
121
+
122
+ def get_browser_path() -> str | None:
123
+ """Auto-select the best browser path.
124
+
125
+ Priority:
126
+ 1. HTMLREADER_CHROME_PATH env var (if set and valid)
127
+ 2. First detected path (newest by mtime)
128
+
129
+ Returns None if no browser is found.
130
+ """
131
+ # ENV override
132
+ env_path = os.environ.get("HTMLREADER_CHROME_PATH", "").strip()
133
+ if env_path:
134
+ try:
135
+ return validate_browser_path(env_path)
136
+ except BrowserNotFoundError:
137
+ logger.warning(
138
+ "HTMLREADER_CHROME_PATH=%s is invalid, falling back to auto-detect",
139
+ env_path,
140
+ )
141
+
142
+ paths = detect_browser_paths()
143
+ return paths[0] if paths else None
144
+
145
+
146
+ def validate_browser_path(path: str) -> str:
147
+ """Validate a user-provided browser path.
148
+
149
+ Args:
150
+ path: Path to Chrome/Edge executable.
151
+
152
+ Returns:
153
+ Absolute resolved path.
154
+
155
+ Raises:
156
+ BrowserNotFoundError: If the path does not exist or is not a valid file.
157
+ """
158
+ resolved = os.path.abspath(path)
159
+ if not os.path.isfile(resolved):
160
+ raise BrowserNotFoundError(f"Browser executable not found: {resolved}")
161
+ return resolved