UNPKG

mrivals

Version:

Wrapper/Scrapper for Marvel Rival stats.

160 lines (159 loc) 4.96 kB
// src/helpers/fetcher.ts import { exec } from "node:child_process"; var fetchWithCurl = (url) => new Promise((resolve, reject) => { exec(`curl --max-time 5 --user-agent 'Chrome/121' --url ${url}`, (err, result) => { if (!result) { reject(err); } resolve(JSON.parse(result)); }); }); var fetchWithNode = (url) => new Promise((resolve, reject) => { fetch(url).then((res) => res.json()).then((json) => resolve(json)).catch((err) => reject(err)); }); var fetchWithFlaresolverr = (url, { flaresolverrUrl }) => fetch(flaresolverrUrl, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ cmd: "request.get", url, maxTimeout: 6e4 }) }).then(async (res) => { if (res.ok) { const data = await res.json(); const responseText = data.solution.response; let jsonContent = responseText; if (responseText.startsWith("<html>")) { const match = responseText.match(/<pre[^>]*>([\s\S]*?)<\/pre>/i); if (match && match[1]) { jsonContent = match[1].trim(); } } return JSON.parse(jsonContent); } throw new Error(res.statusText); }); var getFetcher = (options) => { if (options?.flaresolverrUrl) { return (url) => fetchWithFlaresolverr(url, { flaresolverrUrl: options.flaresolverrUrl }); } if (options?.useCurl) { return fetchWithCurl; } return fetchWithNode; }; // src/index.ts var BASE_URL = `https://api.tracker.gg/api/v2/marvel-rivals/standard/profile/ign/{USERNAME}`; var API = class _API { username; _raw; constructor(username) { this.username = username; } static async fetchUser(username, options = {}) { const api = new _API(username); const fetchData = getFetcher({ flaresolverrUrl: options.flaresolverrUrl, useCurl: options.useCurl ?? false }); api._raw = await fetchData(BASE_URL.replace("{USERNAME}", username)); if (api._raw.errors) throw new Error(api._raw.errors[0].message); return api; } overview(options = {}) { const result = {}; const raw = options.raw ?? false; const data = this._raw.data.segments.find((x) => x.type === "overview"); if (raw) { result._raw = data; } if (data?.stats) { for (const key in data.stats) { result[key] = data.stats[key].value; } } return result; } heroes() { const result = {}; const heroes = this._raw.data.segments.filter((x) => x.type === "hero"); for (const hero of heroes) { const heroName = hero.metadata.name; result[heroName] = {}; if (hero) { for (const key in hero.stats) { result[heroName][key] = hero.stats[key].value; } } } return result; } roles() { const result = {}; const roles = this._raw.data.segments.filter((x) => x.type === "hero-role"); for (const role of roles) { const roleName = role.metadata.name; result[roleName] = {}; if (role) { for (const key in role.stats) { result[roleName][key] = role.stats[key].value; } } } return result; } peakRank() { const data = this._raw.data.segments.find((x) => x.type === "ranked-peaks"); const { lifetimePeakRanked } = data?.stats ?? {}; const peakTiers = Array.isArray(data?.stats.peakTiers?.value) ? data?.stats.peakTiers.value : []; const result = { peakTiers: peakTiers?.map((x) => ({ displayName: x.displayName, tierName: x.metadata.tierName, tierShortName: x.metadata.tierShortName, tierIcon: x.metadata.iconUrl, tierColor: x.metadata.color, season: x.metadata.seasonShortName, seasonName: x.metadata.seasonName, mmr: x.value })), lifetimePeakRanked: { displayName: lifetimePeakRanked.metadata.tierName, tierName: lifetimePeakRanked.metadata.tierName, tierShortName: lifetimePeakRanked.metadata.tierShortName, tierIcon: lifetimePeakRanked.metadata.iconUrl, tierColor: lifetimePeakRanked.metadata.color, season: lifetimePeakRanked.metadata.seasonShortName, seasonName: lifetimePeakRanked.metadata.seasonName, mmr: lifetimePeakRanked.value } }; return result; } info() { const platform = this._raw.data.platformInfo; const info = this._raw.data.userInfo; const data = this._raw.data.segments.find((x) => x.type == "overview"); const { ranked, peakRanked } = data?.stats ?? {}; const result = { platform: platform.platformSlug, uuid: platform.platformUserId, name: platform.platformUserHandle, userid: platform.platformUserIdentifier, avatar: platform.avatarUrl, pageViews: info.pageviews, rank: ranked?.metadata?.tierName, peakRank: peakRanked?.metadata?.tierName }; return result; } raw() { return this._raw; } }; export { API }; //# sourceMappingURL=index.mjs.map