internal changes only

- remade config module
- renamed loc to i18n because that's what all developers do
- moved code to src to make repo look cleaner
- fixed some i18n strings
This commit is contained in:
wukko
2022-07-17 17:08:49 +06:00
parent 227dc8436c
commit 67223b3acd
62 changed files with 50 additions and 55 deletions

35
src/modules/api.js Normal file
View File

@@ -0,0 +1,35 @@
import UrlPattern from "url-pattern";
import { services as patterns } from "./config.js";
import { cleanURL, apiJSON } from "./sub/api-helper.js";
import { errorUnsupported } from "./sub/errors.js";
import loc from "./sub/i18n.js";
import match from "./match.js";
export async function getJSON(originalURL, ip, lang, format, quality) {
try {
let url = decodeURI(originalURL);
if (!url.includes('http://')) {
let hostname = url.replace("https://", "").replace(' ', '').split('&')[0].split("/")[0].split("."),
host = hostname[hostname.length - 2],
patternMatch;
if (host == "youtu") {
host = "youtube";
url = `https://youtube.com/watch?v=${url.replace("youtu.be/", "").replace("https://", "")}`;
}
if (host && host.length < 20 && host in patterns && patterns[host]["enabled"]) {
for (let i in patterns[host]["patterns"]) {
patternMatch = new UrlPattern(patterns[host]["patterns"][i]).match(cleanURL(url, host).split(".com/")[1]);
}
if (patternMatch) {
return await match(host, patternMatch, url, ip, lang, format, quality);
} else throw Error()
} else throw Error()
} else {
return apiJSON(0, { t: errorUnsupported(lang) } )
}
} catch (e) {
return apiJSON(0, { t: loc(lang, 'apiError', 'generic') + loc(lang, 'apiError', 'letMeKnow') });
}
}