diff --git a/src/modules/api.js b/src/modules/api.js index c3549bb3..209bc14a 100644 --- a/src/modules/api.js +++ b/src/modules/api.js @@ -1,32 +1,17 @@ -import { services } from "./config.js"; - import { apiJSON } from "./sub/utils.js"; import { errorUnsupported } from "./sub/errors.js"; import loc from "../localization/manager.js"; import match from "./processing/match.js"; -import { getHostIfValid } from "./processing/url.js"; +import { extract } from "./processing/url.js"; export async function getJSON(url, lang, obj) { try { - const host = getHostIfValid(url); - - if (!host || !services[host].enabled) { + const parsed = extract(url); + if (parsed === null) { return apiJSON(0, { t: errorUnsupported(lang) }); } - let patternMatch; - for (const pattern of services[host].patterns) { - patternMatch = pattern.match( - url.pathname.substring(1) + url.search - ); - if (patternMatch) break; - } - - if (!patternMatch) { - return apiJSON(0, { t: errorUnsupported(lang) }); - } - - return await match(host, patternMatch, url, lang, obj) + return await match(parsed.host, parsed.patternMatch, url, lang, obj) } catch (e) { return apiJSON(0, { t: loc(lang, 'ErrorSomethingWentWrong') }) } diff --git a/src/modules/processing/url.js b/src/modules/processing/url.js index c723cb91..b0c60798 100644 --- a/src/modules/processing/url.js +++ b/src/modules/processing/url.js @@ -125,3 +125,29 @@ export function getHostIfValid(url) { return host.sld; } + + +export function extract(url) { + const host = getHostIfValid(url); + + if (!host || !services[host].enabled) { + return null; + } + + let patternMatch; + for (const pattern of services[host].patterns) { + patternMatch = pattern.match( + url.pathname.substring(1) + url.search + ); + + if (patternMatch) { + break; + } + } + + if (!patternMatch) { + return null; + } + + return { host, patternMatch }; +} \ No newline at end of file