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:
60
bin/extract.php
Executable file
60
bin/extract.php
Executable file
@@ -0,0 +1,60 @@
|
||||
#!/usr/bin/env php
|
||||
<?php
|
||||
/**
|
||||
* Standalone harness for the extraction engine.
|
||||
*
|
||||
* The whole point of keeping lib/ free of tt-rss dependencies: rules can be
|
||||
* written and checked against real URLs without a running instance.
|
||||
*
|
||||
* php bin/extract.php <url> [--backend=direct|firecrawl] [--rule=FILE] [--html] [--quiet]
|
||||
*/
|
||||
require_once __DIR__ . '/../autoload.php';
|
||||
|
||||
use AfFulltext\{CurlFetcher, Extractor, FirecrawlFetcher, RuleSet};
|
||||
|
||||
$args = array_slice($argv, 1);
|
||||
$opts = ['backend' => 'direct', 'rule' => null, 'html' => false, 'quiet' => false];
|
||||
$url = null;
|
||||
|
||||
foreach ($args as $arg) {
|
||||
if (str_starts_with($arg, '--backend=')) $opts['backend'] = substr($arg, 10);
|
||||
elseif (str_starts_with($arg, '--rule=')) $opts['rule'] = substr($arg, 7);
|
||||
elseif ($arg === '--html') $opts['html'] = true;
|
||||
elseif ($arg === '--quiet') $opts['quiet'] = true;
|
||||
elseif (!str_starts_with($arg, '--')) $url = $arg;
|
||||
}
|
||||
|
||||
if (!$url) {
|
||||
fwrite(STDERR, "usage: extract.php <url> [--backend=direct|firecrawl] [--rule=FILE] [--html] [--quiet]\n");
|
||||
exit(2);
|
||||
}
|
||||
|
||||
$base = dirname(__DIR__);
|
||||
$dirs = [$base . '/site_config/custom', $base . '/site_config/standard'];
|
||||
|
||||
// --rule points at a single file to try, overriding whatever the site config says.
|
||||
if ($opts['rule']) {
|
||||
$tmp = sys_get_temp_dir() . '/af_fulltext_rule_' . getmypid();
|
||||
@mkdir($tmp, 0700, true);
|
||||
$host = strtolower((string) parse_url($url, PHP_URL_HOST));
|
||||
copy($opts['rule'], "$tmp/$host.txt");
|
||||
array_unshift($dirs, $tmp);
|
||||
}
|
||||
|
||||
$fetcher = $opts['backend'] === 'firecrawl'
|
||||
? new FirecrawlFetcher(getenv('FIRECRAWL_URL') ?: 'http://127.0.0.1:13002')
|
||||
: new CurlFetcher();
|
||||
|
||||
$result = (new Extractor(new RuleSet($dirs)))->extract($url, $fetcher);
|
||||
|
||||
if (!$opts['quiet']) {
|
||||
fwrite(STDERR, $result->summary() . "\n");
|
||||
foreach ($result->rule_sources as $s) fwrite(STDERR, " rule: $s\n");
|
||||
foreach ($result->errors as $e) fwrite(STDERR, " error: $e\n");
|
||||
if ($result->title) fwrite(STDERR, " title: {$result->title}\n");
|
||||
}
|
||||
|
||||
if ($opts['html']) echo $result->html, "\n";
|
||||
else echo trim(preg_replace('/\s+/u', ' ', strip_tags($result->html)) ?? ''), "\n";
|
||||
|
||||
exit($result->ok() ? 0 : 1);
|
||||
Reference in New Issue
Block a user