2022-07-08 19:17:56 +01:00
|
|
|
import UrlPattern from "url-pattern";
|
|
|
|
|
|
|
|
import { services as patterns } from "./config.js";
|
|
|
|
|
2022-07-17 12:21:51 +01:00
|
|
|
import { cleanURL, apiJSON } from "./sub/utils.js";
|
2022-07-08 19:17:56 +01:00
|
|
|
import { errorUnsupported } from "./sub/errors.js";
|
2022-07-24 11:54:05 +01:00
|
|
|
import loc from "../localization/manager.js";
|
2022-10-09 18:44:00 +01:00
|
|
|
import match from "./processing/match.js";
|
2022-07-08 19:17:56 +01:00
|
|
|
|
2022-08-23 15:43:56 +01:00
|
|
|
export async function getJSON(originalURL, lang, obj) {
|
2022-07-08 19:17:56 +01:00
|
|
|
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://", "")}`;
|
|
|
|
}
|
2022-08-22 15:10:54 +01:00
|
|
|
if (host == "goo" && url.substring(0, 30) == "https://soundcloud.app.goo.gl/") {
|
|
|
|
host = "soundcloud"
|
|
|
|
url = `https://soundcloud.com/${url.replace("https://soundcloud.app.goo.gl/", "").split('/')[0]}`
|
|
|
|
}
|
2022-08-01 18:53:44 +01:00
|
|
|
if (host == "tumblr" && !url.includes("blog/view")) {
|
|
|
|
if (url.slice(-1) == '/') url = url.slice(0, -1);
|
|
|
|
url = url.replace(url.split('/')[5], '');
|
|
|
|
}
|
2022-07-08 19:17:56 +01:00
|
|
|
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]);
|
2022-07-28 17:03:17 +01:00
|
|
|
if (patternMatch) break;
|
2022-07-08 19:17:56 +01:00
|
|
|
}
|
|
|
|
if (patternMatch) {
|
2022-08-23 15:43:56 +01:00
|
|
|
return await match(host, patternMatch, url, lang, obj);
|
2022-08-16 08:14:19 +01:00
|
|
|
} return apiJSON(0, { t: errorUnsupported(lang) })
|
|
|
|
} return apiJSON(0, { t: errorUnsupported(lang) })
|
2022-07-08 19:17:56 +01:00
|
|
|
} else {
|
2022-08-16 08:14:19 +01:00
|
|
|
return apiJSON(0, { t: errorUnsupported(lang) })
|
2022-07-08 19:17:56 +01:00
|
|
|
}
|
|
|
|
} catch (e) {
|
2022-07-24 11:54:05 +01:00
|
|
|
return apiJSON(0, { t: loc(lang, 'ErrorSomethingWentWrong') });
|
2022-07-08 19:17:56 +01:00
|
|
|
}
|
2022-08-01 16:48:37 +01:00
|
|
|
}
|