web / app /Models /Video.php
souliya_pps
Devin
feat: update UI and add video 404 page
282827b
Raw History Blame Contribute Delete
5.04 kB
<?php
namespace App\Models;
require_once __DIR__ . '/../Helpers/cache.php';
class Video {
public static function getAll($refresh = false) {
$cacheFile = 'video_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['VIDEO_API_URL'] ?? getenv('VIDEO_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 [];
}
public static function parseLink($link) {
$link = trim((string) $link);
$out = ['provider' => 'other', 'id' => '', 'key' => '', 'thumbnail' => '', 'isReel' => false];
if ($link === '') {
return $out;
}
if (preg_match('~(?:youtu\.be/|youtube\.com/(?:.*[?&]v=|.*/embed/|.*/shorts/|.*/live/|.*/))([A-Za-z0-9_-]{6,})~i', $link, $m)) {
$out['provider'] = 'youtube';
$out['id'] = $m[1];
$out['key'] = $m[1];
$out['thumbnail'] = 'https://img.youtube.com/vi/' . $m[1] . '/hqdefault.jpg';
return $out;
}
if (preg_match('~(?:facebook\.com|fb\.watch|fb\.me)~i', $link)) {
$id = '';
if (preg_match('~/reel/([A-Za-z0-9_-]+)~i', $link, $m)) {
$id = $m[1];
$out['isReel'] = true;
} elseif (preg_match('~/(?:videos|video\.php)/?(?:[^0-9]*)([0-9]{5,})~i', $link, $m)) {
$id = $m[1];
} elseif (preg_match('~[?&]v=([0-9]{5,})~i', $link, $m)) {
$id = $m[1];
} elseif (preg_match('~/share/(?:v|r)/([A-Za-z0-9_-]+)~i', $link, $m)) {
$id = $m[1];
}
$out['provider'] = 'facebook';
$out['id'] = $id;
$out['key'] = 'fb-' . ($id !== '' ? $id : substr(md5($link), 0, 12));
// Facebook doesn't provide public thumbnail API, use branded placeholder
$out['thumbnail'] = ''; // Will fall back to logo in view
return $out;
}
if (preg_match('~drive\.google\.com/(?:.*/d/|file/d/)([A-Za-z0-9_-]+)~i', $link, $m)) {
$out['provider'] = 'drive';
$out['id'] = $m[1];
$out['key'] = 'gd-' . $m[1];
$out['thumbnail'] = 'https://lh3.googleusercontent.com/d/' . $m[1] . '=s320?authuser=0';
return $out;
}
if (preg_match('~vimeo\.com/(?:video/)?([0-9]{5,})~i', $link, $m)) {
$out['provider'] = 'vimeo';
$out['id'] = $m[1];
$out['key'] = 'vm-' . $m[1];
$out['thumbnail'] = 'https://vumbnail.com/' . $m[1] . '.jpg';
return $out;
}
if (preg_match('~\.(?:mp4|webm|ogg|m3u8)(?:[?#].*)?$~i', $link)) {
$out['provider'] = 'file';
$out['id'] = substr(md5($link), 0, 12);
$out['key'] = 'file-' . $out['id'];
return $out;
}
return $out;
}
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))) {
$link = $rowObject['link'] ?? '';
$parsed = self::parseLink($link);
$rowObject['_provider'] = $parsed['provider'];
$rowObject['_videoId'] = $parsed['id'];
$rowObject['_isReel'] = $parsed['isReel'];
$rowObject['_ytId'] = $parsed['provider'] === 'youtube' ? $parsed['id'] : '';
$rowObject['_thumbnail'] = $parsed['thumbnail'];
$rowObject['_key'] = $parsed['key'] !== '' ? $parsed['key'] : (string) ($rowObject['ID'] ?? '');
$transformed[] = $rowObject;
}
}
return array_reverse($transformed);
}
}