-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcontrolc.py
More file actions
64 lines (49 loc) · 1.99 KB
/
Copy pathcontrolc.py
File metadata and controls
64 lines (49 loc) · 1.99 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
"""Extracción de enlaces de pastes de controlc.com sin renderizar JavaScript."""
from __future__ import annotations
import re
from urllib.parse import urljoin
import aiohttp
from lxml import html
USER_AGENT = (
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 "
"(KHTML, like Gecko) Chrome/126.0 Safari/537.36"
)
URL_RE = re.compile(r"https?://[^\s\"'<>]+")
# Los pastes suelen listar varios mirrors del mismo archivo: se queda con el
# primer host de esta lista que aparezca; si ninguno aparece, devuelve todos.
PASTE_HOST_PRIORITY = ["rapidgator.net", "katfile.com"]
async def _fetch(session: aiohttp.ClientSession, url: str) -> str:
async with session.get(url, headers={"User-Agent": USER_AGENT}) as resp:
resp.raise_for_status()
return await resp.text()
def _visible_text(element) -> str:
for hidden in element.xpath(
'.//*[contains(@style, "display:none") or contains(@style, "display: none")]'
):
hidden.drop_tree() # a diferencia de remove(), conserva el texto posterior al nodo
return element.text_content()
def extract_links(text: str) -> list[str]:
links: list[str] = []
for match in URL_RE.findall(text):
link = match.rstrip(").,;")
if link not in links:
links.append(link)
for host in PASTE_HOST_PRIORITY:
preferred = [link for link in links if host in link]
if preferred:
return preferred
return links
async def get_paste_links(session: aiohttp.ClientSession, url: str) -> list[str]:
page = await _fetch(session, url)
tree = html.fromstring(page)
iframe_src = tree.xpath('//*[@id="pasteFrame"]/@src')
if iframe_src:
page = await _fetch(session, urljoin(url, iframe_src[0]))
tree = html.fromstring(page)
paste = tree.xpath('//*[@id="thepaste"]')
if paste:
text = _visible_text(paste[0])
else:
body = tree.body if tree.body is not None else tree
text = _visible_text(body)
return extract_links(text)