UNPKG

mta-wiki-parser

Version:

Wiki to Discord parser for Multi Theft Auto Wiki: https://wiki.multitheftauto.com/

93 lines (92 loc) 3.6 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const axios_1 = __importDefault(require("axios")); class Fetcher { static async fetch(query, userAgent) { query = encodeURI(query.replace(/\s+/g, '_')); // Convert space to _ and encode const res = await axios_1.default.get('https://wiki.multitheftauto.com/api.php', { headers: { 'User-Agent': userAgent || 'mtasa-wiki-parser @ https://github.com/AfuSensi/mtasa-wiki-parser', }, params: { action: 'parse', page: query, format: 'json', prop: 'wikitext|images|templates|sections|categories', redirects: 'true', disabletoc: 'true', disablelimitreport: 'true', }, }); if (res.data.error) { throw Error(`${res.data.error.code}: ${res.data.error.info}`); } // Image const imageUrl = res.data.parse.images.length > 0 && typeof res.data.parse.images[0] === 'string' ? await Fetcher.getImageUrl(res.data.parse.images[0], userAgent) : false; // Templates const templates = []; res.data.parse.templates.forEach((element) => { templates.push(element['*']); }); // Categories const categories = []; res.data.parse.categories.forEach((element) => { categories.push(element['*']); }); // Sections const sections = []; res.data.parse.sections.forEach((element) => { // Check if index is NaN, to ignore template indexes that start with prefix T- if (!isNaN(element.index)) { sections.push(element); } }); const fetchedArticle = { id: res.data.parse.pageid, url: `https://wiki.multitheftauto.com/wiki/${res.data.parse.title.replace(/\s+/g, '_')}`, title: res.data.parse.title, categories, templates, sections, image: imageUrl, rawText: res.data.parse.wikitext['*'], }; return fetchedArticle; } static async getImageUrl(imageName, userAgent) { try { const res = await axios_1.default.get('https://wiki.multitheftauto.com/api.php', { headers: { 'User-Agent': userAgent || 'mtasa-wiki-parser @ https://github.com/AfuSensi/mtasa-wiki-parser', }, params: { action: 'query', prop: 'imageinfo', iiprop: 'url', titles: `File:${imageName}`, format: 'json', }, }); const pages = Object.keys(res.data.query.pages); for (const id of pages) { if (res.data.query.pages[id].hasOwnProperty('missing') || res.data.query.pages[id].hasOwnProperty('invalid')) { return false; } else { return res.data.query.pages[id].imageinfo[0].url; } } return false; } catch (err) { console.error(err); return false; } } } exports.Fetcher = Fetcher;