aboutsummaryrefslogtreecommitdiff
path: root/update/ponies/infobox.js
blob: d3ca17456be8a7935805cfe369b28ca3330a7cb7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*
 * MIT License
 *
 * Copyright (c) 2022- Minteck
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 */

const fs = require('fs');
const axios = require("axios");
const WikiTextParser = require('parse-wikitext');
const parser = new WikiTextParser("mlp.fandom.com");

console.log("Gathering infobox for each page...");

(async () => {
    let infoboxes = {};
    for (let page of JSON.parse(fs.readFileSync("./data/pages.json").toString())) {
        console.log("Gathering infobox for '" + page.name + "'...");
        try {
            let data = (await axios.get("https://mlp.fandom.com/api.php?action=query&prop=revisions&titles=" + page.name + "&rvslots=*&rvprop=content&formatversion=2&format=json")).data;
            let mwextracts = (await axios.get("https://mlp.fandom.com/api.php?format=json&action=query&prop=extracts&exlimit=max&explaintext&exintro&titles=" + encodeURI(page.name) + "&redirects=")).data;
            let mwtext = (await axios.get("https://mlp.fandom.com/api.php?format=json&action=query&prop=extracts&exlimit=max&explaintext&titles=" + encodeURI(page.name) + "&redirects=")).data;
            let extracts = "";
            try {
                sentences = mwextracts.query.pages[Object.keys(mwextracts.query.pages)[0]].extract.trim().replace(/(.*)\n(.*)/, "$2").replace(/(.*)\n\n(.*)/gm, "$2").replace(/([.?!])\s*(?=[A-Z])/g, "$1|").split("|");
                extracts = sentences[0];
                if (extracts.length < 150 && sentences.length > 1) {
                    extracts = sentences[0] + " " + sentences[1];
                    if (extracts.length < 150 && sentences.length > 2) {
                        extracts = sentences[0] + " " + sentences[1] + " " + sentences[2];
                    }
                }
            } catch (e) {
                extracts = "";
            }
            let extracts_fr = extracts;
            if (fs.existsSync("./modules/translate.php")) {
                try {
                    extracts_fr = require('child_process').spawnSync("php", [ "translate.php", extracts ], { cwd: "./modules" }).stdout.toString()
                } catch (e) {
                    extracts_fr = extracts;
                }
            } else {
                extracts_fr = extracts;
            }
            if (data.query.pages.length > 0) {
                console.log("Results found, adding name to database")
                sections = parser.pageToSectionObject(data.query.pages[0].revisions[0].slots.main.content);
                box = parser.parseInfoBox(sections["content"]);
                if (box.template === "Infobox character") {
                    infoboxes[page.name] = parser.parseInfoBox(sections["content"]).values;
                    infoboxes[page.name]["_extract"] = extracts;
                    infoboxes[page.name]["_extract_fr"] = extracts_fr;
                }
            } else {
                console.log("No results found, ignoring name");
            }
            try {
                if (mwtext.query.pages[Object.keys(mwextracts.query.pages)[0]].extract.toLowerCase().includes("friendship is magic")
                    || mwtext.query.pages[Object.keys(mwextracts.query.pages)[0]].extract.toLowerCase().includes("fim")
                ) {
                    infoboxes[page.name]["_gen"] = 4;
                } else if (mwtext.query.pages[Object.keys(mwextracts.query.pages)[0]].extract.toLowerCase().includes("a new generation")
                    || mwtext.query.pages[Object.keys(mwextracts.query.pages)[0]].extract.replace(/[.,?!;()"'-]/g, " ").replace(/\s+/g, " ").toLowerCase().split(" ").includes("ang")
                    || mwtext.query.pages[Object.keys(mwextracts.query.pages)[0]].extract.replace(/[.,?!;()"'-]/g, " ").replace(/\s+/g, " ").toLowerCase().split(" ").includes("ang")
                ) {
                    infoboxes[page.name]["_gen"] = 5;
                } else {
                    infoboxes[page.name]["_gen"] = -1;
                }
            } catch(e) {}
        } catch (e) {
            console.error(e);
        }
    }

    fs.writeFileSync("./data/boxes.json", JSON.stringify(infoboxes, null, 4))
})()