Spaces:
Running
Running
Upload static/shorts_fresh.js
Browse files- static/shorts_fresh.js +40 -36
static/shorts_fresh.js
CHANGED
|
@@ -1,15 +1,11 @@
|
|
| 1 |
/**
|
| 2 |
-
* VNEWS Fresh Shorts Fetcher
|
| 3 |
-
*
|
| 4 |
-
* + YouTube RSS as backup
|
| 5 |
*/
|
| 6 |
(function() {
|
| 7 |
'use strict';
|
| 8 |
|
| 9 |
-
const
|
| 10 |
-
{ handle: 'baodantri7941', id: 'UC_x5TKhOgd6GhYvv5z4I3jg', name: 'Dân trí', badge: 'DT' },
|
| 11 |
-
{ handle: 'baosuckhoedoisongboyte', id: 'UCBsY5fXTQLkF_JnH9kLkL4g', name: 'SKĐS', badge: 'SKĐS' },
|
| 12 |
-
];
|
| 13 |
|
| 14 |
function toast(msg) {
|
| 15 |
let t = document.getElementById('short-progress-toast');
|
|
@@ -35,12 +31,29 @@
|
|
| 35 |
return url;
|
| 36 |
}
|
| 37 |
|
| 38 |
-
//
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
try {
|
| 41 |
const r = await fetch('/api/shorts?t=' + Date.now());
|
| 42 |
if (r.ok) {
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
if (Array.isArray(data) && data.length > 0) {
|
| 45 |
return data.map(item => ({
|
| 46 |
id: item.id,
|
|
@@ -58,13 +71,14 @@
|
|
| 58 |
return [];
|
| 59 |
}
|
| 60 |
|
| 61 |
-
//
|
| 62 |
async function fetchFromRSS() {
|
| 63 |
try {
|
| 64 |
const r = await fetch('/api/shorts/rss?t=' + Date.now());
|
| 65 |
if (r.ok) {
|
| 66 |
const data = await r.json();
|
| 67 |
if (data.shorts && data.shorts.length > 0) {
|
|
|
|
| 68 |
return data.shorts;
|
| 69 |
}
|
| 70 |
}
|
|
@@ -101,8 +115,8 @@
|
|
| 101 |
const mixed = interleaveShorts(shorts);
|
| 102 |
if (!mixed.length) return;
|
| 103 |
|
| 104 |
-
let h = '<div class="slider-header"><span class="slider-label">📱 Shorts Dân trí & SKĐS</span><span class="slider-note">🟢 vừa cập nhật</span></div><div class="slider-track">';
|
| 105 |
-
mixed.slice(0,
|
| 106 |
const badge = a.channel === 'baosuckhoedoisongboyte' ? 'SKĐS' : 'Dân trí';
|
| 107 |
h += '<div class="slider-item shorts-item" onclick="openYTShortsFeed(' + i + ')"><div class="slider-thumb shorts-thumb">' + (a.img ? '<img src="' + esc(proxyImg(a.img)) + '" loading="lazy" onerror="this.src=\'https://i.ytimg.com/vi/' + a.id + '/hqdefault.jpg\'">' : '') + '<div class="card-play">▶</div></div><div class="slider-title"><span style="color:#f0c040;font-size:8px">' + badge + '</span> ' + esc(a.title) + '</div></div>';
|
| 108 |
});
|
|
@@ -113,13 +127,6 @@
|
|
| 113 |
existingSlide.id = 'shorts-live-slide';
|
| 114 |
existingSlide.style.boxShadow = '0 0 8px #5cb87a';
|
| 115 |
setTimeout(() => { existingSlide.style.boxShadow = 'none'; }, 2000);
|
| 116 |
-
} else {
|
| 117 |
-
const wrap = document.createElement('div');
|
| 118 |
-
wrap.id = 'shorts-live-slide';
|
| 119 |
-
wrap.className = 'slider-wrap';
|
| 120 |
-
wrap.innerHTML = h;
|
| 121 |
-
const homeEl = document.getElementById('view-home');
|
| 122 |
-
if (homeEl) homeEl.insertBefore(wrap, homeEl.children[1] || null);
|
| 123 |
}
|
| 124 |
}
|
| 125 |
|
|
@@ -127,16 +134,13 @@
|
|
| 127 |
try {
|
| 128 |
toast('🔄 Đang tải shorts mới...');
|
| 129 |
|
| 130 |
-
// Try
|
| 131 |
-
let all = await
|
| 132 |
|
| 133 |
-
//
|
| 134 |
-
if (all.length === 0
|
| 135 |
-
console.log('[Shorts]
|
| 136 |
-
|
| 137 |
-
if (rssShorts.length > 0) {
|
| 138 |
-
all = rssShorts;
|
| 139 |
-
}
|
| 140 |
}
|
| 141 |
|
| 142 |
// Deduplicate
|
|
@@ -147,10 +151,10 @@
|
|
| 147 |
window._shortsData = fresh;
|
| 148 |
rerenderShortsSlide(fresh);
|
| 149 |
localStorage.setItem('vnews_shorts', JSON.stringify({ ts: Date.now(), data: fresh }));
|
| 150 |
-
toast('✓ Đã cập nhật ' + fresh.length + ' shorts!');
|
| 151 |
return fresh;
|
| 152 |
} else {
|
| 153 |
-
toast('⚠️ Không
|
| 154 |
return window._shortsData || [];
|
| 155 |
}
|
| 156 |
} catch(e) {
|
|
@@ -164,15 +168,15 @@
|
|
| 164 |
try {
|
| 165 |
const c = JSON.parse(localStorage.getItem('vnews_shorts') || '{}');
|
| 166 |
if (c.data && c.data.length > 0 && (Date.now() - c.ts) < 7200000) {
|
| 167 |
-
return c.data;
|
| 168 |
}
|
| 169 |
} catch(e) {}
|
| 170 |
return null;
|
| 171 |
}
|
| 172 |
|
| 173 |
-
// Load cache first
|
| 174 |
const cached = loadCache();
|
| 175 |
-
if (cached) {
|
| 176 |
window._shortsData = cached;
|
| 177 |
setTimeout(() => rerenderShortsSlide(cached), 2000);
|
| 178 |
}
|
|
@@ -180,5 +184,5 @@
|
|
| 180 |
// Fetch fresh after delay
|
| 181 |
setTimeout(() => { window.refreshShorts().catch(() => {}); }, 3000);
|
| 182 |
|
| 183 |
-
console.log('[Shorts]
|
| 184 |
-
})();
|
|
|
|
| 1 |
/**
|
| 2 |
+
* VNEWS Fresh Shorts Fetcher v8 - 24h filter
|
| 3 |
+
* Only shows posts from last 24h
|
|
|
|
| 4 |
*/
|
| 5 |
(function() {
|
| 6 |
'use strict';
|
| 7 |
|
| 8 |
+
const MAX_AGE = 86400000; // 24h in ms
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
function toast(msg) {
|
| 11 |
let t = document.getElementById('short-progress-toast');
|
|
|
|
| 31 |
return url;
|
| 32 |
}
|
| 33 |
|
| 34 |
+
// Filter items to only 24h
|
| 35 |
+
function filter24h(items) {
|
| 36 |
+
const now = Date.now();
|
| 37 |
+
return items.filter(item => {
|
| 38 |
+
// Check if item has created timestamp
|
| 39 |
+
if (item.created) {
|
| 40 |
+
return (now - item.created) <= MAX_AGE;
|
| 41 |
+
}
|
| 42 |
+
// Default: keep (server-side will filter)
|
| 43 |
+
return true;
|
| 44 |
+
});
|
| 45 |
+
}
|
| 46 |
+
|
| 47 |
+
// Fetch via server-side API
|
| 48 |
+
async function fetchShorts() {
|
| 49 |
try {
|
| 50 |
const r = await fetch('/api/shorts?t=' + Date.now());
|
| 51 |
if (r.ok) {
|
| 52 |
+
let data = await r.json();
|
| 53 |
+
// Filter 24h if items have timestamps
|
| 54 |
+
if (Array.isArray(data)) {
|
| 55 |
+
data = filter24h(data);
|
| 56 |
+
}
|
| 57 |
if (Array.isArray(data) && data.length > 0) {
|
| 58 |
return data.map(item => ({
|
| 59 |
id: item.id,
|
|
|
|
| 71 |
return [];
|
| 72 |
}
|
| 73 |
|
| 74 |
+
// Fetch from YouTube RSS
|
| 75 |
async function fetchFromRSS() {
|
| 76 |
try {
|
| 77 |
const r = await fetch('/api/shorts/rss?t=' + Date.now());
|
| 78 |
if (r.ok) {
|
| 79 |
const data = await r.json();
|
| 80 |
if (data.shorts && data.shorts.length > 0) {
|
| 81 |
+
// RSS already filtered by server
|
| 82 |
return data.shorts;
|
| 83 |
}
|
| 84 |
}
|
|
|
|
| 115 |
const mixed = interleaveShorts(shorts);
|
| 116 |
if (!mixed.length) return;
|
| 117 |
|
| 118 |
+
let h = '<div class="slider-header"><span class="slider-label">📱 Shorts Dân trí & SKĐS (24h)</span><span class="slider-note">🟢 vừa cập nhật</span></div><div class="slider-track">';
|
| 119 |
+
mixed.slice(0, 25).forEach((a, i) => {
|
| 120 |
const badge = a.channel === 'baosuckhoedoisongboyte' ? 'SKĐS' : 'Dân trí';
|
| 121 |
h += '<div class="slider-item shorts-item" onclick="openYTShortsFeed(' + i + ')"><div class="slider-thumb shorts-thumb">' + (a.img ? '<img src="' + esc(proxyImg(a.img)) + '" loading="lazy" onerror="this.src=\'https://i.ytimg.com/vi/' + a.id + '/hqdefault.jpg\'">' : '') + '<div class="card-play">▶</div></div><div class="slider-title"><span style="color:#f0c040;font-size:8px">' + badge + '</span> ' + esc(a.title) + '</div></div>';
|
| 122 |
});
|
|
|
|
| 127 |
existingSlide.id = 'shorts-live-slide';
|
| 128 |
existingSlide.style.boxShadow = '0 0 8px #5cb87a';
|
| 129 |
setTimeout(() => { existingSlide.style.boxShadow = 'none'; }, 2000);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 130 |
}
|
| 131 |
}
|
| 132 |
|
|
|
|
| 134 |
try {
|
| 135 |
toast('🔄 Đang tải shorts mới...');
|
| 136 |
|
| 137 |
+
// Try RSS first (freshest)
|
| 138 |
+
let all = await fetchFromRSS();
|
| 139 |
|
| 140 |
+
// Fallback to server API
|
| 141 |
+
if (all.length === 0) {
|
| 142 |
+
console.log('[Shorts] RSS empty, trying server API...');
|
| 143 |
+
all = await fetchShorts();
|
|
|
|
|
|
|
|
|
|
| 144 |
}
|
| 145 |
|
| 146 |
// Deduplicate
|
|
|
|
| 151 |
window._shortsData = fresh;
|
| 152 |
rerenderShortsSlide(fresh);
|
| 153 |
localStorage.setItem('vnews_shorts', JSON.stringify({ ts: Date.now(), data: fresh }));
|
| 154 |
+
toast('✓ Đã cập nhật ' + fresh.length + ' shorts (24h)!');
|
| 155 |
return fresh;
|
| 156 |
} else {
|
| 157 |
+
toast('⚠️ Không có shorts mới trong 24h');
|
| 158 |
return window._shortsData || [];
|
| 159 |
}
|
| 160 |
} catch(e) {
|
|
|
|
| 168 |
try {
|
| 169 |
const c = JSON.parse(localStorage.getItem('vnews_shorts') || '{}');
|
| 170 |
if (c.data && c.data.length > 0 && (Date.now() - c.ts) < 7200000) {
|
| 171 |
+
return filter24h(c.data);
|
| 172 |
}
|
| 173 |
} catch(e) {}
|
| 174 |
return null;
|
| 175 |
}
|
| 176 |
|
| 177 |
+
// Load cache first
|
| 178 |
const cached = loadCache();
|
| 179 |
+
if (cached && cached.length > 0) {
|
| 180 |
window._shortsData = cached;
|
| 181 |
setTimeout(() => rerenderShortsSlide(cached), 2000);
|
| 182 |
}
|
|
|
|
| 184 |
// Fetch fresh after delay
|
| 185 |
setTimeout(() => { window.refreshShorts().catch(() => {}); }, 3000);
|
| 186 |
|
| 187 |
+
console.log('[Shorts] Fetcher v8 - 24h filter enabled');
|
| 188 |
+
})();
|