refactor: split static/ into css/ js/ html/; add tests/test_json.c (15/15 pass)
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
/* ── Downloads page ─────────────────────────────────────────── */
|
||||
function escH(s) { return String(s).replace(/&/g,'&').replace(/</g,'<').replace(/>/g,'>'); }
|
||||
function fmtSpeed(b) { return b>1048576?(b/1048576).toFixed(1)+' MB/s':b>1024?(b/1024).toFixed(0)+' KB/s':b+' B/s'; }
|
||||
function fmtEta(s) {
|
||||
if (s < 0) return '';
|
||||
if (s < 60) return s + 's';
|
||||
if (s < 3600) return Math.floor(s/60) + 'm ' + (s%60) + 's';
|
||||
return Math.floor(s/3600) + 'h ' + Math.floor((s%3600)/60) + 'm';
|
||||
}
|
||||
|
||||
function renderDownloads() {
|
||||
fetch(BASE + '/api/downloads', {cache: 'no-store'})
|
||||
.then(function(r) { return r.json(); })
|
||||
.then(function(dl) {
|
||||
var el = document.getElementById('dl-table');
|
||||
if (!dl.length) { el.innerHTML = '<p style="color:var(--dim)">No downloads yet.</p>'; return; }
|
||||
var html = '<table><tr><th></th><th>Name</th><th>Status</th><th>Progress</th><th>Speed</th><th>ETA</th><th></th></tr>';
|
||||
dl.slice().reverse().forEach(function(d) {
|
||||
var pct = d.pct || 0, mb = (d.downloaded/1048576).toFixed(1);
|
||||
var cls = d.status==='done' ? 'done' : d.status.startsWith('error') ? 'err' : d.status==='downloading' ? 'dl' : '';
|
||||
var thumb = d.cover_url
|
||||
? '<img src="' + escH(d.cover_url) + '" style="width:32px;height:48px;object-fit:cover;border-radius:3px" onerror="this.style.display=\'none\'">'
|
||||
: '<span style="color:var(--dim);font-size:18px">◻</span>';
|
||||
html += '<tr><td>' + thumb + '</td><td>' + escH(d.name) + '</td><td class=' + cls + '>' + escH(d.status) + '</td><td>';
|
||||
if (d.status === 'downloading')
|
||||
html += '<div class=pbar><div class=pfill style="width:' + pct + '%"></div></div> ' + pct + '% (' + mb + 'MB)';
|
||||
else html += pct + '%';
|
||||
html += '</td><td>' + (d.status==='downloading'&&d.speed_bps>0 ? fmtSpeed(d.speed_bps) : '—') + '</td>';
|
||||
html += '<td>' + (d.status==='downloading'&&d.eta_s>=0 ? fmtEta(d.eta_s) : '—') + '</td><td>';
|
||||
if (d.status==='downloading' || d.status==='queued')
|
||||
html += '<button class="btn btn-s" onclick="cancelDl(\'' + d.id + '\')" style="padding:2px 8px">✕</button>';
|
||||
html += '</td></tr>';
|
||||
});
|
||||
html += '</table>';
|
||||
el.innerHTML = html;
|
||||
}).catch(function() {});
|
||||
}
|
||||
|
||||
function cancelDl(id) {
|
||||
fetch(BASE + '/api/cancel', {method:'POST', headers:{'Content-Type':'application/json'}, body:JSON.stringify({id:id})})
|
||||
.then(function() { setTimeout(renderDownloads, 400); });
|
||||
}
|
||||
function clearDl() { fetch(BASE + '/api/clear', {method:'POST'}).then(function() { setTimeout(renderDownloads, 400); }); }
|
||||
function clearCancelled() { fetch(BASE + '/api/clear-cancelled', {method:'POST'}).then(function() { setTimeout(renderDownloads, 400); }); }
|
||||
function retryInterrupted() { fetch(BASE + '/api/retry-interrupted', {method:'POST'}).then(function() { setTimeout(renderDownloads, 400); }); }
|
||||
function cleanPartials() { fetch(BASE + '/api/clean-partials', {method:'POST'}).then(function() { renderDownloads(); }); }
|
||||
|
||||
renderDownloads();
|
||||
setInterval(renderDownloads, 3000);
|
||||
@@ -0,0 +1,20 @@
|
||||
/* ── Shared IPTV downloader JS ──────────────────────────────── */
|
||||
var BASE = (function(){
|
||||
var m = window.location.pathname.match(/^(\/[^/]+)/);
|
||||
return m ? m[1] : '';
|
||||
})();
|
||||
|
||||
function filter(el) {
|
||||
document.querySelectorAll('.card').forEach(function(c) {
|
||||
c.style.display = c.textContent.toLowerCase().includes(el.value.toLowerCase()) ? '' : 'none';
|
||||
});
|
||||
}
|
||||
|
||||
function dlMov(id, title, ext, icon) {
|
||||
if (!confirm('Download: ' + title + '?')) return;
|
||||
fetch(BASE + '/api/download_movie', {
|
||||
method: 'POST',
|
||||
headers: {'Content-Type': 'application/json'},
|
||||
body: JSON.stringify({id: id, title: title, ext: ext || 'mp4', cover_url: icon || ''})
|
||||
}).then(function() { alert('Queued!'); location.href = BASE + '/downloads'; });
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
/* ── Series episode selector ──────────────────────────────────── */
|
||||
/* TITLE, COVER, SEASONS injected by server as window vars before this script */
|
||||
|
||||
var tabs = document.getElementById('tabs');
|
||||
var sdiv = document.getElementById('seasons');
|
||||
|
||||
Object.keys(SEASONS).sort(function(a,b){return +a-+b;}).forEach(function(s) {
|
||||
var btn = document.createElement('button');
|
||||
btn.className = 'btn b'; btn.textContent = 'S' + s.padStart(2,'0');
|
||||
btn.style.marginRight = '6px';
|
||||
btn.onclick = function() { showSeason(s); };
|
||||
tabs.appendChild(btn);
|
||||
|
||||
var div = document.createElement('div'); div.id = 's-' + s; div.style.display = 'none';
|
||||
var html = '<div style="margin:8px 0">'
|
||||
+ '<button class="btn btn-s" onclick="selAll(\'' + s + '\',true)">All S' + s.padStart(2,'0') + '</button> '
|
||||
+ '<button class="btn g" onclick="dlSeason(\'' + s + '\')" style="margin-left:6px">⬇ Season ' + s + '</button></div>';
|
||||
html += '<table><tr><th><input type=checkbox onchange="selAll(\'' + s + '\',this.checked)"></th>'
|
||||
+ '<th>Ep</th><th>Title</th><th>Duration</th></tr>';
|
||||
SEASONS[s].forEach(function(e) {
|
||||
var epn = e.episode_num || e.episodeNum || '?';
|
||||
var dur = (e.info && e.info.duration) || '';
|
||||
var ext = e.container_extension || 'mp4';
|
||||
html += '<tr>'
|
||||
+ '<td><input type=checkbox class="ep-cb s-cb-' + s + '" data-id="' + e.id + '" data-ep="' + epn + '" data-s="' + s + '" data-ext="' + ext + '"></td>'
|
||||
+ '<td>E' + String(epn).padStart(2,'0') + '</td>'
|
||||
+ '<td>' + (e.title||'') + '</td>'
|
||||
+ '<td>' + dur + '</td></tr>';
|
||||
});
|
||||
html += '</table>';
|
||||
div.innerHTML = html;
|
||||
sdiv.appendChild(div);
|
||||
});
|
||||
|
||||
var firstS = Object.keys(SEASONS).sort(function(a,b){return +a-+b;})[0];
|
||||
if (firstS) showSeason(firstS);
|
||||
|
||||
function showSeason(s) {
|
||||
document.querySelectorAll('[id^=s-]').forEach(function(d) { d.style.display = 'none'; });
|
||||
var d = document.getElementById('s-' + s); if (d) d.style.display = '';
|
||||
}
|
||||
|
||||
function selAll(s, v) {
|
||||
var all = Array.from(document.querySelectorAll('.s-cb-' + s));
|
||||
var checked = v === undefined ? !all.every(function(x){return x.checked;}) : v;
|
||||
all.forEach(function(c) { c.checked = checked; });
|
||||
}
|
||||
|
||||
function dlSeason(s) { selAll(s, true); dlSelected(); }
|
||||
|
||||
function dlSelected() {
|
||||
var eps = Array.from(document.querySelectorAll('.ep-cb:checked')).map(function(c) {
|
||||
return {id: c.dataset.id, ep: c.dataset.ep, s: c.dataset.s, ext: c.dataset.ext};
|
||||
});
|
||||
if (!eps.length) { alert('Select episodes first'); return; }
|
||||
var bySeason = {};
|
||||
eps.forEach(function(e) { (bySeason[e.s] = bySeason[e.s]||[]).push({id:e.id, episode_num:e.ep, ext:e.ext}); });
|
||||
Promise.all(Object.keys(bySeason).map(function(s) {
|
||||
return fetch(BASE + '/api/download', {
|
||||
method: 'POST',
|
||||
headers: {'Content-Type': 'application/json'},
|
||||
body: JSON.stringify({episodes: bySeason[s], series_title: TITLE, season: s, cover_url: COVER})
|
||||
}).then(function(r) { return r.json(); });
|
||||
})).then(function() { alert('Queued!'); location.href = BASE + '/downloads'; });
|
||||
}
|
||||
Reference in New Issue
Block a user