cobalt/src/modules/api.js
wukko 1b4872c1de tiktok is back!
- added support for tiktok (images won't work, they're only accessible through the app)
- hopefully main input bar is now not rounded on ios, i fucking hate apple
- if service is not supported, a correlating error will appear, not generic one
- removed duplicates from config that are present in package json already
- tiny bit of clean up
2022-07-28 22:03:17 +06:00

36 lines
1.6 KiB
JavaScript

import UrlPattern from "url-pattern";
import { services as patterns } from "./config.js";
import { cleanURL, apiJSON } from "./sub/utils.js";
import { errorUnsupported } from "./sub/errors.js";
import loc from "../localization/manager.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) break;
}
if (patternMatch) {
return await match(host, patternMatch, url, ip, lang, format, quality);
} return apiJSON(0, { t: errorUnsupported(lang) } )
} return apiJSON(0, { t: errorUnsupported(lang) } )
} else {
return apiJSON(0, { t: errorUnsupported(lang) } )
}
} catch (e) {
return apiJSON(0, { t: loc(lang, 'ErrorSomethingWentWrong') });
}
}