let there be light

This commit is contained in:
Jurgis Sakalauskas
2026-04-28 11:48:29 +03:00
commit beb1294d86
15 changed files with 966 additions and 0 deletions
+29
View File
@@ -0,0 +1,29 @@
import re
from urllib.parse import urlparse
import requests
JINA_BASE = "https://r.jina.ai/"
def fetch(url: str, api_key: str | None = None, timeout: int = 60) -> tuple[str, str]:
"""Fetch an article via Jina Reader. Returns (title, markdown_body)."""
headers = {"Accept": "text/markdown"}
if api_key:
headers["Authorization"] = f"Bearer {api_key}"
resp = requests.get(JINA_BASE + url, headers=headers, timeout=timeout)
resp.raise_for_status()
body = resp.text
title = _extract_title(body) or _hostname_fallback(url)
return title, body
def _extract_title(body: str) -> str | None:
m = re.match(r"^Title:\s*(.+)$", body, re.MULTILINE)
if m:
return m.group(1).strip()
return None
def _hostname_fallback(url: str) -> str:
return urlparse(url).hostname or "Untitled"