Skip to content

Commit

Permalink
new: Better error handling in Downloader
Browse files Browse the repository at this point in the history
new: More download options
new: Hide unavailable resolutions in Downloader
  • Loading branch information
lscambo13 committed Feb 11, 2024
1 parent 4129ba7 commit a491b4d
Showing 1 changed file with 72 additions and 43 deletions.
115 changes: 72 additions & 43 deletions js_modules/dl.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,71 @@ const wrap = document.getElementById('wrap');
const container = document.getElementById('downloadContainer');
const loading = document.getElementById('progress-bar');

const createDownloadCard = (title, url, thumb) => {
// const tempRes = res;
// let quality = tempRes.match(/\((.*)\)/)?.pop();
// if (!quality) quality = 'View';
const addEntry = (u, res) => {
return `
<div>
<a href="${u}" class="premiumDownloadButton">
<span class="videoResolution">${res}</span>
</a>
</div>
`;
};

const addProResolutionEntries = (url, res) => {
const set = new Set();
res[0].forEach((e) => {
if (e < 720) set.add('SD');
else if (e < 1080) set.add('HD');
else if (e < 1440) set.add('FHD');
else if (e < 2160) set.add('QHD');
else if (e < 4320) set.add('UHD');
else if (e >= 4320) set.add('UHD+');
});

let elements = `
<div>
<a href="${url}" class="premiumDownloadButton">
<span class="videoResolution">MP3</span>
</a>
</div>
`;

set.forEach((e) => {
elements = addEntry(url, e) + elements;
});

return elements;
};

const addFreeResolutionEntries = (streams) => {
let elements = '';
Object.keys(streams).forEach((k) => {
let title;
if (!streams[k].stream.length) return;
if (k === 'bestVideoOnly') {
title = `${streams[k].info}P MUTED`;
} else if (streams[k].info === 'NA') {
title = 'AUDIO';
} else {
title = `${streams[k].info}P`;
}
if (title) {
elements = elements + addEntry(streams[k].stream, title);
}
});
return elements;
};


const createDownloadCard = (title, streams, thumb, resolutions) => {
const free = addFreeResolutionEntries(streams);
const pro = addProResolutionEntries('#', resolutions);
container.insertAdjacentHTML('beforeend', `
<div class="downloadItemContainer">
<video
controls disablepictureinpicture
controlslist="noplaybackrate"
src="${url}" alt="Thumbnail"
src="${streams['bestVideoWithAudio'].stream}" alt="Thumbnail"
poster="${thumb}"
class="downloadItemThumbnail">
</video>
Expand All @@ -31,47 +86,14 @@ const createDownloadCard = (title, url, thumb) => {
<span class="proDownloadContainerLabel
premiumDownloadButton">FREE</span>
<div class="proDownloadOptions">
<div>
<a href="${url}" class="premiumDownloadButton">
<span class="videoResolution">AUTO</span>
</a>
</div>
${free}
</div>
</div>
<div class="proDownloadContainer" >
<div class="proDownloadContainer disabled" >
<span class="proDownloadContainerLabel
premiumDownloadButton">PRO</span>
<div class="proDownloadOptions">
<div>
<a href="${url}" class="premiumDownloadButton disabled">
<span class="videoResolution">MP3</span>
</a>
</div>
<div>
<a href="${url}" class="premiumDownloadButton disabled">
<span class="videoResolution">SD</span>
</a>
</div>
<div>
<a href="${url}" class="premiumDownloadButton disabled">
<span class="videoResolution">HD</span>
</a>
</div>
<div>
<a href="${url}" class="premiumDownloadButton disabled">
<span class="videoResolution">FHD</span>
</a>
</div>
<div>
<a href="${url}" class="premiumDownloadButton disabled">
<span class="videoResolution">FHD+</span>
</a>
</div>
<div>
<a href="${url}" class="premiumDownloadButton disabled">
<span class="videoResolution">UHD</span>
</a>
</div>
${pro}
</div>
</div>
</div>
Expand Down Expand Up @@ -143,9 +165,16 @@ export const parseDl = (event, url = null) => {
result.json().then((res) => {
console.log(res);
res.forEach((i) => {
createDownloadCard(i.title, i.stream, i.thumbnail);
console.log(i.source, i.resolutions);
loading.classList.add('hidden');
if (!i.resolutions.length) {
genericAlert(
`Something went wrong`,
i.err);
closeContainer();
return;
}
createDownloadCard(i.title, i.streams, i.thumbnail, i.resolutions);
console.log(i.source, i.err);
});
});
}).catch((e) => {
Expand Down

0 comments on commit a491b4d

Please sign in to comment.