2022-07-08 18:17:56 +00:00
|
|
|
import UrlPattern from "url-pattern";
|
|
|
|
|
|
|
|
import { services as patterns } from "./config.js";
|
|
|
|
|
2022-07-17 11:21:51 +00:00
|
|
|
import { cleanURL, apiJSON } from "./sub/utils.js";
|
2022-07-08 18:17:56 +00:00
|
|
|
import { errorUnsupported } from "./sub/errors.js";
|
2022-07-24 10:54:05 +00:00
|
|
|
import loc from "../localization/manager.js";
|
2022-10-09 17:44:00 +00:00
|
|
|
import match from "./processing/match.js";
|
2022-07-08 18:17:56 +00:00
|
|
|
|
2022-08-23 14:43:56 +00:00
|
|
|
export async function getJSON(originalURL, lang, obj) {
|
2022-07-08 18:17:56 +00:00
|
|
|
try {
|
2022-11-12 16:40:11 +00:00
|
|
|
let url = decodeURIComponent(originalURL);
|
2023-02-09 14:45:17 +00:00
|
|
|
if (url.startsWith('http://')) {
|
|
|
|
return apiJSON(0, { t: errorUnsupported(lang) });
|
|
|
|
}
|
|
|
|
let hostname = url.replace("https://", "").replace(' ', '').split('&')[0].split("/")[0].split("."),
|
|
|
|
host = hostname[hostname.length - 2],
|
|
|
|
patternMatch;
|
|
|
|
|
|
|
|
// TO-DO: bring all tests into one unified module instead of placing them in several places
|
|
|
|
switch(host) {
|
|
|
|
case "youtu":
|
2022-07-08 18:17:56 +00:00
|
|
|
host = "youtube";
|
|
|
|
url = `https://youtube.com/watch?v=${url.replace("youtu.be/", "").replace("https://", "")}`;
|
2023-02-09 14:45:17 +00:00
|
|
|
break;
|
|
|
|
case "goo":
|
|
|
|
if (url.substring(0, 30) === "https://soundcloud.app.goo.gl/"){
|
|
|
|
host = "soundcloud"
|
|
|
|
url = `https://soundcloud.com/${url.replace("https://soundcloud.app.goo.gl/", "").split('/')[0]}`
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "tumblr":
|
|
|
|
if (!url.includes("blog/view")) {
|
2023-02-12 07:40:49 +00:00
|
|
|
if (url.slice(-1) === '/') url = url.slice(0, -1);
|
2023-02-09 14:45:17 +00:00
|
|
|
url = url.replace(url.split('/')[5], '');
|
2022-07-08 18:17:56 +00:00
|
|
|
}
|
2023-02-09 14:45:17 +00:00
|
|
|
break;
|
|
|
|
}
|
2023-02-13 13:44:58 +00:00
|
|
|
if (!(host && host.length < 20 && host in patterns && patterns[host]["enabled"])) return apiJSON(0, { t: errorUnsupported(lang) });
|
|
|
|
|
2023-02-09 14:45:17 +00:00
|
|
|
for (let i in patterns[host]["patterns"]) {
|
|
|
|
patternMatch = new UrlPattern(patterns[host]["patterns"][i]).match(cleanURL(url, host).split(".com/")[1]);
|
|
|
|
if (patternMatch) break;
|
|
|
|
}
|
2023-02-13 13:44:58 +00:00
|
|
|
if (!patternMatch) return apiJSON(0, { t: errorUnsupported(lang) });
|
|
|
|
|
2023-02-09 14:45:17 +00:00
|
|
|
return await match(host, patternMatch, url, lang, obj);
|
2022-07-08 18:17:56 +00:00
|
|
|
} catch (e) {
|
2022-07-24 10:54:05 +00:00
|
|
|
return apiJSON(0, { t: loc(lang, 'ErrorSomethingWentWrong') });
|
2022-07-08 18:17:56 +00:00
|
|
|
}
|
2022-08-01 15:48:37 +00:00
|
|
|
}
|