Reduce cyclomatic complexity

pull/59/head
MillenniumEarl 2020-12-15 14:00:05 +01:00
parent ef7308e298
commit ee34665b3a
1 changed files with 123 additions and 69 deletions

View File

@ -133,57 +133,19 @@ module.exports.getF95Token = async function() {
*/ */
module.exports.fetchPlatformData = async function() { module.exports.fetchPlatformData = async function() {
// Check if the data are cached // Check if the data are cached
if(existsSync(shared.cachePath)) { if(!_readCache(shared.cachePath)) {
const data = readFileSync(shared.cachePath);
const json = JSON.parse(data);
shared.engines = json.engines;
shared.statuses = json.statuses;
shared.tags = json.tags;
shared.others = json.others;
return;
}
// Load the HTML // Load the HTML
const html = await exports.fetchHTML(f95url.F95_LATEST_UPDATES); const html = await exports.fetchHTML(f95url.F95_LATEST_UPDATES);
const $ = cheerio.load(html);
// Clean the JSON string // Parse data
const unparsedText = $(f95selector.LU_TAGS_SCRIPT).html().trim(); const data = _parseLatestPlatformHTML(html);
const startIndex = unparsedText.indexOf("{");
const endIndex = unparsedText.lastIndexOf("}");
const parsedText = unparsedText.substring(startIndex, endIndex + 1);
const data = JSON.parse(parsedText);
// Extract and parse the data // Assign data
const prefixes = data.prefixes.games.map(e => { _assignLatestPlatformData(data);
return {
element: e.name,
data: e.prefixes
};
});
for(const p of prefixes) {
// Prepare the dict
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 // Cache data
const saveDict = { _saveCache(shared.cachePath);
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
@ -242,24 +204,6 @@ module.exports.isStringAValidURL = function (url) {
else return false; else return false;
}; };
/**
* @private
* Check with Axios if a URL exists.
* @param {String} url
*/
async function _axiosUrlExists(url) {
// Local variables
let valid = false;
try {
const response = await axios.head(url);
valid = response && !/4\d\d/.test(response.status);
} catch (error) {
if (error.code === "ENOTFOUND") valid = false;
else throw error;
}
return valid;
}
/** /**
* @protected * @protected
* Check if a particular URL is valid and reachable on the web. * Check if a particular URL is valid and reachable on the web.
@ -296,3 +240,113 @@ module.exports.getUrlRedirect = async function (url) {
return response.config.url; return response.config.url;
}; };
//#endregion Utility methods //#endregion Utility methods
//#region Private methods
/**
* @private
* Check with Axios if a URL exists.
* @param {String} url
*/
async function _axiosUrlExists(url) {
// Local variables
let valid = false;
try {
const response = await axios.head(url);
valid = response && !/4\d\d/.test(response.status);
} catch (error) {
if (error.code === "ENOTFOUND") valid = false;
else throw error;
}
return valid;
}
/**
* @private
* Read the platform cache (if available)
* @param {String} path Path to cache
*/
function _readCache(path) {
// Local variables
let returnValue = false;
if (existsSync(path)) {
const data = readFileSync(path);
const json = JSON.parse(data);
shared.engines = json.engines;
shared.statuses = json.statuses;
shared.tags = json.tags;
shared.others = json.others;
returnValue = true;
}
return returnValue;
}
/**
* @private
* Save the current platform variables to disk.
* @param {String} path Path to cache
*/
function _saveCache(path) {
const saveDict = {
engines: shared.engines,
statuses: shared.statuses,
tags: shared.tags,
others: shared.others,
};
const json = JSON.stringify(saveDict);
writeFileSync(path, json);
}
/**
* @private
* Given the HTML code of the response from the F95Zone,
* parse it and return the result.
* @param {String} html
* @returns {Object.<string, object>} Parsed data
*/
function _parseLatestPlatformHTML(html) {
const $ = cheerio.load(html);
// Clean the JSON string
const unparsedText = $(f95selector.LU_TAGS_SCRIPT).html().trim();
const startIndex = unparsedText.indexOf("{");
const endIndex = unparsedText.lastIndexOf("}");
const parsedText = unparsedText.substring(startIndex, endIndex + 1);
return JSON.parse(parsedText);
}
/**
* @private
* Assign to the local variables the values from the F95Zone.
* @param {Object.<string, object>} data
*/
function _assignLatestPlatformData(data) {
// Local variables
const propertiesMap = {
"Engine": shared.engines,
"Status": shared.statuses,
"Other": shared.others,
};
// 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
for (const p of prefixes) {
// Prepare the dict
const dict = {};
for (const e of p.data) dict[parseInt(e.id)] = e.name.replace("&#039;", "'");
// Save the property
propertiesMap[p] = dict;
}
// Parse the tags
shared.tags = data.tags;
}
//#endregion