organic: Add extended API

This commit is contained in:
2026-09-01 18:02:57 +02:00
parent 028c16be3d
commit 4572323a14
13 changed files with 978 additions and 266 deletions
+5 -5
View File
@@ -121,7 +121,8 @@ if ($country === null) {
$data = [];
$url = "https://www.bioc.info/search/producerSearchQuery?search[name]=" . urlencode($name) . "&search[citycode]=" . urlencode($postalCode) . "&search[operatorId]=" . urlencode($idNr) . "&search[country]=$country";
$origin = "https://www.bioc.info/search/producersearchresult?producerSearch%5Bcountry%5D={$country}&producerSearch%5Bcitycode%5D=" . urlencode($postalCode) . "&producerSearch%5Bname%5D=" . urlencode($name) . "&producerSearch%5BoperatorId%5D=" . urlencode($idNr);
$url = "https://www.bioc.info/search/producerSearchQuery?search[country]={$country}&search[citycode]=" . urlencode($postalCode) . "&search[name]=" . urlencode($name) . "&search[operatorId]=" . urlencode($idNr);
$m = curl_multi_init();
$requests = [];
foreach ($source_groups as $group) {
@@ -134,6 +135,7 @@ foreach ($source_groups as $group) {
$running = 1;
do {
curl_multi_exec($m, $running);
if ($running) usleep(250_000);
} while ($running);
foreach ($requests as $r) {
curl_multi_remove_handle($m, $r);
@@ -141,7 +143,7 @@ foreach ($requests as $r) {
curl_multi_close($m);
foreach ($requests as $s) {
if (($json = curl_multi_getcontent($s)) === false) {
if (($json = curl_multi_getcontent($s)) === null) {
header('Status: 500');
header('Content-Length: 34');
echo "{\"error\":\"internal server error\"}\n";
@@ -153,10 +155,8 @@ foreach ($requests as $s) {
}
}
header('Content-Type: application/json; charset=UTF-8');
$first = true;
echo "{\"data\":[\n";
echo "{\"originUrl\":\"$origin\",\"data\":[\n";
foreach ($data as $id => $row) {
if (!$first) echo ",\n";
$row = explode("</", $row);
+1
View File
@@ -18,6 +18,7 @@ def main() -> None:
args = parser.parse_args()
s = requests.Session()
s.verify = False
while True:
try:
r = s.get(f'{URL}?menu_sid=5002')
+27 -21
View File
@@ -33,29 +33,35 @@ def main() -> None:
query = {'PartnerCertSearchForm:pcs_' + q.split('=', 1)[0]: urllib.parse.unquote(q.split('=', 1)[-1]) for q in args.query.split('&')}
s = requests.Session()
while True:
try:
r = s.get(f'{URL}?menu_sid=5002')
uri = ACTION_RE.findall(r.text)[0]
break
except IndexError:
pass
hidden = {m[1]: m[2] for m in HIDDEN_RE.finditer(r.text)}
s.verify = False
try:
while True:
try:
r = s.get(f'{URL}?menu_sid=5002')
uri = ACTION_RE.findall(r.text)[0]
break
except IndexError:
pass
hidden = {m[1]: m[2] for m in HIDDEN_RE.finditer(r.text)}
r = s.post(f'{BASE_URL}{uri}', data={
**query,
'PartnerCertSearchForm:button_search': 'Suche starten...',
'PartnerCertSearchForm_SUBMIT': '1',
'javax.faces.ViewState': hidden['javax.faces.ViewState'],
})
r = s.post(f'{BASE_URL}{uri}', data={
**query,
'PartnerCertSearchForm:button_search': 'Suche starten...',
'PartnerCertSearchForm_SUBMIT': '1',
'javax.faces.ViewState': hidden['javax.faces.ViewState'],
})
result_table = r.text[r.text.find('<table'):r.text.rfind('</table>') + 8]
uncollapsed_rows = [tuple(remove_tags(m[1])
for m in TD_RE.finditer(row[0]))
for row in UNCOLLAPSED_ROW_RE.finditer(result_table)]
collapsed_rows = [[tuple(remove_tags((ATTACHMENT_RE.search(m[1]) or m)[1]) for m in TD_RE.finditer(row[1]))
for row in ROW_RE.finditer(tbl[0])]
for tbl in COLLAPSED_ROW_RE.finditer(result_table)]
except:
print('[]')
return
result_table = r.text[r.text.find('<table'):r.text.rfind('</table>') + 8]
uncollapsed_rows = [tuple(remove_tags(m[1])
for m in TD_RE.finditer(row[0]))
for row in UNCOLLAPSED_ROW_RE.finditer(result_table)]
collapsed_rows = [[tuple(remove_tags((ATTACHMENT_RE.search(m[1]) or m)[1]) for m in TD_RE.finditer(row[1]))
for row in ROW_RE.finditer(tbl[0])]
for tbl in COLLAPSED_ROW_RE.finditer(result_table)]
print('[')
first = True
for row, tbl in zip(uncollapsed_rows, collapsed_rows):
+141
View File
@@ -0,0 +1,141 @@
<?php
header('Access-Control-Allow-Origin: *');
header('Content-Type: application/json; charset=UTF-8');
$base = 'https://' . $_SERVER['SERVER_NAME'] . explode('?', $_SERVER['REQUEST_URI'])[0];
if ($_SERVER['REQUEST_METHOD'] !== 'GET' && $_SERVER['REQUEST_METHOD'] !== 'HEAD') {
header('Status: 405');
header('Content-Length: 56');
header('Allow: GET, HEAD');
echo "{\"error\":\"method not allowed\",\"allow\":[\"GET\",\"HEAD\"]}\n";
exit;
}
function jenc($data): string {
return json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
}
$info = $_SERVER['PATH_INFO'];
if ($info !== '') {
header('Status: 404');
header('Content-Length: 22');
echo "{\"error\":\"not found\"}\n";
exit;
}
$country = $_GET['country'] ?? null;
$postalCode = $_GET['postalCode'] ?? null;
$m = curl_multi_init();
$requests = [];
foreach (['bioc', 'bioqs', 'easy-cert', 'lkv'] as $api) {
$s = curl_init("https://elwig.at/organic/external/$api/operators?country=$country&postalCode=$postalCode");
$requests[] = $s;
curl_setopt($s, CURLOPT_RETURNTRANSFER, true);
curl_multi_add_handle($m, $s);
}
$running = 1;
do {
curl_multi_exec($m, $running);
if ($running) usleep(250_000);
} while ($running);
foreach ($requests as $r) {
curl_multi_remove_handle($m, $r);
}
curl_multi_close($m);
$data = [];
foreach ($requests as $s) {
if (($json = curl_multi_getcontent($s)) === null) {
header('Status: 500');
header('Content-Length: 34');
echo "{\"error\":\"internal server error\"}\n";
exit;
}
$data[] = json_decode($json, true)['data'];
}
$operators = [];
foreach ($data[2] as $op) $operators[] = ['easyCert' => $op];
foreach ($data[0] as $op) {
if (($m = array_find($operators, fn($o) => isset($o['easyCert']) && $o['easyCert']['id'] === $op['id'])) !== null) {
$m['bioc'] = $op;
} else if (isset($op['details']) && ($m = array_find($operators, fn($o) => isset($o['easyCert']) && $o['easyCert']['idNr'] === $op['details']['idNr'])) !== null) {
$m['bioc'] = $op;
} else if (isset($op['details']) && ($m = array_find($operators, fn($o) => isset($o['bioc']['details']) && isset($op['details']) && $o['bioc']['name'] === $op['name'] && $o['bioc']['details']['address'] === $op['details']['address'])) !== null) {
$m['bioc'] = $op;
} else {
$operators[] = ['bioc' => $op];
}
}
foreach ($data[1] as $op) {
if (false) {
} else {
$operators[] = ['bioqs' => $op];
}
}
foreach ($data[3] as $op) $operators[] = ['lkv' => $op];
// for (const op of easyCert.data) operators.push({easyCert: op});
// for (const op of bioc.data) {
// let m;
// if ((m = operators.find(o => o.easyCert?.id === op.id))) {
// m.bioc = op;
// } else if ((m = operators.find(o => o.easyCert?.idNr === op.details.idNr))) {
// m.bioc = op;
// } else if ((m = operators.find(o => o.bioc?.name === op.name && o.bioc?.details.address === op.details.address))) {
// m.bioc2 = op;
// } else {
// operators.push({bioc: op});
// }
// }
// for (const op of bioqs.data) {
// let m;
// if (op.lfbisNr && (m = operators.find(o => o.easyCert?.idNr === op.lfbisNr))) {
// m.bioqs = op;
// } else if (op.lfbisNr && (m = operators.find(o => o.bioc?.details.idNr === op.lfbisNr))) {
// m.bioqs = op;
// } else if (op.idNr && (m = operators.find(o => o.bioc?.details.idNr === op.idNr))) {
// m.bioqs = op;
// } else if ((m = operators.find(o => (o.easyCert?.name ?? o.bioc?.name) === op.name && (o.easyCert?.details.address ?? o.bioc?.details.address) === op.address))) {
// m.bioqs = op;
// } else {
// operators.push({bioqs: op});
// }
// }
// for (const op of lkv.data) operators.push({lkv: op});
//
// for (const op of operators) {
// op.name = op.easyCert?.name ?? op.bioc?.name ?? op.bioqs?.name ?? op.lkv?.name ?? null;
// op.address = op.easyCert?.details.address ?? op.bioc?.details.address ?? op.bioqs?.address ?? op.lkv?.address ?? null;
// op.postalCode = op.easyCert?.postalCode ?? op.bioc?.postalCode ?? op.bioqs?.postalCode ?? op.lkv?.postalCode ?? null;
// op.city = op.easyCert?.city ?? op.bioc?.city ?? op.bioqs?.city ?? op.lkv?.city ?? null;
// op.countryCode = op.easyCert?.countryCode ?? op.bioc?.details.countryCode ?? 'AT';
// op.lfbisNr = op.bioqs?.lfbisNr ?? null;
// const certNrs = [...new Set([].concat(
// op.easyCert?.details.certificates.map(c => c.nr) ?? [],
// op.bioc?.details.certificates.map(c => c.nr) ?? [],
// op.bioqs?.certificates.map(c => c.nr) ?? [],
// op.lkv?.certificates.map(c => c.nr) ?? []
// ))];
// op.ooId = certNrs
// .map(nr => nr.match(/[A-Z]{2}-.{3}-[0-9]{3}\.[0-9]{3}-[0-9]{7}\.[0-9]{4}\.[0-9]{3}/))
// .filter(nr => nr !== null)
// .map(nr => nr[0].split('.')[1])[0] ?? null;
// }
//
// operators.sort((a, b) => a.name.localeCompare(b.name));
$first = true;
echo "{\"data\":[\n";
foreach ($operators as $op) {
if (!$first) echo ",\n";
echo " ";
echo jenc($op);
$first = false;
}
echo "\n]}\n";
+2 -4
View File
@@ -1,6 +1,6 @@
<?php
$url = 'https://lkv.at/at/zertifizierung/themen/BIO/zertifizierte-BIO-Betriebe.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');
@@ -25,8 +25,6 @@ if ($info !== '') {
exit;
}
header('Content-Type: application/json; charset=UTF-8');
$postalCode = '';
if (isset($_GET['postalCode']) && ctype_digit($_GET['postalCode'])) {
$postalCode = $_GET['postalCode'];
@@ -52,7 +50,7 @@ curl -s '$url' | grep -A3 '<tr' \
@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/.$//' \
| sed 's@\s*",@",@g;s@:"\s*@:"@g'
EOF);
echo "]}\n";