2ec45b9138
Restructures the install so users run ONE command. The fork's newinstaller.sh clones this repo (instead of upstream Nachtzuster) and runs BirdNET-Pi's installer. install_services.sh symlinks the avian/ overlay into the Caddy site root, so http://birdnet.local/avian/ comes up after reboot with no Caddy config needed. Reviewer-flagged fixes: Frontend (avian/frontend/apt.js) - Switch all API paths from /api/*.json (which didn't route) to relative action-based URLs (./api/birdnet-api.php?action=recent etc.). - Fetch ./masks.json + ./dims.json relatively (were /avian/*.json). - Replace every innerHTML user-content sink with createElement + textContent + setAttribute — species labels are user-editable in BirdNET-Pi, so untrusted. - Add esc() helper for the few remaining innerHTML attribute contexts. - fetchJson throws on non-200 (was silently parsing 404 HTML as JSON). - Defensive null-check in loadMask if MASKS hasn't loaded. Backend (avian/api/*.php) - birdnet-api.php now derives DB_PATH from dirname(__DIR__, 3) so any BirdNET-Pi install user works (no hardcoded /home/birdnet). - recording/spectrogram/cutout use getenv('HOME') for the same reason. - cutout.php reworked as the canonical image resolver: bundled illustration → bundled cutout → cached rembg → fresh Wikipedia+rembg, with the binomial regex guard and atomic rename on cache write. Wikipedia URL host pinned to wikimedia.org / wikipedia.org to block SSRF. - Stale 'X-BirdNET-Proxy-Token' auth claims removed everywhere. Pregen (avian/scripts/pregen.py) - Gemini key moved from URL query to x-goog-api-key header. - Model bumped to gemini-2.5-flash-image-preview. - Bounded retry on 429 + 5xx with Retry-After honoured. - Default --sleep raised from 1s to 4s (under free-tier RPM). - Unified parser handles |/_/comma in all three input modes. - responseModalities now [TEXT, IMAGE] (was IMAGE-only). - --poses validated against POSES keys. - ASCII [ok]/[fail] markers (was ✓/✗ — Windows console crash). - Surfaces finishReason + blockReason on safety blocks. Prompt template - 'Two placeholders' typo → three. - 'warm paper' / 'transparent background' contradiction resolved. Forwarding - mqtt-bridge.py: paho-mqtt 2.x CallbackAPIVersion compat shim. - avian-mqtt.service: dropped broken %i, set User=birdnet + %h path. - forwarding/README.md uses the actual action-based URLs. README - One-line install via curl … newinstaller.sh. - No more 'install BirdNET-Pi separately' step (our fork does it). - License section uses absolute github.com links. - bird.onethreenine.net replaces the not-yet-existent project writeup. - docs/thumb.png replaced with the 24-bird Twitter capture. Removed - avian/caddy/ and avian/scripts/install.sh — no longer needed; the overlay symlink in install_services.sh handles everything.
156 lines
5.4 KiB
PHP
156 lines
5.4 KiB
PHP
<?php
|
|
// AvianVisitors — serves the spectrogram PNG that BirdNET-Pi generates
|
|
// alongside each detection mp3. Same lookup logic as recording.php (find
|
|
// the matching file under By_Date/<date>/<Common_Name>/) — just .png
|
|
// instead of .mp3.
|
|
//
|
|
// Endpoints:
|
|
// ?sci=<sci_name> → newest spectrogram for that species
|
|
// ?file=<original-base>.mp3 → spectrogram next to that specific
|
|
// recording (atlas modal uses this so the
|
|
// strip below each play button is the
|
|
// spectrogram for that recording, not
|
|
// "the most recent" — they can differ).
|
|
|
|
declare(strict_types=1);
|
|
|
|
$sci = trim((string)($_GET['sci'] ?? ''));
|
|
$file = trim((string)($_GET['file'] ?? ''));
|
|
|
|
if ($sci === '' && $file === '') {
|
|
http_response_code(400);
|
|
echo 'sci or file required';
|
|
exit;
|
|
}
|
|
|
|
// Reject any sci-name that isn't a clean Genus species[ subspecies[ tri]]
|
|
// pattern. Same defence-in-depth check as recording.php.
|
|
if ($sci !== '' && !preg_match('/^[A-Za-z]{2,40}(?:[ ][a-z]{2,40}){1,3}$/', $sci)) {
|
|
http_response_code(400);
|
|
echo 'invalid sci';
|
|
exit;
|
|
}
|
|
|
|
$BY_DATE = getenv('HOME') . '/BirdSongs/Extracted/By_Date';
|
|
|
|
// ---- Direct-by-file lookup ----
|
|
// BirdNET-Pi writes <base>.mp3 and <base>.png next to each other under
|
|
// By_Date/<date>/<Common_Name>/. So we accept the mp3 filename, swap
|
|
// the extension, and search the same way recording.php does.
|
|
if ($file !== '') {
|
|
if (!preg_match('/^[A-Za-z0-9_.:-]+\.(mp3|png)$/', $file)) {
|
|
http_response_code(400);
|
|
echo 'invalid file name';
|
|
exit;
|
|
}
|
|
// BirdNET-Pi names the spectrogram as the FULL mp3 filename plus
|
|
// ".png" — e.g. "American_Crow-82-…-20:25:29.mp3" pairs with
|
|
// "American_Crow-82-…-20:25:29.mp3.png" (not "…-20:25:29.png").
|
|
// Accept either form gracefully.
|
|
if (substr($file, -4) === '.png') {
|
|
$png = $file;
|
|
} else {
|
|
$png = $file . '.png';
|
|
}
|
|
$date = null;
|
|
if (preg_match('/(\d{4}-\d{2}-\d{2})/', $png, $m)) $date = $m[1];
|
|
$candidates = [];
|
|
if ($date) {
|
|
$dayDir = "$BY_DATE/$date";
|
|
if (is_dir($dayDir)) {
|
|
foreach (scandir($dayDir) as $sub) {
|
|
if ($sub[0] === '.') continue;
|
|
$p = "$dayDir/$sub/$png";
|
|
if (is_file($p)) { $candidates[] = $p; break; }
|
|
}
|
|
}
|
|
}
|
|
if (!$candidates) {
|
|
if (is_dir($BY_DATE)) {
|
|
foreach (scandir($BY_DATE) as $d) {
|
|
if ($d[0] === '.') continue;
|
|
$dayDir = "$BY_DATE/$d";
|
|
if (!is_dir($dayDir)) continue;
|
|
foreach (scandir($dayDir) as $sub) {
|
|
if ($sub[0] === '.') continue;
|
|
$p = "$dayDir/$sub/$png";
|
|
if (is_file($p)) { $candidates[] = $p; break 2; }
|
|
}
|
|
}
|
|
}
|
|
}
|
|
if (!$candidates || filesize($candidates[0]) < 64) {
|
|
http_response_code(404);
|
|
echo 'spectrogram not found';
|
|
exit;
|
|
}
|
|
$path = $candidates[0];
|
|
header('Content-Type: image/png');
|
|
header('Content-Length: ' . filesize($path));
|
|
header('Cache-Control: public, max-age=86400');
|
|
readfile($path);
|
|
exit;
|
|
}
|
|
|
|
function resolve_common(string $sci): ?string {
|
|
$f = getenv('HOME') . '/BirdNET-Pi/scripts/birds.json';
|
|
if (is_readable($f)) {
|
|
$list = json_decode((string)file_get_contents($f), true);
|
|
if (is_array($list)) {
|
|
foreach ($list as $row) {
|
|
if (!is_array($row)) continue;
|
|
$rowSci = $row['sci'] ?? $row['scientific'] ?? $row['scientificName'] ?? '';
|
|
$rowCom = $row['com'] ?? $row['common'] ?? $row['commonName'] ?? '';
|
|
if (strcasecmp(trim((string)$rowSci), $sci) === 0 && $rowCom) {
|
|
return str_replace(' ', '_', (string)$rowCom);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
$labels = getenv('HOME') . '/BirdNET-Pi/model/labels.txt';
|
|
if (is_readable($labels)) {
|
|
foreach (file($labels, FILE_IGNORE_NEW_LINES) as $line) {
|
|
if (strpos($line, '_') !== false) {
|
|
[$s, $c] = explode('_', $line, 2);
|
|
if (strcasecmp(trim($s), $sci) === 0) {
|
|
return str_replace(' ', '_', trim($c));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
$common = resolve_common($sci) ?? str_replace(' ', '_', $sci);
|
|
|
|
function newest_spectrogram(string $rootDir, string $common): ?string {
|
|
if (!is_dir($rootDir)) return null;
|
|
$dates = scandir($rootDir, SCANDIR_SORT_DESCENDING);
|
|
if (!$dates) return null;
|
|
foreach ($dates as $date) {
|
|
if ($date[0] === '.') continue;
|
|
$speciesDir = "$rootDir/$date/$common";
|
|
if (!is_dir($speciesDir)) continue;
|
|
$files = scandir($speciesDir, SCANDIR_SORT_DESCENDING);
|
|
if (!$files) continue;
|
|
foreach ($files as $f) {
|
|
if (substr($f, -4) === '.png') {
|
|
return "$speciesDir/$f";
|
|
}
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
$path = newest_spectrogram($BY_DATE, $common);
|
|
if ($path === null || !is_file($path) || filesize($path) < 64) {
|
|
http_response_code(404);
|
|
echo 'no spectrogram for ' . htmlspecialchars($sci);
|
|
exit;
|
|
}
|
|
|
|
header('Content-Type: image/png');
|
|
header('Content-Length: ' . filesize($path));
|
|
header('Cache-Control: public, max-age=60');
|
|
readfile($path);
|