rns: store cleaned text instead of raw HTML body
This commit is contained in:
parent
21264b941f
commit
5c208ce3e3
2 changed files with 22 additions and 14 deletions
|
|
@ -90,7 +90,7 @@ def handle_add_submit(body):
|
||||||
return handle_add_form(f"Error: {esc(str(e))}")
|
return handle_add_form(f"Error: {esc(str(e))}")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
error_msg = str(e).lower()
|
error_msg = str(e).lower()
|
||||||
if any(x in error_msg for x in ("block", "cloudflare", "403", "ssl", "handshake", "max retries", "timeout", "connection")):
|
if any(x in error_msg for x in ("block", "cloudflare", "403", "429", "ssl", "handshake", "max retries", "timeout", "connection")):
|
||||||
return _respond(
|
return _respond(
|
||||||
f"<h1>add url (manual entry)</h1>"
|
f"<h1>add url (manual entry)</h1>"
|
||||||
f"<p><strong>{esc(url)}</strong> blocks automated access. "
|
f"<p><strong>{esc(url)}</strong> blocks automated access. "
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,8 @@ import json
|
||||||
import time
|
import time
|
||||||
import threading
|
import threading
|
||||||
import traceback
|
import traceback
|
||||||
|
import datetime
|
||||||
|
from bs4 import BeautifulSoup
|
||||||
from tinyweb.db import get_db, return_db
|
from tinyweb.db import get_db, return_db
|
||||||
from tinyweb.rns_client import fetch_remote_page
|
from tinyweb.rns_client import fetch_remote_page
|
||||||
from tinyweb.templates import esc
|
from tinyweb.templates import esc
|
||||||
|
|
@ -69,26 +71,32 @@ def handle_rns_add_hash(dest_hash, name=""):
|
||||||
try:
|
try:
|
||||||
resp = fetch_remote_page(dest_hash, "/")
|
resp = fetch_remote_page(dest_hash, "/")
|
||||||
if resp.get("status") == 200:
|
if resp.get("status") == 200:
|
||||||
body = resp.get("body", "")
|
body_raw = resp.get("body", "")
|
||||||
|
soup = BeautifulSoup(body_raw, 'html.parser')
|
||||||
|
for tag in soup(["script", "style", "nav", "footer", "header", "noscript", "aside"]):
|
||||||
|
tag.decompose()
|
||||||
|
cleaned = soup.get_text(separator=" ", strip=True)
|
||||||
|
|
||||||
title = name or dest_hash[:16]
|
title = name or dest_hash[:16]
|
||||||
import re
|
if soup.title and soup.title.string:
|
||||||
m = re.search(r"<title[^>]*>(.*?)</title>", body, re.IGNORECASE | re.DOTALL)
|
title = soup.title.string.strip()
|
||||||
if m:
|
|
||||||
title = m.group(1).strip()
|
|
||||||
desc = ""
|
desc = ""
|
||||||
m = re.search(r'<meta\s+name="description"\s+content="([^"]*)"', body, re.IGNORECASE)
|
m = soup.find("meta", attrs={"name": "description"})
|
||||||
if m:
|
if m and m.get("content"):
|
||||||
desc = m.group(1).strip()
|
desc = m["content"].strip()
|
||||||
if not desc:
|
if not desc:
|
||||||
text = re.sub(r"<[^>]+>", " ", body)
|
m = soup.find("meta", attrs={"property": "og:description"})
|
||||||
text = re.sub(r"\s+", " ", text).strip()
|
if m and m.get("content"):
|
||||||
desc = text[:200].strip()
|
desc = m["content"].strip()
|
||||||
|
if not desc:
|
||||||
|
desc = cleaned[:200].strip()
|
||||||
|
|
||||||
url = f"rns:{dest_hash}"
|
url = f"rns:{dest_hash}"
|
||||||
import datetime
|
|
||||||
now = datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%S")
|
now = datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%S")
|
||||||
db.execute(
|
db.execute(
|
||||||
"INSERT OR REPLACE INTO pages (url, title, body, last_modified, summary) VALUES (?, ?, ?, ?, ?)",
|
"INSERT OR REPLACE INTO pages (url, title, body, last_modified, summary) VALUES (?, ?, ?, ?, ?)",
|
||||||
(url, title, body, now, desc),
|
(url, title, cleaned, now, desc),
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
errors.append(f"Remote returned status {resp.get('status')}")
|
errors.append(f"Remote returned status {resp.get('status')}")
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue