2020-10-16 08:06:03 +00:00
|
|
|
"use strict";
|
2020-10-01 19:13:23 +00:00
|
|
|
|
|
|
|
// Core modules
|
2020-10-16 08:06:03 +00:00
|
|
|
const fs = require("fs");
|
2020-09-29 15:11:43 +00:00
|
|
|
|
2020-10-01 19:13:23 +00:00
|
|
|
// Modules from file
|
2020-10-16 08:06:03 +00:00
|
|
|
const shared = require("./scripts/shared.js");
|
|
|
|
const constURLs = require("./scripts/constants/urls.js");
|
|
|
|
const selectors = require("./scripts/constants/css-selectors.js");
|
2020-10-10 10:06:19 +00:00
|
|
|
const {
|
|
|
|
isStringAValidURL,
|
|
|
|
urlExists,
|
2020-10-16 08:06:03 +00:00
|
|
|
isF95URL,
|
|
|
|
} = require("./scripts/urls-helper.js");
|
|
|
|
const scraper = require("./scripts/game-scraper.js");
|
2020-10-01 19:13:23 +00:00
|
|
|
const {
|
2020-10-02 15:52:59 +00:00
|
|
|
prepareBrowser,
|
2020-10-16 08:06:03 +00:00
|
|
|
preparePage,
|
|
|
|
} = require("./scripts/puppeteer-helper.js");
|
|
|
|
const searcher = require("./scripts/game-searcher.js");
|
2020-10-12 08:27:48 +00:00
|
|
|
|
|
|
|
// Classes from file
|
2020-10-16 08:06:03 +00:00
|
|
|
const GameInfo = require("./scripts/classes/game-info.js");
|
|
|
|
const LoginResult = require("./scripts/classes/login-result.js");
|
|
|
|
const UserData = require("./scripts/classes/user-data.js");
|
2020-09-29 15:11:43 +00:00
|
|
|
|
2020-10-03 16:16:45 +00:00
|
|
|
//#region Export classes
|
2020-10-02 12:01:51 +00:00
|
|
|
module.exports.GameInfo = GameInfo;
|
|
|
|
module.exports.LoginResult = LoginResult;
|
|
|
|
module.exports.UserData = UserData;
|
2020-10-03 16:16:45 +00:00
|
|
|
//#endregion Export classes
|
2020-09-29 15:11:43 +00:00
|
|
|
|
2020-10-03 16:16:45 +00:00
|
|
|
//#region Export properties
|
2020-09-29 15:11:43 +00:00
|
|
|
/**
|
2020-10-02 15:43:14 +00:00
|
|
|
* Shows log messages and other useful functions for module debugging.
|
2020-10-02 15:52:59 +00:00
|
|
|
* @param {Boolean} value
|
2020-09-29 15:11:43 +00:00
|
|
|
*/
|
2020-09-29 21:38:08 +00:00
|
|
|
module.exports.debug = function (value) {
|
2020-10-02 15:52:59 +00:00
|
|
|
shared.debug = value;
|
|
|
|
};
|
2020-10-02 15:43:14 +00:00
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
* Indicates whether a user is logged in to the F95Zone platform or not.
|
|
|
|
* @returns {String}
|
|
|
|
*/
|
2020-09-29 21:38:08 +00:00
|
|
|
module.exports.isLogged = function () {
|
2020-10-02 15:52:59 +00:00
|
|
|
return shared.isLogged;
|
2020-09-29 15:11:43 +00:00
|
|
|
};
|
2020-10-02 15:43:14 +00:00
|
|
|
/**
|
|
|
|
* @public
|
2020-10-02 15:52:59 +00:00
|
|
|
* If true, it opens a new browser for each request
|
2020-10-02 15:43:14 +00:00
|
|
|
* to the F95Zone platform, otherwise it reuses the same.
|
|
|
|
* @returns {String}
|
|
|
|
*/
|
2020-10-02 15:52:59 +00:00
|
|
|
module.exports.setIsolation = function (value) {
|
|
|
|
shared.isolation = value;
|
|
|
|
};
|
2020-10-02 15:43:14 +00:00
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
* Path to the cache directory
|
|
|
|
* @returns {String}
|
|
|
|
*/
|
2020-10-02 15:52:59 +00:00
|
|
|
module.exports.getCacheDir = function () {
|
|
|
|
return shared.cacheDir;
|
|
|
|
};
|
2020-10-02 15:43:14 +00:00
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
* Set path to the cache directory
|
|
|
|
* @returns {String}
|
|
|
|
*/
|
2020-10-02 15:52:59 +00:00
|
|
|
module.exports.setCacheDir = function (value) {
|
|
|
|
shared.cacheDir = value;
|
2020-10-02 12:01:51 +00:00
|
|
|
|
2020-10-02 15:52:59 +00:00
|
|
|
// Create directory if it doesn't exist
|
|
|
|
if (!fs.existsSync(shared.cacheDir)) fs.mkdirSync(shared.cacheDir);
|
|
|
|
};
|
2020-10-13 13:40:10 +00:00
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
* Set local chromium path.
|
|
|
|
* @returns {String}
|
|
|
|
*/
|
|
|
|
module.exports.setChromiumPath = function (value) {
|
|
|
|
shared.chromiumLocalPath = value;
|
|
|
|
};
|
2020-10-03 16:16:45 +00:00
|
|
|
//#endregion Export properties
|
2020-09-29 15:11:43 +00:00
|
|
|
|
2020-10-02 12:01:51 +00:00
|
|
|
//#region Global variables
|
|
|
|
var _browser = null;
|
|
|
|
//#endregion
|
|
|
|
|
2020-09-29 15:11:43 +00:00
|
|
|
//#region Export methods
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
* Log in to the F95Zone platform.
|
|
|
|
* This **must** be the first operation performed before accessing any other script functions.
|
|
|
|
* @param {String} username Username used for login
|
|
|
|
* @param {String} password Password used for login
|
|
|
|
* @returns {Promise<LoginResult>} Result of the operation
|
|
|
|
*/
|
|
|
|
module.exports.login = async function (username, password) {
|
2020-10-02 15:52:59 +00:00
|
|
|
if (shared.isLogged) {
|
2020-10-16 08:06:03 +00:00
|
|
|
if (shared.debug) console.log("Already logged in");
|
2020-10-16 07:21:19 +00:00
|
|
|
const result = new LoginResult();
|
2020-10-02 15:52:59 +00:00
|
|
|
result.success = true;
|
2020-10-16 08:06:03 +00:00
|
|
|
result.message = "Already logged in";
|
2020-10-02 15:52:59 +00:00
|
|
|
return result;
|
|
|
|
}
|
2020-09-29 21:38:08 +00:00
|
|
|
|
2020-10-02 15:52:59 +00:00
|
|
|
// If cookies are loaded, use them to authenticate
|
|
|
|
shared.cookies = loadCookies();
|
|
|
|
if (shared.cookies !== null) {
|
2020-10-16 08:06:03 +00:00
|
|
|
if (shared.debug) console.log("Valid session, no need to re-authenticate");
|
2020-10-02 15:52:59 +00:00
|
|
|
shared.isLogged = true;
|
2020-10-16 07:21:19 +00:00
|
|
|
const result = new LoginResult();
|
2020-10-02 15:52:59 +00:00
|
|
|
result.success = true;
|
2020-10-16 08:06:03 +00:00
|
|
|
result.message = "Logged with cookies";
|
2020-10-02 15:52:59 +00:00
|
|
|
return result;
|
|
|
|
}
|
2020-09-29 15:11:43 +00:00
|
|
|
|
2020-10-02 15:52:59 +00:00
|
|
|
// Else, log in throught browser
|
|
|
|
if (shared.debug)
|
2020-10-16 08:06:03 +00:00
|
|
|
console.log("No saved sessions or expired session, login on the platform");
|
2020-10-02 12:01:51 +00:00
|
|
|
|
2020-10-12 12:11:36 +00:00
|
|
|
if (_browser === null && !shared.isolation) _browser = await prepareBrowser();
|
2020-10-16 07:21:19 +00:00
|
|
|
const browser = shared.isolation ? await prepareBrowser() : _browser;
|
2020-10-02 12:01:51 +00:00
|
|
|
|
2020-10-16 07:21:19 +00:00
|
|
|
const result = await loginF95(browser, username, password);
|
2020-10-02 15:52:59 +00:00
|
|
|
shared.isLogged = result.success;
|
2020-09-29 15:11:43 +00:00
|
|
|
|
2020-10-02 15:52:59 +00:00
|
|
|
if (result.success) {
|
|
|
|
// Reload cookies
|
|
|
|
shared.cookies = loadCookies();
|
2020-10-16 08:06:03 +00:00
|
|
|
if (shared.debug) console.log("User logged in through the platform");
|
2020-10-02 15:52:59 +00:00
|
|
|
} else {
|
2020-10-16 08:06:03 +00:00
|
|
|
console.warn("Error during authentication: " + result.message);
|
2020-10-02 15:52:59 +00:00
|
|
|
}
|
|
|
|
if (shared.isolation) await browser.close();
|
|
|
|
return result;
|
|
|
|
};
|
2020-09-29 15:11:43 +00:00
|
|
|
/**
|
|
|
|
* @public
|
2020-10-02 15:52:59 +00:00
|
|
|
* This method loads the main data from the F95 portal
|
|
|
|
* used to provide game information. You **must** be logged
|
2020-09-29 15:11:43 +00:00
|
|
|
* in to the portal before calling this method.
|
|
|
|
* @returns {Promise<Boolean>} Result of the operation
|
|
|
|
*/
|
|
|
|
module.exports.loadF95BaseData = async function () {
|
2020-10-12 12:11:36 +00:00
|
|
|
if (!shared.isLogged || !shared.cookies) {
|
2020-10-16 08:06:03 +00:00
|
|
|
console.warn("User not authenticated, unable to continue");
|
2020-10-02 15:52:59 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-10-16 08:06:03 +00:00
|
|
|
if (shared.debug) console.log("Loading base data...");
|
2020-10-02 15:52:59 +00:00
|
|
|
|
|
|
|
// Prepare a new web page
|
2020-10-12 12:11:36 +00:00
|
|
|
if (_browser === null && !shared.isolation) _browser = await prepareBrowser();
|
2020-10-16 07:21:19 +00:00
|
|
|
const browser = shared.isolation ? await prepareBrowser() : _browser;
|
2020-10-02 15:52:59 +00:00
|
|
|
|
2020-10-16 07:21:19 +00:00
|
|
|
const page = await preparePage(browser); // Set new isolated page
|
2020-10-02 15:52:59 +00:00
|
|
|
await page.setCookie(...shared.cookies); // Set cookies to avoid login
|
|
|
|
|
|
|
|
// Go to latest update page and wait for it to load
|
|
|
|
await page.goto(constURLs.F95_LATEST_UPDATES, {
|
2020-10-16 08:06:03 +00:00
|
|
|
waitUntil: shared.WAIT_STATEMENT,
|
2020-10-02 15:52:59 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// Obtain engines (disc/online)
|
2020-10-03 16:16:45 +00:00
|
|
|
await page.waitForSelector(selectors.ENGINE_ID_SELECTOR);
|
2020-10-02 15:52:59 +00:00
|
|
|
shared.engines = await loadValuesFromLatestPage(
|
|
|
|
page,
|
|
|
|
shared.enginesCachePath,
|
2020-10-03 16:16:45 +00:00
|
|
|
selectors.ENGINE_ID_SELECTOR,
|
2020-10-16 08:06:03 +00:00
|
|
|
"engines"
|
2020-10-02 15:52:59 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// Obtain statuses (disc/online)
|
2020-10-03 16:16:45 +00:00
|
|
|
await page.waitForSelector(selectors.STATUS_ID_SELECTOR);
|
2020-10-02 15:52:59 +00:00
|
|
|
shared.statuses = await loadValuesFromLatestPage(
|
|
|
|
page,
|
|
|
|
shared.statusesCachePath,
|
2020-10-03 16:16:45 +00:00
|
|
|
selectors.STATUS_ID_SELECTOR,
|
2020-10-16 08:06:03 +00:00
|
|
|
"statuses"
|
2020-10-02 15:52:59 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
if (shared.isolation) await browser.close();
|
2020-10-16 08:06:03 +00:00
|
|
|
if (shared.debug) console.log("Base data loaded");
|
2020-10-02 15:52:59 +00:00
|
|
|
return true;
|
|
|
|
};
|
2020-09-29 15:11:43 +00:00
|
|
|
/**
|
|
|
|
* @public
|
2020-10-10 09:33:54 +00:00
|
|
|
* Chek if exists a new version of the game.
|
2020-09-29 15:11:43 +00:00
|
|
|
* You **must** be logged in to the portal before calling this method.
|
|
|
|
* @param {GameInfo} info Information about the game to get the version for
|
2020-10-10 09:33:54 +00:00
|
|
|
* @returns {Promise<Boolean>} true if an update is available, false otherwise
|
2020-09-29 15:11:43 +00:00
|
|
|
*/
|
2020-10-10 09:33:54 +00:00
|
|
|
module.exports.chekIfGameHasUpdate = async function (info) {
|
2020-10-12 12:11:36 +00:00
|
|
|
if (!shared.isLogged || !shared.cookies) {
|
2020-10-16 08:06:03 +00:00
|
|
|
console.warn("user not authenticated, unable to continue");
|
2020-10-02 15:52:59 +00:00
|
|
|
return info.version;
|
|
|
|
}
|
2020-09-29 15:11:43 +00:00
|
|
|
|
2020-10-10 10:06:19 +00:00
|
|
|
// F95 change URL at every game update,
|
2020-10-12 12:11:36 +00:00
|
|
|
// so if the URL is different an update is available
|
2020-10-16 07:21:19 +00:00
|
|
|
const exists = await urlExists(info.f95url, true);
|
2020-10-12 12:11:36 +00:00
|
|
|
if (!exists) return true;
|
|
|
|
|
|
|
|
// Parse version from title
|
|
|
|
if (_browser === null && !shared.isolation) _browser = await prepareBrowser();
|
2020-10-16 07:21:19 +00:00
|
|
|
const browser = shared.isolation ? await prepareBrowser() : _browser;
|
2020-10-12 12:11:36 +00:00
|
|
|
|
2020-10-16 07:21:19 +00:00
|
|
|
const onlineVersion = await scraper.getGameVersionFromTitle(browser, info);
|
2020-10-12 12:11:36 +00:00
|
|
|
|
|
|
|
if (shared.isolation) await browser.close();
|
|
|
|
|
|
|
|
return onlineVersion.toUpperCase() !== info.version.toUpperCase();
|
2020-10-02 15:52:59 +00:00
|
|
|
};
|
2020-09-29 15:11:43 +00:00
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
* Starting from the name, it gets all the information about the game you are looking for.
|
|
|
|
* You **must** be logged in to the portal before calling this method.
|
|
|
|
* @param {String} name Name of the game searched
|
|
|
|
* @param {Boolean} includeMods Indicates whether to also take mods into account when searching
|
2020-10-02 15:52:59 +00:00
|
|
|
* @returns {Promise<GameInfo[]>} List of information obtained where each item corresponds to
|
2020-09-29 21:38:08 +00:00
|
|
|
* an identified game (in the case of homonymy). If no games were found, null is returned
|
2020-09-29 15:11:43 +00:00
|
|
|
*/
|
|
|
|
module.exports.getGameData = async function (name, includeMods) {
|
2020-10-12 12:11:36 +00:00
|
|
|
if (!shared.isLogged || !shared.cookies) {
|
2020-10-16 08:06:03 +00:00
|
|
|
console.warn("user not authenticated, unable to continue");
|
2020-10-02 15:52:59 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Gets the search results of the game being searched for
|
2020-10-12 12:11:36 +00:00
|
|
|
if (_browser === null && !shared.isolation) _browser = await prepareBrowser();
|
2020-10-16 07:21:19 +00:00
|
|
|
const browser = shared.isolation ? await prepareBrowser() : _browser;
|
|
|
|
const urlList = await searcher.getSearchGameResults(browser, name);
|
2020-10-02 15:52:59 +00:00
|
|
|
|
|
|
|
// Process previous partial results
|
2020-10-16 07:21:19 +00:00
|
|
|
const promiseList = [];
|
|
|
|
for (const url of urlList) {
|
2020-10-02 15:52:59 +00:00
|
|
|
// Start looking for information
|
2020-10-12 08:27:48 +00:00
|
|
|
promiseList.push(scraper.getGameInfo(browser, url));
|
2020-10-02 15:52:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Filter for mods
|
2020-10-16 07:21:19 +00:00
|
|
|
const result = [];
|
|
|
|
for (const info of await Promise.all(promiseList)) {
|
2020-10-02 15:52:59 +00:00
|
|
|
// Skip mods if not required
|
2020-10-12 08:27:48 +00:00
|
|
|
if (!info) continue;
|
2020-10-02 15:52:59 +00:00
|
|
|
if (info.isMod && !includeMods) continue;
|
|
|
|
else result.push(info);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (shared.isolation) await browser.close();
|
|
|
|
return result;
|
|
|
|
};
|
2020-10-10 09:45:43 +00:00
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
* Starting from the url, it gets all the information about the game you are looking for.
|
|
|
|
* You **must** be logged in to the portal before calling this method.
|
|
|
|
* @param {String} url URL of the game to obtain information of
|
|
|
|
* @returns {Promise<GameInfo>} Information about the game. If no game was found, null is returned
|
|
|
|
*/
|
|
|
|
module.exports.getGameDataFromURL = async function (url) {
|
2020-10-12 12:11:36 +00:00
|
|
|
if (!shared.isLogged || !shared.cookies) {
|
2020-10-16 08:06:03 +00:00
|
|
|
console.warn("user not authenticated, unable to continue");
|
2020-10-10 09:45:43 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check URL
|
2020-10-10 10:06:19 +00:00
|
|
|
if (!urlExists(url)) return null;
|
2020-10-16 08:06:03 +00:00
|
|
|
if (!isF95URL(url)) throw new Error(url + " is not a valid F95Zone URL");
|
2020-10-10 09:45:43 +00:00
|
|
|
|
|
|
|
// Gets the search results of the game being searched for
|
2020-10-12 12:11:36 +00:00
|
|
|
if (_browser === null && !shared.isolation) _browser = await prepareBrowser();
|
2020-10-16 07:21:19 +00:00
|
|
|
const browser = shared.isolation ? await prepareBrowser() : _browser;
|
2020-10-10 09:45:43 +00:00
|
|
|
|
|
|
|
// Get game data
|
2020-10-16 07:21:19 +00:00
|
|
|
const result = await scraper.getGameInfo(browser, url);
|
2020-10-10 09:45:43 +00:00
|
|
|
|
|
|
|
if (shared.isolation) await browser.close();
|
|
|
|
return result;
|
|
|
|
};
|
2020-09-29 15:11:43 +00:00
|
|
|
/**
|
|
|
|
* @public
|
2020-09-29 21:38:08 +00:00
|
|
|
* Gets the data of the currently logged in user.
|
2020-10-02 15:43:14 +00:00
|
|
|
* You **must** be logged in to the portal before calling this method.
|
2020-09-29 21:38:08 +00:00
|
|
|
* @returns {Promise<UserData>} Data of the user currently logged in or null if an error arise
|
2020-09-29 15:11:43 +00:00
|
|
|
*/
|
2020-09-29 21:38:08 +00:00
|
|
|
module.exports.getUserData = async function () {
|
2020-10-12 12:11:36 +00:00
|
|
|
if (!shared.isLogged || !shared.cookies) {
|
2020-10-16 08:06:03 +00:00
|
|
|
console.warn("user not authenticated, unable to continue");
|
2020-10-02 15:52:59 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prepare a new web page
|
2020-10-12 12:11:36 +00:00
|
|
|
if (_browser === null && !shared.isolation) _browser = await prepareBrowser();
|
2020-10-16 07:21:19 +00:00
|
|
|
const browser = shared.isolation ? await prepareBrowser() : _browser;
|
|
|
|
const page = await preparePage(browser); // Set new isolated page
|
2020-10-02 15:52:59 +00:00
|
|
|
await page.setCookie(...shared.cookies); // Set cookies to avoid login
|
|
|
|
await page.goto(constURLs.F95_BASE_URL); // Go to base page
|
|
|
|
|
|
|
|
// Explicitly wait for the required items to load
|
2020-10-03 16:16:45 +00:00
|
|
|
await page.waitForSelector(selectors.USERNAME_ELEMENT);
|
|
|
|
await page.waitForSelector(selectors.AVATAR_PIC);
|
2020-10-02 15:52:59 +00:00
|
|
|
|
2020-10-16 07:21:19 +00:00
|
|
|
const threads = getUserWatchedGameThreads(browser);
|
2020-10-02 15:52:59 +00:00
|
|
|
|
2020-10-16 07:21:19 +00:00
|
|
|
const username = await page.evaluate(
|
2020-10-02 15:52:59 +00:00
|
|
|
/* istanbul ignore next */ (selector) =>
|
|
|
|
document.querySelector(selector).innerText,
|
2020-10-03 16:16:45 +00:00
|
|
|
selectors.USERNAME_ELEMENT
|
2020-10-02 15:52:59 +00:00
|
|
|
);
|
|
|
|
|
2020-10-16 07:21:19 +00:00
|
|
|
const avatarSrc = await page.evaluate(
|
2020-10-02 15:52:59 +00:00
|
|
|
/* istanbul ignore next */ (selector) =>
|
2020-10-16 08:06:03 +00:00
|
|
|
document.querySelector(selector).getAttribute("src"),
|
2020-10-03 16:16:45 +00:00
|
|
|
selectors.AVATAR_PIC
|
2020-10-02 15:52:59 +00:00
|
|
|
);
|
|
|
|
|
2020-10-16 07:21:19 +00:00
|
|
|
const ud = new UserData();
|
2020-10-02 15:52:59 +00:00
|
|
|
ud.username = username;
|
2020-10-08 21:08:20 +00:00
|
|
|
ud.avatarSrc = isStringAValidURL(avatarSrc) ? avatarSrc : null;
|
2020-10-02 15:52:59 +00:00
|
|
|
ud.watchedThreads = await threads;
|
|
|
|
|
|
|
|
await page.close();
|
|
|
|
if (shared.isolation) await browser.close();
|
|
|
|
|
|
|
|
return ud;
|
|
|
|
};
|
2020-10-02 15:43:14 +00:00
|
|
|
/**
|
|
|
|
* @public
|
2020-10-08 19:19:54 +00:00
|
|
|
* Logout from the current user and gracefully close shared browser.
|
2020-10-02 15:43:14 +00:00
|
|
|
* You **must** be logged in to the portal before calling this method.
|
|
|
|
*/
|
2020-10-12 12:11:36 +00:00
|
|
|
module.exports.logout = async function () {
|
|
|
|
if (!shared.isLogged || !shared.cookies) {
|
2020-10-16 08:06:03 +00:00
|
|
|
console.warn("user not authenticated, unable to continue");
|
2020-10-02 15:52:59 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
shared.isLogged = false;
|
2020-10-08 19:19:54 +00:00
|
|
|
|
|
|
|
// Gracefully close shared browser
|
2020-10-08 20:39:03 +00:00
|
|
|
if (!shared.isolation && _browser !== null) {
|
2020-10-12 12:11:36 +00:00
|
|
|
await _browser.close();
|
|
|
|
_browser = null;
|
2020-10-08 19:19:54 +00:00
|
|
|
}
|
2020-10-02 15:52:59 +00:00
|
|
|
};
|
2020-09-29 15:11:43 +00:00
|
|
|
//#endregion
|
|
|
|
|
|
|
|
//#region Private methods
|
|
|
|
|
|
|
|
//#region Cookies functions
|
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
* Loads and verifies the expiration of previously stored cookies from disk
|
|
|
|
* if they exist, otherwise it returns null.
|
|
|
|
* @return {object[]} List of dictionaries or null if cookies don't exist
|
|
|
|
*/
|
|
|
|
function loadCookies() {
|
2020-10-02 15:52:59 +00:00
|
|
|
// Check the existence of the cookie file
|
|
|
|
if (fs.existsSync(shared.cookiesCachePath)) {
|
|
|
|
// Read cookies
|
2020-10-16 07:21:19 +00:00
|
|
|
const cookiesJSON = fs.readFileSync(shared.cookiesCachePath);
|
|
|
|
const cookies = JSON.parse(cookiesJSON);
|
2020-10-02 15:52:59 +00:00
|
|
|
|
|
|
|
// Check if the cookies have expired
|
2020-10-16 07:21:19 +00:00
|
|
|
for (const cookie of cookies) {
|
2020-10-02 15:52:59 +00:00
|
|
|
if (isCookieExpired(cookie)) return null;
|
|
|
|
}
|
2020-09-29 15:11:43 +00:00
|
|
|
|
2020-10-02 15:52:59 +00:00
|
|
|
// Cookies loaded and verified
|
|
|
|
return cookies;
|
|
|
|
} else return null;
|
2020-09-29 15:11:43 +00:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
* Check the validity of a cookie.
|
|
|
|
* @param {object} cookie Cookies to verify the validity. It's a dictionary
|
|
|
|
* @returns {Boolean} true if the cookie has expired, false otherwise
|
|
|
|
*/
|
|
|
|
function isCookieExpired(cookie) {
|
2020-10-02 15:52:59 +00:00
|
|
|
// Local variables
|
|
|
|
let expiredCookies = false;
|
|
|
|
|
|
|
|
// Ignore cookies that never expire
|
2020-10-16 07:45:33 +00:00
|
|
|
const expirationUnixTimestamp = cookie.expire;
|
2020-10-02 15:52:59 +00:00
|
|
|
|
2020-10-16 08:06:03 +00:00
|
|
|
if (expirationUnixTimestamp !== "-1") {
|
2020-10-02 15:52:59 +00:00
|
|
|
// Convert UNIX epoch timestamp to normal Date
|
2020-10-16 07:21:19 +00:00
|
|
|
const expirationDate = new Date(expirationUnixTimestamp * 1000);
|
2020-10-02 15:52:59 +00:00
|
|
|
|
|
|
|
if (expirationDate < Date.now()) {
|
|
|
|
if (shared.debug)
|
|
|
|
console.log(
|
2020-10-16 08:06:03 +00:00
|
|
|
"Cookie " + cookie.name + " expired, you need to re-authenticate"
|
2020-10-02 15:52:59 +00:00
|
|
|
);
|
|
|
|
expiredCookies = true;
|
2020-09-29 15:11:43 +00:00
|
|
|
}
|
2020-10-02 15:52:59 +00:00
|
|
|
}
|
2020-09-29 15:11:43 +00:00
|
|
|
|
2020-10-02 15:52:59 +00:00
|
|
|
return expiredCookies;
|
2020-09-29 15:11:43 +00:00
|
|
|
}
|
|
|
|
//#endregion Cookies functions
|
|
|
|
|
|
|
|
//#region Latest Updates page parserer
|
|
|
|
/**
|
|
|
|
* @private
|
2020-10-02 15:52:59 +00:00
|
|
|
* If present, it reads the file containing the searched values (engines or states)
|
|
|
|
* from the disk, otherwise it connects to the F95 portal (at the page
|
2020-09-29 15:11:43 +00:00
|
|
|
* https://f95zone.to/latest) and downloads them.
|
|
|
|
* @param {puppeteer.Page} page Page used to locate the required elements
|
|
|
|
* @param {String} path Path to disk of the JSON file containing the data to read / write
|
|
|
|
* @param {String} selector CSS selector of the required elements
|
|
|
|
* @param {String} elementRequested Required element (engines or states) used to detail log messages
|
|
|
|
* @returns {Promise<String[]>} List of required values in uppercase
|
|
|
|
*/
|
2020-10-02 15:52:59 +00:00
|
|
|
async function loadValuesFromLatestPage(
|
|
|
|
page,
|
|
|
|
path,
|
|
|
|
selector,
|
|
|
|
elementRequested
|
|
|
|
) {
|
|
|
|
// If the values already exist they are loaded from disk without having to connect to F95
|
2020-10-16 08:06:03 +00:00
|
|
|
if (shared.debug) console.log("Load " + elementRequested + " from disk...");
|
2020-10-02 15:52:59 +00:00
|
|
|
if (fs.existsSync(path)) {
|
2020-10-16 07:21:19 +00:00
|
|
|
const valueJSON = fs.readFileSync(path);
|
2020-10-02 15:52:59 +00:00
|
|
|
return JSON.parse(valueJSON);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, connect and download the data from the portal
|
|
|
|
if (shared.debug)
|
2020-10-16 08:06:03 +00:00
|
|
|
console.log("No " + elementRequested + " cached, downloading...");
|
2020-10-16 07:21:19 +00:00
|
|
|
const values = await getValuesFromLatestPage(
|
2020-10-02 15:52:59 +00:00
|
|
|
page,
|
|
|
|
selector,
|
2020-10-16 08:06:03 +00:00
|
|
|
"Getting " + elementRequested + " from page"
|
2020-10-02 15:52:59 +00:00
|
|
|
);
|
|
|
|
fs.writeFileSync(path, JSON.stringify(values));
|
|
|
|
return values;
|
2020-09-29 15:11:43 +00:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @private
|
2020-10-02 15:52:59 +00:00
|
|
|
* Gets all the textual values of the elements present
|
|
|
|
* in the F95 portal page and identified by the selector
|
2020-09-29 15:11:43 +00:00
|
|
|
* passed by parameter
|
|
|
|
* @param {puppeteer.Page} page Page used to locate items specified by the selector
|
|
|
|
* @param {String} selector CSS selector
|
|
|
|
* @param {String} logMessage Log message indicating which items the selector is requesting
|
|
|
|
* @return {Promise<String[]>} List of uppercase strings indicating the textual values of the elements identified by the selector
|
|
|
|
*/
|
|
|
|
async function getValuesFromLatestPage(page, selector, logMessage) {
|
2020-10-02 15:52:59 +00:00
|
|
|
if (shared.debug) console.log(logMessage);
|
2020-09-29 15:11:43 +00:00
|
|
|
|
2020-10-16 07:21:19 +00:00
|
|
|
const result = [];
|
|
|
|
const elements = await page.$$(selector);
|
2020-09-29 15:11:43 +00:00
|
|
|
|
2020-10-16 07:21:19 +00:00
|
|
|
for (const element of elements) {
|
|
|
|
const text = await element.evaluate(
|
2020-10-02 15:52:59 +00:00
|
|
|
/* istanbul ignore next */ (e) => e.innerText
|
|
|
|
);
|
2020-09-29 15:11:43 +00:00
|
|
|
|
2020-10-02 15:52:59 +00:00
|
|
|
// Save as upper text for better match if used in query
|
|
|
|
result.push(text.toUpperCase());
|
|
|
|
}
|
|
|
|
return result;
|
2020-09-29 15:11:43 +00:00
|
|
|
}
|
|
|
|
//#endregion
|
|
|
|
|
|
|
|
//#region User
|
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
* Log in to the F95Zone portal and, if successful, save the cookies.
|
|
|
|
* @param {puppeteer.Browser} browser Browser object used for navigation
|
|
|
|
* @param {String} username Username to use during login
|
|
|
|
* @param {String} password Password to use during login
|
|
|
|
* @returns {Promise<LoginResult>} Result of the operation
|
|
|
|
*/
|
|
|
|
async function loginF95(browser, username, password) {
|
2020-10-16 07:21:19 +00:00
|
|
|
const page = await preparePage(browser); // Set new isolated page
|
2020-10-02 15:52:59 +00:00
|
|
|
await page.goto(constURLs.F95_LOGIN_URL); // Go to login page
|
|
|
|
|
|
|
|
// Explicitly wait for the required items to load
|
2020-10-20 20:47:36 +00:00
|
|
|
await Promise.all([
|
|
|
|
page.waitForSelector(selectors.USERNAME_INPUT),
|
|
|
|
page.waitForSelector(selectors.PASSWORD_INPUT),
|
|
|
|
page.waitForSelector(selectors.LOGIN_BUTTON),
|
|
|
|
]);
|
|
|
|
|
2020-10-03 16:16:45 +00:00
|
|
|
await page.type(selectors.USERNAME_INPUT, username); // Insert username
|
|
|
|
await page.type(selectors.PASSWORD_INPUT, password); // Insert password
|
2020-10-14 13:12:35 +00:00
|
|
|
await Promise.all([
|
|
|
|
page.click(selectors.LOGIN_BUTTON), // Click on the login button
|
|
|
|
page.waitForNavigation({
|
2020-10-16 08:06:03 +00:00
|
|
|
waitUntil: shared.WAIT_STATEMENT,
|
2020-10-14 13:15:34 +00:00
|
|
|
}), // Wait for page to load
|
2020-10-14 13:12:35 +00:00
|
|
|
]);
|
2020-10-12 12:12:29 +00:00
|
|
|
|
2020-10-02 15:52:59 +00:00
|
|
|
// Prepare result
|
2020-10-16 07:21:19 +00:00
|
|
|
const result = new LoginResult();
|
2020-10-02 15:52:59 +00:00
|
|
|
|
|
|
|
// Check if the user is logged in
|
|
|
|
result.success = await page.evaluate(
|
|
|
|
/* istanbul ignore next */ (selector) =>
|
|
|
|
document.querySelector(selector) !== null,
|
2020-10-03 16:16:45 +00:00
|
|
|
selectors.AVATAR_INFO
|
2020-10-02 15:52:59 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// Save cookies to avoid re-auth
|
|
|
|
if (result.success) {
|
2020-10-16 07:21:19 +00:00
|
|
|
const c = await page.cookies();
|
2020-10-02 15:52:59 +00:00
|
|
|
fs.writeFileSync(shared.cookiesCachePath, JSON.stringify(c));
|
2020-10-16 08:06:03 +00:00
|
|
|
result.message = "Authentication successful";
|
2020-10-16 07:45:58 +00:00
|
|
|
} else if (
|
|
|
|
// Obtain the error message
|
2020-10-02 15:52:59 +00:00
|
|
|
await page.evaluate(
|
|
|
|
/* istanbul ignore next */ (selector) =>
|
2020-10-01 19:13:23 +00:00
|
|
|
document.querySelector(selector) !== null,
|
2020-10-03 16:16:45 +00:00
|
|
|
selectors.LOGIN_MESSAGE_ERROR
|
2020-10-02 15:52:59 +00:00
|
|
|
)
|
|
|
|
) {
|
2020-10-16 07:21:19 +00:00
|
|
|
const errorMessage = await page.evaluate(
|
2020-10-02 15:52:59 +00:00
|
|
|
/* istanbul ignore next */ (selector) =>
|
|
|
|
document.querySelector(selector).innerText,
|
2020-10-03 16:16:45 +00:00
|
|
|
selectors.LOGIN_MESSAGE_ERROR
|
2020-10-02 15:52:59 +00:00
|
|
|
);
|
|
|
|
|
2020-10-16 08:06:03 +00:00
|
|
|
if (errorMessage === "Incorrect password. Please try again.") {
|
|
|
|
result.message = "Incorrect password";
|
2020-10-02 15:52:59 +00:00
|
|
|
} else if (
|
|
|
|
errorMessage ===
|
2020-10-16 07:58:08 +00:00
|
|
|
'The requested user "' + username + '" could not be found.'
|
2020-10-02 15:52:59 +00:00
|
|
|
) {
|
2020-10-16 08:06:03 +00:00
|
|
|
result.message = "Incorrect username";
|
2020-10-02 15:52:59 +00:00
|
|
|
} else result.message = errorMessage;
|
2020-10-16 08:06:03 +00:00
|
|
|
} else result.message = "Unknown error";
|
2020-10-02 15:52:59 +00:00
|
|
|
|
|
|
|
await page.close(); // Close the page
|
|
|
|
return result;
|
2020-09-29 15:11:43 +00:00
|
|
|
}
|
|
|
|
/**
|
2020-09-29 21:38:08 +00:00
|
|
|
* @private
|
|
|
|
* Gets the list of URLs of threads the user follows.
|
|
|
|
* @param {puppeteer.Browser} browser Browser object used for navigation
|
2020-10-08 21:08:20 +00:00
|
|
|
* @returns {Promise<String[]>} URL list
|
2020-09-29 15:11:43 +00:00
|
|
|
*/
|
2020-09-29 21:38:08 +00:00
|
|
|
async function getUserWatchedGameThreads(browser) {
|
2020-10-16 07:21:19 +00:00
|
|
|
const page = await preparePage(browser); // Set new isolated page
|
2020-10-02 15:52:59 +00:00
|
|
|
await page.goto(constURLs.F95_WATCHED_THREADS); // Go to the thread page
|
|
|
|
|
|
|
|
// Explicitly wait for the required items to load
|
2020-10-03 16:16:45 +00:00
|
|
|
await page.waitForSelector(selectors.WATCHED_THREAD_FILTER_POPUP_BUTTON);
|
2020-10-02 15:52:59 +00:00
|
|
|
|
|
|
|
// Show the popup
|
2020-10-20 20:47:36 +00:00
|
|
|
await Promise.all([
|
|
|
|
page.click(selectors.WATCHED_THREAD_FILTER_POPUP_BUTTON),
|
|
|
|
page.waitForSelector(selectors.UNREAD_THREAD_CHECKBOX),
|
|
|
|
page.waitForSelector(selectors.ONLY_GAMES_THREAD_OPTION),
|
|
|
|
page.waitForSelector(selectors.FILTER_THREADS_BUTTON),
|
|
|
|
]);
|
2020-10-02 15:52:59 +00:00
|
|
|
|
|
|
|
// Set the filters
|
|
|
|
await page.evaluate(
|
|
|
|
/* istanbul ignore next */ (selector) =>
|
2020-10-16 08:06:03 +00:00
|
|
|
document.querySelector(selector).removeAttribute("checked"),
|
2020-10-03 16:16:45 +00:00
|
|
|
selectors.UNREAD_THREAD_CHECKBOX
|
2020-10-02 15:52:59 +00:00
|
|
|
); // Also read the threads already read
|
2020-10-20 20:47:36 +00:00
|
|
|
|
2020-10-02 15:52:59 +00:00
|
|
|
// Filter the threads
|
2020-10-20 20:47:36 +00:00
|
|
|
await Promise.all([
|
|
|
|
page.click(selectors.ONLY_GAMES_THREAD_OPTION),
|
|
|
|
page.click(selectors.FILTER_THREADS_BUTTON),
|
|
|
|
page.waitForSelector(selectors.WATCHED_THREAD_URLS),
|
|
|
|
]);
|
2020-10-02 15:52:59 +00:00
|
|
|
|
|
|
|
// Get the threads urls
|
2020-10-16 07:21:19 +00:00
|
|
|
const urls = [];
|
2020-10-02 15:52:59 +00:00
|
|
|
let nextPageExists = false;
|
|
|
|
do {
|
|
|
|
// Get all the URLs
|
2020-10-16 07:21:19 +00:00
|
|
|
for (const handle of await page.$$(selectors.WATCHED_THREAD_URLS)) {
|
|
|
|
const src = await page.evaluate(
|
2020-10-02 15:52:59 +00:00
|
|
|
/* istanbul ignore next */ (element) => element.href,
|
|
|
|
handle
|
|
|
|
);
|
|
|
|
// If 'unread' is left, it will redirect to the last unread post
|
2020-10-16 08:06:03 +00:00
|
|
|
const url = src.replace("/unread", "");
|
2020-10-02 15:52:59 +00:00
|
|
|
urls.push(url);
|
2020-09-29 21:38:08 +00:00
|
|
|
}
|
|
|
|
|
2020-10-02 15:52:59 +00:00
|
|
|
nextPageExists = await page.evaluate(
|
|
|
|
/* istanbul ignore next */ (selector) => document.querySelector(selector),
|
2020-10-03 16:16:45 +00:00
|
|
|
selectors.WATCHED_THREAD_NEXT_PAGE
|
2020-10-02 15:52:59 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// Click to next page
|
|
|
|
if (nextPageExists) {
|
2020-10-03 16:16:45 +00:00
|
|
|
await page.click(selectors.WATCHED_THREAD_NEXT_PAGE);
|
|
|
|
await page.waitForSelector(selectors.WATCHED_THREAD_URLS);
|
2020-10-02 15:52:59 +00:00
|
|
|
}
|
|
|
|
} while (nextPageExists);
|
|
|
|
|
|
|
|
await page.close();
|
|
|
|
return urls;
|
2020-09-29 15:11:43 +00:00
|
|
|
}
|
|
|
|
//#endregion User
|
|
|
|
|
2020-10-02 15:52:59 +00:00
|
|
|
//#endregion Private methods
|