F95API/app/index.js

258 lines
8.3 KiB
JavaScript
Raw Normal View History

"use strict";
// Modules from file
const shared = require("./scripts/shared.js");
const f95url = require("./scripts/constants/url.js");
const f95selector = require("./scripts/constants/css-selector.js");
const networkHelper = require("./scripts/network-helper.js");
const scraper = require("./scripts/scraper.js");
const searcher = require("./scripts/searcher.js");
2020-10-12 08:27:48 +00:00
// Classes from file
const Credentials = require("./scripts/classes/credentials.js");
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
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.
* @param {Boolean} value
2020-09-29 15:11:43 +00:00
*/
module.exports.debug = function (value) {
2020-10-29 21:14:40 +00:00
shared.debug = value;
2020-10-29 21:14:40 +00:00
// Configure logger
shared.logger.level = value ? "trace" : "warn";
};
2020-10-02 15:43:14 +00:00
/**
* @public
* Indicates whether a user is logged in to the F95Zone platform or not.
* @returns {String}
*/
module.exports.isLogged = function () {
2020-10-29 21:14:40 +00:00
return shared.isLogged;
2020-09-29 15:11:43 +00:00
};
2020-10-03 16:16:45 +00:00
//#endregion Export properties
2020-09-29 15:11:43 +00:00
//#region Global variables
const USER_NOT_LOGGED = "User not authenticated, unable to continue";
//#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-29 21:14:40 +00:00
if (shared.isLogged) {
shared.logger.info(`${username} already authenticated`);
return new LoginResult(true, `${username} already authenticated`);
2020-10-29 21:14:40 +00:00
}
shared.logger.trace("Fetching token...");
const creds = new Credentials(username, password);
await creds.fetchToken();
shared.logger.trace(`Authentication for ${username}`);
const result = await networkHelper.autenticate(creds);
2020-10-29 21:14:40 +00:00
shared.isLogged = result.success;
2020-09-29 15:11:43 +00:00
if (result.success) shared.logger.info("User logged in through the platform");
else shared.logger.warn(`Error during authentication: ${result.message}`);
2020-10-29 21:14:40 +00:00
return result;
};
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
*/
module.exports.checkIfGameHasUpdate = async function (info) {
if (!shared.isLogged) {
2020-10-29 21:14:40 +00:00
shared.logger.warn(USER_NOT_LOGGED);
return false;
}
2020-09-29 15:11:43 +00:00
2020-10-29 21:14:40 +00:00
// F95 change URL at every game update,
// so if the URL is different an update is available
const exists = await networkHelper.urlExists(info.url, true);
2020-10-29 21:14:40 +00:00
if (!exists) return true;
2020-10-29 21:14:40 +00:00
// Parse version from title
const onlineVersion = await scraper.getGameInfo(info.url).version;
// Compare the versions
2020-10-29 21:14:40 +00:00
return onlineVersion.toUpperCase() !== info.version.toUpperCase();
};
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} mod Indicate if you are looking for mods or games
* @returns {Promise<GameInfo[]>} List of information obtained where each item corresponds to
* an identified game (in the case of homonymy of titles)
2020-09-29 15:11:43 +00:00
*/
module.exports.getGameData = async function (name, mod) {
if (!shared.isLogged) {
2020-10-29 21:14:40 +00:00
shared.logger.warn(USER_NOT_LOGGED);
return null;
}
// Gets the search results of the game/mod being searched for
let urls = [];
if(mod) urls = await searcher.searchMod(name);
else urls = await searcher.searchGame(name);
2020-10-29 21:14:40 +00:00
// Process previous partial results
const results = [];
for (const url of urls) {
// Start looking for information
const info = scraper.getGameInfo(url);
results.push(info);
2020-10-29 21:14:40 +00:00
}
return results;
};
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) {
if (!shared.isLogged) {
2020-10-29 21:14:40 +00:00
shared.logger.warn(USER_NOT_LOGGED);
return null;
}
// Check URL validity
const exists = await networkHelper.urlExists(url);
if (!exists) throw new URIError(`${url} is not a valid URL`);
if (!networkHelper.isF95URL(url)) throw new Error(`${url} is not a valid F95Zone URL`);
2020-10-29 21:14:40 +00:00
// Get game data
return await scraper.getGameInfo(url);
2020-10-10 09:45:43 +00:00
};
2020-09-29 15:11:43 +00:00
/**
* @public
* 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.
* @returns {Promise<UserData>} Data of the user currently logged in
2020-09-29 15:11:43 +00:00
*/
module.exports.getUserData = async function () {
if (!shared.isLogged) {
2020-10-29 21:14:40 +00:00
shared.logger.warn(USER_NOT_LOGGED);
return null;
}
const threads = await getUserWatchedGameThreads(null);
2020-10-29 21:14:40 +00:00
const username = await page.evaluate(
/* istanbul ignore next */ (selector) =>
2020-10-29 21:14:40 +00:00
document.querySelector(selector).innerText,
f95selector.USERNAME_ELEMENT
2020-10-29 21:14:40 +00:00
);
2020-10-29 21:14:40 +00:00
const avatarSrc = await page.evaluate(
/* istanbul ignore next */ (selector) =>
2020-10-29 21:14:40 +00:00
document.querySelector(selector).getAttribute("src"),
f95selector.AVATAR_PIC
2020-10-29 21:14:40 +00:00
);
2020-10-29 21:14:40 +00:00
const ud = new UserData();
ud.username = username;
ud.avatarSrc = networkHelper.isStringAValidURL(avatarSrc) ? avatarSrc : null;
ud.watchedThreads = threads;
2020-10-29 21:14:40 +00:00
return ud;
};
2020-09-29 15:11:43 +00:00
//#endregion
//#region Private methods
//#region User
/**
* @private
* Gets the list of URLs of threads the user follows.
* @param {puppeteer.Browser} browser Browser object used for navigation
* @returns {Promise<String[]>} URL list
2020-09-29 15:11:43 +00:00
*/
async function getUserWatchedGameThreads() {
const page = null;
await page.goto(f95url.F95_WATCHED_THREADS); // Go to the thread page
2020-10-29 21:14:40 +00:00
// Explicitly wait for the required items to load
await page.waitForSelector(f95selector.WATCHED_THREAD_FILTER_POPUP_BUTTON);
2020-10-29 21:14:40 +00:00
// Show the popup
await Promise.all([
page.click(f95selector.WATCHED_THREAD_FILTER_POPUP_BUTTON),
page.waitForSelector(f95selector.UNREAD_THREAD_CHECKBOX),
page.waitForSelector(f95selector.ONLY_GAMES_THREAD_OPTION),
page.waitForSelector(f95selector.FILTER_THREADS_BUTTON),
2020-10-29 21:14:40 +00:00
]);
// Set the filters
await page.evaluate(
/* istanbul ignore next */ (selector) =>
2020-10-29 21:14:40 +00:00
document.querySelector(selector).removeAttribute("checked"),
f95selector.UNREAD_THREAD_CHECKBOX
2020-10-29 21:14:40 +00:00
); // Also read the threads already read
// Filter the threads
await page.click(f95selector.ONLY_GAMES_THREAD_OPTION);
await page.click(f95selector.FILTER_THREADS_BUTTON);
await page.waitForSelector(f95selector.WATCHED_THREAD_URLS);
2020-10-29 21:14:40 +00:00
// Get the threads urls
const urls = [];
let nextPageExists = false;
do {
// Get all the URLs
for (const handle of await page.$$(f95selector.WATCHED_THREAD_URLS)) {
2020-10-29 21:14:40 +00:00
const src = await page.evaluate(
/* istanbul ignore next */ (element) => element.href,
handle
);
// If 'unread' is left, it will redirect to the last unread post
const url = src.replace("/unread", "");
urls.push(url);
}
nextPageExists = await page.evaluate(
/* istanbul ignore next */ (selector) => document.querySelector(selector),
f95selector.WATCHED_THREAD_NEXT_PAGE
2020-10-29 21:14:40 +00:00
);
// Click to next page
if (nextPageExists) {
await page.click(f95selector.WATCHED_THREAD_NEXT_PAGE);
await page.waitForSelector(f95selector.WATCHED_THREAD_URLS);
2020-10-29 21:14:40 +00:00
}
} while (nextPageExists);
await page.close();
return urls;
2020-09-29 15:11:43 +00:00
}
//#endregion User
//#endregion Private methods