Translate scripts

pull/73/head
MillenniumEarl 2021-02-15 21:29:32 +01:00
parent 23c971ca32
commit a69acbbef4
1 changed files with 61 additions and 44 deletions

View File

@ -1,56 +1,85 @@
"use strict"; "use strict";
// Core modules // Core modules
const {readFileSync, writeFileSync, existsSync} = require("fs"); import { readFileSync, writeFileSync, existsSync } from "fs";
// Public modules from npm // Public modules from npm
const cheerio = require("cheerio"); import cheerio from "cheerio";
// Modules from file // Modules from file
const shared = require("./shared.js"); import shared, { DictType } from "./shared";
const f95url = require("./constants/url.js"); import { urls as f95url } from "./constants/url";
const f95selector = require("./constants/css-selector.js"); import { selectors as f95selector} from "./constants/css-selector";
const {fetchHTML} = require("./network-helper.js"); import { fetchHTML } from "./network-helper";
//#region Interface definitions
/**
* Represents the single element contained in the data categories.
*/
interface SingleOptionObj {
ID: number,
Name: string,
Class: string
}
/** /**
* @protected * Represents the set of values associated with a specific category of data.
*/
interface CategoryResObj {
ID: number,
Name: string,
Prefixes: SingleOptionObj[]
}
/**
* Represents the set of tags present on the platform-
*/
interface LatestResObj {
Prefixes: CategoryResObj[],
Tags: DictType,
Options: string
}
//#endregion Interface definitions
//#region Public methods
/**
* 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)
*/ */
module.exports.fetchPlatformData = async function () { export async function fetchPlatformData(): Promise<void> {
// Check if the data are cached // Check if the data are cached
if (!_readCache(shared.cachePath)) { if (!readCache(shared.cachePath)) {
// Load the HTML // Load the HTML
const html = await fetchHTML(f95url.F95_LATEST_UPDATES); const html = await fetchHTML(f95url.F95_LATEST_UPDATES);
// Parse data // Parse data
const data = _parseLatestPlatformHTML(html); const data = parseLatestPlatformHTML(html);
// Assign data // Assign data
_assignLatestPlatformData(data); assignLatestPlatformData(data);
// Cache data // Cache data
_saveCache(shared.cachePath); saveCache(shared.cachePath);
} }
}; }
//#endregion Public methods
//#region Private methods //#region Private methods
/** /**
* @private * @private
* Read the platform cache (if available) * Read the platform cache (if available)
* @param {String} path Path to cache
*/ */
function _readCache(path) { function readCache(path: string) {
// Local variables // Local variables
let returnValue = false; let returnValue = false;
if (existsSync(path)) { if (existsSync(path)) {
const data = readFileSync(path); const data = readFileSync(path, {encoding: "utf-8", flag: "r"});
const json = JSON.parse(data); const json: { [s: string]: DictType } = JSON.parse(data);
shared.engines = json.engines; shared.setEngines(json.engines);
shared.statuses = json.statuses; shared.setStatuses(json.statuses);
shared.tags = json.tags; shared.setTags(json.tags);
shared.others = json.others; shared.setOthers(json.others);
returnValue = true; returnValue = true;
} }
return returnValue; return returnValue;
@ -59,9 +88,8 @@ function _readCache(path) {
/** /**
* @private * @private
* Save the current platform variables to disk. * Save the current platform variables to disk.
* @param {String} path Path to cache
*/ */
function _saveCache(path) { function saveCache(path: string): void {
const saveDict = { const saveDict = {
engines: shared.engines, engines: shared.engines,
statuses: shared.statuses, statuses: shared.statuses,
@ -76,10 +104,8 @@ function _saveCache(path) {
* @private * @private
* Given the HTML code of the response from the F95Zone, * Given the HTML code of the response from the F95Zone,
* parse it and return the result. * parse it and return the result.
* @param {String} html
* @returns {Object.<string, object>} Parsed data
*/ */
function _parseLatestPlatformHTML(html) { function parseLatestPlatformHTML(html: string): LatestResObj{
const $ = cheerio.load(html); const $ = cheerio.load(html);
// Clean the JSON string // Clean the JSON string
@ -93,34 +119,25 @@ function _parseLatestPlatformHTML(html) {
/** /**
* @private * @private
* Assign to the local variables the values from the F95Zone. * Assign to the local variables the values from the F95Zone.
* @param {Object.<string, object>} data
*/ */
function _assignLatestPlatformData(data) { function assignLatestPlatformData(data: LatestResObj): void {
// Local variables // Local variables
const scrapedData = {}; const scrapedData = {};
// Extract and parse the data
const prefixes = data.prefixes.games.map(e => {
return {
element: e.name,
data: e.prefixes
};
});
// Parse and assign the values that are NOT tags // Parse and assign the values that are NOT tags
for (const p of prefixes) { for (const p of data.Prefixes) {
// Prepare the dict // Prepare the dict
const dict = {}; const dict: DictType = {};
for (const e of p.data) dict[parseInt(e.id)] = e.name.replace("&#039;", "'"); for (const e of p.Prefixes) dict[e.ID] = e.Name.replace("&#039;", "'");
// Save the property // Save the property
scrapedData[p.element] = dict; scrapedData[p.Name] = dict;
} }
// Save the values // Save the values
shared.engines = Object.assign({}, scrapedData["Engine"]); shared.setEngines(Object.assign({}, scrapedData["Engine"]));
shared.statuses = Object.assign({}, scrapedData["Status"]); shared.setStatuses(Object.assign({}, scrapedData["Status"]));
shared.others = Object.assign({}, scrapedData["Other"]); shared.setOthers(Object.assign({}, scrapedData["Other"]));
shared.tags = data.tags; shared.setTags(data.Tags);
} }
//#endregion //#endregion