vega-cli 0.1.0__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.
- vega_cli/__init__.py +1 -0
- vega_cli/__main__.py +6 -0
- vega_cli/cli.py +247 -0
- vega_cli/config.py +99 -0
- vega_cli/display.py +138 -0
- vega_cli/html.py +540 -0
- vega_cli/interactive.py +220 -0
- vega_cli/player.py +82 -0
- vega_cli/providers/__init__.py +13 -0
- vega_cli/providers/ani_dream.py +1089 -0
- vega_cli/providers/anikoto.py +820 -0
- vega_cli/providers/animesalt.py +649 -0
- vega_cli/providers/animesub.py +1374 -0
- vega_cli/providers/animeworld.py +504 -0
- vega_cli/providers/auto_embed.py +479 -0
- vega_cli/providers/castle.py +497 -0
- vega_cli/providers/cncverse.py +873 -0
- vega_cli/providers/common/__init__.py +51 -0
- vega_cli/providers/common/aes.py +152 -0
- vega_cli/providers/common/aniskip.py +186 -0
- vega_cli/providers/common/base_url.py +102 -0
- vega_cli/providers/common/cinemeta.py +231 -0
- vega_cli/providers/common/errors.py +25 -0
- vega_cli/providers/common/extractors/__init__.py +23 -0
- vega_cli/providers/common/extractors/abbasy.py +247 -0
- vega_cli/providers/common/extractors/buzzheavier.py +72 -0
- vega_cli/providers/common/extractors/cinejoy.py +33 -0
- vega_cli/providers/common/extractors/gdflix.py +170 -0
- vega_cli/providers/common/extractors/gofile.py +167 -0
- vega_cli/providers/common/extractors/hubcloud.py +277 -0
- vega_cli/providers/common/extractors/movy.py +349 -0
- vega_cli/providers/common/extractors/st111477.py +139 -0
- vega_cli/providers/common/extractors/transferit.py +53 -0
- vega_cli/providers/common/extractors/vegadrive.py +161 -0
- vega_cli/providers/common/extractors/vidstuck.py +379 -0
- vega_cli/providers/common/extractors/zcloud.py +158 -0
- vega_cli/providers/common/extractors/zypter.py +72 -0
- vega_cli/providers/common/headers.py +25 -0
- vega_cli/providers/common/http.py +121 -0
- vega_cli/providers/common/net_mirror.py +1494 -0
- vega_cli/providers/common/theintrodb.py +112 -0
- vega_cli/providers/common/unpack.py +54 -0
- vega_cli/providers/disney_mirror.py +31 -0
- vega_cli/providers/dramacool.py +297 -0
- vega_cli/providers/drive.py +695 -0
- vega_cli/providers/durbin_live.py +354 -0
- vega_cli/providers/eon_movies.py +752 -0
- vega_cli/providers/extraflix.py +701 -0
- vega_cli/providers/four_khdhub.py +307 -0
- vega_cli/providers/goku_hd.py +336 -0
- vega_cli/providers/hdhub4u.py +665 -0
- vega_cli/providers/internet_archive.py +552 -0
- vega_cli/providers/kartoons.py +642 -0
- vega_cli/providers/kartoons2.py +395 -0
- vega_cli/providers/katmovies.py +816 -0
- vega_cli/providers/kick_ass_anime.py +445 -0
- vega_cli/providers/kiss_kh.py +490 -0
- vega_cli/providers/live_tg_tv.py +540 -0
- vega_cli/providers/lux_movies.py +650 -0
- vega_cli/providers/mod.py +676 -0
- vega_cli/providers/movie_box_web.py +921 -0
- vega_cli/providers/movielinkdb.py +734 -0
- vega_cli/providers/movies4u.py +580 -0
- vega_cli/providers/moviezwap.py +199 -0
- vega_cli/providers/mxplayer.py +470 -0
- vega_cli/providers/netflix_mirror.py +32 -0
- vega_cli/providers/one_cinevood.py +596 -0
- vega_cli/providers/prime_mirror.py +31 -0
- vega_cli/providers/rareanimes.py +676 -0
- vega_cli/providers/registry.py +124 -0
- vega_cli/providers/showbox.py +245 -0
- vega_cli/providers/topmovies.py +707 -0
- vega_cli/providers/torrentio.py +398 -0
- vega_cli/providers/uhd.py +483 -0
- vega_cli/providers/vega.py +891 -0
- vega_cli/providers/youtube.py +566 -0
- vega_cli/providers/zeefliz.py +563 -0
- vega_cli/store.py +45 -0
- vega_cli/utils.py +90 -0
- vega_cli-0.1.0.dist-info/METADATA +8 -0
- vega_cli-0.1.0.dist-info/RECORD +84 -0
- vega_cli-0.1.0.dist-info/WHEEL +5 -0
- vega_cli-0.1.0.dist-info/entry_points.txt +2 -0
- vega_cli-0.1.0.dist-info/top_level.txt +1 -0
vega_cli/html.py
ADDED
|
@@ -0,0 +1,540 @@
|
|
|
1
|
+
"""Lightweight stdlib HTML DOM parser with CSS selector support (no bs4/lxml dependency)."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import html as html_mod
|
|
6
|
+
import re
|
|
7
|
+
from dataclasses import dataclass, field
|
|
8
|
+
from html.parser import HTMLParser
|
|
9
|
+
from typing import Iterable
|
|
10
|
+
|
|
11
|
+
VOID_TAGS = {
|
|
12
|
+
"area",
|
|
13
|
+
"base",
|
|
14
|
+
"br",
|
|
15
|
+
"col",
|
|
16
|
+
"embed",
|
|
17
|
+
"hr",
|
|
18
|
+
"img",
|
|
19
|
+
"input",
|
|
20
|
+
"link",
|
|
21
|
+
"meta",
|
|
22
|
+
"param",
|
|
23
|
+
"source",
|
|
24
|
+
"track",
|
|
25
|
+
"wbr",
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
_RAW_TEXT_TAGS = {"script", "style"}
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
@dataclass
|
|
32
|
+
class Node:
|
|
33
|
+
"""Minimal DOM tree node with CSS selector and cheerio-like helpers."""
|
|
34
|
+
|
|
35
|
+
tag: str
|
|
36
|
+
attrs: dict[str, str] = field(default_factory=dict)
|
|
37
|
+
children: list[Node | str] = field(default_factory=list)
|
|
38
|
+
parent: Node | None = None
|
|
39
|
+
|
|
40
|
+
def attr(self, name: str, default: str = "") -> str:
|
|
41
|
+
return self.attrs.get(name.lower(), default)
|
|
42
|
+
|
|
43
|
+
def has_attr(self, name: str) -> bool:
|
|
44
|
+
return name.lower() in self.attrs
|
|
45
|
+
|
|
46
|
+
def classes(self) -> set[str]:
|
|
47
|
+
return set(self.attr("class").split())
|
|
48
|
+
|
|
49
|
+
def text(self) -> str:
|
|
50
|
+
parts: list[str] = []
|
|
51
|
+
for child in self.children:
|
|
52
|
+
if isinstance(child, str):
|
|
53
|
+
parts.append(child)
|
|
54
|
+
else:
|
|
55
|
+
parts.append(child.text())
|
|
56
|
+
return "".join(parts)
|
|
57
|
+
|
|
58
|
+
def html(self) -> str:
|
|
59
|
+
"""Return inner HTML string of this node."""
|
|
60
|
+
parts: list[str] = []
|
|
61
|
+
for child in self.children:
|
|
62
|
+
if isinstance(child, str):
|
|
63
|
+
parts.append(child)
|
|
64
|
+
else:
|
|
65
|
+
parts.append(child.outer_html())
|
|
66
|
+
return "".join(parts)
|
|
67
|
+
|
|
68
|
+
def outer_html(self) -> str:
|
|
69
|
+
"""Return outer HTML string of this node."""
|
|
70
|
+
if self.tag == "document":
|
|
71
|
+
return self.html()
|
|
72
|
+
attr_str = "".join(
|
|
73
|
+
f' {k}="{html_mod.escape(v, quote=True)}"' for k, v in self.attrs.items()
|
|
74
|
+
)
|
|
75
|
+
if self.tag in VOID_TAGS:
|
|
76
|
+
return f"<{self.tag}{attr_str}>"
|
|
77
|
+
return f"<{self.tag}{attr_str}>{self.html()}</{self.tag}>"
|
|
78
|
+
|
|
79
|
+
def children_elements(self) -> list[Node]:
|
|
80
|
+
return [c for c in self.children if isinstance(c, Node)]
|
|
81
|
+
|
|
82
|
+
def iter_elements(self) -> list[Node]:
|
|
83
|
+
result: list[Node] = []
|
|
84
|
+
for child in self.children:
|
|
85
|
+
if isinstance(child, Node):
|
|
86
|
+
result.append(child)
|
|
87
|
+
result.extend(child.iter_elements())
|
|
88
|
+
return result
|
|
89
|
+
|
|
90
|
+
def find_all(
|
|
91
|
+
self,
|
|
92
|
+
*,
|
|
93
|
+
tag: str | None = None,
|
|
94
|
+
class_name: str | None = None,
|
|
95
|
+
id_name: str | None = None,
|
|
96
|
+
) -> list[Node]:
|
|
97
|
+
matches: list[Node] = []
|
|
98
|
+
for node in self.iter_elements():
|
|
99
|
+
if tag and node.tag != tag:
|
|
100
|
+
continue
|
|
101
|
+
if class_name and class_name not in node.classes():
|
|
102
|
+
continue
|
|
103
|
+
if id_name and node.attr("id") != id_name:
|
|
104
|
+
continue
|
|
105
|
+
matches.append(node)
|
|
106
|
+
return matches
|
|
107
|
+
|
|
108
|
+
def find_first(
|
|
109
|
+
self,
|
|
110
|
+
*,
|
|
111
|
+
tag: str | None = None,
|
|
112
|
+
class_name: str | None = None,
|
|
113
|
+
id_name: str | None = None,
|
|
114
|
+
) -> Node | None:
|
|
115
|
+
for node in self.iter_elements():
|
|
116
|
+
if tag and node.tag != tag:
|
|
117
|
+
continue
|
|
118
|
+
if class_name and class_name not in node.classes():
|
|
119
|
+
continue
|
|
120
|
+
if id_name and node.attr("id") != id_name:
|
|
121
|
+
continue
|
|
122
|
+
return node
|
|
123
|
+
return None
|
|
124
|
+
|
|
125
|
+
def next_element_sibling(self, tag: str | None = None) -> Node | None:
|
|
126
|
+
if not self.parent:
|
|
127
|
+
return None
|
|
128
|
+
siblings = [c for c in self.parent.children if isinstance(c, Node)]
|
|
129
|
+
try:
|
|
130
|
+
idx = siblings.index(self)
|
|
131
|
+
except ValueError:
|
|
132
|
+
return None
|
|
133
|
+
for sib in siblings[idx + 1 :]:
|
|
134
|
+
if tag is None or sib.tag == tag:
|
|
135
|
+
return sib
|
|
136
|
+
return None
|
|
137
|
+
|
|
138
|
+
def prev_element_sibling(self, tag: str | None = None) -> Node | None:
|
|
139
|
+
if not self.parent:
|
|
140
|
+
return None
|
|
141
|
+
siblings = [c for c in self.parent.children if isinstance(c, Node)]
|
|
142
|
+
try:
|
|
143
|
+
idx = siblings.index(self)
|
|
144
|
+
except ValueError:
|
|
145
|
+
return None
|
|
146
|
+
for sib in reversed(siblings[:idx]):
|
|
147
|
+
if tag is None or sib.tag == tag:
|
|
148
|
+
return sib
|
|
149
|
+
return None
|
|
150
|
+
|
|
151
|
+
def select(self, selector: str) -> list[Node]:
|
|
152
|
+
"""Find all descendant elements matching a CSS selector."""
|
|
153
|
+
return _select_nodes(self, selector)
|
|
154
|
+
|
|
155
|
+
def select_one(self, selector: str) -> Node | None:
|
|
156
|
+
"""Find the first descendant element matching a CSS selector."""
|
|
157
|
+
matches = _select_nodes(self, selector)
|
|
158
|
+
return matches[0] if matches else None
|
|
159
|
+
|
|
160
|
+
def matches(self, selector: str) -> bool:
|
|
161
|
+
"""Return True if this node matches the given CSS selector."""
|
|
162
|
+
for group in _split_top_level(selector, ","):
|
|
163
|
+
tokens = _tokenize_selector(group)
|
|
164
|
+
if tokens and _matches_chain(self, tokens, root=None):
|
|
165
|
+
return True
|
|
166
|
+
return False
|
|
167
|
+
|
|
168
|
+
def closest(self, selector: str) -> Node | None:
|
|
169
|
+
"""Find the closest ancestor (including self) matching selector."""
|
|
170
|
+
cur: Node | None = self
|
|
171
|
+
while cur is not None and cur.tag != "document":
|
|
172
|
+
if cur.matches(selector):
|
|
173
|
+
return cur
|
|
174
|
+
cur = cur.parent
|
|
175
|
+
return None
|
|
176
|
+
|
|
177
|
+
|
|
178
|
+
# ---------------------------------------------------------------------------
|
|
179
|
+
# CSS selector parser and matcher
|
|
180
|
+
# ---------------------------------------------------------------------------
|
|
181
|
+
|
|
182
|
+
def _split_top_level(text: str, sep: str) -> list[str]:
|
|
183
|
+
parts: list[str] = []
|
|
184
|
+
buf: list[str] = []
|
|
185
|
+
depth_paren = 0
|
|
186
|
+
depth_bracket = 0
|
|
187
|
+
quote: str | None = None
|
|
188
|
+
for ch in text:
|
|
189
|
+
if quote:
|
|
190
|
+
buf.append(ch)
|
|
191
|
+
if ch == quote:
|
|
192
|
+
quote = None
|
|
193
|
+
continue
|
|
194
|
+
if ch in ("'", '"'):
|
|
195
|
+
quote = ch
|
|
196
|
+
buf.append(ch)
|
|
197
|
+
elif ch == "(":
|
|
198
|
+
depth_paren += 1
|
|
199
|
+
buf.append(ch)
|
|
200
|
+
elif ch == ")":
|
|
201
|
+
depth_paren = max(0, depth_paren - 1)
|
|
202
|
+
buf.append(ch)
|
|
203
|
+
elif ch == "[":
|
|
204
|
+
depth_bracket += 1
|
|
205
|
+
buf.append(ch)
|
|
206
|
+
elif ch == "]":
|
|
207
|
+
depth_bracket = max(0, depth_bracket - 1)
|
|
208
|
+
buf.append(ch)
|
|
209
|
+
elif ch == sep and depth_paren == 0 and depth_bracket == 0:
|
|
210
|
+
part = "".join(buf).strip()
|
|
211
|
+
if part:
|
|
212
|
+
parts.append(part)
|
|
213
|
+
buf.clear()
|
|
214
|
+
else:
|
|
215
|
+
buf.append(ch)
|
|
216
|
+
tail = "".join(buf).strip()
|
|
217
|
+
if tail:
|
|
218
|
+
parts.append(tail)
|
|
219
|
+
return parts
|
|
220
|
+
|
|
221
|
+
|
|
222
|
+
def _tokenize_selector(selector: str) -> list[tuple[str, str]]:
|
|
223
|
+
"""Return list of (combinator, compound_selector), where combinator in ('', ' ', '>', '+', '~')."""
|
|
224
|
+
s = selector.strip()
|
|
225
|
+
if not s:
|
|
226
|
+
return []
|
|
227
|
+
tokens: list[tuple[str, str]] = []
|
|
228
|
+
i = 0
|
|
229
|
+
n = len(s)
|
|
230
|
+
combinator = ""
|
|
231
|
+
while i < n:
|
|
232
|
+
# Skip whitespace / combinators
|
|
233
|
+
had_space = False
|
|
234
|
+
while i < n and s[i].isspace():
|
|
235
|
+
had_space = True
|
|
236
|
+
i += 1
|
|
237
|
+
if i >= n:
|
|
238
|
+
break
|
|
239
|
+
if s[i] in (">", "+", "~"):
|
|
240
|
+
combinator = s[i]
|
|
241
|
+
i += 1
|
|
242
|
+
while i < n and s[i].isspace():
|
|
243
|
+
i += 1
|
|
244
|
+
elif had_space and tokens:
|
|
245
|
+
combinator = " "
|
|
246
|
+
|
|
247
|
+
# Read one compound selector
|
|
248
|
+
start = i
|
|
249
|
+
depth_paren = 0
|
|
250
|
+
depth_bracket = 0
|
|
251
|
+
quote: str | None = None
|
|
252
|
+
while i < n:
|
|
253
|
+
ch = s[i]
|
|
254
|
+
if quote:
|
|
255
|
+
if ch == quote:
|
|
256
|
+
quote = None
|
|
257
|
+
i += 1
|
|
258
|
+
continue
|
|
259
|
+
if ch in ("'", '"'):
|
|
260
|
+
quote = ch
|
|
261
|
+
i += 1
|
|
262
|
+
elif ch == "(":
|
|
263
|
+
depth_paren += 1
|
|
264
|
+
i += 1
|
|
265
|
+
elif ch == ")":
|
|
266
|
+
depth_paren = max(0, depth_paren - 1)
|
|
267
|
+
i += 1
|
|
268
|
+
elif ch == "[":
|
|
269
|
+
depth_bracket += 1
|
|
270
|
+
i += 1
|
|
271
|
+
elif ch == "]":
|
|
272
|
+
depth_bracket = max(0, depth_bracket - 1)
|
|
273
|
+
i += 1
|
|
274
|
+
elif depth_paren == 0 and depth_bracket == 0 and (ch.isspace() or ch in (">", "+", "~")):
|
|
275
|
+
break
|
|
276
|
+
else:
|
|
277
|
+
i += 1
|
|
278
|
+
compound = s[start:i].strip()
|
|
279
|
+
if compound:
|
|
280
|
+
tokens.append((combinator, compound))
|
|
281
|
+
combinator = ""
|
|
282
|
+
return tokens
|
|
283
|
+
|
|
284
|
+
|
|
285
|
+
_ATTR_RE = re.compile(
|
|
286
|
+
r"""^\[\s*([^\s=~|^$*\]]+)\s*(?:([~|^$*]?=)\s*(?:"([^"]*)"|'([^']*)'|([^\]]*?)))?\s*\]$"""
|
|
287
|
+
)
|
|
288
|
+
|
|
289
|
+
|
|
290
|
+
def _match_compound(node: Node, compound: str) -> bool:
|
|
291
|
+
if node.tag == "document":
|
|
292
|
+
return False
|
|
293
|
+
i = 0
|
|
294
|
+
n = len(compound)
|
|
295
|
+
# Optional leading tag or *
|
|
296
|
+
if i < n and compound[i] not in (".", "#", "[", ":"):
|
|
297
|
+
start = i
|
|
298
|
+
while i < n and compound[i] not in (".", "#", "[", ":"):
|
|
299
|
+
i += 1
|
|
300
|
+
tag = compound[start:i].lower()
|
|
301
|
+
if tag != "*" and node.tag != tag:
|
|
302
|
+
return False
|
|
303
|
+
|
|
304
|
+
while i < n:
|
|
305
|
+
ch = compound[i]
|
|
306
|
+
if ch == ".":
|
|
307
|
+
i += 1
|
|
308
|
+
start = i
|
|
309
|
+
while i < n and compound[i] not in (".", "#", "[", ":"):
|
|
310
|
+
i += 1
|
|
311
|
+
cls = compound[start:i]
|
|
312
|
+
if cls not in node.classes():
|
|
313
|
+
return False
|
|
314
|
+
elif ch == "#":
|
|
315
|
+
i += 1
|
|
316
|
+
start = i
|
|
317
|
+
while i < n and compound[i] not in (".", "#", "[", ":"):
|
|
318
|
+
i += 1
|
|
319
|
+
ident = compound[start:i]
|
|
320
|
+
if node.attr("id") != ident:
|
|
321
|
+
return False
|
|
322
|
+
elif ch == "[":
|
|
323
|
+
depth = 0
|
|
324
|
+
quote: str | None = None
|
|
325
|
+
start = i
|
|
326
|
+
while i < n:
|
|
327
|
+
c = compound[i]
|
|
328
|
+
if quote:
|
|
329
|
+
if c == quote:
|
|
330
|
+
quote = None
|
|
331
|
+
elif c in ("'", '"'):
|
|
332
|
+
quote = c
|
|
333
|
+
elif c == "[":
|
|
334
|
+
depth += 1
|
|
335
|
+
elif c == "]":
|
|
336
|
+
depth -= 1
|
|
337
|
+
if depth == 0:
|
|
338
|
+
i += 1
|
|
339
|
+
break
|
|
340
|
+
i += 1
|
|
341
|
+
attr_expr = compound[start:i]
|
|
342
|
+
m = _ATTR_RE.match(attr_expr)
|
|
343
|
+
if not m:
|
|
344
|
+
return False
|
|
345
|
+
attr_name = m.group(1).lower()
|
|
346
|
+
op = m.group(2)
|
|
347
|
+
val = m.group(3) if m.group(3) is not None else (
|
|
348
|
+
m.group(4) if m.group(4) is not None else (m.group(5) or "").strip()
|
|
349
|
+
)
|
|
350
|
+
if op is None:
|
|
351
|
+
if not node.has_attr(attr_name):
|
|
352
|
+
return False
|
|
353
|
+
else:
|
|
354
|
+
if not node.has_attr(attr_name):
|
|
355
|
+
return False
|
|
356
|
+
actual = node.attr(attr_name)
|
|
357
|
+
if op == "=" and actual != val:
|
|
358
|
+
return False
|
|
359
|
+
elif op == "*=" and val not in actual:
|
|
360
|
+
return False
|
|
361
|
+
elif op == "^=" and not actual.startswith(val):
|
|
362
|
+
return False
|
|
363
|
+
elif op == "$=" and not actual.endswith(val):
|
|
364
|
+
return False
|
|
365
|
+
elif op == "~=" and val not in actual.split():
|
|
366
|
+
return False
|
|
367
|
+
elif op == "|=" and actual != val and not actual.startswith(f"{val}-"):
|
|
368
|
+
return False
|
|
369
|
+
elif ch == ":":
|
|
370
|
+
i += 1
|
|
371
|
+
start = i
|
|
372
|
+
while i < n and compound[i] not in (".", "#", "[", ":", "("):
|
|
373
|
+
i += 1
|
|
374
|
+
pseudo = compound[start:i].lower()
|
|
375
|
+
arg = ""
|
|
376
|
+
if i < n and compound[i] == "(":
|
|
377
|
+
depth = 0
|
|
378
|
+
quote = None
|
|
379
|
+
i += 1
|
|
380
|
+
arg_start = i
|
|
381
|
+
while i < n:
|
|
382
|
+
c = compound[i]
|
|
383
|
+
if quote:
|
|
384
|
+
if c == quote:
|
|
385
|
+
quote = None
|
|
386
|
+
elif c in ("'", '"'):
|
|
387
|
+
quote = c
|
|
388
|
+
elif c == "(":
|
|
389
|
+
depth += 1
|
|
390
|
+
elif c == ")":
|
|
391
|
+
if depth == 0:
|
|
392
|
+
arg = compound[arg_start:i]
|
|
393
|
+
i += 1
|
|
394
|
+
break
|
|
395
|
+
depth -= 1
|
|
396
|
+
i += 1
|
|
397
|
+
if not _match_pseudo(node, pseudo, arg):
|
|
398
|
+
return False
|
|
399
|
+
else:
|
|
400
|
+
return False
|
|
401
|
+
return True
|
|
402
|
+
|
|
403
|
+
|
|
404
|
+
def _match_pseudo(node: Node, pseudo: str, arg: str) -> bool:
|
|
405
|
+
if pseudo == "first-child":
|
|
406
|
+
if not node.parent:
|
|
407
|
+
return False
|
|
408
|
+
elems = node.parent.children_elements()
|
|
409
|
+
return bool(elems and elems[0] is node)
|
|
410
|
+
if pseudo == "last-child":
|
|
411
|
+
if not node.parent:
|
|
412
|
+
return False
|
|
413
|
+
elems = node.parent.children_elements()
|
|
414
|
+
return bool(elems and elems[-1] is node)
|
|
415
|
+
if pseudo == "only-child":
|
|
416
|
+
if not node.parent:
|
|
417
|
+
return False
|
|
418
|
+
elems = node.parent.children_elements()
|
|
419
|
+
return len(elems) == 1 and elems[0] is node
|
|
420
|
+
if pseudo == "nth-child":
|
|
421
|
+
if not node.parent:
|
|
422
|
+
return False
|
|
423
|
+
elems = node.parent.children_elements()
|
|
424
|
+
try:
|
|
425
|
+
idx = elems.index(node) + 1
|
|
426
|
+
except ValueError:
|
|
427
|
+
return False
|
|
428
|
+
a = arg.strip().lower()
|
|
429
|
+
if a == "odd":
|
|
430
|
+
return idx % 2 == 1
|
|
431
|
+
if a == "even":
|
|
432
|
+
return idx % 2 == 0
|
|
433
|
+
if a.isdigit():
|
|
434
|
+
return idx == int(a)
|
|
435
|
+
m = re.match(r"^(\d*)n(?:\+(\d+))?$", a)
|
|
436
|
+
if m:
|
|
437
|
+
step = int(m.group(1)) if m.group(1) else 1
|
|
438
|
+
offset = int(m.group(2)) if m.group(2) else 0
|
|
439
|
+
return step > 0 and idx >= offset and (idx - offset) % step == 0
|
|
440
|
+
return False
|
|
441
|
+
if pseudo == "not":
|
|
442
|
+
return not node.matches(arg)
|
|
443
|
+
if pseudo == "contains":
|
|
444
|
+
needle = arg.strip().strip("'\"")
|
|
445
|
+
return needle in node.text()
|
|
446
|
+
if pseudo == "empty":
|
|
447
|
+
return not node.children_elements() and not node.text().strip()
|
|
448
|
+
return True
|
|
449
|
+
|
|
450
|
+
|
|
451
|
+
def _matches_chain(
|
|
452
|
+
node: Node,
|
|
453
|
+
tokens: list[tuple[str, str]],
|
|
454
|
+
root: Node | None,
|
|
455
|
+
) -> bool:
|
|
456
|
+
def _rec(cur: Node, idx: int) -> bool:
|
|
457
|
+
if not _match_compound(cur, tokens[idx][1]):
|
|
458
|
+
return False
|
|
459
|
+
if idx == 0:
|
|
460
|
+
return True
|
|
461
|
+
comb = tokens[idx][0]
|
|
462
|
+
if comb == ">":
|
|
463
|
+
p = cur.parent
|
|
464
|
+
return p is not None and p is not root and _rec(p, idx - 1)
|
|
465
|
+
if comb == " ":
|
|
466
|
+
p = cur.parent
|
|
467
|
+
while p is not None and p is not root:
|
|
468
|
+
if _rec(p, idx - 1):
|
|
469
|
+
return True
|
|
470
|
+
p = p.parent
|
|
471
|
+
return False
|
|
472
|
+
if comb == "+":
|
|
473
|
+
prev = cur.prev_element_sibling()
|
|
474
|
+
return prev is not None and _rec(prev, idx - 1)
|
|
475
|
+
if comb == "~":
|
|
476
|
+
prev = cur.prev_element_sibling()
|
|
477
|
+
while prev is not None:
|
|
478
|
+
if _rec(prev, idx - 1):
|
|
479
|
+
return True
|
|
480
|
+
prev = prev.prev_element_sibling()
|
|
481
|
+
return False
|
|
482
|
+
return False
|
|
483
|
+
|
|
484
|
+
return _rec(node, len(tokens) - 1)
|
|
485
|
+
|
|
486
|
+
|
|
487
|
+
def _select_nodes(root: Node, selector: str) -> list[Node]:
|
|
488
|
+
groups = _split_top_level(selector, ",")
|
|
489
|
+
if not groups:
|
|
490
|
+
return []
|
|
491
|
+
tokenized = [_tokenize_selector(g) for g in groups]
|
|
492
|
+
tokenized = [t for t in tokenized if t]
|
|
493
|
+
if not tokenized:
|
|
494
|
+
return []
|
|
495
|
+
results: list[Node] = []
|
|
496
|
+
seen: set[int] = set()
|
|
497
|
+
for node in root.iter_elements():
|
|
498
|
+
for tokens in tokenized:
|
|
499
|
+
if _matches_chain(node, tokens, root=root):
|
|
500
|
+
nid = id(node)
|
|
501
|
+
if nid not in seen:
|
|
502
|
+
seen.add(nid)
|
|
503
|
+
results.append(node)
|
|
504
|
+
break
|
|
505
|
+
return results
|
|
506
|
+
|
|
507
|
+
|
|
508
|
+
class _DOMBuilder(HTMLParser):
|
|
509
|
+
def __init__(self) -> None:
|
|
510
|
+
super().__init__(convert_charrefs=True)
|
|
511
|
+
self.root = Node(tag="document")
|
|
512
|
+
self.current = self.root
|
|
513
|
+
|
|
514
|
+
def handle_starttag(self, tag: str, attrs: list[tuple[str, str | None]]) -> None:
|
|
515
|
+
tag = tag.lower()
|
|
516
|
+
attr_dict = {k.lower(): (v or "") for k, v in attrs}
|
|
517
|
+
node = Node(tag=tag, attrs=attr_dict, parent=self.current)
|
|
518
|
+
self.current.children.append(node)
|
|
519
|
+
if tag not in VOID_TAGS:
|
|
520
|
+
self.current = node
|
|
521
|
+
|
|
522
|
+
def handle_endtag(self, tag: str) -> None:
|
|
523
|
+
tag = tag.lower()
|
|
524
|
+
cursor: Node | None = self.current
|
|
525
|
+
while cursor and cursor is not self.root:
|
|
526
|
+
if cursor.tag == tag:
|
|
527
|
+
self.current = cursor.parent or self.root
|
|
528
|
+
return
|
|
529
|
+
cursor = cursor.parent
|
|
530
|
+
|
|
531
|
+
def handle_data(self, data: str) -> None:
|
|
532
|
+
self.current.children.append(data)
|
|
533
|
+
|
|
534
|
+
|
|
535
|
+
def parse_html(html: str) -> Node:
|
|
536
|
+
"""Parse an HTML string into a Node tree."""
|
|
537
|
+
builder = _DOMBuilder()
|
|
538
|
+
builder.feed(html)
|
|
539
|
+
builder.close()
|
|
540
|
+
return builder.root
|