organic: Enhance external apis

This commit is contained in:
2025-12-22 16:56:23 +01:00
parent 551ddd3c11
commit 0cd3b3de8e
10 changed files with 350 additions and 110 deletions

View File

@@ -1,11 +1,15 @@
<?php
$url = 'https://lkv.at/at/zertifizierung/themen/BIO/zertifizierte-BIO-Betriebe.php';
header('Access-Control-Allow-Origin: *');
header('Content-Type: application/json; charset=UTF-8');
if ($_SERVER['REQUEST_METHOD'] !== 'GET' && $_SERVER['REQUEST_METHOD'] !== 'HEAD') {
header('Status: 405');
header('Content-Length: 0');
header('Content-Length: 56');
header('Allow: GET, HEAD');
echo "{\"error\":\"method not allowed\",\"allow\":[\"GET\",\"HEAD\"]}\n";
exit;
}
@@ -16,18 +20,39 @@ function jenc($data): string {
$info = $_SERVER['PATH_INFO'];
if ($info !== '') {
header('Status: 404');
header('Content-Length: 0');
header('Content-Length: 22');
echo "{\"error\":\"not found\"}\n";
exit;
}
header('Content-Type: application/json; charset=UTF-8');
echo "{\"data\":[\n";
$postalCode = '';
if (isset($_GET['postalCode']) && ctype_digit($_GET['postalCode'])) {
$postalCode = $_GET['postalCode'];
}
$lfbisNr = '';
if (isset($_GET['lfbisNr'])) {
if (strlen($_GET['lfbisNr']) === 7) {
$lfbisNr = '"' . $_GET['lfbisNr'];
} else {
$lfbisNr = 'invalid';
}
}
echo "{\"sourceUrl\":\"$url\",\"data\":[\n";
passthru(<<<EOF
curl -s 'https://lkv.at/at/zertifizierung/themen/BIO/zertifizierte-BIO-Betriebe.php' | grep '<tr' -A 3 \
| sed 's@<tr>@ {@;s@</tr>@ },@;s@<td><a .*href="\([^"]*\)".*>\(.*\)</a></td>@ "certUrl":"\\1","certNr":"\\2",@;s@<td>\(.*\)<br/>\(.*\)<br/>\([0-9]*\) \(.*\)</td>@ "name":"\\1","address":"\\2","postalCode":"\\3","city":"\\4"@;s@<td>\(.*\)<br/>\(.*\)</td>@ "name":"\\1","type":"\\2"@;\$s/.$//' \
| sed 's@"certUrl":"\(/[^"]*\)"@"certUrl":"https://lkv.at\\1"@' \
curl -s '$url' | grep -A3 '<tr' \
| sed 's@<tr>@ {@;s@</tr>@ },@;s@<td><a .*href="\([^"]*\)".*>\(.*\)</a></td>@ "certUrl":"\\1","certNr":"\\2",@;s@<td>\(.*\)<br/>\(.*\)<br/>\([0-9]*\) \([^<]*\)\(<br/>\)\?</td>@ "name":"\\1","address":"\\2","postalCode":"\\3","city":"\\4"@;s@<td>\(.*\)<br/>\(.*\)</td>@ "name":"\\1","type":"\\2"@' \
| sed 's@"certUrl":"\(/[^"]*\)"@"certUrl":"https://lkv.at\\1"@;s@"certNr":"[^0-9]*0\?\([0-9]\{7\}\|[0-9]\{1,4\}\|[0-9]\{7,\}\)_[^"]*"@\\0,"id":"\\1"@;s@"id":"\([0-9]\{7\}\)"@\\0,"lfbisNr":"\\1"@;s@"id":"\([^"]\{,6\}\|[^"]\{8,\}\)"@\\0,"lfbisNr":null@' \
| sed 's@"certUrl":"\([^"]*\)","certNr":"\([^"]*\)",@"certificates":[{"pdfUrl":"\\1","nr":"\\2"}],\\n @' \
| perl -ne 'BEGIN { our \$N = 8; our @buf; } push @buf, \$_; if (@buf > \$N) { print shift @buf; } my \$chunk = join "", @buf;
if (\$chunk =~ /(}],\n(?:.*\n){6}\s*"name":"[^"]*","type":)/) {
\$chunk =~ s/(}],\n((?:.*\n){2})(?:.*\n){2}\s*"certificates":\[\{"pdfUrl":"([^"]*)","nr":"([^"]*)"\}\],\n.*\n\s*"name":"[^"]*","type":"([^"]*)")/},{"pdfUrl":"\$3","nr":"\$4","type":"\$5"}],\n\$2/;
@buf = map { "\$_\n" } split /\n/, \$chunk;
} END { print for @buf; }' \
| grep -B3 -A1 --no-group-separator '"postalCode":"$postalCode' | grep -B2 -A2 --no-group-separator '"lfbisNr":$lfbisNr' \
| sed '\$s/.$//'
| sed 's@\s*",@",@g;s@:"\s*@:"@g'
EOF);
echo "]}\n";