af_fulltext: rule-driven extraction with a pluggable renderer
Replaces subscribing feeds through a self-hosted Full-Text RSS proxy. Feed URLs go back to being real feed URLs and extraction happens inside tt-rss, using the same ftr-site-config rules the proxy used. The engine in lib/ has no tt-rss dependencies, so rules can be developed and audited from the command line; init.php is a thin adapter over it. Two findings from measuring the real subscription first, both of which shaped the design: - Firecrawl's own onlyMainContent is far too coarse to extract with (73KB of chrome on a Cloudflare post), but it is an excellent renderer. So it is used for rawHtml only and the rule engine does the extraction. - A body rule that stops matching after a redesign falls through to Readability and still produces a plausible article, so the breakage is invisible. Every extraction now records which rule matched and whether it fell back; auditing the 34 live feeds surfaced five community rules that match nothing. Custom rules included for the sites that needed them, including three comics where the article is an image and text-scoring extractors return the wrong thing or nothing at all. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011sSJdftQx5bW5HZHgUKF3i
This commit is contained in:
74
lib/CurlFetcher.php
Normal file
74
lib/CurlFetcher.php
Normal file
@@ -0,0 +1,74 @@
|
||||
<?php
|
||||
namespace AfFulltext;
|
||||
|
||||
/**
|
||||
* Plain HTTP GET via cURL.
|
||||
*
|
||||
* Used by the CLI harness. Inside tt-rss the `direct` backend is TtrssFetcher
|
||||
* instead, so article fetches keep tt-rss's own SSRF guard (UrlHelper::validate).
|
||||
*/
|
||||
final class CurlFetcher implements Fetcher {
|
||||
public function __construct(
|
||||
private readonly int $timeout = 20,
|
||||
private readonly string $user_agent = 'Mozilla/5.0 (compatible; af_fulltext/1.0; +https://tt-rss.org/)',
|
||||
) {}
|
||||
|
||||
public function name(): string { return 'direct'; }
|
||||
|
||||
public function fetch(string $url, array $headers = []): FetchResult {
|
||||
$ch = curl_init();
|
||||
|
||||
$hdr = [];
|
||||
foreach ($headers as $k => $v) $hdr[] = "$k: $v";
|
||||
|
||||
curl_setopt_array($ch, [
|
||||
CURLOPT_URL => $url,
|
||||
CURLOPT_RETURNTRANSFER => true,
|
||||
CURLOPT_FOLLOWLOCATION => true,
|
||||
CURLOPT_MAXREDIRS => 8,
|
||||
CURLOPT_TIMEOUT => $this->timeout,
|
||||
CURLOPT_CONNECTTIMEOUT => 10,
|
||||
CURLOPT_ENCODING => '',
|
||||
CURLOPT_USERAGENT => $headers['user-agent'] ?? $this->user_agent,
|
||||
CURLOPT_HTTPHEADER => $hdr,
|
||||
]);
|
||||
|
||||
$body = curl_exec($ch);
|
||||
$err = curl_error($ch);
|
||||
$status = (int) curl_getinfo($ch, CURLINFO_RESPONSE_CODE);
|
||||
$effective = (string) curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
|
||||
$content_type = (string) curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
|
||||
curl_close($ch);
|
||||
|
||||
if ($err !== '' || !is_string($body))
|
||||
return new FetchResult(error: $err !== '' ? $err : 'empty response', status: $status, effective_url: $effective ?: $url);
|
||||
|
||||
if ($status >= 400)
|
||||
return new FetchResult(error: "HTTP $status", status: $status, effective_url: $effective ?: $url);
|
||||
|
||||
return new FetchResult(
|
||||
html: self::to_utf8($body, $content_type),
|
||||
effective_url: $effective ?: $url,
|
||||
status: $status,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Normalise to UTF-8. Several of these feeds are Polish, and a page served as
|
||||
* ISO-8859-2 that is treated as UTF-8 loses every accented character.
|
||||
*/
|
||||
public static function to_utf8(string $body, string $content_type): string {
|
||||
$charset = '';
|
||||
|
||||
if (preg_match('/charset=["\']?([\w-]+)/i', $content_type, $m)) $charset = $m[1];
|
||||
|
||||
if ($charset === '' && preg_match('/<meta[^>]+charset=["\']?([\w-]+)/i', substr($body, 0, 4096), $m))
|
||||
$charset = $m[1];
|
||||
|
||||
if ($charset === '' || preg_match('/^utf-?8$/i', $charset)) return $body;
|
||||
|
||||
$converted = @mb_convert_encoding($body, 'UTF-8', $charset);
|
||||
|
||||
return is_string($converted) && $converted !== '' ? $converted : $body;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user