Files
ttrss-plugin-af-fulltext/lib/FirecrawlFetcher.php
Michal 9bc854ec44 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
2026-08-25 00:16:16 +01:00

81 lines
2.7 KiB
PHP

<?php
namespace AfFulltext;
/**
* Fetch through a Firecrawl instance so the page arrives with its JavaScript
* already run.
*
* We ask for `rawHtml` on purpose. Firecrawl's own `onlyMainContent` extraction
* was measured against these feeds and is far too coarse -- on a Cloudflare blog
* post it returned 73KB still containing skip-links, analytics markup and the
* language-picker footer. Firecrawl is the better *renderer*; the rule engine in
* this plugin is the better *extractor*. So: browser here, extraction ours.
*/
final class FirecrawlFetcher implements Fetcher {
public function __construct(
private readonly string $endpoint,
private readonly int $timeout = 60,
private readonly ?string $api_key = null,
) {}
public function name(): string { return 'firecrawl'; }
public function fetch(string $url, array $headers = []): FetchResult {
$payload = [
'url' => $url,
'formats' => ['rawHtml'],
// Firecrawl's own boilerplate stripping is deliberately off; see above.
'onlyMainContent' => false,
'timeout' => $this->timeout * 1000,
];
if ($headers) $payload['headers'] = $headers;
$request_headers = ['Content-Type: application/json'];
if ($this->api_key) $request_headers[] = 'Authorization: Bearer ' . $this->api_key;
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => rtrim($this->endpoint, '/') . '/v1/scrape',
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => json_encode($payload, JSON_UNESCAPED_SLASHES),
CURLOPT_HTTPHEADER => $request_headers,
CURLOPT_RETURNTRANSFER => true,
// The browser render dominates; allow slack over the scrape timeout.
CURLOPT_TIMEOUT => $this->timeout + 15,
CURLOPT_CONNECTTIMEOUT => 10,
]);
$body = curl_exec($ch);
$err = curl_error($ch);
$status = (int) curl_getinfo($ch, CURLINFO_RESPONSE_CODE);
curl_close($ch);
if ($err !== '' || !is_string($body))
return new FetchResult(error: 'firecrawl: ' . ($err !== '' ? $err : 'empty response'), status: $status);
$json = json_decode($body, true);
if (!is_array($json))
return new FetchResult(error: 'firecrawl: unparseable response', status: $status);
if (empty($json['success'])) {
$msg = is_string($json['error'] ?? null) ? $json['error'] : "HTTP $status";
return new FetchResult(error: "firecrawl: $msg", status: $status);
}
$data = $json['data'] ?? [];
$html = $data['rawHtml'] ?? $data['html'] ?? '';
if (!is_string($html) || $html === '')
return new FetchResult(error: 'firecrawl: no html in response', status: $status);
$effective = $data['metadata']['sourceURL'] ?? $data['metadata']['url'] ?? $url;
return new FetchResult(
html: $html,
effective_url: is_string($effective) ? $effective : $url,
status: $status,
);
}
}