-
Notifications
You must be signed in to change notification settings - Fork 54
/
WikiaDataScraper.mjs
190 lines (168 loc) 路 5.58 KB
/
WikiaDataScraper.mjs
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import fs from 'fs/promises';
import path from 'path';
import { load } from 'cheerio';
import { tmpdir } from 'os';
import { promisify } from 'util';
import nodeCmd from 'node-cmd';
import fetch, { Request } from 'node-fetch';
const run = promisify(nodeCmd.get);
const blueprintUrl = 'https://warframe.fandom.com/wiki/Module:Blueprints/data?action=edit';
const getLuaData = async (url) => {
try {
const $ = load(await fetch(url).then((data) => data.text()));
return $('#wpTextbox1').text();
} catch (err) {
console.error('Failed to fetch latest Lua data:');
console.error(err);
return '';
}
};
const convertLuaDataToJson = async (luaData, luaDataName) => {
const objReturn = `return ${luaDataName}Data`;
const hasObjReturn = luaData.includes(objReturn);
const modifiedScript = hasObjReturn
? luaData.replace(objReturn, '')
: luaData.replace('return {', `local ${luaDataName}Data = {`);
// Add JSON conversion
const luaToJsonScript = `JSON = (loadfile "build/wikia/JSON.lua")()
${modifiedScript}
print(JSON:encode(${luaDataName}Data))
`;
// Run updated JSON lua script
const temp = await fs.mkdtemp(path.join(tmpdir(), 'temp-'));
const lua = path.join(temp, 'dataraw.lua');
const json = path.join(temp, 'dataraw.json');
await fs.writeFile(path.join(temp, 'dataraw.lua'), luaToJsonScript, {
encoding: 'utf8',
flag: 'w',
});
try {
await run(`lua ${lua} > ${json}`);
const dataRaw = await fs.readFile(json, { encoding: 'utf8' });
return JSON.parse(dataRaw);
} catch (err) {
console.error(`Failed to execute modified lua script:\n${err}`);
console.error(err);
} finally {
await fs.rm(temp, { recursive: true, force: true });
}
};
const getImageUrls = async (things) => {
const titles = [];
Object.keys(things).forEach((thingName) => {
titles.push(`File:${things[thingName].Image}`);
});
// Split titles into batches of 50, the max allowed by the wikimedia API
const titleBatches = [];
while (titles.length > 0) {
titleBatches.push(titles.splice(0, 50));
}
const urlRequests = titleBatches.map((titleBatch) => {
const params = ['action=query', `titles=${titleBatch.join('|')}`, 'prop=imageinfo', 'iiprop=url', 'format=json'];
const request = new Request(`https://warframe.fandom.com/api.php?${params.join('&')}`);
return fetch(request).then((d) => d.json());
});
try {
return Promise.all(urlRequests)
.then((res) => {
const urls = {};
res.forEach((data) => {
Object.keys(data.query.pages).forEach((id) => {
if (id > -1) {
const title = data.query.pages[id].title.replace('File:', '');
const { url } = data.query.pages[id].imageinfo[0];
urls[title] = url;
}
});
});
return urls;
})
.catch(console.error);
} catch (err) {
console.error('Failed to fetch image URLs:');
console.error(err);
return {};
}
};
const defaultTransform = async () => false;
const nameCompare = (first, second) => {
if (first.name < second.name) {
return -1;
}
if (first.name > second.name) {
return 1;
}
return 0;
};
let blueprints;
(async () => {
const { Blueprints, Suits } = await convertLuaDataToJson(await getLuaData(blueprintUrl), 'BlueprintsData');
blueprints = { ...Blueprints, ...Suits };
})();
/**
* Scrape Wikia data from data modules
*/
export default class WikiaDataScraper {
constructor(url, luaObjectName, transformFunction) {
if (Array.isArray(url)) {
this.urls = url;
} else {
this.url = url;
}
this.luaObjectName = luaObjectName;
this.transformFunction = transformFunction;
if (typeof transformFunction === 'undefined') {
this.transformFunction = defaultTransform;
}
}
async scrape() {
const jsonData = {};
jsonData[`${this.luaObjectName}s`] = {};
if (this.url) {
const luaData = await getLuaData(this.url);
const jTemp = await convertLuaDataToJson(luaData, this.luaObjectName);
Object.keys(jTemp).forEach((key) => {
if (!jTemp[key].name) {
jsonData[`${this.luaObjectName}s`] = {
...jsonData[`${this.luaObjectName}s`],
...jTemp,
};
}
});
} else if (this.urls.length) {
await Promise.all(
this.urls.map(async (url) => {
const luaData = await getLuaData(url);
const jTemp = await convertLuaDataToJson(luaData, this.luaObjectName);
jsonData[`${this.luaObjectName}s`] = {
...jsonData[`${this.luaObjectName}s`],
...jTemp,
};
})
);
}
if (jsonData[`${this.luaObjectName}s`][`${this.luaObjectName}s`]) {
jsonData[`${this.luaObjectName}s`] = jsonData[`${this.luaObjectName}s`][`${this.luaObjectName}s`];
}
if (!Object.keys(jsonData).length) {
throw new Error('No json data');
}
const imageUrls = await getImageUrls(jsonData[`${this.luaObjectName}s`]);
const things = [];
try {
await Promise.all(
Object.keys(jsonData[`${this.luaObjectName}s`]).map(async (thingName) => {
const thingToTransform = jsonData[`${this.luaObjectName}s`][thingName];
if (thingToTransform && !thingToTransform.Name) thingToTransform.Name = thingName;
const transformedThing = await this.transformFunction(thingToTransform, imageUrls, blueprints);
things.push(transformedThing);
})
);
things.sort(nameCompare);
} catch (e) {
console.error(e.stack);
}
if (!things.length) console.error(`scraped no ${this.luaObjectName}`);
return things;
}
}