Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Video-Downloader/api/src/misc/utils.js
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
67 lines (61 sloc)
1.72 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const forbiddenCharsString = ['}', '{', '%', '>', '<', '^', ';', '`', '$', '"', "@", '=']; | |
export function metadataManager(obj) { | |
const keys = Object.keys(obj); | |
const tags = [ | |
"album", | |
"copyright", | |
"title", | |
"artist", | |
"track", | |
"date" | |
] | |
let commands = [] | |
for (const i in keys) { | |
if (tags.includes(keys[i])) | |
commands.push('-metadata', `${keys[i]}=${obj[keys[i]]}`) | |
} | |
return commands; | |
} | |
export function cleanString(string) { | |
for (const i in forbiddenCharsString) { | |
string = string.replaceAll("/", "_") | |
.replaceAll(forbiddenCharsString[i], '') | |
} | |
return string; | |
} | |
export function verifyLanguageCode(code) { | |
const langCode = String(code.slice(0, 2).toLowerCase()); | |
if (RegExp(/[a-z]{2}/).test(code)) { | |
return langCode | |
} | |
return "en" | |
} | |
export function languageCode(req) { | |
if (req.header('Accept-Language')) { | |
return verifyLanguageCode(req.header('Accept-Language')) | |
} | |
return "en" | |
} | |
export function cleanHTML(html) { | |
let clean = html.replace(/ {4}/g, ''); | |
clean = clean.replace(/\n/g, ''); | |
return clean | |
} | |
export function getRedirectingURL(url) { | |
return fetch(url, { redirect: 'manual' }).then((r) => { | |
if ([301, 302, 303].includes(r.status) && r.headers.has('location')) | |
return r.headers.get('location'); | |
}).catch(() => null); | |
} | |
export function merge(a, b) { | |
for (const k of Object.keys(b)) { | |
if (Array.isArray(b[k])) { | |
a[k] = [...(a[k] ?? []), ...b[k]]; | |
} else if (typeof b[k] === 'object') { | |
a[k] = merge(a[k], b[k]); | |
} else { | |
a[k] = b[k]; | |
} | |
} | |
return a; | |
} |