File size: 5,041 Bytes
7731906
 
 
03bdc4a
 
7731906
 
03bdc4a
7731906
 
03bdc4a
 
 
 
 
 
7731906
03bdc4a
 
 
 
 
 
 
 
 
 
7731906
 
03bdc4a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7731906
 
03bdc4a
 
7731906
282827b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
03bdc4a
7731906
 
 
 
 
 
 
 
 
 
3beb45f
282827b
 
 
 
 
 
 
7731906
 
 
 
6d8cf3e
7731906
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<?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);
    }
}