Fixed timeout errors (probably), fixed missing preview for games

pull/34/head
samuele.berlusconi 2020-10-21 15:31:11 +02:00
parent 3144f28849
commit b2df5df740
14 changed files with 336 additions and 208 deletions

View File

@ -7,3 +7,10 @@
# F95API # F95API
Unofficial Node JS module for scraping F95Zone platform Unofficial Node JS module for scraping F95Zone platform
# Guidelines for errors
+ If you can, return a meaningful value
+ Return `null` only if the function should return a complex object (including strings)
+ Return an empty array if the function should return an array
+ Return `false`, `-1` when the function should retrn `boolean` or `number`
+ Throw an exception only if it is an error or if a wrong value could mess up the functioning of the library

View File

@ -5,13 +5,9 @@ const fs = require("fs");
// Modules from file // Modules from file
const shared = require("./scripts/shared.js"); const shared = require("./scripts/shared.js");
const constURLs = require("./scripts/constants/urls.js"); const urlK = require("./scripts/constants/url.js");
const selectors = require("./scripts/constants/css-selectors.js"); const selectorK = require("./scripts/constants/css-selector.js");
const { const urlHelper = require("./scripts/url-helper.js");
isStringAValidURL,
urlExists,
isF95URL,
} = require("./scripts/urls-helper.js");
const scraper = require("./scripts/game-scraper.js"); const scraper = require("./scripts/game-scraper.js");
const { const {
prepareBrowser, prepareBrowser,
@ -37,6 +33,9 @@ module.exports.UserData = UserData;
*/ */
module.exports.debug = function (value) { module.exports.debug = function (value) {
shared.debug = value; shared.debug = value;
// Configure logger
shared.logger.level = value ? "debug" : "OFF";
}; };
/** /**
* @public * @public
@ -86,6 +85,7 @@ module.exports.setChromiumPath = function (value) {
//#region Global variables //#region Global variables
var _browser = null; var _browser = null;
const USER_NOT_LOGGED = "User not authenticated, unable to continue";
//#endregion //#endregion
//#region Export methods //#region Export methods
@ -99,27 +99,22 @@ var _browser = null;
*/ */
module.exports.login = async function (username, password) { module.exports.login = async function (username, password) {
if (shared.isLogged) { if (shared.isLogged) {
if (shared.debug) console.log("Already logged in"); shared.logger.info("Already logged in");
const result = new LoginResult(); const result = new LoginResult(true, "Already logged in");
result.success = true;
result.message = "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(); shared.cookies = loadCookies();
if (shared.cookies !== null) { if (shared.cookies !== null) {
if (shared.debug) console.log("Valid session, no need to re-authenticate"); shared.logger.info("Valid session, no need to re-authenticate");
shared.isLogged = true; shared.isLogged = true;
const result = new LoginResult(); const result = new LoginResult(true, "Logged with cookies");
result.success = true;
result.message = "Logged with cookies";
return result; return result;
} }
// Else, log in throught browser // Else, log in throught browser
if (shared.debug) shared.logger.info("No saved sessions or expired session, login on the platform");
console.log("No saved sessions or expired session, login on the platform");
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;
@ -130,9 +125,9 @@ module.exports.login = async function (username, password) {
if (result.success) { if (result.success) {
// Reload cookies // Reload cookies
shared.cookies = loadCookies(); shared.cookies = loadCookies();
if (shared.debug) console.log("User logged in through the platform"); shared.logger.info("User logged in through the platform");
} else { } else {
console.warn("Error during authentication: " + result.message); shared.logger.warn("Error during authentication: " + result.message);
} }
if (shared.isolation) await browser.close(); if (shared.isolation) await browser.close();
return result; return result;
@ -146,11 +141,11 @@ module.exports.login = async function (username, password) {
*/ */
module.exports.loadF95BaseData = async function () { module.exports.loadF95BaseData = async function () {
if (!shared.isLogged || !shared.cookies) { if (!shared.isLogged || !shared.cookies) {
console.warn("User not authenticated, unable to continue"); shared.logger.warn(USER_NOT_LOGGED);
return false; return false;
} }
if (shared.debug) console.log("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();
@ -160,30 +155,31 @@ module.exports.loadF95BaseData = async function () {
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(constURLs.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(selectors.ENGINE_ID_SELECTOR); await page.waitForSelector(selectorK.ENGINE_ID_SELECTOR);
shared.engines = await loadValuesFromLatestPage( shared.engines = await loadValuesFromLatestPage(
page, page,
shared.enginesCachePath, shared.enginesCachePath,
selectors.ENGINE_ID_SELECTOR, selectorK.ENGINE_ID_SELECTOR,
"engines" "engines"
); );
// Obtain statuses (disk/online) // Obtain statuses (disk/online)
await page.waitForSelector(selectors.STATUS_ID_SELECTOR); await page.waitForSelector(selectorK.STATUS_ID_SELECTOR);
shared.statuses = await loadValuesFromLatestPage( shared.statuses = await loadValuesFromLatestPage(
page, page,
shared.statusesCachePath, shared.statusesCachePath,
selectors.STATUS_ID_SELECTOR, selectorK.STATUS_ID_SELECTOR,
"statuses" "statuses"
); );
await page.close();
if (shared.isolation) await browser.close(); if (shared.isolation) await browser.close();
if (shared.debug) console.log("Base data loaded"); shared.logger.info("Base data loaded");
return true; return true;
}; };
/** /**
@ -195,13 +191,13 @@ module.exports.loadF95BaseData = async function () {
*/ */
module.exports.chekIfGameHasUpdate = async function (info) { module.exports.chekIfGameHasUpdate = async function (info) {
if (!shared.isLogged || !shared.cookies) { if (!shared.isLogged || !shared.cookies) {
console.warn("user not authenticated, unable to continue"); shared.logger.warn(USER_NOT_LOGGED);
return info.version; 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 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
@ -221,11 +217,11 @@ module.exports.chekIfGameHasUpdate = async function (info) {
* @param {String} name Name of the game searched * @param {String} name Name of the game searched
* @param {Boolean} includeMods Indicates whether to also take mods into account when searching * @param {Boolean} includeMods Indicates whether to also take mods into account when searching
* @returns {Promise<GameInfo[]>} List of information obtained where each item corresponds to * @returns {Promise<GameInfo[]>} List of information obtained where each item corresponds to
* an identified game (in the case of homonymy). If no games were found, null is returned * 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) {
console.warn("user not authenticated, unable to continue"); shared.logger.warn(USER_NOT_LOGGED);
return null; return null;
} }
@ -244,10 +240,12 @@ module.exports.getGameData = async function (name, includeMods) {
// 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)) {
// Skip mods if not required // Ignore empty results
if (!info) continue; if (!info) continue;
// Skip mods if not required
if (info.isMod && !includeMods) continue; if (info.isMod && !includeMods) continue;
else result.push(info); // Else save data
result.push(info);
} }
if (shared.isolation) await browser.close(); if (shared.isolation) await browser.close();
@ -262,13 +260,14 @@ module.exports.getGameData = async function (name, includeMods) {
*/ */
module.exports.getGameDataFromURL = async function (url) { module.exports.getGameDataFromURL = async function (url) {
if (!shared.isLogged || !shared.cookies) { if (!shared.isLogged || !shared.cookies) {
console.warn("user not authenticated, unable to continue"); shared.logger.warn(USER_NOT_LOGGED);
return null; return null;
} }
// Check URL // Check URL
if (!urlExists(url)) return null; const exists = await urlHelper.urlExists(url);
if (!isF95URL(url)) throw new Error(url + " is not a valid F95Zone URL"); if (!exists) throw new URIError(url + " is not a valid URL");
if (!urlHelper.isF95URL(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();
@ -284,11 +283,11 @@ module.exports.getGameDataFromURL = async function (url) {
* @public * @public
* Gets the data of the currently logged in user. * Gets the data of the currently logged in user.
* You **must** be logged in to the portal before calling this method. * You **must** be logged in to the portal before calling this method.
* @returns {Promise<UserData>} Data of the user currently logged in or null if an error arise * @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) {
console.warn("user not authenticated, unable to continue"); shared.logger.warn(USER_NOT_LOGGED);
return null; return null;
} }
@ -297,29 +296,31 @@ module.exports.getUserData = async function () {
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(constURLs.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 page.waitForSelector(selectors.USERNAME_ELEMENT); await Promise.all([
await page.waitForSelector(selectors.AVATAR_PIC); page.waitForSelector(selectorK.USERNAME_ELEMENT),
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,
selectors.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"),
selectors.AVATAR_PIC selectorK.AVATAR_PIC
); );
const ud = new UserData(); const ud = new UserData();
ud.username = username; ud.username = username;
ud.avatarSrc = 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();
@ -334,9 +335,11 @@ module.exports.getUserData = async function () {
*/ */
module.exports.logout = async function () { module.exports.logout = async function () {
if (!shared.isLogged || !shared.cookies) { if (!shared.isLogged || !shared.cookies) {
console.warn("user not authenticated, unable to continue"); shared.logger.warn(USER_NOT_LOGGED);
return; return;
} }
// Logout
shared.isLogged = false; shared.isLogged = false;
// Gracefully close shared browser // Gracefully close shared browser
@ -390,10 +393,7 @@ function isCookieExpired(cookie) {
const expirationDate = new Date(expirationUnixTimestamp * 1000); const expirationDate = new Date(expirationUnixTimestamp * 1000);
if (expirationDate < Date.now()) { if (expirationDate < Date.now()) {
if (shared.debug) shared.logger.warn("Cookie " + cookie.name + " expired, you need to re-authenticate");
console.log(
"Cookie " + cookie.name + " expired, you need to re-authenticate"
);
expiredCookies = true; expiredCookies = true;
} }
} }
@ -421,15 +421,14 @@ async function loadValuesFromLatestPage(
elementRequested elementRequested
) { ) {
// If the values already exist they are loaded from disk without having to connect to F95 // If the values already exist they are loaded from disk without having to connect to F95
if (shared.debug) console.log("Load " + elementRequested + " from disk..."); shared.logger.info("Load " + elementRequested + " from disk...");
if (fs.existsSync(path)) { if (fs.existsSync(path)) {
const valueJSON = fs.readFileSync(path); const valueJSON = fs.readFileSync(path);
return JSON.parse(valueJSON); return JSON.parse(valueJSON);
} }
// Otherwise, connect and download the data from the portal // Otherwise, connect and download the data from the portal
if (shared.debug) shared.logger.info("No " + elementRequested + " cached, downloading...");
console.log("No " + elementRequested + " cached, downloading...");
const values = await getValuesFromLatestPage( const values = await getValuesFromLatestPage(
page, page,
selector, selector,
@ -449,7 +448,7 @@ 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) {
if (shared.debug) console.log(logMessage); shared.logger.info(logMessage);
const result = []; const result = [];
const elements = await page.$$(selector); const elements = await page.$$(selector);
@ -477,65 +476,63 @@ async function getValuesFromLatestPage(page, selector, logMessage) {
*/ */
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(constURLs.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(selectors.USERNAME_INPUT), page.waitForSelector(selectorK.USERNAME_INPUT),
page.waitForSelector(selectors.PASSWORD_INPUT), page.waitForSelector(selectorK.PASSWORD_INPUT),
page.waitForSelector(selectors.LOGIN_BUTTON), page.waitForSelector(selectorK.LOGIN_BUTTON),
]); ]);
await page.type(selectors.USERNAME_INPUT, username); // Insert username await page.type(selectorK.USERNAME_INPUT, username); // Insert username
await page.type(selectors.PASSWORD_INPUT, password); // Insert password await page.type(selectorK.PASSWORD_INPUT, password); // Insert password
await Promise.all([ await Promise.all([
page.click(selectors.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
const result = new LoginResult(); let message = "";
// Check if the user is logged in // Check if the user is logged in
result.success = await page.evaluate( let success = await page.evaluate(
/* istanbul ignore next */ (selector) => /* istanbul ignore next */ (selector) =>
document.querySelector(selector) !== null, document.querySelector(selector) !== null,
selectors.AVATAR_INFO selectorK.AVATAR_INFO
); );
let errorMessageExists = await page.evaluate(
/* istanbul ignore next */
(selector) =>
document.querySelector(selector) !== null,
selectorK.LOGIN_MESSAGE_ERROR
)
// Save cookies to avoid re-auth // Save cookies to avoid re-auth
if (result.success) { if (success) {
const c = await page.cookies(); const c = await page.cookies();
fs.writeFileSync(shared.cookiesCachePath, JSON.stringify(c)); fs.writeFileSync(shared.cookiesCachePath, JSON.stringify(c));
result.message = "Authentication successful"; message = "Authentication successful";
} else if ( } else if (errorMessageExists) {
// Obtain the error message
await page.evaluate(
/* istanbul ignore next */ (selector) =>
document.querySelector(selector) !== null,
selectors.LOGIN_MESSAGE_ERROR
)
) {
const errorMessage = await page.evaluate( const errorMessage = await page.evaluate(
/* istanbul ignore next */ (selector) => /* istanbul ignore next */ (selector) =>
document.querySelector(selector).innerText, document.querySelector(selector).innerText,
selectors.LOGIN_MESSAGE_ERROR selectorK.LOGIN_MESSAGE_ERROR
); );
if (errorMessage === "Incorrect password. Please try again.") { if (errorMessage === "Incorrect password. Please try again.") {
result.message = "Incorrect password"; message = "Incorrect password";
} else if ( } else if (errorMessage ==='The requested user \'' + username + '\' could not be found.') {
errorMessage === // The escaped quotes are important!
'The requested user "' + username + '" could not be found.' message = "Incorrect username";
) { } else message = errorMessage;
result.message = "Incorrect username"; } else message = "Unknown error";
} else result.message = errorMessage;
} else result.message = "Unknown error";
await page.close(); // Close the page await page.close(); // Close the page
return result; return new LoginResult(success, message);
} }
/** /**
* @private * @private
@ -545,39 +542,37 @@ async function loginF95(browser, username, password) {
*/ */
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(constURLs.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(selectors.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(selectors.WATCHED_THREAD_FILTER_POPUP_BUTTON), page.click(selectorK.WATCHED_THREAD_FILTER_POPUP_BUTTON),
page.waitForSelector(selectors.UNREAD_THREAD_CHECKBOX), page.waitForSelector(selectorK.UNREAD_THREAD_CHECKBOX),
page.waitForSelector(selectors.ONLY_GAMES_THREAD_OPTION), page.waitForSelector(selectorK.ONLY_GAMES_THREAD_OPTION),
page.waitForSelector(selectors.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"),
selectors.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 Promise.all([ await page.click(selectorK.ONLY_GAMES_THREAD_OPTION);
page.click(selectors.ONLY_GAMES_THREAD_OPTION), await page.click(selectorK.FILTER_THREADS_BUTTON);
page.click(selectors.FILTER_THREADS_BUTTON), await page.waitForSelector(selectorK.WATCHED_THREAD_URLS);
page.waitForSelector(selectors.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.$$(selectors.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
@ -589,13 +584,13 @@ async function getUserWatchedGameThreads(browser) {
nextPageExists = await page.evaluate( nextPageExists = await page.evaluate(
/* istanbul ignore next */ (selector) => document.querySelector(selector), /* istanbul ignore next */ (selector) => document.querySelector(selector),
selectors.WATCHED_THREAD_NEXT_PAGE selectorK.WATCHED_THREAD_NEXT_PAGE
); );
// Click to next page // Click to next page
if (nextPageExists) { if (nextPageExists) {
await page.click(selectors.WATCHED_THREAD_NEXT_PAGE); await page.click(selectorK.WATCHED_THREAD_NEXT_PAGE);
await page.waitForSelector(selectors.WATCHED_THREAD_URLS); await page.waitForSelector(selectorK.WATCHED_THREAD_URLS);
} }
} while (nextPageExists); } while (nextPageExists);

View File

@ -1,7 +1,5 @@
"use strict"; "use strict";
const UNKNOWN = "Unknown";
class GameInfo { class GameInfo {
constructor() { constructor() {
//#region Properties //#region Properties
@ -9,12 +7,12 @@ class GameInfo {
* Game name * Game name
* @type String * @type String
*/ */
this.name = UNKNOWN; this.name = null;
/** /**
* Game author * Game author
* @type String * @type String
*/ */
this.author = UNKNOWN; 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
@ -24,7 +22,7 @@ class GameInfo {
* Game description * Game description
* @type String * @type String
*/ */
this.overview = UNKNOWN; this.overview = null;
/** /**
* List of tags associated with the game * List of tags associated with the game
* @type String[] * @type String[]
@ -34,12 +32,12 @@ class GameInfo {
* Graphics engine used for game development * Graphics engine used for game development
* @type String * @type String
*/ */
this.engine = UNKNOWN; this.engine = null;
/** /**
* Progress of the game * Progress of the game
* @type String * @type String
*/ */
this.status = UNKNOWN; this.status = null;
/** /**
* Game description image URL * Game description image URL
* @type String * @type String
@ -49,17 +47,17 @@ class GameInfo {
* Game version * Game version
* @type String * @type String
*/ */
this.version = UNKNOWN; this.version = null;
/** /**
* Last time the game underwent updates * Last time the game underwent updates
* @type String * @type String
*/ */
this.lastUpdate = UNKNOWN; 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 = UNKNOWN; 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
@ -74,7 +72,7 @@ class GameInfo {
* Directory containing the local copy of the game * Directory containing the local copy of the game
* @type String * @type String
*/ */
this.gameDir = UNKNOWN; 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

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() { constructor(success, message) {
/** /**
* Result of the login operation * Result of the login operation
* @type Boolean * @type Boolean
*/ */
this.success = false; this.success = success;
/** /**
* Login response message * Login response message
* @type String * @type String
*/ */
this.message = ""; this.message = message;
} }
} }
module.exports = LoginResult; module.exports = LoginResult;

View File

@ -6,11 +6,12 @@ const puppeteer = require("puppeteer"); // skipcq: JS-0128
// Modules from file // Modules from file
const shared = require("./shared.js"); const shared = require("./shared.js");
const selectors = require("./constants/css-selectors.js"); const selectorK = require("./constants/css-selector.js");
const { preparePage } = require("./puppeteer-helper.js"); const { preparePage } = require("./puppeteer-helper.js");
const GameDownload = require("./classes/game-download.js"); const GameDownload = require("./classes/game-download.js");
const GameInfo = require("./classes/game-info.js"); const GameInfo = require("./classes/game-info.js");
const urlsHelper = require("./urls-helper.js"); const urlHelper = require("./url-helper.js");
const { TimeoutError } = require("ky-universal");
/** /**
* @protected * @protected
@ -18,16 +19,15 @@ const urlsHelper = require("./urls-helper.js");
* @param {puppeteer.Browser} browser Browser object used for navigation * @param {puppeteer.Browser} browser Browser object used for navigation
* @param {String} url URL (String) of the game/mod to extract data from * @param {String} url URL (String) of the game/mod to extract data from
* @return {Promise<GameInfo>} Complete information about the game you are * @return {Promise<GameInfo>} Complete information about the game you are
* looking for or null if the URL doesn't exists * looking for
*/ */
module.exports.getGameInfo = async function (browser, url) { module.exports.getGameInfo = async function (browser, url) {
if (shared.debug) console.log("Obtaining game info"); shared.logger.info("Obtaining game info");
// Verify the correctness of the URL // Verify the correctness of the URL
if (!urlsHelper.isF95URL(url)) const exists = await urlHelper.urlExists(url);
throw new Error(url + " is not a valid F95Zone URL"); if (!exists) throw new URIError(url + " is not a valid URL");
const exists = await urlsHelper.urlExists(url); if (!urlHelper.isF95URL(url)) throw new Error(url + " is not a valid F95Zone URL");
if (!exists) return null;
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
@ -41,7 +41,7 @@ module.exports.getGameInfo = async function (browser, url) {
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 = urlsHelper.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);
@ -60,7 +60,7 @@ module.exports.getGameInfo = async function (browser, url) {
? parsedInfos.UPDATED ? parsedInfos.UPDATED
: parsedInfos.THREAD_UPDATED; : parsedInfos.THREAD_UPDATED;
info.previewSource = await previewSource; info.previewSource = await previewSource;
info.changelog = (await changelog) || "Unknown changelog"; info.changelog = await changelog;
//let downloadData = getGameDownloadLink(page); //let downloadData = getGameDownloadLink(page);
//info.downloadInfo = await downloadData; //info.downloadInfo = await downloadData;
@ -70,7 +70,7 @@ module.exports.getGameInfo = async function (browser, url) {
* 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
if (shared.debug) console.log("Founded data for " + info.name); shared.logger.info("Founded data for " + info.name);
return info; return info;
}; };
@ -91,7 +91,7 @@ module.exports.getGameVersionFromTitle = async function (browser, info) {
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,
selectors.GAME_TITLE selectorK.GAME_TITLE
); );
const title = HTMLParser.parse(titleHTML).childNodes.pop().rawText; const title = HTMLParser.parse(titleHTML).childNodes.pop().rawText;
@ -129,7 +129,7 @@ function getOverview(text, isMod) {
*/ */
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.$$(selectors.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(
@ -151,7 +151,7 @@ async function getGameAuthor(page) {
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,
selectors.GAME_TITLE selectorK.GAME_TITLE
); );
const structuredTitle = HTMLParser.parse(titleHTML); const structuredTitle = HTMLParser.parse(titleHTML);
@ -180,7 +180,7 @@ function parseConversationPage(text) {
// Create pair key/value // Create pair key/value
const splitted = line.split(":"); const splitted = line.split(":");
const key = splitted[0].trim().toUpperCase().replaceAll(" ", "_"); // 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
@ -194,23 +194,24 @@ function parseConversationPage(text) {
* @private * @private
* Gets the URL of the image used as a preview for the game in the conversation. * Gets the URL of the image used as a preview for the game in the conversation.
* @param {puppeteer.Page} page Page containing the URL to be extrapolated * @param {puppeteer.Page} page Page containing the URL to be extrapolated
* @returns {Promise<String>} URL (String) of the image or null if failed to get it * @returns {Promise<String>} URL (String) of the image or a empty string if failed to get it
*/ */
async function getGamePreviewSource(page) { async function getGamePreviewSource(page) {
await page.waitForSelector(selectorK.GAME_IMAGES);
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;
}, },
selectors.GAME_IMAGES selectorK.GAME_IMAGES
); );
// Check if the URL is valid // Check if the URL is valid
return urlsHelper.isStringAValidURL(src) ? src : null; return urlHelper.isStringAValidURL(src) ? src : "";
} }
/** /**
@ -224,7 +225,7 @@ async function getGameTitle(page) {
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,
selectors.GAME_TITLE selectorK.GAME_TITLE
); );
const structuredTitle = HTMLParser.parse(titleHTML); const structuredTitle = HTMLParser.parse(titleHTML);
@ -244,7 +245,7 @@ async function getGameTags(page) {
const tags = []; const tags = [];
// Get the game tags // Get the game tags
for (const handle of await page.$$(selectors.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,
@ -268,7 +269,7 @@ 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.$$(selectors.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,
@ -277,10 +278,10 @@ async function parsePrefixes(page, info) {
// 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 = capitalize(prefix);
else if (shared.engines.includes(prefix)) info.engine = prefix; else if (shared.engines.includes(prefix)) info.engine = capitalize(prefix);
// This is not a game but a mod // This is not a game but a mod
else if (prefix === MOD_PREFIX) info.isMod = true; else if (prefix === MOD_PREFIX) info.isMod = true;
} }
@ -291,22 +292,26 @@ async function parsePrefixes(page, info) {
* @private * @private
* Get the last changelog available for the game. * Get the last changelog available for the game.
* @param {puppeteer.Page} page Page containing the changelog * @param {puppeteer.Page} page Page containing the changelog
* @returns {Promise<String>} Changelog for the last version or null 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.$$(selectors.THREAD_POSTS))[0]; const post = (await page.$$(selectorK.THREAD_POSTS))[0];
const spoiler = await post.$(selectors.THREAD_LAST_CHANGELOG); const spoiler = await post.$(selectorK.THREAD_LAST_CHANGELOG);
if (!spoiler) return null; 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
); );
const parsedText = HTMLParser.parse(changelogHTML).structuredText; let parsedText = HTMLParser.parse(changelogHTML).structuredText;
return parsedText.replace("Spoiler", "").trim();
// Clean the text
if (parsedText.startsWith("Spoiler")) parsedText = parsedText.replace("Spoiler", "");
if (parsedText.startsWith(":")) parsedText = parsedText.replace(":", "");
return parsedText.trim();
} }
/** /**
@ -314,8 +319,10 @@ async function getLastChangelog(page) {
* Get game download links for different platforms. * Get game download links for different platforms.
* @param {puppeteer.Page} page Page containing the links to be extrapolated * @param {puppeteer.Page} page Page containing the links to be extrapolated
* @returns {Promise<GameDownload[]>} List of objects used for game download * @returns {Promise<GameDownload[]>} List of objects used for game download
* @deprecated
*/ */
// skipcq: JS-0128 // skipcq: JS-0128
/* istanbul ignore next */
async function getGameDownloadLink(page) { async function getGameDownloadLink(page) {
// Most used hosting platforms // Most used hosting platforms
const hostingPlatforms = [ const hostingPlatforms = [
@ -332,7 +339,7 @@ async function getGameDownloadLink(page) {
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.$$(selectors.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
@ -384,7 +391,9 @@ async function getGameDownloadLink(page) {
* It can only be *WIN/LINUX/MAC/ALL* * It can only be *WIN/LINUX/MAC/ALL*
* @param {String} text HTML string to extract links from * @param {String} text HTML string to extract links from
* @returns {GameDownload[]} List of game download links for the selected platform * @returns {GameDownload[]} List of game download links for the selected platform
* @deprecated
*/ */
/* 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>";
@ -427,7 +436,7 @@ function extractGameHostingData(platform, text) {
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 (urlsHelper.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;
@ -438,4 +447,12 @@ function extractGameHostingData(platform, text) {
} }
return downloadData; return downloadData;
} }
/**
* Capitalize a string
* @param {String} string
*/
function capitalize(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
//#endregion Private methods //#endregion Private methods

View File

@ -5,10 +5,10 @@ const puppeteer = require("puppeteer"); // skipcq: JS-0128
// Modules from file // Modules from file
const shared = require("./shared.js"); const shared = require("./shared.js");
const constURLs = require("./constants/urls.js"); const urlK = require("./constants/url.js");
const selectors = require("./constants/css-selectors.js"); const selectorK = require("./constants/css-selector.js");
const { preparePage } = require("./puppeteer-helper.js"); const { preparePage } = require("./puppeteer-helper.js");
const { isF95URL } = require("./urls-helper.js"); const { isF95URL } = require("./url-helper.js");
/** /**
* @protected * @protected
@ -18,41 +18,41 @@ const { isF95URL } = require("./urls-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) {
if (shared.debug) console.log("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(constURLs.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(selectors.SEARCH_FORM_TEXTBOX), page.waitForSelector(selectorK.SEARCH_FORM_TEXTBOX),
page.waitForSelector(selectors.TITLE_ONLY_CHECKBOX), page.waitForSelector(selectorK.TITLE_ONLY_CHECKBOX),
page.waitForSelector(selectors.SEARCH_BUTTON), page.waitForSelector(selectorK.SEARCH_BUTTON),
]); ]);
await page.type(selectors.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 Promise.all([ await Promise.all([
page.click(selectors.TITLE_ONLY_CHECKBOX), // Select only the thread with the game in the titles page.click(selectorK.SEARCH_BUTTON), // Execute search
page.click(selectors.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.$$(selectors.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
if (shared.debug) console.log("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);
} }
if (shared.debug) console.log("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;
@ -68,8 +68,8 @@ module.exports.getSearchGameResults = async function (browser, gamename) {
*/ */
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.$(selectors.THREAD_TITLE); const titleHandle = await divHandle.$(selectorK.THREAD_TITLE);
const forumHandle = await divHandle.$(selectors.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);
@ -120,12 +120,13 @@ async function getThreadURL(page, handle) {
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
const urlThread = new URL( const urlThread = new URL(
relativeURLThread, relativeURLThread,
constURLs.F95_BASE_URL urlK.F95_BASE_URL
).toString(); ).toString();
return urlThread; return urlThread;
} }

View File

@ -3,6 +3,8 @@
// Core modules // Core modules
const { join } = require("path"); const { join } = require("path");
const log4js = require("log4js");
/** /**
* Class containing variables shared between modules. * Class containing variables shared between modules.
*/ */
@ -12,27 +14,27 @@ class Shared {
* Shows log messages and other useful functions for module debugging. * Shows log messages and other useful functions for module debugging.
* @type Boolean * @type Boolean
*/ */
static _debug = false; static #_debug = false;
/** /**
* 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.
* @type Boolean * @type Boolean
*/ */
static _isLogged = false; static #_isLogged = false;
/** /**
* List of cookies obtained from the F95Zone platform. * List of cookies obtained from the F95Zone platform.
* @type Object[] * @type Object[]
*/ */
static _cookies = null; static #_cookies = null;
/** /**
* List of possible game engines used for development. * List of possible game engines used for development.
* @type String[] * @type String[]
*/ */
static _engines = null; static #_engines = null;
/** /**
* List of possible development statuses that a game can assume. * List of possible development statuses that a game can assume.
* @type String[] * @type String[]
*/ */
static _statuses = null; static #_statuses = null;
/** /**
* Wait instruction for the browser created by puppeteer. * Wait instruction for the browser created by puppeteer.
* @type String * @type String
@ -42,13 +44,18 @@ class Shared {
* Path to the directory to save the cache generated by the API. * Path to the directory to save the cache generated by the API.
* @type String * @type String
*/ */
static _cacheDir = "./f95cache"; static #_cacheDir = "./f95cache";
/** /**
* If true, it opens a new browser for each request to * If true, it opens a new browser for each request to
* the F95Zone platform, otherwise it reuses the same. * the F95Zone platform, otherwise it reuses the same.
* @type Boolean * @type Boolean
*/ */
static _isolation = false; static #_isolation = false;
/**
* Logger object used to write to both file and console.
* @type log4js.Logger
*/
static #_logger = log4js.getLogger();
//#endregion Properties //#endregion Properties
//#region Getters //#region Getters
@ -57,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
@ -121,37 +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.
* @returns {log4js.Logger}
*/
static get 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

@ -6,7 +6,7 @@ const ky = require("ky-universal").create({
}); });
// Modules from file // Modules from file
const { F95_BASE_URL } = require("./constants/urls.js"); const { F95_BASE_URL } = require("./constants/url.js");
/** /**
* @protected * @protected

72
package-lock.json generated
View File

@ -1,6 +1,6 @@
{ {
"name": "f95api", "name": "f95api",
"version": "1.2.4", "version": "1.3.4",
"lockfileVersion": 1, "lockfileVersion": 1,
"requires": true, "requires": true,
"dependencies": { "dependencies": {
@ -560,6 +560,11 @@
"resolved": "https://registry.npmjs.org/data-uri-to-buffer/-/data-uri-to-buffer-3.0.1.tgz", "resolved": "https://registry.npmjs.org/data-uri-to-buffer/-/data-uri-to-buffer-3.0.1.tgz",
"integrity": "sha512-WboRycPNsVw3B3TL559F7kuBUM4d8CgMEvk6xEJlOp7OBPjt6G7z8WMWlD2rOFZLk6OYfFIUGsCOWzcQH9K2og==" "integrity": "sha512-WboRycPNsVw3B3TL559F7kuBUM4d8CgMEvk6xEJlOp7OBPjt6G7z8WMWlD2rOFZLk6OYfFIUGsCOWzcQH9K2og=="
}, },
"date-format": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/date-format/-/date-format-3.0.0.tgz",
"integrity": "sha512-eyTcpKOcamdhWJXj56DpQMo1ylSQpcGtGKXcU0Tb97+K56/CF5amAqqqNj0+KvA0iw2ynxtHWFsPDSClCxe48w=="
},
"debug": { "debug": {
"version": "4.2.0", "version": "4.2.0",
"resolved": "https://registry.npmjs.org/debug/-/debug-4.2.0.tgz", "resolved": "https://registry.npmjs.org/debug/-/debug-4.2.0.tgz",
@ -768,6 +773,11 @@
"is-buffer": "~2.0.3" "is-buffer": "~2.0.3"
} }
}, },
"flatted": {
"version": "2.0.2",
"resolved": "https://registry.npmjs.org/flatted/-/flatted-2.0.2.tgz",
"integrity": "sha512-r5wGx7YeOwNWNlCA0wQ86zKyDLMQr+/RB8xy74M4hTphfmjlijTSSXGuH8rnvKZnfT9i+75zmd8jcKdMR4O6jA=="
},
"foreground-child": { "foreground-child": {
"version": "2.0.0", "version": "2.0.0",
"resolved": "https://registry.npmjs.org/foreground-child/-/foreground-child-2.0.0.tgz", "resolved": "https://registry.npmjs.org/foreground-child/-/foreground-child-2.0.0.tgz",
@ -789,6 +799,16 @@
"resolved": "https://registry.npmjs.org/fs-constants/-/fs-constants-1.0.0.tgz", "resolved": "https://registry.npmjs.org/fs-constants/-/fs-constants-1.0.0.tgz",
"integrity": "sha512-y6OAwoSIf7FyjMIv94u+b5rdheZEjzR63GTyZJm5qh4Bi+2YgwLCcI/fPFZkL5PSixOt6ZNKm+w+Hfp/Bciwow==" "integrity": "sha512-y6OAwoSIf7FyjMIv94u+b5rdheZEjzR63GTyZJm5qh4Bi+2YgwLCcI/fPFZkL5PSixOt6ZNKm+w+Hfp/Bciwow=="
}, },
"fs-extra": {
"version": "8.1.0",
"resolved": "https://registry.npmjs.org/fs-extra/-/fs-extra-8.1.0.tgz",
"integrity": "sha512-yhlQgA6mnOJUKOsRUFsgJdQCvkKhcz8tlZG5HBQfReYZy46OwLcY+Zia0mtdHsOo9y/hP+CxMN0TU9QxoOtG4g==",
"requires": {
"graceful-fs": "^4.2.0",
"jsonfile": "^4.0.0",
"universalify": "^0.1.0"
}
},
"fs.realpath": { "fs.realpath": {
"version": "1.0.0", "version": "1.0.0",
"resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz", "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz",
@ -870,8 +890,7 @@
"graceful-fs": { "graceful-fs": {
"version": "4.2.4", "version": "4.2.4",
"resolved": "https://registry.npmjs.org/graceful-fs/-/graceful-fs-4.2.4.tgz", "resolved": "https://registry.npmjs.org/graceful-fs/-/graceful-fs-4.2.4.tgz",
"integrity": "sha512-WjKPNJF79dtJAVniUlGGWHYGz2jWxT6VhN/4m1NdkbZ2nOsEF+cI1Edgql5zCRhs/VsQYRvrXctxktVXZUkixw==", "integrity": "sha512-WjKPNJF79dtJAVniUlGGWHYGz2jWxT6VhN/4m1NdkbZ2nOsEF+cI1Edgql5zCRhs/VsQYRvrXctxktVXZUkixw=="
"dev": true
}, },
"growl": { "growl": {
"version": "1.10.5", "version": "1.10.5",
@ -1239,6 +1258,14 @@
"minimist": "^1.2.5" "minimist": "^1.2.5"
} }
}, },
"jsonfile": {
"version": "4.0.0",
"resolved": "https://registry.npmjs.org/jsonfile/-/jsonfile-4.0.0.tgz",
"integrity": "sha1-h3Gq4HmbZAdrdmQPygWPnBDjPss=",
"requires": {
"graceful-fs": "^4.1.6"
}
},
"ky": { "ky": {
"version": "0.24.0", "version": "0.24.0",
"resolved": "https://registry.npmjs.org/ky/-/ky-0.24.0.tgz", "resolved": "https://registry.npmjs.org/ky/-/ky-0.24.0.tgz",
@ -1334,6 +1361,18 @@
} }
} }
}, },
"log4js": {
"version": "6.3.0",
"resolved": "https://registry.npmjs.org/log4js/-/log4js-6.3.0.tgz",
"integrity": "sha512-Mc8jNuSFImQUIateBFwdOQcmC6Q5maU0VVvdC2R6XMb66/VnT+7WS4D/0EeNMZu1YODmJe5NIn2XftCzEocUgw==",
"requires": {
"date-format": "^3.0.0",
"debug": "^4.1.1",
"flatted": "^2.0.1",
"rfdc": "^1.1.4",
"streamroller": "^2.2.4"
}
},
"make-dir": { "make-dir": {
"version": "3.1.0", "version": "3.1.0",
"resolved": "https://registry.npmjs.org/make-dir/-/make-dir-3.1.0.tgz", "resolved": "https://registry.npmjs.org/make-dir/-/make-dir-3.1.0.tgz",
@ -1919,6 +1958,11 @@
"integrity": "sha512-qYg9KP24dD5qka9J47d0aVky0N+b4fTU89LN9iDnjB5waksiC49rvMB0PrUJQGoTmH50XPiqOvAjDfaijGxYZw==", "integrity": "sha512-qYg9KP24dD5qka9J47d0aVky0N+b4fTU89LN9iDnjB5waksiC49rvMB0PrUJQGoTmH50XPiqOvAjDfaijGxYZw==",
"dev": true "dev": true
}, },
"rfdc": {
"version": "1.1.4",
"resolved": "https://registry.npmjs.org/rfdc/-/rfdc-1.1.4.tgz",
"integrity": "sha512-5C9HXdzK8EAqN7JDif30jqsBzavB7wLpaubisuQIGHWf2gUXSpzy6ArX/+Da8RjFpagWsCn+pIgxTMAmKw9Zug=="
},
"rimraf": { "rimraf": {
"version": "3.0.2", "version": "3.0.2",
"resolved": "https://registry.npmjs.org/rimraf/-/rimraf-3.0.2.tgz", "resolved": "https://registry.npmjs.org/rimraf/-/rimraf-3.0.2.tgz",
@ -2010,6 +2054,23 @@
"integrity": "sha1-BOaSb2YolTVPPdAVIDYzuFcpfiw=", "integrity": "sha1-BOaSb2YolTVPPdAVIDYzuFcpfiw=",
"dev": true "dev": true
}, },
"streamroller": {
"version": "2.2.4",
"resolved": "https://registry.npmjs.org/streamroller/-/streamroller-2.2.4.tgz",
"integrity": "sha512-OG79qm3AujAM9ImoqgWEY1xG4HX+Lw+yY6qZj9R1K2mhF5bEmQ849wvrb+4vt4jLMLzwXttJlQbOdPOQVRv7DQ==",
"requires": {
"date-format": "^2.1.0",
"debug": "^4.1.1",
"fs-extra": "^8.1.0"
},
"dependencies": {
"date-format": {
"version": "2.1.0",
"resolved": "https://registry.npmjs.org/date-format/-/date-format-2.1.0.tgz",
"integrity": "sha512-bYQuGLeFxhkxNOF3rcMtiZxvCBAquGzZm6oWA1oZ0g2THUzivaRhv8uOhdr19LmoobSOLoIAxeUK2RdbM8IFTA=="
}
}
},
"string-width": { "string-width": {
"version": "4.2.0", "version": "4.2.0",
"resolved": "https://registry.npmjs.org/string-width/-/string-width-4.2.0.tgz", "resolved": "https://registry.npmjs.org/string-width/-/string-width-4.2.0.tgz",
@ -2170,6 +2231,11 @@
"through": "^2.3.8" "through": "^2.3.8"
} }
}, },
"universalify": {
"version": "0.1.2",
"resolved": "https://registry.npmjs.org/universalify/-/universalify-0.1.2.tgz",
"integrity": "sha512-rBJeI5CXAlmy1pV+617WB9J63U6XcazHHF2f2dbJix4XzpUF0RS3Zbj0FGIOCAva5P/d/GBOYaACQ1w+0azUkg=="
},
"util-deprecate": { "util-deprecate": {
"version": "1.0.2", "version": "1.0.2",
"resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz", "resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz",

View File

@ -1,7 +1,7 @@
{ {
"main": "./app/index.js", "main": "./app/index.js",
"name": "f95api", "name": "f95api",
"version": "1.3.4", "version": "1.3.5",
"author": { "author": {
"name": "Millennium Earl" "name": "Millennium Earl"
}, },
@ -33,6 +33,7 @@
"dependencies": { "dependencies": {
"ky": "^0.24.0", "ky": "^0.24.0",
"ky-universal": "^0.8.2", "ky-universal": "^0.8.2",
"log4js": "^6.3.0",
"node-html-parser": "^1.2.21", "node-html-parser": "^1.2.21",
"puppeteer": "^5.3.1" "puppeteer": "^5.3.1"
}, },

View File

@ -1,5 +1,6 @@
"use strict"; "use strict";
const urlHelper = require("../app/scripts/url-helper.js");
const expect = require("chai").expect; const expect = require("chai").expect;
const F95API = require("../app/index"); const F95API = require("../app/index");
const fs = require("fs"); const fs = require("fs");
@ -142,6 +143,8 @@ describe("Search game data", function () {
}); });
//#endregion Set-up //#endregion Set-up
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;
@ -162,11 +165,17 @@ describe("Search game data", function () {
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);
}); });
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() {
let json = JSON.stringify(testGame);
let parsedGameInfo = JSON.parse(json);
expect(parsedGameInfo).to.be.equal(testGame);
});
}); });
describe("Load user data", function () { describe("Load user data", function () {
@ -230,3 +239,36 @@ describe("Check game update", function () {
expect(update).to.be.true; expect(update).to.be.true;
}); });
}); });
describe("Test url-helper", function () {
//#region Set-up
this.timeout(30000); // All tests in this suite get 30 seconds before timeout
//#endregion Set-up
it("Check if URL exists", async function () {
// Check generic URLs...
let exists = urlHelper.urlExists("https://www.google.com/");
expect(exists).to.be.true;
exists = urlHelper.urlExists("www.google.com");
expect(exists).to.be.true;
exists = urlHelper.urlExists("https://www.google/");
expect(exists).to.be.false;
// Now check for more specific URLs (with redirect)...
exists = urlHelper.urlExists("https://f95zone.to/threads/perverted-education-v0-9601-april-ryan.1854/");
expect(exists).to.be.true;
exists = urlHelper.urlExists("https://f95zone.to/threads/perverted-education-v0-9601-april-ryan.1854/", true);
expect(exists).to.be.false;
});
it("Check if URL belong to the platform", async function () {
let belong = urlHelper.isF95URL("https://f95zone.to/threads/perverted-education-v0-9601-april-ryan.1854/");
expect(belong).to.be.true;
belong = urlHelper.isF95URL("https://www.google/");
expect(belong).to.be.false;
});
});

View File

@ -1,4 +1,3 @@
const { join } = require("path");
const { const {
debug, debug,
login, login,
@ -7,18 +6,16 @@ const {
getUserData, getUserData,
logout, logout,
} = require("../app/index.js"); } = require("../app/index.js");
const GameDownload = require("../app/scripts/classes/game-download.js");
debug(true); debug(true);
main(); main();
//downloadGameMEGA();
async function main() { async function main() {
const loginResult = await login("MillenniumEarl", "f9vTcRNuvxj4YpK"); const loginResult = await login("MillenniumEarl", "f9vTcRNuvxj4YpK");
if (loginResult.success) { if (loginResult.success) {
await loadF95BaseData(); await loadF95BaseData();
const gameData = await getGameData("queen's brothel", false); const gameData = await getGameData("kingdom of deception", false);
console.log(gameData); console.log(gameData);
// let userData = await getUserData(); // let userData = await getUserData();
@ -26,13 +23,3 @@ async function main() {
} }
logout(); logout();
} }
async function downloadGameMEGA() {
const gd = new GameDownload();
gd.hosting = "NOPY";
gd.link =
"https://f95zone.to/masked/mega.nz/2733/1470797/4O5LKwMx4ZSlw0QYTVMP0uDK660/hoobTb44f0IKx7Yio2SE2w/loX_px2vLRyNRQCnkNn5U7nnQe7jGmpEVERvH1tk7RjAFkQbs2kH_vCK6zVmRDuqiypPoIx358MNHHCd3QCdVvEsClSiAq4rwjK0r_ruXIs";
const savepath = join(__dirname, "Kingdom_of_Deception-pc0.10.8.zip");
const result = await gd.download(savepath);
console.log(result);
}