2020-10-16 08:06:03 +00:00
|
|
|
"use strict";
|
2020-10-02 12:01:51 +00:00
|
|
|
|
2020-10-01 19:13:23 +00:00
|
|
|
// Public modules from npm
|
2020-10-16 08:06:03 +00:00
|
|
|
const HTMLParser = require("node-html-parser");
|
|
|
|
const puppeteer = require("puppeteer"); // skipcq: JS-0128
|
2020-10-01 19:13:23 +00:00
|
|
|
|
|
|
|
// Modules from file
|
2020-10-16 08:06:03 +00:00
|
|
|
const shared = require("./shared.js");
|
2020-10-21 13:31:11 +00:00
|
|
|
const selectorK = require("./constants/css-selector.js");
|
2020-10-16 08:06:03 +00:00
|
|
|
const { preparePage } = require("./puppeteer-helper.js");
|
|
|
|
const GameDownload = require("./classes/game-download.js");
|
|
|
|
const GameInfo = require("./classes/game-info.js");
|
2020-10-21 13:31:11 +00:00
|
|
|
const urlHelper = require("./url-helper.js");
|
2020-10-01 19:13:23 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @protected
|
|
|
|
* Get information from the game's main page.
|
|
|
|
* @param {puppeteer.Browser} browser Browser object used for navigation
|
2020-10-08 21:08:20 +00:00
|
|
|
* @param {String} url URL (String) of the game/mod to extract data from
|
2020-10-10 14:27:28 +00:00
|
|
|
* @return {Promise<GameInfo>} Complete information about the game you are
|
2020-10-21 13:31:11 +00:00
|
|
|
* looking for
|
2020-10-01 19:13:23 +00:00
|
|
|
*/
|
2020-10-03 14:42:29 +00:00
|
|
|
module.exports.getGameInfo = async function (browser, url) {
|
2020-10-21 13:31:11 +00:00
|
|
|
shared.logger.info("Obtaining game info");
|
2020-10-03 14:42:29 +00:00
|
|
|
|
|
|
|
// Verify the correctness of the URL
|
2020-10-21 13:31:11 +00:00
|
|
|
const exists = await urlHelper.urlExists(url);
|
|
|
|
if (!exists) throw new URIError(url + " is not a valid URL");
|
2020-10-21 13:31:39 +00:00
|
|
|
if (!urlHelper.isF95URL(url))
|
|
|
|
throw new Error(url + " is not a valid F95Zone URL");
|
2020-10-03 14:42:29 +00:00
|
|
|
|
2020-10-16 07:21:19 +00:00
|
|
|
const page = await preparePage(browser); // Set new isolated page
|
2020-10-03 14:42:29 +00:00
|
|
|
await page.setCookie(...shared.cookies); // Set cookies to avoid login
|
2020-10-08 21:08:20 +00:00
|
|
|
await page.goto(url, {
|
2020-10-16 08:06:03 +00:00
|
|
|
waitUntil: shared.WAIT_STATEMENT,
|
2020-10-03 14:42:29 +00:00
|
|
|
}); // Go to the game page and wait until it loads
|
|
|
|
|
|
|
|
// It asynchronously searches for the elements and
|
|
|
|
// then waits at the end to compile the object to be returned
|
|
|
|
let info = new GameInfo();
|
2020-10-16 07:21:19 +00:00
|
|
|
const title = getGameTitle(page);
|
|
|
|
const author = getGameAuthor(page);
|
|
|
|
const tags = getGameTags(page);
|
2020-10-21 13:31:11 +00:00
|
|
|
const redirectUrl = urlHelper.getUrlRedirect(url);
|
2020-10-03 14:42:29 +00:00
|
|
|
info = await parsePrefixes(page, info); // Fill status/engines/isMod
|
2020-10-16 07:21:19 +00:00
|
|
|
const structuredText = await getMainPostStructuredText(page);
|
|
|
|
const overview = getOverview(structuredText, info.isMod);
|
2020-10-22 12:08:29 +00:00
|
|
|
|
2020-10-16 07:21:19 +00:00
|
|
|
const parsedInfos = parseConversationPage(structuredText);
|
2020-10-20 20:47:36 +00:00
|
|
|
const previewSource = getGamePreviewSource(page);
|
2020-10-16 07:21:19 +00:00
|
|
|
const changelog = getLastChangelog(page);
|
2020-10-03 14:42:29 +00:00
|
|
|
|
|
|
|
// Fill in the GameInfo element with the information obtained
|
|
|
|
info.name = await title;
|
|
|
|
info.author = await author;
|
|
|
|
info.tags = await tags;
|
2020-10-20 20:47:36 +00:00
|
|
|
info.f95url = await redirectUrl;
|
|
|
|
info.overview = overview;
|
2020-10-03 14:42:29 +00:00
|
|
|
info.lastUpdate = info.isMod
|
2020-10-16 07:45:33 +00:00
|
|
|
? parsedInfos.UPDATED
|
2020-10-16 07:52:20 +00:00
|
|
|
: parsedInfos.THREAD_UPDATED;
|
2020-10-03 14:42:29 +00:00
|
|
|
info.previewSource = await previewSource;
|
2020-10-21 13:31:11 +00:00
|
|
|
info.changelog = await changelog;
|
2020-10-22 12:08:01 +00:00
|
|
|
info.version = await exports.getGameVersionFromTitle(browser, info);
|
2020-10-20 20:47:36 +00:00
|
|
|
|
|
|
|
//let downloadData = getGameDownloadLink(page);
|
2020-10-10 14:27:01 +00:00
|
|
|
//info.downloadInfo = await downloadData;
|
2020-10-10 14:27:28 +00:00
|
|
|
/* Downloading games without going directly to
|
|
|
|
* the platform appears to be prohibited by
|
|
|
|
* the guidelines. It is therefore useless to
|
|
|
|
* keep the links for downloading the games. */
|
2020-10-03 14:42:29 +00:00
|
|
|
|
|
|
|
await page.close(); // Close the page
|
2020-10-21 13:31:11 +00:00
|
|
|
shared.logger.info("Founded data for " + info.name);
|
2020-10-03 14:42:29 +00:00
|
|
|
return info;
|
|
|
|
};
|
2020-10-01 19:13:23 +00:00
|
|
|
|
2020-10-12 12:11:36 +00:00
|
|
|
/**
|
|
|
|
* Obtain the game version without parsing again all the data of the game.
|
|
|
|
* @param {puppeteer.Browser} browser Browser object used for navigation
|
|
|
|
* @param {GameInfo} info Information about the game
|
|
|
|
* @returns {Promise<String>} Online version of the game
|
|
|
|
*/
|
2020-10-12 12:12:29 +00:00
|
|
|
module.exports.getGameVersionFromTitle = async function (browser, info) {
|
2020-10-16 07:21:19 +00:00
|
|
|
const page = await preparePage(browser); // Set new isolated page
|
2020-10-12 12:11:36 +00:00
|
|
|
await page.setCookie(...shared.cookies); // Set cookies to avoid login
|
|
|
|
await page.goto(info.f95url, {
|
2020-10-16 08:06:03 +00:00
|
|
|
waitUntil: shared.WAIT_STATEMENT,
|
2020-10-12 12:11:36 +00:00
|
|
|
}); // Go to the game page and wait until it loads
|
|
|
|
|
|
|
|
// Get the title
|
2020-10-16 07:21:19 +00:00
|
|
|
const titleHTML = await page.evaluate(
|
2020-10-12 12:11:36 +00:00
|
|
|
/* istanbul ignore next */
|
2020-10-12 12:12:29 +00:00
|
|
|
(selector) => document.querySelector(selector).innerHTML,
|
2020-10-21 13:31:11 +00:00
|
|
|
selectorK.GAME_TITLE
|
2020-10-12 12:11:36 +00:00
|
|
|
);
|
2020-10-16 07:21:19 +00:00
|
|
|
const title = HTMLParser.parse(titleHTML).childNodes.pop().rawText;
|
2020-10-12 12:11:36 +00:00
|
|
|
|
|
|
|
// The title is in the following format: [PREFIXES] NAME GAME [VERSION] [AUTHOR]
|
2020-10-16 08:06:03 +00:00
|
|
|
const startIndex = title.indexOf("[") + 1;
|
|
|
|
const endIndex = title.indexOf("]", startIndex);
|
2020-10-12 12:11:36 +00:00
|
|
|
let version = title.substring(startIndex, endIndex).trim().toUpperCase();
|
2020-10-16 08:06:03 +00:00
|
|
|
if (version.startsWith("V")) version = version.replace("V", ""); // Replace only the first occurrence
|
2020-10-22 11:50:58 +00:00
|
|
|
await page.close();
|
2020-10-22 12:06:23 +00:00
|
|
|
return cleanFSString(version);
|
2020-10-12 12:12:29 +00:00
|
|
|
};
|
2020-10-12 12:11:36 +00:00
|
|
|
|
2020-10-01 19:13:23 +00:00
|
|
|
//#region Private methods
|
2020-10-22 11:48:26 +00:00
|
|
|
/**
|
|
|
|
* Clean a string from invalid File System chars.
|
2020-10-22 12:08:29 +00:00
|
|
|
* @param {String} s
|
2020-10-22 11:48:26 +00:00
|
|
|
* @returns {String}
|
|
|
|
*/
|
|
|
|
function cleanFSString(s) {
|
|
|
|
const rx = /[/\\?%*:|"<>]/g;
|
|
|
|
return s.replace(rx, "");
|
|
|
|
}
|
|
|
|
|
2020-10-01 19:13:23 +00:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
* Get the game description from its web page.
|
|
|
|
* Different processing depending on whether the game is a mod or not.
|
|
|
|
* @param {String} text Structured text extracted from the game's web page
|
|
|
|
* @param {Boolean} isMod Specify if it is a game or a mod
|
|
|
|
* @returns {Promise<String>} Game description
|
|
|
|
*/
|
|
|
|
function getOverview(text, isMod) {
|
2020-10-03 14:42:29 +00:00
|
|
|
// Get overview (different parsing for game and mod)
|
|
|
|
let overviewEndIndex;
|
2020-10-16 08:06:03 +00:00
|
|
|
if (isMod) overviewEndIndex = text.indexOf("Updated");
|
|
|
|
else overviewEndIndex = text.indexOf("Thread Updated");
|
|
|
|
return text.substring(0, overviewEndIndex).replace("Overview:\n", "").trim();
|
2020-10-01 19:13:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @private
|
2020-10-03 14:42:29 +00:00
|
|
|
* Extrapolate the page structure by removing the element tags
|
2020-10-01 19:13:23 +00:00
|
|
|
* and leaving only the text and its spacing.
|
|
|
|
* @param {puppeteer.Page} page Page containing the text
|
|
|
|
* @returns {Promise<String>} Structured text
|
|
|
|
*/
|
|
|
|
async function getMainPostStructuredText(page) {
|
2020-10-03 14:42:29 +00:00
|
|
|
// Gets the first post, where are listed all the game's informations
|
2020-10-21 13:31:11 +00:00
|
|
|
const post = (await page.$$(selectorK.THREAD_POSTS))[0];
|
2020-10-03 14:42:29 +00:00
|
|
|
|
|
|
|
// The info are plain text so we need to parse the HTML code
|
2020-10-16 07:21:19 +00:00
|
|
|
const bodyHTML = await page.evaluate(
|
2020-10-16 07:45:58 +00:00
|
|
|
/* istanbul ignore next */
|
|
|
|
(mainPost) => mainPost.innerHTML,
|
2020-10-03 14:42:29 +00:00
|
|
|
post
|
|
|
|
);
|
|
|
|
return HTMLParser.parse(bodyHTML).structuredText;
|
2020-10-01 19:13:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
* Extrapolates and cleans the author from the page passed by parameter.
|
|
|
|
* @param {puppeteer.Page} page Page containing the author to be extrapolated
|
|
|
|
* @returns {Promise<String>} Game author
|
|
|
|
*/
|
|
|
|
async function getGameAuthor(page) {
|
2020-10-03 14:42:29 +00:00
|
|
|
// Get the game/mod name (without square brackets)
|
2020-10-16 07:21:19 +00:00
|
|
|
const titleHTML = await page.evaluate(
|
2020-10-16 07:45:58 +00:00
|
|
|
/* istanbul ignore next */
|
2020-10-16 07:49:16 +00:00
|
|
|
(selector) => document.querySelector(selector).innerHTML,
|
2020-10-21 13:31:11 +00:00
|
|
|
selectorK.GAME_TITLE
|
2020-10-03 14:42:29 +00:00
|
|
|
);
|
2020-10-16 07:21:19 +00:00
|
|
|
const structuredTitle = HTMLParser.parse(titleHTML);
|
2020-10-03 14:42:29 +00:00
|
|
|
|
|
|
|
// The last element **shoud be** the title without prefixes (engines, status, other...)
|
2020-10-16 07:21:19 +00:00
|
|
|
const gameTitle = structuredTitle.childNodes.pop().rawText;
|
2020-10-03 14:42:29 +00:00
|
|
|
|
|
|
|
// The last square brackets contain the author
|
2020-10-16 08:06:03 +00:00
|
|
|
const startTitleIndex = gameTitle.lastIndexOf("[") + 1;
|
2020-10-03 14:42:29 +00:00
|
|
|
return gameTitle.substring(startTitleIndex, gameTitle.length - 1).trim();
|
2020-10-01 19:13:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @private
|
2020-10-03 14:42:29 +00:00
|
|
|
* Process the post text to get all the useful
|
2020-10-01 19:13:23 +00:00
|
|
|
* information in the format *DESCRIPTOR : VALUE*.
|
|
|
|
* @param {String} text Structured text of the post
|
|
|
|
* @returns {Object} Dictionary of information
|
|
|
|
*/
|
|
|
|
function parseConversationPage(text) {
|
2020-10-16 07:21:19 +00:00
|
|
|
const dataPairs = {};
|
2020-10-01 19:13:23 +00:00
|
|
|
|
2020-10-03 14:42:29 +00:00
|
|
|
// The information searched in the game post are one per line
|
2020-10-16 08:06:03 +00:00
|
|
|
const splittedText = text.split("\n");
|
2020-10-16 07:21:19 +00:00
|
|
|
for (const line of splittedText) {
|
2020-10-16 08:06:03 +00:00
|
|
|
if (!line.includes(":")) continue;
|
2020-10-01 19:13:23 +00:00
|
|
|
|
2020-10-03 14:42:29 +00:00
|
|
|
// Create pair key/value
|
2020-10-16 08:06:03 +00:00
|
|
|
const splitted = line.split(":");
|
2020-10-21 13:31:11 +00:00
|
|
|
const key = splitted[0].trim().toUpperCase().replace(/ /g, "_"); // Uppercase to avoid mismatch
|
2020-10-16 07:21:19 +00:00
|
|
|
const value = splitted[1].trim();
|
2020-10-01 19:13:23 +00:00
|
|
|
|
2020-10-03 14:42:29 +00:00
|
|
|
// Add pair to the dict if valid
|
2020-10-16 08:06:03 +00:00
|
|
|
if (value !== "") dataPairs[key] = value;
|
2020-10-03 14:42:29 +00:00
|
|
|
}
|
2020-10-01 19:13:23 +00:00
|
|
|
|
2020-10-03 14:42:29 +00:00
|
|
|
return dataPairs;
|
2020-10-01 19:13:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
* Gets the URL of the image used as a preview for the game in the conversation.
|
|
|
|
* @param {puppeteer.Page} page Page containing the URL to be extrapolated
|
2020-10-21 20:35:48 +00:00
|
|
|
* @returns {Promise<String>} URL (String) of the image or null if failed to get it
|
2020-10-01 19:13:23 +00:00
|
|
|
*/
|
|
|
|
async function getGamePreviewSource(page) {
|
2020-10-21 20:35:48 +00:00
|
|
|
// Wait for the selector or return an empty value
|
|
|
|
try {
|
|
|
|
await page.waitForSelector(selectorK.GAME_IMAGES);
|
|
|
|
} catch {
|
|
|
|
return null;
|
|
|
|
}
|
2020-10-21 20:36:13 +00:00
|
|
|
|
2020-10-16 07:21:19 +00:00
|
|
|
const src = await page.evaluate(
|
2020-10-16 07:45:58 +00:00
|
|
|
/* istanbul ignore next */
|
|
|
|
(selector) => {
|
2020-10-03 14:42:29 +00:00
|
|
|
// Get the firs image available
|
2020-10-16 07:21:19 +00:00
|
|
|
const img = document.querySelector(selector);
|
2020-10-21 13:31:39 +00:00
|
|
|
|
2020-10-16 08:06:03 +00:00
|
|
|
if (img) return img.getAttribute("src");
|
2020-10-03 14:42:29 +00:00
|
|
|
else return null;
|
|
|
|
},
|
2020-10-21 13:31:11 +00:00
|
|
|
selectorK.GAME_IMAGES
|
2020-10-03 14:42:29 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// Check if the URL is valid
|
2020-10-21 20:35:48 +00:00
|
|
|
return urlHelper.isStringAValidURL(src) ? src : null;
|
2020-10-01 19:13:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
* Extrapolates and cleans the title from the page passed by parameter.
|
|
|
|
* @param {puppeteer.Page} page Page containing the title to be extrapolated
|
|
|
|
* @returns {Promise<String>} Game title
|
|
|
|
*/
|
|
|
|
async function getGameTitle(page) {
|
2020-10-03 14:42:29 +00:00
|
|
|
// Get the game/mod name (without square brackets)
|
2020-10-16 07:21:19 +00:00
|
|
|
const titleHTML = await page.evaluate(
|
2020-10-16 07:45:58 +00:00
|
|
|
/* istanbul ignore next */
|
2020-10-16 07:49:16 +00:00
|
|
|
(selector) => document.querySelector(selector).innerHTML,
|
2020-10-21 13:31:11 +00:00
|
|
|
selectorK.GAME_TITLE
|
2020-10-03 14:42:29 +00:00
|
|
|
);
|
2020-10-16 07:21:19 +00:00
|
|
|
const structuredTitle = HTMLParser.parse(titleHTML);
|
2020-10-03 14:42:29 +00:00
|
|
|
|
|
|
|
// The last element **shoud be** the title without prefixes (engines, status, other...)
|
2020-10-16 07:21:19 +00:00
|
|
|
const gameTitle = structuredTitle.childNodes.pop().rawText;
|
2020-10-16 08:06:03 +00:00
|
|
|
const endTitleIndex = gameTitle.indexOf("[");
|
2020-10-03 14:42:29 +00:00
|
|
|
return gameTitle.substring(0, endTitleIndex).trim();
|
2020-10-01 19:13:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
* Get the alphabetically sorted list of tags associated with the game.
|
|
|
|
* @param {puppeteer.Page} page Page containing the tags to be extrapolated
|
|
|
|
* @returns {Promise<String[]>} List of uppercase tags
|
|
|
|
*/
|
|
|
|
async function getGameTags(page) {
|
2020-10-16 07:21:19 +00:00
|
|
|
const tags = [];
|
2020-10-03 14:42:29 +00:00
|
|
|
|
|
|
|
// Get the game tags
|
2020-10-21 13:31:11 +00:00
|
|
|
for (const handle of await page.$$(selectorK.GAME_TAGS)) {
|
2020-10-16 07:21:19 +00:00
|
|
|
const tag = await page.evaluate(
|
2020-10-16 07:45:58 +00:00
|
|
|
/* istanbul ignore next */
|
|
|
|
(element) => element.innerText,
|
2020-10-03 14:42:29 +00:00
|
|
|
handle
|
|
|
|
);
|
|
|
|
tags.push(tag.toUpperCase());
|
|
|
|
}
|
|
|
|
return tags.sort();
|
2020-10-01 19:13:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @private
|
2020-10-03 14:42:29 +00:00
|
|
|
* Process the game title prefixes to extract information such as game status,
|
2020-10-01 19:13:23 +00:00
|
|
|
* graphics engine used, and whether it is a mod or original game.
|
|
|
|
* @param {puppeteer.Page} page Page containing the prefixes to be extrapolated
|
|
|
|
* @param {GameInfo} info Object to assign the identified information to
|
|
|
|
* @returns {Promise<GameInfo>} GameInfo object passed in to which the identified information has been added
|
|
|
|
*/
|
|
|
|
async function parsePrefixes(page, info) {
|
2020-10-16 08:06:03 +00:00
|
|
|
const MOD_PREFIX = "MOD";
|
2020-10-03 14:42:29 +00:00
|
|
|
|
|
|
|
// The 'Ongoing' status is not specified, only 'Abandoned'/'OnHold'/'Complete'
|
2020-10-16 08:06:03 +00:00
|
|
|
info.status = "Ongoing";
|
2020-10-21 13:31:11 +00:00
|
|
|
for (const handle of await page.$$(selectorK.GAME_TITLE_PREFIXES)) {
|
2020-10-16 07:21:19 +00:00
|
|
|
const value = await page.evaluate(
|
2020-10-16 07:45:58 +00:00
|
|
|
/* istanbul ignore next */
|
|
|
|
(element) => element.innerText,
|
2020-10-03 14:42:29 +00:00
|
|
|
handle
|
|
|
|
);
|
|
|
|
|
|
|
|
// Clean the prefix
|
2020-10-16 08:06:03 +00:00
|
|
|
const prefix = value.toUpperCase().replace("[", "").replace("]", "").trim();
|
2020-10-21 13:31:39 +00:00
|
|
|
|
2020-10-03 14:42:29 +00:00
|
|
|
// Getting infos...
|
2020-10-21 13:31:11 +00:00
|
|
|
if (shared.statuses.includes(prefix)) info.status = capitalize(prefix);
|
|
|
|
else if (shared.engines.includes(prefix)) info.engine = capitalize(prefix);
|
2020-10-03 14:42:29 +00:00
|
|
|
// This is not a game but a mod
|
|
|
|
else if (prefix === MOD_PREFIX) info.isMod = true;
|
|
|
|
}
|
|
|
|
return info;
|
2020-10-01 19:13:23 +00:00
|
|
|
}
|
|
|
|
|
2020-10-14 14:04:50 +00:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
* Get the last changelog available for the game.
|
|
|
|
* @param {puppeteer.Page} page Page containing the changelog
|
2020-10-21 13:31:11 +00:00
|
|
|
* @returns {Promise<String>} Changelog for the last version or a empty string if no changelog is found
|
2020-10-14 14:04:50 +00:00
|
|
|
*/
|
|
|
|
async function getLastChangelog(page) {
|
|
|
|
// Gets the first post, where are listed all the game's informations
|
2020-10-21 13:31:11 +00:00
|
|
|
const post = (await page.$$(selectorK.THREAD_POSTS))[0];
|
2020-10-15 15:09:29 +00:00
|
|
|
|
2020-10-21 13:31:11 +00:00
|
|
|
const spoiler = await post.$(selectorK.THREAD_LAST_CHANGELOG);
|
|
|
|
if (!spoiler) return "";
|
2020-10-15 14:47:52 +00:00
|
|
|
|
2020-10-16 07:21:19 +00:00
|
|
|
const changelogHTML = await page.evaluate(
|
2020-10-14 14:04:50 +00:00
|
|
|
/* istanbul ignore next */
|
|
|
|
(e) => e.innerText,
|
|
|
|
spoiler
|
|
|
|
);
|
2020-10-21 13:31:11 +00:00
|
|
|
let parsedText = HTMLParser.parse(changelogHTML).structuredText;
|
|
|
|
|
|
|
|
// Clean the text
|
2020-10-21 13:31:39 +00:00
|
|
|
if (parsedText.startsWith("Spoiler"))
|
|
|
|
parsedText = parsedText.replace("Spoiler", "");
|
2020-10-21 13:31:11 +00:00
|
|
|
if (parsedText.startsWith(":")) parsedText = parsedText.replace(":", "");
|
|
|
|
return parsedText.trim();
|
2020-10-14 14:04:50 +00:00
|
|
|
}
|
|
|
|
|
2020-10-01 19:13:23 +00:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
* Get game download links for different platforms.
|
|
|
|
* @param {puppeteer.Page} page Page containing the links to be extrapolated
|
|
|
|
* @returns {Promise<GameDownload[]>} List of objects used for game download
|
2020-10-21 13:31:11 +00:00
|
|
|
* @deprecated
|
2020-10-01 19:13:23 +00:00
|
|
|
*/
|
2020-10-21 13:31:11 +00:00
|
|
|
/* istanbul ignore next */
|
2020-10-21 13:36:50 +00:00
|
|
|
// skipcq: JS-0128
|
2020-10-01 19:13:23 +00:00
|
|
|
async function getGameDownloadLink(page) {
|
2020-10-03 14:42:29 +00:00
|
|
|
// Most used hosting platforms
|
2020-10-16 07:21:19 +00:00
|
|
|
const hostingPlatforms = [
|
2020-10-16 08:06:03 +00:00
|
|
|
"MEGA",
|
|
|
|
"NOPY",
|
|
|
|
"FILESUPLOAD",
|
|
|
|
"MIXDROP",
|
|
|
|
"UPLOADHAVEN",
|
|
|
|
"PIXELDRAIN",
|
|
|
|
"FILESFM",
|
2020-10-03 14:42:29 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
// Supported OS platforms
|
2020-10-16 08:06:03 +00:00
|
|
|
const platformOS = ["WIN", "LINUX", "MAC", "ALL"];
|
2020-10-03 14:42:29 +00:00
|
|
|
|
|
|
|
// Gets the <span> which contains the download links
|
2020-10-21 13:31:11 +00:00
|
|
|
const temp = await page.$$(selectorK.DOWNLOAD_LINKS_CONTAINER);
|
2020-10-03 14:42:29 +00:00
|
|
|
if (temp.length === 0) return [];
|
|
|
|
|
|
|
|
// Look for the container that contains the links
|
|
|
|
// It is necessary because the same css selector
|
|
|
|
// also identifies other elements on the page
|
|
|
|
let container = null;
|
2020-10-16 07:21:19 +00:00
|
|
|
for (const candidate of temp) {
|
2020-10-03 14:42:29 +00:00
|
|
|
if (container !== null) break;
|
2020-10-16 07:21:19 +00:00
|
|
|
const upperText = (
|
2020-10-03 14:42:29 +00:00
|
|
|
await page.evaluate(
|
2020-10-16 07:45:58 +00:00
|
|
|
/* istanbul ignore next */
|
|
|
|
(e) => e.innerText,
|
2020-10-03 14:42:29 +00:00
|
|
|
candidate
|
|
|
|
)
|
|
|
|
).toUpperCase();
|
|
|
|
|
|
|
|
// Search if the container contains the name of a hosting platform
|
2020-10-16 07:21:19 +00:00
|
|
|
for (const p of hostingPlatforms) {
|
2020-10-03 14:42:29 +00:00
|
|
|
if (upperText.includes(p)) {
|
|
|
|
container = candidate;
|
|
|
|
break;
|
|
|
|
}
|
2020-10-01 19:13:23 +00:00
|
|
|
}
|
2020-10-03 14:42:29 +00:00
|
|
|
}
|
|
|
|
if (container === null) return [];
|
|
|
|
|
|
|
|
// Extract the HTML text from the container
|
2020-10-16 07:21:19 +00:00
|
|
|
const searchText = (
|
2020-10-03 14:42:29 +00:00
|
|
|
await page.evaluate(
|
2020-10-16 07:45:58 +00:00
|
|
|
/* istanbul ignore next */
|
|
|
|
(e) => e.innerHTML,
|
2020-10-03 14:42:29 +00:00
|
|
|
container
|
|
|
|
)
|
|
|
|
).toLowerCase();
|
|
|
|
|
|
|
|
// Parse the download links
|
2020-10-16 07:21:19 +00:00
|
|
|
const downloadData = [];
|
|
|
|
for (const platform of platformOS) {
|
|
|
|
const data = extractGameHostingData(platform, searchText);
|
2020-10-03 14:42:29 +00:00
|
|
|
downloadData.push(...data);
|
|
|
|
}
|
|
|
|
return downloadData;
|
2020-10-01 19:13:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
* From the HTML text it extracts the game download links for the specified operating system.
|
|
|
|
* @param {String} platform Name of the operating system to look for a compatible link to.
|
|
|
|
* It can only be *WIN/LINUX/MAC/ALL*
|
|
|
|
* @param {String} text HTML string to extract links from
|
|
|
|
* @returns {GameDownload[]} List of game download links for the selected platform
|
2020-10-21 13:31:11 +00:00
|
|
|
* @deprecated
|
2020-10-01 19:13:23 +00:00
|
|
|
*/
|
2020-10-21 13:31:11 +00:00
|
|
|
/* istanbul ignore next */
|
2020-10-01 19:13:23 +00:00
|
|
|
function extractGameHostingData(platform, text) {
|
2020-10-16 08:06:03 +00:00
|
|
|
const PLATFORM_BOLD_OPEN = "<b>";
|
|
|
|
const CONTAINER_SPAN_CLOSE = "</span>";
|
|
|
|
const LINK_OPEN = "<a";
|
|
|
|
const LINK_CLOSE = "</a>";
|
|
|
|
const HREF_START = "href='";
|
|
|
|
const HREF_END = "'";
|
|
|
|
const TAG_CLOSE = ">";
|
2020-10-03 14:42:29 +00:00
|
|
|
|
|
|
|
// Identify the individual platforms
|
|
|
|
let startIndex = text.indexOf(platform.toLowerCase());
|
|
|
|
if (startIndex === -1) return [];
|
|
|
|
else startIndex += platform.length;
|
|
|
|
|
|
|
|
// Find the <b>platform</b>
|
|
|
|
let endIndex =
|
|
|
|
text.indexOf(PLATFORM_BOLD_OPEN, startIndex) + PLATFORM_BOLD_OPEN.length;
|
|
|
|
|
|
|
|
// Find the end of the container
|
|
|
|
if (endIndex === -1)
|
2020-10-16 07:27:28 +00:00
|
|
|
endIndex =
|
2020-10-16 07:49:16 +00:00
|
|
|
text.indexOf(CONTAINER_SPAN_CLOSE, startIndex) +
|
|
|
|
CONTAINER_SPAN_CLOSE.length;
|
2020-10-03 14:42:29 +00:00
|
|
|
|
|
|
|
text = text.substring(startIndex, endIndex);
|
|
|
|
|
2020-10-16 07:21:19 +00:00
|
|
|
const downloadData = [];
|
|
|
|
const linkTags = text.split(LINK_OPEN);
|
|
|
|
for (const tag of linkTags) {
|
2020-10-03 14:42:29 +00:00
|
|
|
// Ignore non-link string
|
|
|
|
if (!tag.includes(HREF_START)) continue;
|
|
|
|
|
|
|
|
// Find the hosting platform name
|
|
|
|
startIndex = tag.indexOf(TAG_CLOSE) + TAG_CLOSE.length;
|
|
|
|
endIndex = tag.indexOf(LINK_CLOSE, startIndex);
|
2020-10-16 07:21:19 +00:00
|
|
|
const hosting = tag.substring(startIndex, endIndex);
|
2020-10-03 14:42:29 +00:00
|
|
|
|
|
|
|
// Find the 'href' attribute
|
|
|
|
startIndex = tag.indexOf(HREF_START) + HREF_START.length;
|
|
|
|
endIndex = tag.indexOf(HREF_END, startIndex);
|
2020-10-16 07:21:19 +00:00
|
|
|
const link = tag.substring(startIndex, endIndex);
|
2020-10-03 14:42:29 +00:00
|
|
|
|
2020-10-21 13:31:11 +00:00
|
|
|
if (urlHelper.isStringAValidURL(link)) {
|
2020-10-16 07:21:19 +00:00
|
|
|
const gd = new GameDownload();
|
2020-10-03 14:42:29 +00:00
|
|
|
gd.hosting = hosting.toUpperCase();
|
2020-10-08 21:08:20 +00:00
|
|
|
gd.link = link;
|
2020-10-03 14:42:29 +00:00
|
|
|
gd.supportedOS = platform.toUpperCase();
|
|
|
|
|
|
|
|
downloadData.push(gd);
|
2020-10-01 19:13:23 +00:00
|
|
|
}
|
2020-10-03 14:42:29 +00:00
|
|
|
}
|
|
|
|
return downloadData;
|
2020-10-01 19:13:23 +00:00
|
|
|
}
|
2020-10-21 13:31:11 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Capitalize a string
|
2020-10-21 13:31:39 +00:00
|
|
|
* @param {String} string
|
2020-10-21 13:31:11 +00:00
|
|
|
*/
|
|
|
|
function capitalize(string) {
|
2020-10-21 20:35:48 +00:00
|
|
|
return string.toLowerCase().charAt(0).toUpperCase() + string.slice(1);
|
2020-10-21 13:31:11 +00:00
|
|
|
}
|
2020-10-03 14:42:29 +00:00
|
|
|
//#endregion Private methods
|