Style fix

pull/44/head
samuele.berlusconi 2020-10-29 22:14:40 +01:00
parent 08b23b0c6f
commit f7dd2d8e4e
14 changed files with 1072 additions and 1071 deletions

View File

@ -10,8 +10,8 @@ const selectorK = require("./scripts/constants/css-selector.js");
const urlHelper = require("./scripts/url-helper.js"); const urlHelper = require("./scripts/url-helper.js");
const scraper = require("./scripts/game-scraper.js"); const scraper = require("./scripts/game-scraper.js");
const { const {
prepareBrowser, prepareBrowser,
preparePage, preparePage,
} = require("./scripts/puppeteer-helper.js"); } = require("./scripts/puppeteer-helper.js");
const searcher = require("./scripts/game-searcher.js"); const searcher = require("./scripts/game-searcher.js");
@ -32,10 +32,10 @@ module.exports.UserData = UserData;
* @param {Boolean} value * @param {Boolean} value
*/ */
module.exports.debug = function (value) { module.exports.debug = function (value) {
shared.debug = value; shared.debug = value;
// Configure logger // Configure logger
shared.logger.level = value ? "debug" : "warn"; shared.logger.level = value ? "debug" : "warn";
}; };
/** /**
* @public * @public
@ -43,7 +43,7 @@ module.exports.debug = function (value) {
* @returns {String} * @returns {String}
*/ */
module.exports.isLogged = function () { module.exports.isLogged = function () {
return shared.isLogged; return shared.isLogged;
}; };
/** /**
* @public * @public
@ -52,7 +52,7 @@ module.exports.isLogged = function () {
* @returns {String} * @returns {String}
*/ */
module.exports.setIsolation = function (value) { module.exports.setIsolation = function (value) {
shared.isolation = value; shared.isolation = value;
}; };
/** /**
* @public * @public
@ -60,7 +60,7 @@ module.exports.setIsolation = function (value) {
* @returns {String} * @returns {String}
*/ */
module.exports.getCacheDir = function () { module.exports.getCacheDir = function () {
return shared.cacheDir; return shared.cacheDir;
}; };
/** /**
* @public * @public
@ -68,10 +68,10 @@ module.exports.getCacheDir = function () {
* @returns {String} * @returns {String}
*/ */
module.exports.setCacheDir = function (value) { module.exports.setCacheDir = function (value) {
shared.cacheDir = value; shared.cacheDir = value;
// Create directory if it doesn't exist // Create directory if it doesn't exist
if (!fs.existsSync(shared.cacheDir)) fs.mkdirSync(shared.cacheDir); if (!fs.existsSync(shared.cacheDir)) fs.mkdirSync(shared.cacheDir);
}; };
/** /**
* @public * @public
@ -79,7 +79,7 @@ module.exports.setCacheDir = function (value) {
* @returns {String} * @returns {String}
*/ */
module.exports.setChromiumPath = function (value) { module.exports.setChromiumPath = function (value) {
shared.chromiumLocalPath = value; shared.chromiumLocalPath = value;
}; };
//#endregion Export properties //#endregion Export properties
@ -98,41 +98,41 @@ const USER_NOT_LOGGED = "User not authenticated, unable to continue";
* @returns {Promise<LoginResult>} Result of the operation * @returns {Promise<LoginResult>} Result of the operation
*/ */
module.exports.login = async function (username, password) { module.exports.login = async function (username, password) {
if (shared.isLogged) { if (shared.isLogged) {
shared.logger.info("Already logged in"); shared.logger.info("Already logged in");
const result = new LoginResult(true, "Already logged in"); const result = new LoginResult(true, "Already logged in");
return result; return result;
} }
// If cookies are loaded, use them to authenticate // If cookies are loaded, use them to authenticate
shared.cookies = loadCookies();
if (shared.cookies !== null) {
shared.logger.info("Valid session, no need to re-authenticate");
shared.isLogged = true;
const result = new LoginResult(true, "Logged with cookies");
return result;
}
// Else, log in throught browser
shared.logger.info(
"No saved sessions or expired session, login on the platform"
);
if (_browser === null && !shared.isolation) _browser = await prepareBrowser();
const browser = shared.isolation ? await prepareBrowser() : _browser;
const result = await loginF95(browser, username, password);
shared.isLogged = result.success;
if (result.success) {
// Reload cookies
shared.cookies = loadCookies(); shared.cookies = loadCookies();
shared.logger.info("User logged in through the platform"); if (shared.cookies !== null) {
} else { shared.logger.info("Valid session, no need to re-authenticate");
shared.logger.warn("Error during authentication: " + result.message); shared.isLogged = true;
} const result = new LoginResult(true, "Logged with cookies");
if (shared.isolation) await browser.close(); return result;
return result; }
// Else, log in throught browser
shared.logger.info(
"No saved sessions or expired session, login on the platform"
);
if (_browser === null && !shared.isolation) _browser = await prepareBrowser();
const browser = shared.isolation ? await prepareBrowser() : _browser;
const result = await loginF95(browser, username, password);
shared.isLogged = result.success;
if (result.success) {
// Reload cookies
shared.cookies = loadCookies();
shared.logger.info("User logged in through the platform");
} else {
shared.logger.warn("Error during authentication: " + result.message);
}
if (shared.isolation) await browser.close();
return result;
}; };
/** /**
* @public * @public
@ -142,47 +142,47 @@ module.exports.login = async function (username, password) {
* @returns {Promise<Boolean>} Result of the operation * @returns {Promise<Boolean>} Result of the operation
*/ */
module.exports.loadF95BaseData = async function () { module.exports.loadF95BaseData = async function () {
if (!shared.isLogged || !shared.cookies) { if (!shared.isLogged || !shared.cookies) {
shared.logger.warn(USER_NOT_LOGGED); shared.logger.warn(USER_NOT_LOGGED);
return false; return false;
} }
shared.logger.info("Loading base data..."); shared.logger.info("Loading base data...");
// Prepare a new web page // Prepare a new web page
if (_browser === null && !shared.isolation) _browser = await prepareBrowser(); if (_browser === null && !shared.isolation) _browser = await prepareBrowser();
const browser = shared.isolation ? await prepareBrowser() : _browser; const browser = shared.isolation ? await prepareBrowser() : _browser;
const page = await preparePage(browser); // Set new isolated page const page = await preparePage(browser); // Set new isolated page
await page.setCookie(...shared.cookies); // Set cookies to avoid login await page.setCookie(...shared.cookies); // Set cookies to avoid login
// Go to latest update page and wait for it to load // Go to latest update page and wait for it to load
await page.goto(urlK.F95_LATEST_UPDATES, { await page.goto(urlK.F95_LATEST_UPDATES, {
waitUntil: shared.WAIT_STATEMENT, waitUntil: shared.WAIT_STATEMENT,
}); });
// Obtain engines (disk/online) // Obtain engines (disk/online)
await page.waitForSelector(selectorK.ENGINE_ID_SELECTOR); await page.waitForSelector(selectorK.ENGINE_ID_SELECTOR);
shared.engines = await loadValuesFromLatestPage( shared.engines = await loadValuesFromLatestPage(
page, page,
shared.enginesCachePath, shared.enginesCachePath,
selectorK.ENGINE_ID_SELECTOR, selectorK.ENGINE_ID_SELECTOR,
"engines" "engines"
); );
// Obtain statuses (disk/online) // Obtain statuses (disk/online)
await page.waitForSelector(selectorK.STATUS_ID_SELECTOR); await page.waitForSelector(selectorK.STATUS_ID_SELECTOR);
shared.statuses = await loadValuesFromLatestPage( shared.statuses = await loadValuesFromLatestPage(
page, page,
shared.statusesCachePath, shared.statusesCachePath,
selectorK.STATUS_ID_SELECTOR, selectorK.STATUS_ID_SELECTOR,
"statuses" "statuses"
); );
await page.close(); await page.close();
if (shared.isolation) await browser.close(); if (shared.isolation) await browser.close();
shared.logger.info("Base data loaded"); shared.logger.info("Base data loaded");
return true; return true;
}; };
/** /**
* @public * @public
@ -192,25 +192,25 @@ module.exports.loadF95BaseData = async function () {
* @returns {Promise<Boolean>} true if an update is available, false otherwise * @returns {Promise<Boolean>} true if an update is available, false otherwise
*/ */
module.exports.chekIfGameHasUpdate = async function (info) { module.exports.chekIfGameHasUpdate = async function (info) {
if (!shared.isLogged || !shared.cookies) { if (!shared.isLogged || !shared.cookies) {
shared.logger.warn(USER_NOT_LOGGED); shared.logger.warn(USER_NOT_LOGGED);
return false; return false;
} }
// F95 change URL at every game update, // F95 change URL at every game update,
// so if the URL is different an update is available // so if the URL is different an update is available
const exists = await urlHelper.urlExists(info.f95url, true); const exists = await urlHelper.urlExists(info.f95url, true);
if (!exists) return true; if (!exists) return true;
// Parse version from title // Parse version from title
if (_browser === null && !shared.isolation) _browser = await prepareBrowser(); if (_browser === null && !shared.isolation) _browser = await prepareBrowser();
const browser = shared.isolation ? await prepareBrowser() : _browser; const browser = shared.isolation ? await prepareBrowser() : _browser;
const onlineVersion = await scraper.getGameVersionFromTitle(browser, info); const onlineVersion = await scraper.getGameVersionFromTitle(browser, info);
if (shared.isolation) await browser.close(); if (shared.isolation) await browser.close();
return onlineVersion.toUpperCase() !== info.version.toUpperCase(); return onlineVersion.toUpperCase() !== info.version.toUpperCase();
}; };
/** /**
* @public * @public
@ -222,36 +222,36 @@ module.exports.chekIfGameHasUpdate = async function (info) {
* an identified game (in the case of homonymy of titles) * an identified game (in the case of homonymy of titles)
*/ */
module.exports.getGameData = async function (name, includeMods) { module.exports.getGameData = async function (name, includeMods) {
if (!shared.isLogged || !shared.cookies) { if (!shared.isLogged || !shared.cookies) {
shared.logger.warn(USER_NOT_LOGGED); shared.logger.warn(USER_NOT_LOGGED);
return null; return null;
} }
// Gets the search results of the game being searched for // Gets the search results of the game being searched for
if (_browser === null && !shared.isolation) _browser = await prepareBrowser(); if (_browser === null && !shared.isolation) _browser = await prepareBrowser();
const browser = shared.isolation ? await prepareBrowser() : _browser; const browser = shared.isolation ? await prepareBrowser() : _browser;
const urlList = await searcher.getSearchGameResults(browser, name); const urlList = await searcher.getSearchGameResults(browser, name);
// Process previous partial results // Process previous partial results
const promiseList = []; const promiseList = [];
for (const url of urlList) { for (const url of urlList) {
// Start looking for information // Start looking for information
promiseList.push(scraper.getGameInfo(browser, url)); promiseList.push(scraper.getGameInfo(browser, url));
} }
// Filter for mods // Filter for mods
const result = []; const result = [];
for (const info of await Promise.all(promiseList)) { for (const info of await Promise.all(promiseList)) {
// Ignore empty results // Ignore empty results
if (!info) continue; if (!info) continue;
// Skip mods if not required // Skip mods if not required
if (info.isMod && !includeMods) continue; if (info.isMod && !includeMods) continue;
// Else save data // Else save data
result.push(info); result.push(info);
} }
if (shared.isolation) await browser.close(); if (shared.isolation) await browser.close();
return result; return result;
}; };
/** /**
* @public * @public
@ -261,26 +261,26 @@ module.exports.getGameData = async function (name, includeMods) {
* @returns {Promise<GameInfo>} Information about the game. If no game was found, null is returned * @returns {Promise<GameInfo>} Information about the game. If no game was found, null is returned
*/ */
module.exports.getGameDataFromURL = async function (url) { module.exports.getGameDataFromURL = async function (url) {
if (!shared.isLogged || !shared.cookies) { if (!shared.isLogged || !shared.cookies) {
shared.logger.warn(USER_NOT_LOGGED); shared.logger.warn(USER_NOT_LOGGED);
return null; return null;
} }
// Check URL // Check URL
const exists = await urlHelper.urlExists(url); const exists = await urlHelper.urlExists(url);
if (!exists) throw new URIError(url + " is not a valid URL"); if (!exists) throw new URIError(url + " is not a valid URL");
if (!urlHelper.isF95URL(url)) if (!urlHelper.isF95URL(url))
throw new Error(url + " is not a valid F95Zone URL"); throw new Error(url + " is not a valid F95Zone URL");
// Gets the search results of the game being searched for // Gets the search results of the game being searched for
if (_browser === null && !shared.isolation) _browser = await prepareBrowser(); if (_browser === null && !shared.isolation) _browser = await prepareBrowser();
const browser = shared.isolation ? await prepareBrowser() : _browser; const browser = shared.isolation ? await prepareBrowser() : _browser;
// Get game data // Get game data
const result = await scraper.getGameInfo(browser, url); const result = await scraper.getGameInfo(browser, url);
if (shared.isolation) await browser.close(); if (shared.isolation) await browser.close();
return result; return result;
}; };
/** /**
* @public * @public
@ -289,47 +289,47 @@ module.exports.getGameDataFromURL = async function (url) {
* @returns {Promise<UserData>} Data of the user currently logged in * @returns {Promise<UserData>} Data of the user currently logged in
*/ */
module.exports.getUserData = async function () { module.exports.getUserData = async function () {
if (!shared.isLogged || !shared.cookies) { if (!shared.isLogged || !shared.cookies) {
shared.logger.warn(USER_NOT_LOGGED); shared.logger.warn(USER_NOT_LOGGED);
return null; return null;
} }
// Prepare a new web page // Prepare a new web page
if (_browser === null && !shared.isolation) _browser = await prepareBrowser(); if (_browser === null && !shared.isolation) _browser = await prepareBrowser();
const browser = shared.isolation ? await prepareBrowser() : _browser; const browser = shared.isolation ? await prepareBrowser() : _browser;
const page = await preparePage(browser); // Set new isolated page const page = await preparePage(browser); // Set new isolated page
await page.setCookie(...shared.cookies); // Set cookies to avoid login await page.setCookie(...shared.cookies); // Set cookies to avoid login
await page.goto(urlK.F95_BASE_URL); // Go to base page await page.goto(urlK.F95_BASE_URL); // Go to base page
// Explicitly wait for the required items to load // Explicitly wait for the required items to load
await Promise.all([ await Promise.all([
page.waitForSelector(selectorK.USERNAME_ELEMENT), page.waitForSelector(selectorK.USERNAME_ELEMENT),
page.waitForSelector(selectorK.AVATAR_PIC), page.waitForSelector(selectorK.AVATAR_PIC),
]); ]);
const threads = getUserWatchedGameThreads(browser); const threads = getUserWatchedGameThreads(browser);
const username = await page.evaluate( const username = await page.evaluate(
/* istanbul ignore next */ (selector) => /* istanbul ignore next */ (selector) =>
document.querySelector(selector).innerText, document.querySelector(selector).innerText,
selectorK.USERNAME_ELEMENT selectorK.USERNAME_ELEMENT
); );
const avatarSrc = await page.evaluate( const avatarSrc = await page.evaluate(
/* istanbul ignore next */ (selector) => /* istanbul ignore next */ (selector) =>
document.querySelector(selector).getAttribute("src"), document.querySelector(selector).getAttribute("src"),
selectorK.AVATAR_PIC selectorK.AVATAR_PIC
); );
const ud = new UserData(); const ud = new UserData();
ud.username = username; ud.username = username;
ud.avatarSrc = urlHelper.isStringAValidURL(avatarSrc) ? avatarSrc : null; ud.avatarSrc = urlHelper.isStringAValidURL(avatarSrc) ? avatarSrc : null;
ud.watchedThreads = await threads; ud.watchedThreads = await threads;
await page.close(); await page.close();
if (shared.isolation) await browser.close(); if (shared.isolation) await browser.close();
return ud; return ud;
}; };
/** /**
* @public * @public
@ -337,19 +337,19 @@ module.exports.getUserData = async function () {
* You **must** be logged in to the portal before calling this method. * You **must** be logged in to the portal before calling this method.
*/ */
module.exports.logout = async function () { module.exports.logout = async function () {
if (!shared.isLogged || !shared.cookies) { if (!shared.isLogged || !shared.cookies) {
shared.logger.warn(USER_NOT_LOGGED); shared.logger.warn(USER_NOT_LOGGED);
return; return;
} }
// Logout // Logout
shared.isLogged = false; shared.isLogged = false;
// Gracefully close shared browser // Gracefully close shared browser
if (!shared.isolation && _browser !== null) { if (!shared.isolation && _browser !== null) {
await _browser.close(); await _browser.close();
_browser = null; _browser = null;
} }
}; };
//#endregion //#endregion
@ -363,20 +363,20 @@ module.exports.logout = async function () {
* @return {object[]} List of dictionaries or null if cookies don't exist * @return {object[]} List of dictionaries or null if cookies don't exist
*/ */
function loadCookies() { function loadCookies() {
// Check the existence of the cookie file // Check the existence of the cookie file
if (fs.existsSync(shared.cookiesCachePath)) { if (fs.existsSync(shared.cookiesCachePath)) {
// Read cookies // Read cookies
const cookiesJSON = fs.readFileSync(shared.cookiesCachePath); const cookiesJSON = fs.readFileSync(shared.cookiesCachePath);
const cookies = JSON.parse(cookiesJSON); const cookies = JSON.parse(cookiesJSON);
// Check if the cookies have expired // Check if the cookies have expired
for (const cookie of cookies) { for (const cookie of cookies) {
if (isCookieExpired(cookie)) return null; if (isCookieExpired(cookie)) return null;
} }
// Cookies loaded and verified // Cookies loaded and verified
return cookies; return cookies;
} else return null; } else return null;
} }
/** /**
* @private * @private
@ -385,25 +385,25 @@ function loadCookies() {
* @returns {Boolean} true if the cookie has expired, false otherwise * @returns {Boolean} true if the cookie has expired, false otherwise
*/ */
function isCookieExpired(cookie) { function isCookieExpired(cookie) {
// Local variables // Local variables
let expiredCookies = false; let expiredCookies = false;
// Ignore cookies that never expire // Ignore cookies that never expire
const expirationUnixTimestamp = cookie.expire; const expirationUnixTimestamp = cookie.expire;
if (expirationUnixTimestamp !== "-1") { if (expirationUnixTimestamp !== "-1") {
// Convert UNIX epoch timestamp to normal Date // Convert UNIX epoch timestamp to normal Date
const expirationDate = new Date(expirationUnixTimestamp * 1000); const expirationDate = new Date(expirationUnixTimestamp * 1000);
if (expirationDate < Date.now()) { if (expirationDate < Date.now()) {
shared.logger.warn( shared.logger.warn(
"Cookie " + cookie.name + " expired, you need to re-authenticate" "Cookie " + cookie.name + " expired, you need to re-authenticate"
); );
expiredCookies = true; expiredCookies = true;
}
} }
}
return expiredCookies; return expiredCookies;
} }
//#endregion Cookies functions //#endregion Cookies functions
@ -420,27 +420,27 @@ function isCookieExpired(cookie) {
* @returns {Promise<String[]>} List of required values in uppercase * @returns {Promise<String[]>} List of required values in uppercase
*/ */
async function loadValuesFromLatestPage( async function loadValuesFromLatestPage(
page,
path,
selector,
elementRequested
) {
// If the values already exist they are loaded from disk without having to connect to F95
shared.logger.info("Load " + elementRequested + " from disk...");
if (fs.existsSync(path)) {
const valueJSON = fs.readFileSync(path);
return JSON.parse(valueJSON);
}
// Otherwise, connect and download the data from the portal
shared.logger.info("No " + elementRequested + " cached, downloading...");
const values = await getValuesFromLatestPage(
page, page,
path,
selector, selector,
"Getting " + elementRequested + " from page" elementRequested
); ) {
fs.writeFileSync(path, JSON.stringify(values)); // If the values already exist they are loaded from disk without having to connect to F95
return values; shared.logger.info("Load " + elementRequested + " from disk...");
if (fs.existsSync(path)) {
const valueJSON = fs.readFileSync(path);
return JSON.parse(valueJSON);
}
// Otherwise, connect and download the data from the portal
shared.logger.info("No " + elementRequested + " cached, downloading...");
const values = await getValuesFromLatestPage(
page,
selector,
"Getting " + elementRequested + " from page"
);
fs.writeFileSync(path, JSON.stringify(values));
return values;
} }
/** /**
* @private * @private
@ -453,20 +453,20 @@ async function loadValuesFromLatestPage(
* @return {Promise<String[]>} List of uppercase strings indicating the textual values of the elements identified by the selector * @return {Promise<String[]>} List of uppercase strings indicating the textual values of the elements identified by the selector
*/ */
async function getValuesFromLatestPage(page, selector, logMessage) { async function getValuesFromLatestPage(page, selector, logMessage) {
shared.logger.info(logMessage); shared.logger.info(logMessage);
const result = []; const result = [];
const elements = await page.$$(selector); const elements = await page.$$(selector);
for (const element of elements) { for (const element of elements) {
const text = await element.evaluate( const text = await element.evaluate(
/* istanbul ignore next */ (e) => e.innerText /* istanbul ignore next */ (e) => e.innerText
); );
// Save as upper text for better match if used in query // Save as upper text for better match if used in query
result.push(text.toUpperCase()); result.push(text.toUpperCase());
} }
return result; return result;
} }
//#endregion //#endregion
@ -480,66 +480,66 @@ async function getValuesFromLatestPage(page, selector, logMessage) {
* @returns {Promise<LoginResult>} Result of the operation * @returns {Promise<LoginResult>} Result of the operation
*/ */
async function loginF95(browser, username, password) { async function loginF95(browser, username, password) {
const page = await preparePage(browser); // Set new isolated page const page = await preparePage(browser); // Set new isolated page
await page.goto(urlK.F95_LOGIN_URL); // Go to login page await page.goto(urlK.F95_LOGIN_URL); // Go to login page
// Explicitly wait for the required items to load // Explicitly wait for the required items to load
await Promise.all([ await Promise.all([
page.waitForSelector(selectorK.USERNAME_INPUT), page.waitForSelector(selectorK.USERNAME_INPUT),
page.waitForSelector(selectorK.PASSWORD_INPUT), page.waitForSelector(selectorK.PASSWORD_INPUT),
page.waitForSelector(selectorK.LOGIN_BUTTON), page.waitForSelector(selectorK.LOGIN_BUTTON),
]); ]);
await page.type(selectorK.USERNAME_INPUT, username); // Insert username await page.type(selectorK.USERNAME_INPUT, username); // Insert username
await page.type(selectorK.PASSWORD_INPUT, password); // Insert password await page.type(selectorK.PASSWORD_INPUT, password); // Insert password
await Promise.all([ await Promise.all([
page.click(selectorK.LOGIN_BUTTON), // Click on the login button page.click(selectorK.LOGIN_BUTTON), // Click on the login button
page.waitForNavigation({ page.waitForNavigation({
waitUntil: shared.WAIT_STATEMENT, waitUntil: shared.WAIT_STATEMENT,
}), // Wait for page to load }), // Wait for page to load
]); ]);
// Prepare result // Prepare result
let message = ""; let message = "";
// Check if the user is logged in // Check if the user is logged in
const success = await page.evaluate( const success = await page.evaluate(
/* istanbul ignore next */ (selector) => /* istanbul ignore next */ (selector) =>
document.querySelector(selector) !== null, document.querySelector(selector) !== null,
selectorK.AVATAR_INFO selectorK.AVATAR_INFO
);
const errorMessageExists = await page.evaluate(
/* istanbul ignore next */
(selector) => document.querySelector(selector) !== null,
selectorK.LOGIN_MESSAGE_ERROR
);
// Save cookies to avoid re-auth
if (success) {
const c = await page.cookies();
fs.writeFileSync(shared.cookiesCachePath, JSON.stringify(c));
message = "Authentication successful";
} else if (errorMessageExists) {
const errorMessage = await page.evaluate(
/* istanbul ignore next */ (selector) =>
document.querySelector(selector).innerText,
selectorK.LOGIN_MESSAGE_ERROR
); );
if (errorMessage === "Incorrect password. Please try again.") { const errorMessageExists = await page.evaluate(
message = "Incorrect password"; /* istanbul ignore next */
} else if ( (selector) => document.querySelector(selector) !== null,
errorMessage === selectorK.LOGIN_MESSAGE_ERROR
"The requested user '" + username + "' could not be found." );
) {
// The escaped quotes are important!
message = "Incorrect username";
} else message = errorMessage;
} else message = "Unknown error";
await page.close(); // Close the page // Save cookies to avoid re-auth
return new LoginResult(success, message); if (success) {
const c = await page.cookies();
fs.writeFileSync(shared.cookiesCachePath, JSON.stringify(c));
message = "Authentication successful";
} else if (errorMessageExists) {
const errorMessage = await page.evaluate(
/* istanbul ignore next */ (selector) =>
document.querySelector(selector).innerText,
selectorK.LOGIN_MESSAGE_ERROR
);
if (errorMessage === "Incorrect password. Please try again.") {
message = "Incorrect password";
} else if (
errorMessage ===
"The requested user '" + username + "' could not be found."
) {
// The escaped quotes are important!
message = "Incorrect username";
} else message = errorMessage;
} else message = "Unknown error";
await page.close(); // Close the page
return new LoginResult(success, message);
} }
/** /**
* @private * @private
@ -548,61 +548,61 @@ async function loginF95(browser, username, password) {
* @returns {Promise<String[]>} URL list * @returns {Promise<String[]>} URL list
*/ */
async function getUserWatchedGameThreads(browser) { async function getUserWatchedGameThreads(browser) {
const page = await preparePage(browser); // Set new isolated page const page = await preparePage(browser); // Set new isolated page
await page.goto(urlK.F95_WATCHED_THREADS); // Go to the thread page await page.goto(urlK.F95_WATCHED_THREADS); // Go to the thread page
// Explicitly wait for the required items to load // Explicitly wait for the required items to load
await page.waitForSelector(selectorK.WATCHED_THREAD_FILTER_POPUP_BUTTON); await page.waitForSelector(selectorK.WATCHED_THREAD_FILTER_POPUP_BUTTON);
// Show the popup // Show the popup
await Promise.all([ await Promise.all([
page.click(selectorK.WATCHED_THREAD_FILTER_POPUP_BUTTON), page.click(selectorK.WATCHED_THREAD_FILTER_POPUP_BUTTON),
page.waitForSelector(selectorK.UNREAD_THREAD_CHECKBOX), page.waitForSelector(selectorK.UNREAD_THREAD_CHECKBOX),
page.waitForSelector(selectorK.ONLY_GAMES_THREAD_OPTION), page.waitForSelector(selectorK.ONLY_GAMES_THREAD_OPTION),
page.waitForSelector(selectorK.FILTER_THREADS_BUTTON), page.waitForSelector(selectorK.FILTER_THREADS_BUTTON),
]); ]);
// Set the filters // Set the filters
await page.evaluate( await page.evaluate(
/* istanbul ignore next */ (selector) => /* istanbul ignore next */ (selector) =>
document.querySelector(selector).removeAttribute("checked"), document.querySelector(selector).removeAttribute("checked"),
selectorK.UNREAD_THREAD_CHECKBOX selectorK.UNREAD_THREAD_CHECKBOX
); // Also read the threads already read ); // Also read the threads already read
// Filter the threads // Filter the threads
await page.click(selectorK.ONLY_GAMES_THREAD_OPTION); await page.click(selectorK.ONLY_GAMES_THREAD_OPTION);
await page.click(selectorK.FILTER_THREADS_BUTTON); await page.click(selectorK.FILTER_THREADS_BUTTON);
await page.waitForSelector(selectorK.WATCHED_THREAD_URLS); await page.waitForSelector(selectorK.WATCHED_THREAD_URLS);
// Get the threads urls // Get the threads urls
const urls = []; const urls = [];
let nextPageExists = false; let nextPageExists = false;
do { do {
// Get all the URLs // Get all the URLs
for (const handle of await page.$$(selectorK.WATCHED_THREAD_URLS)) { for (const handle of await page.$$(selectorK.WATCHED_THREAD_URLS)) {
const src = await page.evaluate( const src = await page.evaluate(
/* istanbul ignore next */ (element) => element.href, /* istanbul ignore next */ (element) => element.href,
handle handle
); );
// If 'unread' is left, it will redirect to the last unread post // If 'unread' is left, it will redirect to the last unread post
const url = src.replace("/unread", ""); const url = src.replace("/unread", "");
urls.push(url); urls.push(url);
} }
nextPageExists = await page.evaluate( nextPageExists = await page.evaluate(
/* istanbul ignore next */ (selector) => document.querySelector(selector), /* istanbul ignore next */ (selector) => document.querySelector(selector),
selectorK.WATCHED_THREAD_NEXT_PAGE selectorK.WATCHED_THREAD_NEXT_PAGE
); );
// Click to next page // Click to next page
if (nextPageExists) { if (nextPageExists) {
await page.click(selectorK.WATCHED_THREAD_NEXT_PAGE); await page.click(selectorK.WATCHED_THREAD_NEXT_PAGE);
await page.waitForSelector(selectorK.WATCHED_THREAD_URLS); await page.waitForSelector(selectorK.WATCHED_THREAD_URLS);
} }
} while (nextPageExists); } while (nextPageExists);
await page.close(); await page.close();
return urls; return urls;
} }
//#endregion User //#endregion User

View File

@ -4,6 +4,7 @@
// Core modules // Core modules
const fs = require("fs"); const fs = require("fs");
const path = require("path");
// Public modules from npm // Public modules from npm
// const { File } = require('megajs'); // const { File } = require('megajs');
@ -13,98 +14,98 @@ const { prepareBrowser, preparePage } = require("../puppeteer-helper.js");
const shared = require("../shared.js"); const shared = require("../shared.js");
class GameDownload { class GameDownload {
constructor() { constructor() {
/** /**
* @public * @public
* Platform that hosts game files * Platform that hosts game files
* @type String * @type String
*/ */
this.hosting = ""; this.hosting = "";
/** /**
* @public * @public
* Link to game files * Link to game files
* @type String * @type String
*/ */
this.link = null; this.link = null;
/** /**
* @public * @public
* Operating systems supported by the game version indicated in this class. * Operating systems supported by the game version indicated in this class.
* Can be *WINDOWS/LINUX/MACOS* * Can be *WINDOWS/LINUX/MACOS*
* @type String[] * @type String[]
*/ */
this.supportedOS = []; this.supportedOS = [];
} }
/** /**
* @public * @public
* Download the game data in the indicated path. * Download the game data in the indicated path.
* Supported hosting platforms: MEGA, NOPY * Supported hosting platforms: MEGA, NOPY
* @param {String} path Save path * @param {String} path Save path
* @return {Promise<Boolean>} Result of the operation * @return {Promise<Boolean>} Result of the operation
*/ */
async download(path) { async download(path) {
if (this.link.includes("mega.nz")) if (this.link.includes("mega.nz"))
return await downloadMEGA(this.link, path); return await downloadMEGA(this.link, path);
else if (this.link.includes("nopy.to")) else if (this.link.includes("nopy.to"))
return await downloadNOPY(this.link, path); return await downloadNOPY(this.link, path);
} }
} }
module.exports = GameDownload; module.exports = GameDownload;
async function downloadMEGA(url, savepath) { async function downloadMEGA(url, savepath) {
// The URL is masked // The URL is masked
const browser = await prepareBrowser(); const browser = await prepareBrowser();
const page = await preparePage(browser); const page = await preparePage(browser);
await page.setCookie(...shared.cookies); // Set cookies to avoid login await page.setCookie(...shared.cookies); // Set cookies to avoid login
await page.goto(url); await page.goto(url);
await page.waitForSelector("a.host_link"); await page.waitForSelector("a.host_link");
// Obtain the link for the unmasked page and click it // Obtain the link for the unmasked page and click it
const link = await page.$("a.host_link"); const link = await page.$("a.host_link");
await link.click(); await link.click();
await page.goto(url, { await page.goto(url, {
waitUntil: shared.WAIT_STATEMENT, waitUntil: shared.WAIT_STATEMENT,
}); // Go to the game page and wait until it loads }); // Go to the game page and wait until it loads
// Obtain the URL after the redirect // Obtain the URL after the redirect
const downloadURL = page.url(); const downloadURL = page.url();
// Close browser and page // Close browser and page
await page.close(); await page.close();
await browser.close(); await browser.close();
const stream = fs.createWriteStream(savepath); const stream = fs.createWriteStream(savepath);
const file = File.fromURL(downloadURL); const file = File.fromURL(downloadURL);
file.download().pipe(stream); file.download().pipe(stream);
return fs.existsSync(savepath); return fs.existsSync(savepath);
} }
async function downloadNOPY(url, savepath) { async function downloadNOPY(url, savepath) {
// Prepare browser // Prepare browser
const browser = await prepareBrowser(); const browser = await prepareBrowser();
const page = await preparePage(browser); const page = await preparePage(browser);
await page.goto(url); await page.goto(url);
await page.waitForSelector("#download"); await page.waitForSelector("#download");
// Set the save path // Set the save path
await page._client.send("Page.setDownloadBehavior", { await page._client.send("Page.setDownloadBehavior", {
behavior: "allow", behavior: "allow",
downloadPath: path.basename(path.dirname(savepath)), // It's a directory downloadPath: path.basename(path.dirname(savepath)), // It's a directory
}); });
// Obtain the download button and click it // Obtain the download button and click it
const downloadButton = await page.$("#download"); const downloadButton = await page.$("#download");
await downloadButton.click(); await downloadButton.click();
// Await for all the connections to close // Await for all the connections to close
await page.waitForNavigation({ await page.waitForNavigation({
waitUntil: "networkidle0", waitUntil: "networkidle0",
timeout: 0, // Disable timeout timeout: 0, // Disable timeout
}); });
// Close browser and page // Close browser and page
await page.close(); await page.close();
await browser.close(); await browser.close();
return fs.existsSync(savepath); return fs.existsSync(savepath);
} }

View File

@ -1,119 +1,119 @@
"use strict"; "use strict";
class GameInfo { class GameInfo {
constructor() { constructor() {
//#region Properties //#region Properties
/** /**
* Game name * Game name
* @type String * @type String
*/ */
this.name = null; this.name = null;
/** /**
* Game author * Game author
* @type String * @type String
*/ */
this.author = null; this.author = null;
/** /**
* URL to the game's official conversation on the F95Zone portal * URL to the game's official conversation on the F95Zone portal
* @type String * @type String
*/ */
this.f95url = null; this.f95url = null;
/** /**
* Game description * Game description
* @type String * @type String
*/ */
this.overview = null; this.overview = null;
/** /**
* List of tags associated with the game * List of tags associated with the game
* @type String[] * @type String[]
*/ */
this.tags = []; this.tags = [];
/** /**
* Graphics engine used for game development * Graphics engine used for game development
* @type String * @type String
*/ */
this.engine = null; this.engine = null;
/** /**
* Progress of the game * Progress of the game
* @type String * @type String
*/ */
this.status = null; this.status = null;
/** /**
* Game description image URL * Game description image URL
* @type String * @type String
*/ */
this.previewSource = null; this.previewSource = null;
/** /**
* Game version * Game version
* @type String * @type String
*/ */
this.version = null; this.version = null;
/** /**
* Last time the game underwent updates * Last time the game underwent updates
* @type String * @type String
*/ */
this.lastUpdate = null; this.lastUpdate = null;
/** /**
* Last time the local copy of the game was run * Last time the local copy of the game was run
* @type String * @type String
*/ */
this.lastPlayed = null; this.lastPlayed = null;
/** /**
* Specifies if the game is original or a mod * Specifies if the game is original or a mod
* @type Boolean * @type Boolean
*/ */
this.isMod = false; this.isMod = false;
/** /**
* Changelog for the last version. * Changelog for the last version.
* @type String * @type String
*/ */
this.changelog = null; this.changelog = null;
/** /**
* Directory containing the local copy of the game * Directory containing the local copy of the game
* @type String * @type String
*/ */
this.gameDir = null; this.gameDir = null;
/** /**
* Information on game file download links, * Information on game file download links,
* including information on hosting platforms * including information on hosting platforms
* and operating system supported by the specific link * and operating system supported by the specific link
* @type GameDownload[] * @type GameDownload[]
*/ */
this.downloadInfo = []; this.downloadInfo = [];
//#endregion Properties //#endregion Properties
} }
/** /**
* Converts the object to a dictionary used for JSON serialization * Converts the object to a dictionary used for JSON serialization
*/ */
/* istanbul ignore next */ /* istanbul ignore next */
toJSON() { toJSON() {
return { return {
name: this.name, name: this.name,
author: this.author, author: this.author,
f95url: this.f95url, f95url: this.f95url,
overview: this.overview, overview: this.overview,
engine: this.engine, engine: this.engine,
status: this.status, status: this.status,
previewSource: this.previewSource, previewSource: this.previewSource,
version: this.version, version: this.version,
lastUpdate: this.lastUpdate, lastUpdate: this.lastUpdate,
lastPlayed: this.lastPlayed, lastPlayed: this.lastPlayed,
isMod: this.isMod, isMod: this.isMod,
changelog: this.changelog, changelog: this.changelog,
gameDir: this.gameDir, gameDir: this.gameDir,
downloadInfo: this.downloadInfo, downloadInfo: this.downloadInfo,
}; };
} }
/** /**
* Return a new GameInfo from a JSON string * Return a new GameInfo from a JSON string
* @param {String} json JSON string used to create the new object * @param {String} json JSON string used to create the new object
* @returns {GameInfo} * @returns {GameInfo}
*/ */
/* istanbul ignore next */ /* istanbul ignore next */
static fromJSON(json) { static fromJSON(json) {
return Object.assign(new GameInfo(), json); return Object.assign(new GameInfo(), json);
} }
} }
module.exports = GameInfo; module.exports = GameInfo;

View File

@ -4,17 +4,17 @@
* Object obtained in response to an attempt to login to the portal. * Object obtained in response to an attempt to login to the portal.
*/ */
class LoginResult { class LoginResult {
constructor(success, message) { constructor(success, message) {
/** /**
* Result of the login operation * Result of the login operation
* @type Boolean * @type Boolean
*/ */
this.success = success; this.success = success;
/** /**
* Login response message * Login response message
* @type String * @type String
*/ */
this.message = message; this.message = message;
} }
} }
module.exports = LoginResult; module.exports = LoginResult;

View File

@ -4,23 +4,23 @@
* Class containing the data of the user currently connected to the F95Zone platform. * Class containing the data of the user currently connected to the F95Zone platform.
*/ */
class UserData { class UserData {
constructor() { constructor() {
/** /**
* User username. * User username.
* @type String * @type String
*/ */
this.username = ""; this.username = "";
/** /**
* Path to the user's profile picture. * Path to the user's profile picture.
* @type String * @type String
*/ */
this.avatarSrc = null; this.avatarSrc = null;
/** /**
* List of followed thread URLs. * List of followed thread URLs.
* @type URL[] * @type URL[]
*/ */
this.watchedThreads = []; this.watchedThreads = [];
} }
} }
module.exports = UserData; module.exports = UserData;

View File

@ -1,33 +1,33 @@
module.exports = Object.freeze({ module.exports = Object.freeze({
AVATAR_INFO: "span.avatar", AVATAR_INFO: "span.avatar",
AVATAR_PIC: 'a[href="/account/"] > span.avatar > img[class^="avatar"]', AVATAR_PIC: "a[href=\"/account/\"] > span.avatar > img[class^=\"avatar\"]",
ENGINE_ID_SELECTOR: 'div[id^="btn-prefix_1_"]>span', ENGINE_ID_SELECTOR: "div[id^=\"btn-prefix_1_\"]>span",
FILTER_THREADS_BUTTON: 'button[class="button--primary button"]', FILTER_THREADS_BUTTON: "button[class=\"button--primary button\"]",
GAME_IMAGES: 'img[src^="https://attachments.f95zone.to"]', GAME_IMAGES: "img[src^=\"https://attachments.f95zone.to\"]",
GAME_TAGS: "a.tagItem", GAME_TAGS: "a.tagItem",
GAME_TITLE: "h1.p-title-value", GAME_TITLE: "h1.p-title-value",
GAME_TITLE_PREFIXES: 'h1.p-title-value > a.labelLink > span[dir="auto"]', GAME_TITLE_PREFIXES: "h1.p-title-value > a.labelLink > span[dir=\"auto\"]",
LOGIN_BUTTON: "button.button--icon--login", LOGIN_BUTTON: "button.button--icon--login",
LOGIN_MESSAGE_ERROR: LOGIN_MESSAGE_ERROR:
"div.blockMessage.blockMessage--error.blockMessage--iconic", "div.blockMessage.blockMessage--error.blockMessage--iconic",
ONLY_GAMES_THREAD_OPTION: 'select[name="nodes[]"] > option[value="2"]', ONLY_GAMES_THREAD_OPTION: "select[name=\"nodes[]\"] > option[value=\"2\"]",
PASSWORD_INPUT: 'input[name="password"]', PASSWORD_INPUT: "input[name=\"password\"]",
SEARCH_BUTTON: "form.block > * button.button--icon--search", SEARCH_BUTTON: "form.block > * button.button--icon--search",
SEARCH_FORM_TEXTBOX: 'input[name="keywords"][type="search"]', SEARCH_FORM_TEXTBOX: "input[name=\"keywords\"][type=\"search\"]",
SEARCH_ONLY_GAMES_OPTION: 'select[name="c[nodes][]"] > option[value="1"]', SEARCH_ONLY_GAMES_OPTION: "select[name=\"c[nodes][]\"] > option[value=\"1\"]",
STATUS_ID_SELECTOR: 'div[id^="btn-prefix_4_"]>span', STATUS_ID_SELECTOR: "div[id^=\"btn-prefix_4_\"]>span",
THREAD_POSTS: THREAD_POSTS:
"article.message-body:first-child > div.bbWrapper:first-of-type", "article.message-body:first-child > div.bbWrapper:first-of-type",
THREAD_TITLE: "h3.contentRow-title", THREAD_TITLE: "h3.contentRow-title",
TITLE_ONLY_CHECKBOX: 'form.block > * input[name="c[title_only]"]', TITLE_ONLY_CHECKBOX: "form.block > * input[name=\"c[title_only]\"]",
UNREAD_THREAD_CHECKBOX: 'input[type="checkbox"][name="unread"]', UNREAD_THREAD_CHECKBOX: "input[type=\"checkbox\"][name=\"unread\"]",
USERNAME_ELEMENT: 'a[href="/account/"] > span.p-navgroup-linkText', USERNAME_ELEMENT: "a[href=\"/account/\"] > span.p-navgroup-linkText",
USERNAME_INPUT: 'input[name="login"]', USERNAME_INPUT: "input[name=\"login\"]",
WATCHED_THREAD_FILTER_POPUP_BUTTON: "a.filterBar-menuTrigger", WATCHED_THREAD_FILTER_POPUP_BUTTON: "a.filterBar-menuTrigger",
WATCHED_THREAD_NEXT_PAGE: "a.pageNav-jump--next", WATCHED_THREAD_NEXT_PAGE: "a.pageNav-jump--next",
WATCHED_THREAD_URLS: 'a[href^="/threads/"][data-tp-primary]', WATCHED_THREAD_URLS: "a[href^=\"/threads/\"][data-tp-primary]",
DOWNLOAD_LINKS_CONTAINER: 'span[style="font-size: 18px"]', DOWNLOAD_LINKS_CONTAINER: "span[style=\"font-size: 18px\"]",
SEARCH_THREADS_RESULTS_BODY: "div.contentRow-main", SEARCH_THREADS_RESULTS_BODY: "div.contentRow-main",
SEARCH_THREADS_MEMBERSHIP: "li > a:not(.username)", SEARCH_THREADS_MEMBERSHIP: "li > a:not(.username)",
THREAD_LAST_CHANGELOG: "div.bbCodeBlock-content > div:first-of-type", THREAD_LAST_CHANGELOG: "div.bbCodeBlock-content > div:first-of-type",
}); });

View File

@ -1,7 +1,7 @@
module.exports = Object.freeze({ module.exports = Object.freeze({
F95_BASE_URL: "https://f95zone.to", F95_BASE_URL: "https://f95zone.to",
F95_SEARCH_URL: "https://f95zone.to/search/?type=post", F95_SEARCH_URL: "https://f95zone.to/search/?type=post",
F95_LATEST_UPDATES: "https://f95zone.to/latest", F95_LATEST_UPDATES: "https://f95zone.to/latest",
F95_LOGIN_URL: "https://f95zone.to/login", F95_LOGIN_URL: "https://f95zone.to/login",
F95_WATCHED_THREADS: "https://f95zone.to/watched/threads", F95_WATCHED_THREADS: "https://f95zone.to/watched/threads",
}); });

View File

@ -21,57 +21,57 @@ const urlHelper = require("./url-helper.js");
* looking for * looking for
*/ */
module.exports.getGameInfo = async function (browser, url) { module.exports.getGameInfo = async function (browser, url) {
shared.logger.info("Obtaining game info"); shared.logger.info("Obtaining game info");
// Verify the correctness of the URL // Verify the correctness of the URL
const exists = await urlHelper.urlExists(url); const exists = await urlHelper.urlExists(url);
if (!exists) throw new URIError(`${url} is not a valid URL`); if (!exists) throw new URIError(`${url} is not a valid URL`);
if (!urlHelper.isF95URL(url)) if (!urlHelper.isF95URL(url))
throw new Error(`${url} is not a valid F95Zone URL`); throw new Error(`${url} is not a valid F95Zone URL`);
const page = await preparePage(browser); // Set new isolated page const page = await preparePage(browser); // Set new isolated page
await page.setCookie(...shared.cookies); // Set cookies to avoid login await page.setCookie(...shared.cookies); // Set cookies to avoid login
await page.goto(url, { await page.goto(url, {
waitUntil: shared.WAIT_STATEMENT, waitUntil: shared.WAIT_STATEMENT,
}); // Go to the game page and wait until it loads }); // Go to the game page and wait until it loads
// It asynchronously searches for the elements and // It asynchronously searches for the elements and
// then waits at the end to compile the object to be returned // then waits at the end to compile the object to be returned
let info = new GameInfo(); let info = new GameInfo();
const title = getGameTitle(page); const title = getGameTitle(page);
const author = getGameAuthor(page); const author = getGameAuthor(page);
const tags = getGameTags(page); const tags = getGameTags(page);
const redirectUrl = urlHelper.getUrlRedirect(url); const redirectUrl = urlHelper.getUrlRedirect(url);
info = await parsePrefixes(page, info); // Fill status/engines/isMod info = await parsePrefixes(page, info); // Fill status/engines/isMod
const structuredText = await getMainPostStructuredText(page); const structuredText = await getMainPostStructuredText(page);
const overview = getOverview(structuredText, info.isMod); const overview = getOverview(structuredText, info.isMod);
const parsedInfos = parseConversationPage(structuredText); const parsedInfos = parseConversationPage(structuredText);
const previewSource = getGamePreviewSource(page); const previewSource = getGamePreviewSource(page);
const changelog = getLastChangelog(page); const changelog = getLastChangelog(page);
// Fill in the GameInfo element with the information obtained // Fill in the GameInfo element with the information obtained
info.name = await title; info.name = await title;
info.author = await author; info.author = await author;
info.tags = await tags; info.tags = await tags;
info.f95url = await redirectUrl; info.f95url = await redirectUrl;
info.overview = overview; info.overview = overview;
info.lastUpdate = info.isMod info.lastUpdate = info.isMod
? parsedInfos.UPDATED ? parsedInfos.UPDATED
: parsedInfos.THREAD_UPDATED; : parsedInfos.THREAD_UPDATED;
info.previewSource = await previewSource; info.previewSource = await previewSource;
info.changelog = await changelog; info.changelog = await changelog;
info.version = await exports.getGameVersionFromTitle(browser, info); info.version = await exports.getGameVersionFromTitle(browser, info);
//let downloadData = getGameDownloadLink(page); //let downloadData = getGameDownloadLink(page);
//info.downloadInfo = await downloadData; //info.downloadInfo = await downloadData;
/* Downloading games without going directly to /* Downloading games without going directly to
* the platform appears to be prohibited by * the platform appears to be prohibited by
* the guidelines. It is therefore useless to * the guidelines. It is therefore useless to
* keep the links for downloading the games. */ * keep the links for downloading the games. */
await page.close(); // Close the page await page.close(); // Close the page
shared.logger.info("Founded data for " + info.name); shared.logger.info("Founded data for " + info.name);
return info; return info;
}; };
/** /**
@ -81,27 +81,27 @@ module.exports.getGameInfo = async function (browser, url) {
* @returns {Promise<String>} Online version of the game * @returns {Promise<String>} Online version of the game
*/ */
module.exports.getGameVersionFromTitle = async function (browser, info) { module.exports.getGameVersionFromTitle = async function (browser, info) {
const page = await preparePage(browser); // Set new isolated page const page = await preparePage(browser); // Set new isolated page
await page.setCookie(...shared.cookies); // Set cookies to avoid login await page.setCookie(...shared.cookies); // Set cookies to avoid login
await page.goto(info.f95url, { await page.goto(info.f95url, {
waitUntil: shared.WAIT_STATEMENT, waitUntil: shared.WAIT_STATEMENT,
}); // Go to the game page and wait until it loads }); // Go to the game page and wait until it loads
// Get the title // Get the title
const titleHTML = await page.evaluate( const titleHTML = await page.evaluate(
/* istanbul ignore next */ /* istanbul ignore next */
(selector) => document.querySelector(selector).innerHTML, (selector) => document.querySelector(selector).innerHTML,
selectorK.GAME_TITLE selectorK.GAME_TITLE
); );
const title = HTMLParser.parse(titleHTML).childNodes.pop().rawText; const title = HTMLParser.parse(titleHTML).childNodes.pop().rawText;
// The title is in the following format: [PREFIXES] NAME GAME [VERSION] [AUTHOR] // The title is in the following format: [PREFIXES] NAME GAME [VERSION] [AUTHOR]
const startIndex = title.indexOf("[") + 1; const startIndex = title.indexOf("[") + 1;
const endIndex = title.indexOf("]", startIndex); const endIndex = title.indexOf("]", startIndex);
let version = title.substring(startIndex, endIndex).trim().toUpperCase(); let version = title.substring(startIndex, endIndex).trim().toUpperCase();
if (version.startsWith("V")) version = version.replace("V", ""); // Replace only the first occurrence if (version.startsWith("V")) version = version.replace("V", ""); // Replace only the first occurrence
await page.close(); await page.close();
return cleanFSString(version); return cleanFSString(version);
}; };
//#region Private methods //#region Private methods
@ -111,8 +111,8 @@ module.exports.getGameVersionFromTitle = async function (browser, info) {
* @returns {String} * @returns {String}
*/ */
function cleanFSString(s) { function cleanFSString(s) {
const rx = /[/\\?%*:|"<>]/g; const rx = /[/\\?%*:|"<>]/g;
return s.replace(rx, ""); return s.replace(rx, "");
} }
/** /**
@ -124,11 +124,11 @@ function cleanFSString(s) {
* @returns {Promise<String>} Game description * @returns {Promise<String>} Game description
*/ */
function getOverview(text, isMod) { function getOverview(text, isMod) {
// Get overview (different parsing for game and mod) // Get overview (different parsing for game and mod)
let overviewEndIndex; let overviewEndIndex;
if (isMod) overviewEndIndex = text.indexOf("Updated"); if (isMod) overviewEndIndex = text.indexOf("Updated");
else overviewEndIndex = text.indexOf("Thread Updated"); else overviewEndIndex = text.indexOf("Thread Updated");
return text.substring(0, overviewEndIndex).replace("Overview:\n", "").trim(); return text.substring(0, overviewEndIndex).replace("Overview:\n", "").trim();
} }
/** /**
@ -139,16 +139,16 @@ function getOverview(text, isMod) {
* @returns {Promise<String>} Structured text * @returns {Promise<String>} Structured text
*/ */
async function getMainPostStructuredText(page) { async function getMainPostStructuredText(page) {
// Gets the first post, where are listed all the game's informations // Gets the first post, where are listed all the game's informations
const post = (await page.$$(selectorK.THREAD_POSTS))[0]; const post = (await page.$$(selectorK.THREAD_POSTS))[0];
// The info are plain text so we need to parse the HTML code // The info are plain text so we need to parse the HTML code
const bodyHTML = await page.evaluate( const bodyHTML = await page.evaluate(
/* istanbul ignore next */ /* istanbul ignore next */
(mainPost) => mainPost.innerHTML, (mainPost) => mainPost.innerHTML,
post post
); );
return HTMLParser.parse(bodyHTML).structuredText; return HTMLParser.parse(bodyHTML).structuredText;
} }
/** /**
@ -158,20 +158,20 @@ async function getMainPostStructuredText(page) {
* @returns {Promise<String>} Game author * @returns {Promise<String>} Game author
*/ */
async function getGameAuthor(page) { async function getGameAuthor(page) {
// Get the game/mod name (without square brackets) // Get the game/mod name (without square brackets)
const titleHTML = await page.evaluate( const titleHTML = await page.evaluate(
/* istanbul ignore next */ /* istanbul ignore next */
(selector) => document.querySelector(selector).innerHTML, (selector) => document.querySelector(selector).innerHTML,
selectorK.GAME_TITLE selectorK.GAME_TITLE
); );
const structuredTitle = HTMLParser.parse(titleHTML); const structuredTitle = HTMLParser.parse(titleHTML);
// The last element **shoud be** the title without prefixes (engines, status, other...) // The last element **shoud be** the title without prefixes (engines, status, other...)
const gameTitle = structuredTitle.childNodes.pop().rawText; const gameTitle = structuredTitle.childNodes.pop().rawText;
// The last square brackets contain the author // The last square brackets contain the author
const startTitleIndex = gameTitle.lastIndexOf("[") + 1; const startTitleIndex = gameTitle.lastIndexOf("[") + 1;
return gameTitle.substring(startTitleIndex, gameTitle.length - 1).trim(); return gameTitle.substring(startTitleIndex, gameTitle.length - 1).trim();
} }
/** /**
@ -182,23 +182,23 @@ async function getGameAuthor(page) {
* @returns {Object} Dictionary of information * @returns {Object} Dictionary of information
*/ */
function parseConversationPage(text) { function parseConversationPage(text) {
const dataPairs = {}; const dataPairs = {};
// The information searched in the game post are one per line // The information searched in the game post are one per line
const splittedText = text.split("\n"); const splittedText = text.split("\n");
for (const line of splittedText) { for (const line of splittedText) {
if (!line.includes(":")) continue; if (!line.includes(":")) continue;
// Create pair key/value // Create pair key/value
const splitted = line.split(":"); const splitted = line.split(":");
const key = splitted[0].trim().toUpperCase().replace(/ /g, "_"); // Uppercase to avoid mismatch const key = splitted[0].trim().toUpperCase().replace(/ /g, "_"); // Uppercase to avoid mismatch
const value = splitted[1].trim(); const value = splitted[1].trim();
// Add pair to the dict if valid // Add pair to the dict if valid
if (value !== "") dataPairs[key] = value; if (value !== "") dataPairs[key] = value;
} }
return dataPairs; return dataPairs;
} }
/** /**
@ -208,27 +208,27 @@ function parseConversationPage(text) {
* @returns {Promise<String>} URL (String) of the image or null if failed to get it * @returns {Promise<String>} URL (String) of the image or null if failed to get it
*/ */
async function getGamePreviewSource(page) { async function getGamePreviewSource(page) {
// Wait for the selector or return an empty value // Wait for the selector or return an empty value
try { try {
await page.waitForSelector(selectorK.GAME_IMAGES); await page.waitForSelector(selectorK.GAME_IMAGES);
} catch { } catch {
return null; return null;
} }
const src = await page.evaluate( const src = await page.evaluate(
/* istanbul ignore next */ /* istanbul ignore next */
(selector) => { (selector) => {
// Get the firs image available // Get the firs image available
const img = document.querySelector(selector); const img = document.querySelector(selector);
if (img) return img.getAttribute("src"); if (img) return img.getAttribute("src");
else return null; else return null;
}, },
selectorK.GAME_IMAGES selectorK.GAME_IMAGES
); );
// Check if the URL is valid // Check if the URL is valid
return urlHelper.isStringAValidURL(src) ? src : null; return urlHelper.isStringAValidURL(src) ? src : null;
} }
/** /**
@ -238,18 +238,18 @@ async function getGamePreviewSource(page) {
* @returns {Promise<String>} Game title * @returns {Promise<String>} Game title
*/ */
async function getGameTitle(page) { async function getGameTitle(page) {
// Get the game/mod name (without square brackets) // Get the game/mod name (without square brackets)
const titleHTML = await page.evaluate( const titleHTML = await page.evaluate(
/* istanbul ignore next */ /* istanbul ignore next */
(selector) => document.querySelector(selector).innerHTML, (selector) => document.querySelector(selector).innerHTML,
selectorK.GAME_TITLE selectorK.GAME_TITLE
); );
const structuredTitle = HTMLParser.parse(titleHTML); const structuredTitle = HTMLParser.parse(titleHTML);
// The last element **shoud be** the title without prefixes (engines, status, other...) // The last element **shoud be** the title without prefixes (engines, status, other...)
const gameTitle = structuredTitle.childNodes.pop().rawText; const gameTitle = structuredTitle.childNodes.pop().rawText;
const endTitleIndex = gameTitle.indexOf("["); const endTitleIndex = gameTitle.indexOf("[");
return gameTitle.substring(0, endTitleIndex).trim(); return gameTitle.substring(0, endTitleIndex).trim();
} }
/** /**
@ -259,18 +259,18 @@ async function getGameTitle(page) {
* @returns {Promise<String[]>} List of uppercase tags * @returns {Promise<String[]>} List of uppercase tags
*/ */
async function getGameTags(page) { async function getGameTags(page) {
const tags = []; const tags = [];
// Get the game tags // Get the game tags
for (const handle of await page.$$(selectorK.GAME_TAGS)) { for (const handle of await page.$$(selectorK.GAME_TAGS)) {
const tag = await page.evaluate( const tag = await page.evaluate(
/* istanbul ignore next */ /* istanbul ignore next */
(element) => element.innerText, (element) => element.innerText,
handle handle
); );
tags.push(tag.toUpperCase()); tags.push(tag.toUpperCase());
} }
return tags.sort(); return tags.sort();
} }
/** /**
@ -282,25 +282,25 @@ async function getGameTags(page) {
* @returns {Promise<GameInfo>} GameInfo object passed in to which the identified information has been added * @returns {Promise<GameInfo>} GameInfo object passed in to which the identified information has been added
*/ */
async function parsePrefixes(page, info) { async function parsePrefixes(page, info) {
// The 'Ongoing' status is not specified, only 'Abandoned'/'OnHold'/'Complete' // The 'Ongoing' status is not specified, only 'Abandoned'/'OnHold'/'Complete'
info.status = "ONGOING"; info.status = "ONGOING";
for (const handle of await page.$$(selectorK.GAME_TITLE_PREFIXES)) { for (const handle of await page.$$(selectorK.GAME_TITLE_PREFIXES)) {
const value = await page.evaluate( const value = await page.evaluate(
/* istanbul ignore next */ /* istanbul ignore next */
(element) => element.innerText, (element) => element.innerText,
handle handle
); );
// Clean the prefix // Clean the prefix
const prefix = value.toUpperCase().replace("[", "").replace("]", "").trim(); const prefix = value.toUpperCase().replace("[", "").replace("]", "").trim();
// Getting infos... // Getting infos...
if (shared.statuses.includes(prefix)) info.status = prefix; if (shared.statuses.includes(prefix)) info.status = prefix;
else if (shared.engines.includes(prefix)) info.engine = prefix; else if (shared.engines.includes(prefix)) info.engine = prefix;
// This is not a game but a mod // This is not a game but a mod
else if (prefix === "MOD" || prefix === "CHEAT MOD") info.isMod = true; else if (prefix === "MOD" || prefix === "CHEAT MOD") info.isMod = true;
} }
return info; return info;
} }
/** /**
@ -310,24 +310,24 @@ async function parsePrefixes(page, info) {
* @returns {Promise<String>} Changelog for the last version or a empty string if no changelog is found * @returns {Promise<String>} Changelog for the last version or a empty string if no changelog is found
*/ */
async function getLastChangelog(page) { async function getLastChangelog(page) {
// Gets the first post, where are listed all the game's informations // Gets the first post, where are listed all the game's informations
const post = (await page.$$(selectorK.THREAD_POSTS))[0]; const post = (await page.$$(selectorK.THREAD_POSTS))[0];
const spoiler = await post.$(selectorK.THREAD_LAST_CHANGELOG); const spoiler = await post.$(selectorK.THREAD_LAST_CHANGELOG);
if (!spoiler) return ""; if (!spoiler) return "";
const changelogHTML = await page.evaluate( const changelogHTML = await page.evaluate(
/* istanbul ignore next */ /* istanbul ignore next */
(e) => e.innerText, (e) => e.innerText,
spoiler spoiler
); );
let parsedText = HTMLParser.parse(changelogHTML).structuredText; let parsedText = HTMLParser.parse(changelogHTML).structuredText;
// Clean the text // Clean the text
if (parsedText.startsWith("Spoiler")) if (parsedText.startsWith("Spoiler"))
parsedText = parsedText.replace("Spoiler", ""); parsedText = parsedText.replace("Spoiler", "");
if (parsedText.startsWith(":")) parsedText = parsedText.replace(":", ""); if (parsedText.startsWith(":")) parsedText = parsedText.replace(":", "");
return parsedText.trim(); return parsedText.trim();
} }
/** /**
@ -340,64 +340,64 @@ async function getLastChangelog(page) {
/* istanbul ignore next */ /* istanbul ignore next */
// skipcq: JS-0128 // skipcq: JS-0128
async function getGameDownloadLink(page) { async function getGameDownloadLink(page) {
// Most used hosting platforms // Most used hosting platforms
const hostingPlatforms = [ const hostingPlatforms = [
"MEGA", "MEGA",
"NOPY", "NOPY",
"FILESUPLOAD", "FILESUPLOAD",
"MIXDROP", "MIXDROP",
"UPLOADHAVEN", "UPLOADHAVEN",
"PIXELDRAIN", "PIXELDRAIN",
"FILESFM", "FILESFM",
]; ];
// Supported OS platforms // Supported OS platforms
const platformOS = ["WIN", "LINUX", "MAC", "ALL"]; const platformOS = ["WIN", "LINUX", "MAC", "ALL"];
// Gets the <span> which contains the download links // Gets the <span> which contains the download links
const temp = await page.$$(selectorK.DOWNLOAD_LINKS_CONTAINER); const temp = await page.$$(selectorK.DOWNLOAD_LINKS_CONTAINER);
if (temp.length === 0) return []; if (temp.length === 0) return [];
// Look for the container that contains the links // Look for the container that contains the links
// It is necessary because the same css selector // It is necessary because the same css selector
// also identifies other elements on the page // also identifies other elements on the page
let container = null; let container = null;
for (const candidate of temp) { for (const candidate of temp) {
if (container !== null) break; if (container !== null) break;
const upperText = ( const upperText = (
await page.evaluate( await page.evaluate(
/* istanbul ignore next */ /* istanbul ignore next */
(e) => e.innerText, (e) => e.innerText,
candidate candidate
) )
).toUpperCase(); ).toUpperCase();
// Search if the container contains the name of a hosting platform // Search if the container contains the name of a hosting platform
for (const p of hostingPlatforms) { for (const p of hostingPlatforms) {
if (upperText.includes(p)) { if (upperText.includes(p)) {
container = candidate; container = candidate;
break; break;
} }
}
} }
} if (container === null) return [];
if (container === null) return [];
// Extract the HTML text from the container // Extract the HTML text from the container
const searchText = ( const searchText = (
await page.evaluate( await page.evaluate(
/* istanbul ignore next */ /* istanbul ignore next */
(e) => e.innerHTML, (e) => e.innerHTML,
container container
) )
).toLowerCase(); ).toLowerCase();
// Parse the download links // Parse the download links
const downloadData = []; const downloadData = [];
for (const platform of platformOS) { for (const platform of platformOS) {
const data = extractGameHostingData(platform, searchText); const data = extractGameHostingData(platform, searchText);
downloadData.push(...data); downloadData.push(...data);
} }
return downloadData; return downloadData;
} }
/** /**
@ -411,57 +411,57 @@ async function getGameDownloadLink(page) {
*/ */
/* istanbul ignore next */ /* istanbul ignore next */
function extractGameHostingData(platform, text) { function extractGameHostingData(platform, text) {
const PLATFORM_BOLD_OPEN = "<b>"; const PLATFORM_BOLD_OPEN = "<b>";
const CONTAINER_SPAN_CLOSE = "</span>"; const CONTAINER_SPAN_CLOSE = "</span>";
const LINK_OPEN = "<a"; const LINK_OPEN = "<a";
const LINK_CLOSE = "</a>"; const LINK_CLOSE = "</a>";
const HREF_START = "href='"; const HREF_START = "href='";
const HREF_END = "'"; const HREF_END = "'";
const TAG_CLOSE = ">"; const TAG_CLOSE = ">";
// Identify the individual platforms // Identify the individual platforms
let startIndex = text.indexOf(platform.toLowerCase()); let startIndex = text.indexOf(platform.toLowerCase());
if (startIndex === -1) return []; if (startIndex === -1) return [];
else startIndex += platform.length; else startIndex += platform.length;
// Find the <b>platform</b> // Find the <b>platform</b>
let endIndex = let endIndex =
text.indexOf(PLATFORM_BOLD_OPEN, startIndex) + PLATFORM_BOLD_OPEN.length; text.indexOf(PLATFORM_BOLD_OPEN, startIndex) + PLATFORM_BOLD_OPEN.length;
// Find the end of the container // Find the end of the container
if (endIndex === -1) if (endIndex === -1)
endIndex = endIndex =
text.indexOf(CONTAINER_SPAN_CLOSE, startIndex) + text.indexOf(CONTAINER_SPAN_CLOSE, startIndex) +
CONTAINER_SPAN_CLOSE.length; CONTAINER_SPAN_CLOSE.length;
text = text.substring(startIndex, endIndex); text = text.substring(startIndex, endIndex);
const downloadData = []; const downloadData = [];
const linkTags = text.split(LINK_OPEN); const linkTags = text.split(LINK_OPEN);
for (const tag of linkTags) { for (const tag of linkTags) {
// Ignore non-link string // Ignore non-link string
if (!tag.includes(HREF_START)) continue; if (!tag.includes(HREF_START)) continue;
// Find the hosting platform name // Find the hosting platform name
startIndex = tag.indexOf(TAG_CLOSE) + TAG_CLOSE.length; startIndex = tag.indexOf(TAG_CLOSE) + TAG_CLOSE.length;
endIndex = tag.indexOf(LINK_CLOSE, startIndex); endIndex = tag.indexOf(LINK_CLOSE, startIndex);
const hosting = tag.substring(startIndex, endIndex); const hosting = tag.substring(startIndex, endIndex);
// Find the 'href' attribute // Find the 'href' attribute
startIndex = tag.indexOf(HREF_START) + HREF_START.length; startIndex = tag.indexOf(HREF_START) + HREF_START.length;
endIndex = tag.indexOf(HREF_END, startIndex); endIndex = tag.indexOf(HREF_END, startIndex);
const link = tag.substring(startIndex, endIndex); const link = tag.substring(startIndex, endIndex);
if (urlHelper.isStringAValidURL(link)) { if (urlHelper.isStringAValidURL(link)) {
const gd = new GameDownload(); const gd = new GameDownload();
gd.hosting = hosting.toUpperCase(); gd.hosting = hosting.toUpperCase();
gd.link = link; gd.link = link;
gd.supportedOS = platform.toUpperCase(); gd.supportedOS = platform.toUpperCase();
downloadData.push(gd); downloadData.push(gd);
}
} }
} return downloadData;
return downloadData;
} }
//#endregion Private methods //#endregion Private methods

View File

@ -18,46 +18,46 @@ const { isF95URL } = require("./url-helper.js");
* @returns {Promise<String[]>} List of URL of possible games obtained from the preliminary research on the F95 portal * @returns {Promise<String[]>} List of URL of possible games obtained from the preliminary research on the F95 portal
*/ */
module.exports.getSearchGameResults = async function (browser, gamename) { module.exports.getSearchGameResults = async function (browser, gamename) {
shared.logger.info(`Searching ${gamename} on F95Zone`); shared.logger.info(`Searching ${gamename} on F95Zone`);
const page = await preparePage(browser); // Set new isolated page const page = await preparePage(browser); // Set new isolated page
await page.setCookie(...shared.cookies); // Set cookies to avoid login await page.setCookie(...shared.cookies); // Set cookies to avoid login
await page.goto(urlK.F95_SEARCH_URL, { await page.goto(urlK.F95_SEARCH_URL, {
waitUntil: shared.WAIT_STATEMENT, waitUntil: shared.WAIT_STATEMENT,
}); // Go to the search form and wait for it }); // Go to the search form and wait for it
// Explicitly wait for the required items to load // Explicitly wait for the required items to load
await Promise.all([ await Promise.all([
page.waitForSelector(selectorK.SEARCH_FORM_TEXTBOX), page.waitForSelector(selectorK.SEARCH_FORM_TEXTBOX),
page.waitForSelector(selectorK.TITLE_ONLY_CHECKBOX), page.waitForSelector(selectorK.TITLE_ONLY_CHECKBOX),
page.waitForSelector(selectorK.SEARCH_ONLY_GAMES_OPTION), page.waitForSelector(selectorK.SEARCH_ONLY_GAMES_OPTION),
page.waitForSelector(selectorK.SEARCH_BUTTON), page.waitForSelector(selectorK.SEARCH_BUTTON),
]); ]);
await page.type(selectorK.SEARCH_FORM_TEXTBOX, gamename); // Type the game we desire await page.type(selectorK.SEARCH_FORM_TEXTBOX, gamename); // Type the game we desire
await page.click(selectorK.TITLE_ONLY_CHECKBOX); // Select only the thread with the game in the titles await page.click(selectorK.TITLE_ONLY_CHECKBOX); // Select only the thread with the game in the titles
await page.click(selectorK.SEARCH_ONLY_GAMES_OPTION); // Search only games and mod await page.click(selectorK.SEARCH_ONLY_GAMES_OPTION); // Search only games and mod
await Promise.all([ await Promise.all([
page.click(selectorK.SEARCH_BUTTON), // Execute search page.click(selectorK.SEARCH_BUTTON), // Execute search
page.waitForNavigation({ page.waitForNavigation({
waitUntil: shared.WAIT_STATEMENT, waitUntil: shared.WAIT_STATEMENT,
}), // Wait for page to load }), // Wait for page to load
]); ]);
// Select all conversation titles // Select all conversation titles
const resultsThread = await page.$$(selectorK.SEARCH_THREADS_RESULTS_BODY); const resultsThread = await page.$$(selectorK.SEARCH_THREADS_RESULTS_BODY);
// For each element found extract the info about the conversation // For each element found extract the info about the conversation
shared.logger.info("Extracting info from conversations"); shared.logger.info("Extracting info from conversations");
const results = []; const results = [];
for (const element of resultsThread) { for (const element of resultsThread) {
const gameUrl = await getOnlyGameThreads(page, element); const gameUrl = await getOnlyGameThreads(page, element);
if (gameUrl !== null) results.push(gameUrl); if (gameUrl !== null) results.push(gameUrl);
} }
shared.logger.info(`Find ${results.length} conversations`); shared.logger.info(`Find ${results.length} conversations`);
await page.close(); // Close the page await page.close(); // Close the page
return results; return results;
}; };
//#region Private methods //#region Private methods
@ -69,16 +69,16 @@ module.exports.getSearchGameResults = async function (browser, gamename) {
* @return {Promise<String>} URL of the game/mod or null if the URL is not of a game * @return {Promise<String>} URL of the game/mod or null if the URL is not of a game
*/ */
async function getOnlyGameThreads(page, divHandle) { async function getOnlyGameThreads(page, divHandle) {
// Obtain the elements containing the basic information // Obtain the elements containing the basic information
const titleHandle = await divHandle.$(selectorK.THREAD_TITLE); const titleHandle = await divHandle.$(selectorK.THREAD_TITLE);
const forumHandle = await divHandle.$(selectorK.SEARCH_THREADS_MEMBERSHIP); const forumHandle = await divHandle.$(selectorK.SEARCH_THREADS_MEMBERSHIP);
// Get the forum where the thread was posted // Get the forum where the thread was posted
const forum = await getMembershipForum(page, forumHandle); const forum = await getMembershipForum(page, forumHandle);
if (forum !== "GAMES" && forum !== "MODS") return null; if (forum !== "GAMES" && forum !== "MODS") return null;
// Get the URL of the thread from the title // Get the URL of the thread from the title
return await getThreadURL(page, titleHandle); return await getThreadURL(page, titleHandle);
} }
/** /**
@ -89,23 +89,23 @@ async function getOnlyGameThreads(page, divHandle) {
* @returns {Promise<String>} Uppercase membership category * @returns {Promise<String>} Uppercase membership category
*/ */
async function getMembershipForum(page, handle) { async function getMembershipForum(page, handle) {
// The link can be something like: // The link can be something like:
// + /forums/request.NUMBER/ // + /forums/request.NUMBER/
// + /forums/game-recommendations-identification.NUMBER/ // + /forums/game-recommendations-identification.NUMBER/
// + /forums/games.NUMBER/ <-- We need this // + /forums/games.NUMBER/ <-- We need this
let link = await page.evaluate( let link = await page.evaluate(
/* istanbul ignore next */ /* istanbul ignore next */
(e) => e.getAttribute("href"), (e) => e.getAttribute("href"),
handle handle
); );
// Parse link // Parse link
link = link.replace("/forums/", ""); link = link.replace("/forums/", "");
const endIndex = link.indexOf("."); const endIndex = link.indexOf(".");
const forum = link.substring(0, endIndex); const forum = link.substring(0, endIndex);
return forum.toUpperCase(); return forum.toUpperCase();
} }
/** /**
@ -116,17 +116,17 @@ async function getMembershipForum(page, handle) {
* @returns {Promise<String>} URL of the thread * @returns {Promise<String>} URL of the thread
*/ */
async function getThreadURL(page, handle) { async function getThreadURL(page, handle) {
const relativeURLThread = await page.evaluate( const relativeURLThread = await page.evaluate(
/* istanbul ignore next */ /* istanbul ignore next */
(e) => e.querySelector("a").href, (e) => e.querySelector("a").href,
handle handle
); );
// Some game already have a full URL... // Some game already have a full URL...
if (isF95URL(relativeURLThread)) return relativeURLThread; if (isF95URL(relativeURLThread)) return relativeURLThread;
// ... else compose the URL and return // ... else compose the URL and return
const urlThread = new URL(relativeURLThread, urlK.F95_BASE_URL).toString(); const urlThread = new URL(relativeURLThread, urlK.F95_BASE_URL).toString();
return urlThread; return urlThread;
} }
//#endregion Private methods //#endregion Private methods

View File

@ -13,20 +13,20 @@ const shared = require("./shared.js");
* @returns {Promise<puppeteer.Browser>} Created browser * @returns {Promise<puppeteer.Browser>} Created browser
*/ */
module.exports.prepareBrowser = async function () { module.exports.prepareBrowser = async function () {
// Create a headless browser // Create a headless browser
let browser = null; let browser = null;
if (shared.chromiumLocalPath) { if (shared.chromiumLocalPath) {
browser = await puppeteer.launch({ browser = await puppeteer.launch({
executablePath: shared.chromiumLocalPath, executablePath: shared.chromiumLocalPath,
headless: !shared.debug, // Use GUI when debug = true headless: !shared.debug, // Use GUI when debug = true
}); });
} else { } else {
browser = await puppeteer.launch({ browser = await puppeteer.launch({
headless: !shared.debug, // Use GUI when debug = true headless: !shared.debug, // Use GUI when debug = true
}); });
} }
return browser; return browser;
}; };
/** /**
@ -37,24 +37,24 @@ module.exports.prepareBrowser = async function () {
* @returns {Promise<puppeteer.Page>} New page * @returns {Promise<puppeteer.Page>} New page
*/ */
module.exports.preparePage = async function (browser) { module.exports.preparePage = async function (browser) {
// Create new page in the browser argument // Create new page in the browser argument
const page = await browser.newPage(); const page = await browser.newPage();
// Block image download // Block image download
await page.setRequestInterception(true); await page.setRequestInterception(true);
page.on("request", (request) => { page.on("request", (request) => {
if (request.resourceType() === "image") request.abort(); if (request.resourceType() === "image") request.abort();
else if (request.resourceType === "font") request.abort(); else if (request.resourceType === "font") request.abort();
// else if (request.resourceType() == 'stylesheet') request.abort(); // else if (request.resourceType() == 'stylesheet') request.abort();
// else if(request.resourceType == 'media') request.abort(); // else if(request.resourceType == 'media') request.abort();
else request.continue(); else request.continue();
}); });
// Set custom user-agent // Set custom user-agent
const userAgent = const userAgent =
"Mozilla/5.0 (X11; Linux x86_64)" + "Mozilla/5.0 (X11; Linux x86_64)" +
"AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.39 Safari/537.36"; "AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.39 Safari/537.36";
await page.setUserAgent(userAgent); await page.setUserAgent(userAgent);
return page; return page;
}; };

View File

@ -9,7 +9,7 @@ const log4js = require("log4js");
* Class containing variables shared between modules. * Class containing variables shared between modules.
*/ */
class Shared { class Shared {
//#region Properties //#region Properties
/** /**
* Shows log messages and other useful functions for module debugging. * Shows log messages and other useful functions for module debugging.
* @type Boolean * @type Boolean
@ -64,63 +64,63 @@ class Shared {
* @returns {Boolean} * @returns {Boolean}
*/ */
static get debug() { static get debug() {
return this.#_debug; return this.#_debug;
} }
/** /**
* Indicates whether a user is logged in to the F95Zone platform or not. * Indicates whether a user is logged in to the F95Zone platform or not.
* @returns {Boolean} * @returns {Boolean}
*/ */
static get isLogged() { static get isLogged() {
return this.#_isLogged; return this.#_isLogged;
} }
/** /**
* List of cookies obtained from the F95Zone platform. * List of cookies obtained from the F95Zone platform.
* @returns {Object[]} * @returns {Object[]}
*/ */
static get cookies() { static get cookies() {
return this.#_cookies; return this.#_cookies;
} }
/** /**
* List of possible game engines used for development. * List of possible game engines used for development.
* @returns {String[]} * @returns {String[]}
*/ */
static get engines() { static get engines() {
return this.#_engines; return this.#_engines;
} }
/** /**
* List of possible development states that a game can assume. * List of possible development states that a game can assume.
* @returns {String[]} * @returns {String[]}
*/ */
static get statuses() { static get statuses() {
return this.#_statuses; return this.#_statuses;
} }
/** /**
* Directory to save the API cache. * Directory to save the API cache.
* @returns {String} * @returns {String}
*/ */
static get cacheDir() { static get cacheDir() {
return this.#_cacheDir; return this.#_cacheDir;
} }
/** /**
* Path to the F95 platform cache. * Path to the F95 platform cache.
* @returns {String} * @returns {String}
*/ */
static get cookiesCachePath() { static get cookiesCachePath() {
return join(this.#_cacheDir, "cookies.json"); return join(this.#_cacheDir, "cookies.json");
} }
/** /**
* Path to the game engine cache. * Path to the game engine cache.
* @returns {String} * @returns {String}
*/ */
static get enginesCachePath() { static get enginesCachePath() {
return join(this.#_cacheDir, "engines.json"); return join(this.#_cacheDir, "engines.json");
} }
/** /**
* Path to the cache of possible game states. * Path to the cache of possible game states.
* @returns {String} * @returns {String}
*/ */
static get statusesCachePath() { static get statusesCachePath() {
return join(this.#_cacheDir, "statuses.json"); return join(this.#_cacheDir, "statuses.json");
} }
/** /**
* If true, it opens a new browser for each request * If true, it opens a new browser for each request
@ -128,44 +128,44 @@ class Shared {
* @returns {Boolean} * @returns {Boolean}
*/ */
static get isolation() { static get isolation() {
return this.#_isolation; return this.#_isolation;
} }
/** /**
* Logger object used to write to both file and console. * Logger object used to write to both file and console.
* @returns {log4js.Logger} * @returns {log4js.Logger}
*/ */
static get logger() { static get logger() {
return this.#_logger; return this.#_logger;
} }
//#endregion Getters //#endregion Getters
//#region Setters //#region Setters
static set cookies(val) { static set cookies(val) {
this.#_cookies = val; this.#_cookies = val;
} }
static set engines(val) { static set engines(val) {
this.#_engines = val; this.#_engines = val;
} }
static set statuses(val) { static set statuses(val) {
this.#_statuses = val; this.#_statuses = val;
} }
static set cacheDir(val) { static set cacheDir(val) {
this.#_cacheDir = val; this.#_cacheDir = val;
} }
static set debug(val) { static set debug(val) {
this.#_debug = val; this.#_debug = val;
} }
static set isLogged(val) { static set isLogged(val) {
this.#_isLogged = val; this.#_isLogged = val;
} }
static set isolation(val) { static set isolation(val) {
this.#_isolation = val; this.#_isolation = val;
} }
//#endregion Setters //#endregion Setters
} }

View File

@ -2,7 +2,7 @@
// Public modules from npm // Public modules from npm
const ky = require("ky-universal").create({ const ky = require("ky-universal").create({
throwHttpErrors: false, throwHttpErrors: false,
}); });
// Modules from file // Modules from file
@ -15,8 +15,8 @@ const { F95_BASE_URL } = require("./constants/url.js");
* @returns {Boolean} true if the url belongs to the domain, false otherwise * @returns {Boolean} true if the url belongs to the domain, false otherwise
*/ */
module.exports.isF95URL = function (url) { module.exports.isF95URL = function (url) {
if (url.toString().startsWith(F95_BASE_URL)) return true; if (url.toString().startsWith(F95_BASE_URL)) return true;
else return false; else return false;
}; };
/** /**
@ -26,12 +26,12 @@ module.exports.isF95URL = function (url) {
* @returns {Boolean} true if the string is a valid URL, false otherwise * @returns {Boolean} true if the string is a valid URL, false otherwise
*/ */
module.exports.isStringAValidURL = function (url) { module.exports.isStringAValidURL = function (url) {
try { try {
new URL(url); // skipcq: JS-0078 new URL(url); // skipcq: JS-0078
return true; return true;
} catch (err) { } catch (err) {
return false; return false;
} }
}; };
/** /**
@ -42,22 +42,22 @@ module.exports.isStringAValidURL = function (url) {
* @returns {Promise<Boolean>} true if the URL exists, false otherwise * @returns {Promise<Boolean>} true if the URL exists, false otherwise
*/ */
module.exports.urlExists = async function (url, checkRedirect) { module.exports.urlExists = async function (url, checkRedirect) {
if (!exports.isStringAValidURL(url)) { if (!exports.isStringAValidURL(url)) {
return false; return false;
} }
const response = await ky.head(url); const response = await ky.head(url);
let valid = response !== undefined && !/4\d\d/.test(response.status); let valid = response !== undefined && !/4\d\d/.test(response.status);
if (!valid) return false; if (!valid) return false;
if (checkRedirect) { if (checkRedirect) {
const redirectUrl = await exports.getUrlRedirect(url); const redirectUrl = await exports.getUrlRedirect(url);
if (redirectUrl === url) valid = true; if (redirectUrl === url) valid = true;
else valid = false; else valid = false;
} }
return valid; return valid;
}; };
/** /**
@ -67,6 +67,6 @@ module.exports.urlExists = async function (url, checkRedirect) {
* @returns {Promise<String>} Redirect URL or the passed URL * @returns {Promise<String>} Redirect URL or the passed URL
*/ */
module.exports.getUrlRedirect = async function (url) { module.exports.getUrlRedirect = async function (url) {
const response = await ky.head(url); const response = await ky.head(url);
return response.url; return response.url;
}; };

View File

@ -27,264 +27,264 @@ const FAKE_PASSWORD = "fake_password";
//F95API.debug(false); //F95API.debug(false);
function randomSleep() { function randomSleep() {
const random = Math.floor(Math.random() * 500) + 50; const random = Math.floor(Math.random() * 500) + 50;
sleep.msleep(500 + random); sleep.msleep(500 + random);
} }
describe("Login without cookies", function () { describe("Login without cookies", function () {
//#region Set-up //#region Set-up
this.timeout(30000); // All tests in this suite get 30 seconds before timeout this.timeout(30000); // All tests in this suite get 30 seconds before timeout
before("Set isolation", function () { before("Set isolation", function () {
F95API.setIsolation(true); F95API.setIsolation(true);
}); });
beforeEach("Remove all cookies", function () { beforeEach("Remove all cookies", function () {
// Runs before each test in this block // Runs before each test in this block
if (fs.existsSync(COOKIES_SAVE_PATH)) fs.unlinkSync(COOKIES_SAVE_PATH); if (fs.existsSync(COOKIES_SAVE_PATH)) fs.unlinkSync(COOKIES_SAVE_PATH);
if (F95API.isLogged()) F95API.logout(); if (F95API.isLogged()) F95API.logout();
}); });
//#endregion Set-up //#endregion Set-up
let testOrder = 0; let testOrder = 0;
it("Test with valid credentials", async function () { it("Test with valid credentials", async function () {
// Gain exclusive use of the cookies // Gain exclusive use of the cookies
while (testOrder !== 0) randomSleep(); while (testOrder !== 0) randomSleep();
const result = await F95API.login(USERNAME, PASSWORD); const result = await F95API.login(USERNAME, PASSWORD);
expect(result.success).to.be.true; expect(result.success).to.be.true;
expect(result.message).equal("Authentication successful"); expect(result.message).equal("Authentication successful");
testOrder = 1; testOrder = 1;
}); });
it("Test with invalid username", async function () { it("Test with invalid username", async function () {
// Gain exclusive use of the cookies // Gain exclusive use of the cookies
while (testOrder !== 1) randomSleep(); while (testOrder !== 1) randomSleep();
const result = await F95API.login(FAKE_USERNAME, FAKE_PASSWORD); const result = await F95API.login(FAKE_USERNAME, FAKE_PASSWORD);
expect(result.success).to.be.false; expect(result.success).to.be.false;
expect(result.message).to.equal("Incorrect username"); expect(result.message).to.equal("Incorrect username");
testOrder = 2; testOrder = 2;
}); });
it("Test with invalid password", async function () { it("Test with invalid password", async function () {
// Gain exclusive use of the cookies // Gain exclusive use of the cookies
while (testOrder !== 2) randomSleep(); while (testOrder !== 2) randomSleep();
const result = await F95API.login(USERNAME, FAKE_PASSWORD); const result = await F95API.login(USERNAME, FAKE_PASSWORD);
expect(result.success).to.be.false; expect(result.success).to.be.false;
expect(result.message).to.equal("Incorrect password"); expect(result.message).to.equal("Incorrect password");
testOrder = 3; testOrder = 3;
}); });
it("Test with invalid credentials", async function () { it("Test with invalid credentials", async function () {
// Gain exclusive use of the cookies // Gain exclusive use of the cookies
while (testOrder !== 3) randomSleep(); while (testOrder !== 3) randomSleep();
const result = await F95API.login(FAKE_USERNAME, FAKE_PASSWORD); const result = await F95API.login(FAKE_USERNAME, FAKE_PASSWORD);
expect(result.success).to.be.false; expect(result.success).to.be.false;
expect(result.message).to.equal("Incorrect username"); // It should first check the username expect(result.message).to.equal("Incorrect username"); // It should first check the username
testOrder = 4; testOrder = 4;
}); });
}); });
describe("Login with cookies", function () { describe("Login with cookies", function () {
//#region Set-up //#region Set-up
this.timeout(30000); // All tests in this suite get 30 seconds before timeout this.timeout(30000); // All tests in this suite get 30 seconds before timeout
before("Log in to create cookies then logout", async function () { before("Log in to create cookies then logout", async function () {
// Runs once before the first test in this block // Runs once before the first test in this block
if (!fs.existsSync(COOKIES_SAVE_PATH)) if (!fs.existsSync(COOKIES_SAVE_PATH))
await F95API.login(USERNAME, PASSWORD); // Download cookies await F95API.login(USERNAME, PASSWORD); // Download cookies
if (F95API.isLogged()) F95API.logout(); if (F95API.isLogged()) F95API.logout();
}); });
//#endregion Set-up //#endregion Set-up
it("Test with valid credentials", async function () { it("Test with valid credentials", async function () {
const result = await F95API.login(USERNAME, PASSWORD); const result = await F95API.login(USERNAME, PASSWORD);
expect(result.success).to.be.true; expect(result.success).to.be.true;
expect(result.message).equal("Logged with cookies"); expect(result.message).equal("Logged with cookies");
}); });
}); });
describe("Load base data without cookies", function () { describe("Load base data without cookies", function () {
//#region Set-up //#region Set-up
this.timeout(30000); // All tests in this suite get 30 seconds before timeout this.timeout(30000); // All tests in this suite get 30 seconds before timeout
before("Delete cache if exists", function () { before("Delete cache if exists", function () {
// Runs once before the first test in this block // Runs once before the first test in this block
if (fs.existsSync(ENGINES_SAVE_PATH)) fs.unlinkSync(ENGINES_SAVE_PATH); if (fs.existsSync(ENGINES_SAVE_PATH)) fs.unlinkSync(ENGINES_SAVE_PATH);
if (fs.existsSync(STATUSES_SAVE_PATH)) fs.unlinkSync(STATUSES_SAVE_PATH); if (fs.existsSync(STATUSES_SAVE_PATH)) fs.unlinkSync(STATUSES_SAVE_PATH);
}); });
//#endregion Set-up //#endregion Set-up
it("With login", async function () { it("With login", async function () {
const loginResult = await F95API.login(USERNAME, PASSWORD); const loginResult = await F95API.login(USERNAME, PASSWORD);
expect(loginResult.success).to.be.true; expect(loginResult.success).to.be.true;
const result = await F95API.loadF95BaseData(); const result = await F95API.loadF95BaseData();
const enginesCacheExists = fs.existsSync(ENGINES_SAVE_PATH); const enginesCacheExists = fs.existsSync(ENGINES_SAVE_PATH);
const statusesCacheExists = fs.existsSync(STATUSES_SAVE_PATH); const statusesCacheExists = fs.existsSync(STATUSES_SAVE_PATH);
expect(result).to.be.true; expect(result).to.be.true;
expect(enginesCacheExists).to.be.true; expect(enginesCacheExists).to.be.true;
expect(statusesCacheExists).to.be.true; expect(statusesCacheExists).to.be.true;
}); });
it("Without login", async function () { it("Without login", async function () {
if (F95API.isLogged()) F95API.logout(); if (F95API.isLogged()) F95API.logout();
const result = await F95API.loadF95BaseData(); const result = await F95API.loadF95BaseData();
expect(result).to.be.false; expect(result).to.be.false;
}); });
}); });
describe("Search game data", function () { describe("Search game data", function () {
//#region Set-up //#region Set-up
this.timeout(60000); // All tests in this suite get 60 seconds before timeout this.timeout(60000); // All tests in this suite get 60 seconds before timeout
beforeEach("Prepare API", function () { beforeEach("Prepare API", function () {
// Runs once before the first test in this block // Runs once before the first test in this block
if (F95API.isLogged()) F95API.logout(); if (F95API.isLogged()) F95API.logout();
}); });
//#endregion Set-up //#endregion Set-up
let testGame = null; let testGame = null;
it("Search game when logged", async function () { it("Search game when logged", async function () {
const loginResult = await F95API.login(USERNAME, PASSWORD); const loginResult = await F95API.login(USERNAME, PASSWORD);
expect(loginResult.success).to.be.true; expect(loginResult.success).to.be.true;
const loadResult = await F95API.loadF95BaseData(); const loadResult = await F95API.loadF95BaseData();
expect(loadResult).to.be.true; expect(loadResult).to.be.true;
// This test depend on the data on F95Zone at // This test depend on the data on F95Zone at
// https://f95zone.to/threads/kingdom-of-deception-v0-10-8-hreinn-games.2733/ // https://f95zone.to/threads/kingdom-of-deception-v0-10-8-hreinn-games.2733/
const gamesList = await F95API.getGameData("Kingdom of Deception", false); const gamesList = await F95API.getGameData("Kingdom of Deception", false);
expect(gamesList.length, "Should find only the game").to.equal(1); expect(gamesList.length, "Should find only the game").to.equal(1);
const result = gamesList[0]; const result = gamesList[0];
const src = "https://attachments.f95zone.to/2018/09/162821_f9nXfwF.png"; const src = "https://attachments.f95zone.to/2018/09/162821_f9nXfwF.png";
// Test only the main information // Test only the main information
expect(result.name).to.equal("Kingdom of Deception"); expect(result.name).to.equal("Kingdom of Deception");
expect(result.author).to.equal("Hreinn Games"); expect(result.author).to.equal("Hreinn Games");
expect(result.isMod, "Should be false").to.be.false; expect(result.isMod, "Should be false").to.be.false;
expect(result.engine).to.equal("REN'PY"); expect(result.engine).to.equal("REN'PY");
expect(result.previewSource).to.equal(src); // Could be null -> Why sometimes doesn't get the image? expect(result.previewSource).to.equal(src); // Could be null -> Why sometimes doesn't get the image?
testGame = Object.assign({}, result); testGame = Object.assign({}, result);
}); });
it("Search game when not logged", async function () { it("Search game when not logged", async function () {
const result = await F95API.getGameData("Kingdom of Deception", false); const result = await F95API.getGameData("Kingdom of Deception", false);
expect(result, "Without being logged should return null").to.be.null; expect(result, "Without being logged should return null").to.be.null;
}); });
it("Test game serialization", function () { it("Test game serialization", function () {
const json = JSON.stringify(testGame); const json = JSON.stringify(testGame);
const parsedGameInfo = JSON.parse(json); const parsedGameInfo = JSON.parse(json);
const result = _.isEqual(parsedGameInfo, testGame); const result = _.isEqual(parsedGameInfo, testGame);
expect(result).to.be.true; expect(result).to.be.true;
}); });
}); });
describe("Load user data", function () { describe("Load user data", function () {
//#region Set-up //#region Set-up
this.timeout(30000); // All tests in this suite get 30 seconds before timeout this.timeout(30000); // All tests in this suite get 30 seconds before timeout
//#endregion Set-up //#endregion Set-up
it("Retrieve when logged", async function () { it("Retrieve when logged", async function () {
// Login // Login
await F95API.login(USERNAME, PASSWORD); await F95API.login(USERNAME, PASSWORD);
// Then retrieve user data // Then retrieve user data
const data = await F95API.getUserData(); const data = await F95API.getUserData();
expect(data).to.exist; expect(data).to.exist;
expect(data.username).to.equal(USERNAME); expect(data.username).to.equal(USERNAME);
}); });
it("Retrieve when not logged", async function () { it("Retrieve when not logged", async function () {
// Logout // Logout
if (F95API.isLogged()) F95API.logout(); if (F95API.isLogged()) F95API.logout();
// Try to retrieve user data // Try to retrieve user data
const data = await F95API.getUserData(); const data = await F95API.getUserData();
expect(data).to.be.null; expect(data).to.be.null;
}); });
}); });
describe("Check game update", function () { describe("Check game update", function () {
//#region Set-up //#region Set-up
this.timeout(30000); // All tests in this suite get 30 seconds before timeout this.timeout(30000); // All tests in this suite get 30 seconds before timeout
//#endregion Set-up //#endregion Set-up
it("Get online game and verify that no update exists", async function () { it("Get online game and verify that no update exists", async function () {
const loginResult = await F95API.login(USERNAME, PASSWORD); const loginResult = await F95API.login(USERNAME, PASSWORD);
expect(loginResult.success).to.be.true; expect(loginResult.success).to.be.true;
const loadResult = await F95API.loadF95BaseData(); const loadResult = await F95API.loadF95BaseData();
expect(loadResult).to.be.true; expect(loadResult).to.be.true;
// This test depend on the data on F95Zone at // This test depend on the data on F95Zone at
// https://f95zone.to/threads/kingdom-of-deception-v0-10-8-hreinn-games.2733/ // https://f95zone.to/threads/kingdom-of-deception-v0-10-8-hreinn-games.2733/
const result = (await F95API.getGameData("Kingdom of Deception", false))[0]; const result = (await F95API.getGameData("Kingdom of Deception", false))[0];
const update = await F95API.chekIfGameHasUpdate(result); const update = await F95API.chekIfGameHasUpdate(result);
expect(update).to.be.false; expect(update).to.be.false;
}); });
it("Verify that update exists from old URL", async function () { it("Verify that update exists from old URL", async function () {
const loginResult = await F95API.login(USERNAME, PASSWORD); const loginResult = await F95API.login(USERNAME, PASSWORD);
expect(loginResult.success).to.be.true; expect(loginResult.success).to.be.true;
// This test depend on the data on F95Zone at // This test depend on the data on F95Zone at
// https://f95zone.to/threads/perverted-education-v0-9701-april-ryan.1854/ // https://f95zone.to/threads/perverted-education-v0-9701-april-ryan.1854/
const url = const url =
"https://f95zone.to/threads/perverted-education-v0-9701-april-ryan.1854/"; "https://f95zone.to/threads/perverted-education-v0-9701-april-ryan.1854/";
const result = await F95API.getGameDataFromURL(url); const result = await F95API.getGameDataFromURL(url);
result.version = "0.9600"; result.version = "0.9600";
const update = await F95API.chekIfGameHasUpdate(result); const update = await F95API.chekIfGameHasUpdate(result);
expect(update).to.be.true; expect(update).to.be.true;
}); });
}); });
describe("Test url-helper", function () { describe("Test url-helper", function () {
//#region Set-up //#region Set-up
this.timeout(30000); // All tests in this suite get 30 seconds before timeout this.timeout(30000); // All tests in this suite get 30 seconds before timeout
//#endregion Set-up //#endregion Set-up
it("Check if URL exists", async function () { it("Check if URL exists", async function () {
// Check generic URLs... // Check generic URLs...
let exists = await urlHelper.urlExists("https://www.google.com/"); let exists = await urlHelper.urlExists("https://www.google.com/");
expect(exists, "Complete valid URL").to.be.true; expect(exists, "Complete valid URL").to.be.true;
exists = await urlHelper.urlExists("www.google.com"); exists = await urlHelper.urlExists("www.google.com");
expect(exists, "URl without protocol prefix").to.be.false; expect(exists, "URl without protocol prefix").to.be.false;
exists = await urlHelper.urlExists("https://www.google/"); exists = await urlHelper.urlExists("https://www.google/");
expect(exists, "URL without third level domain").to.be.false; expect(exists, "URL without third level domain").to.be.false;
// Now check for more specific URLs (with redirect)... // Now check for more specific URLs (with redirect)...
exists = await urlHelper.urlExists( exists = await urlHelper.urlExists(
"https://f95zone.to/threads/perverted-education-v0-9601-april-ryan.1854/" "https://f95zone.to/threads/perverted-education-v0-9601-april-ryan.1854/"
); );
expect(exists, "URL with redirect without check").to.be.true; expect(exists, "URL with redirect without check").to.be.true;
exists = await urlHelper.urlExists( exists = await urlHelper.urlExists(
"https://f95zone.to/threads/perverted-education-v0-9601-april-ryan.1854/", "https://f95zone.to/threads/perverted-education-v0-9601-april-ryan.1854/",
true true
); );
expect(exists, "URL with redirect with check").to.be.false; expect(exists, "URL with redirect with check").to.be.false;
}); });
it("Check if URL belong to the platform", async function () { it("Check if URL belong to the platform", async function () {
let belong = urlHelper.isF95URL( let belong = urlHelper.isF95URL(
"https://f95zone.to/threads/perverted-education-v0-9601-april-ryan.1854/" "https://f95zone.to/threads/perverted-education-v0-9601-april-ryan.1854/"
); );
expect(belong).to.be.true; expect(belong).to.be.true;
belong = urlHelper.isF95URL("https://www.google/"); belong = urlHelper.isF95URL("https://www.google/");
expect(belong).to.be.false; expect(belong).to.be.false;
}); });
}); });

View File

@ -4,12 +4,12 @@ F95API.debug(true);
main(); main();
async function main() { async function main() {
const loginResult = await F95API.login("MillenniumEarl", "f9vTcRNuvxj4YpK"); const loginResult = await F95API.login("MillenniumEarl", "f9vTcRNuvxj4YpK");
if (loginResult.success) { if (loginResult.success) {
await F95API.loadF95BaseData(); await F95API.loadF95BaseData();
const gameData = await F95API.getGameData("a struggle with sin", false); const gameData = await F95API.getGameData("a struggle with sin", false);
console.log(gameData); console.log(gameData);
} }
F95API.logout(); F95API.logout();
} }