Spaces:
Running
Running
Download app/Models/Book.php from buddhaword/web: direct link, hf CLI and curl.
- Browser
- Download file 1.99 kB
-
https://huggingface.co/spaces/buddhaword/web/resolve/main/app/Models/Book.php
- Command line
-
hf download hf://spaces/buddhaword/web/app/Models/Book.php
-
curl -L -o Book.php https://huggingface.co/spaces/buddhaword/web/resolve/main/app/Models/Book.php
1.99 kB
| namespace App\Models; | |
| require_once __DIR__ . '/../Helpers/cache.php'; | |
| class Book { | |
| public static function getAll($refresh = false) { | |
| $cacheFile = 'book_api.json'; | |
| $cacheTime = 86400; // 24 hours | |
| if (!$refresh) { | |
| $json = readCache($cacheFile, $cacheTime); | |
| if ($json !== null) { | |
| $data = json_decode($json, true); | |
| if (isset($data['values'])) { | |
| return self::transformData($data); | |
| } | |
| } | |
| } | |
| $url = $_ENV['BOOK_API_URL'] ?? getenv('BOOK_API_URL') ?: ''; | |
| if (!$url) { | |
| $json = readCache($cacheFile, $cacheTime * 365); | |
| if ($json !== null) { | |
| $data = json_decode($json, true); | |
| if (isset($data['values'])) { | |
| return self::transformData($data); | |
| } | |
| } | |
| return []; | |
| } | |
| $json = httpGet($url); | |
| if ($json !== null) { | |
| writeCache($cacheFile, $json); | |
| $data = json_decode($json, true); | |
| if (isset($data['values'])) { | |
| return self::transformData($data); | |
| } | |
| } | |
| $json = readCache($cacheFile, $cacheTime * 365); | |
| if ($json !== null) { | |
| $data = json_decode($json, true); | |
| if (isset($data['values'])) { | |
| return self::transformData($data); | |
| } | |
| } | |
| return []; | |
| } | |
| private static function transformData($data) { | |
| $headers = array_shift($data['values']); | |
| $rows = $data['values']; | |
| $transformed = []; | |
| foreach ($rows as $row) { | |
| $rowObject = []; | |
| foreach ($headers as $index => $header) { | |
| $rowObject[$header] = $row[$index] ?? ""; | |
| } | |
| if (!empty(array_filter($rowObject))) { | |
| $transformed[] = $rowObject; | |
| } | |
| } | |
| return $transformed; | |
| } | |
| } | |