Reduced cyclomatick complexity

pull/59/head
MillenniumEarl 2020-12-12 12:39:16 +01:00
parent 84aff42c6c
commit 0db26c6e26
1 changed files with 27 additions and 20 deletions

View File

@ -22,6 +22,7 @@ module.exports.getGameInfo = async function (url) {
// Fetch HTML and prepare Cheerio // Fetch HTML and prepare Cheerio
const html = await fetchHTML(url); const html = await fetchHTML(url);
if(!html) return null;
const $ = cheerio.load(html); const $ = cheerio.load(html);
const body = $("body"); const body = $("body");
const mainPost = $(f95Selector.GS_POSTS).first(); const mainPost = $(f95Selector.GS_POSTS).first();
@ -99,7 +100,7 @@ function parseGamePrefixes(body) {
}); });
// If the status is not set, then the game in in development (Ongoing) // If the status is not set, then the game in in development (Ongoing)
if (!status) status = "Ongoing"; status = status ?? "Ongoing";
return { return {
engine, engine,
@ -144,7 +145,6 @@ function extractInfoFromTitle(body) {
// Remove the trailing "v" // Remove the trailing "v"
if (version[0] === "v") version = version.replace("v", ""); if (version[0] === "v") version = version.replace("v", "");
} }
else shared.logger.trace(`Malformed title: ${title}`);
// Last element (the regex [[\]]+ remove the square brackets) // Last element (the regex [[\]]+ remove the square brackets)
const author = matches[matches.length - 1].replace(/[[\]]+/g, "").trim(); const author = matches[matches.length - 1].replace(/[[\]]+/g, "").trim();
@ -293,6 +293,27 @@ function parseMainPostText(text) {
return parsedDict; return parsedDict;
} }
/**
* @private
* Parse a JSON-LD element.
* @param {cheerio.Element} element
*/
function parseScriptTag(element) {
// Get the element HTML
const html = cheerio.load(element).html().trim();
// Obtain the JSON-LD
const data = html
.replace("<script type=\"application/ld+json\">", "")
.replace("</script>", "");
// Convert the string to an object
const json = JSON.parse(data);
// Return only the data of the game
if (json["@type"] === "Book") return json;
}
/** /**
* @private * @private
* Extracts and processes the JSON-LD values found at the bottom of the page. * Extracts and processes the JSON-LD values found at the bottom of the page.
@ -301,22 +322,12 @@ function parseMainPostText(text) {
*/ */
function extractStructuredData(body) { function extractStructuredData(body) {
shared.logger.trace("Extracting JSON-LD data..."); shared.logger.trace("Extracting JSON-LD data...");
// Fetch the JSON-LD data
const structuredDataElements = body.find(f95Selector.GT_JSONLD); const structuredDataElements = body.find(f95Selector.GT_JSONLD);
const json = structuredDataElements.map(function parseScriptTag(idx, el) {
// Get the element HTML
const html = cheerio.load(el).html().trim();
// Obtain the JSON-LD // Parse the data
const data = html const json = structuredDataElements.map((idx, el) => parseScriptTag(el)).get();
.replace("<script type=\"application/ld+json\">", "")
.replace("</script>", "");
// Convert the string to an object
const json = JSON.parse(data);
// Return only the data of the game
if (json["@type"] === "Book") return json;
}).get();
return json.lenght !== 0 ? json[0] : null; return json.lenght !== 0 ? json[0] : null;
} }
@ -389,12 +400,8 @@ function extractIDFromURL(url) {
* @private * @private
* Makes an array of strings uppercase. * Makes an array of strings uppercase.
* @param {String[]} a * @param {String[]} a
* @returns {String[]}
*/ */
function toUpperCaseArray(a) { function toUpperCaseArray(a) {
// If the array is empty, return
if(a.length === 0) return [];
/** /**
* Makes a string uppercase. * Makes a string uppercase.
* @param {String} s * @param {String} s