Fetching data like tags and engines from the platform
parent
0e47ab09b4
commit
8c1d39b98d
|
@ -1,5 +1,8 @@
|
||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
|
// Core modules
|
||||||
|
const {readFileSync, writeFileSync, existsSync} = require("fs");
|
||||||
|
|
||||||
// Public modules from npm
|
// Public modules from npm
|
||||||
const axios = require("axios").default;
|
const axios = require("axios").default;
|
||||||
const ky = require("ky-universal").create({
|
const ky = require("ky-universal").create({
|
||||||
|
@ -91,7 +94,11 @@ module.exports.authenticate = async function (credentials, force) {
|
||||||
const errorMessage = $("body").find(f95selector.LOGIN_MESSAGE_ERROR).text().replace(/\n/g, "");
|
const errorMessage = $("body").find(f95selector.LOGIN_MESSAGE_ERROR).text().replace(/\n/g, "");
|
||||||
|
|
||||||
// Return the result of the authentication
|
// Return the result of the authentication
|
||||||
if (errorMessage === "") return new LoginResult(true, "Authentication successful");
|
if (errorMessage === "") {
|
||||||
|
// Fetch data
|
||||||
|
await exports.fetchPlatformData();
|
||||||
|
return new LoginResult(true, "Authentication successful");
|
||||||
|
}
|
||||||
else return new LoginResult(false, errorMessage);
|
else return new LoginResult(false, errorMessage);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
shared.logger.error(`Error ${e.message} occurred while authenticating to ${secureURL}`);
|
shared.logger.error(`Error ${e.message} occurred while authenticating to ${secureURL}`);
|
||||||
|
@ -121,35 +128,60 @@ module.exports.getF95Token = async function() {
|
||||||
* @protected
|
* @protected
|
||||||
* Gets the basic data used for game data processing
|
* Gets the basic data used for game data processing
|
||||||
* (such as graphics engines and progress statuses)
|
* (such as graphics engines and progress statuses)
|
||||||
* @deprecated
|
|
||||||
*/
|
*/
|
||||||
/* istanbul ignore next */
|
|
||||||
module.exports.fetchPlatformData = async function() {
|
module.exports.fetchPlatformData = async function() {
|
||||||
// Fetch the response of the platform
|
// Check if the data are cached
|
||||||
const response = await exports.fetchGETResponse(f95url.F95_LATEST_UPDATES);
|
if(existsSync(shared.cachePath)) {
|
||||||
if (!response) {
|
const data = readFileSync(shared.cachePath);
|
||||||
shared.logger.warn("Unable to get the token for the session");
|
const json = JSON.parse(data);
|
||||||
|
shared.engines = json.engines;
|
||||||
|
shared.statuses = json.statuses;
|
||||||
|
shared.tags = json.tags;
|
||||||
|
shared.others = json.others;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// The response is a HTML page, we need to find
|
// Load the HTML
|
||||||
// the base data, used when scraping the games
|
const html = await exports.fetchHTML(f95url.F95_LATEST_UPDATES);
|
||||||
const $ = cheerio.load(response.data);
|
const $ = cheerio.load(html);
|
||||||
|
|
||||||
// Extract the elements
|
// Clean the JSON string
|
||||||
const engineElements = $("body").find(f95selector.BD_ENGINE_ID_SELECTOR);
|
const unparsedText = $(f95selector.LU_TAGS_SCRIPT).html().trim();
|
||||||
const statusesElements = $("body").find(f95selector.BD_STATUS_ID_SELECTOR);
|
const startIndex = unparsedText.indexOf("{");
|
||||||
|
const endIndex = unparsedText.lastIndexOf("}");
|
||||||
|
const parsedText = unparsedText.substring(startIndex, endIndex + 1);
|
||||||
|
const data = JSON.parse(parsedText);
|
||||||
|
|
||||||
// Extract the raw text
|
// Extract and parse the data
|
||||||
engineElements.each(function extractEngineNames(idx, el) {
|
const prefixes = data.prefixes.games.map(e => {
|
||||||
const engine = cheerio.load(el).text().trim();
|
return {
|
||||||
shared.engines.push(engine);
|
element: e.name,
|
||||||
|
data: e.prefixes
|
||||||
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
statusesElements.each(function extractEngineNames(idx, el) {
|
for(const p of prefixes) {
|
||||||
const status = cheerio.load(el).text().trim();
|
// Prepare the dict
|
||||||
shared.statuses.push(status);
|
const dict = {};
|
||||||
});
|
for (const e of p.data) dict[parseInt(e.id)] = e.name.replace("'", "'");
|
||||||
|
|
||||||
|
if(p.element === "Engine") shared.engines = dict;
|
||||||
|
else if (p.element === "Status") shared.statuses = dict;
|
||||||
|
else if (p.element === "Other") shared.others = dict;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parse the tags
|
||||||
|
shared.tags = data.tags;
|
||||||
|
|
||||||
|
// Cache data
|
||||||
|
const saveDict = {
|
||||||
|
engines: shared.engines,
|
||||||
|
statuses: shared.statuses,
|
||||||
|
tags: shared.tags,
|
||||||
|
others: shared.others,
|
||||||
|
};
|
||||||
|
const json = JSON.stringify(saveDict);
|
||||||
|
writeFileSync(shared.cachePath, json);
|
||||||
};
|
};
|
||||||
|
|
||||||
//#region Utility methods
|
//#region Utility methods
|
||||||
|
|
Loading…
Reference in New Issue