F95API/src/index.ts

184 lines
6.2 KiB
TypeScript
Raw Normal View History

"use strict";
// Modules from file
2021-02-15 21:26:18 +00:00
import shared from "./scripts/shared.js";
2021-02-27 14:33:41 +00:00
import search from "./scripts/search.js";
2021-02-15 21:26:18 +00:00
import { authenticate, urlExists, isF95URL } from "./scripts/network-helper.js";
2021-02-27 14:33:41 +00:00
import { getUserData as retrieveUserData } from "./scripts/scrape-data/scrape-user.js";
import fetchLatestHandiworkURLs from "./scripts/fetch-data/fetch-latest.js";
import fetchPlatformData from "./scripts/fetch-data/fetch-platform-data.js";
2021-03-02 20:00:02 +00:00
import getHandiworkInformation from "./scripts/scrape-data/handiwork-parse.js";
2021-02-27 14:33:41 +00:00
import { IBasic } from "./scripts/interfaces.js";
2020-10-12 08:27:48 +00:00
// Classes from file
2021-02-15 21:26:18 +00:00
import Credentials from "./scripts/classes/credentials.js";
import LoginResult from "./scripts/classes/login-result.js";
import UserData from "./scripts/classes/user-data.js";
2021-02-27 14:33:41 +00:00
import LatestSearchQuery from "./scripts/classes/query/latest-search-query.js";
import HandiworkSearchQuery from "./scripts/classes/query/handiwork-search-query.js";
import HandiWork from "./scripts/classes/handiwork/handiwork.js";
2020-09-29 15:11:43 +00:00
2020-12-26 14:23:41 +00:00
//#region Global variables
const USER_NOT_LOGGED = "User not authenticated, unable to continue";
//#endregion
2020-10-03 16:16:45 +00:00
//#region Export classes
2021-02-27 14:33:41 +00:00
// module.exports.GameInfo = GameInfo;
// module.exports.LoginResult = LoginResult;
// module.exports.UserData = UserData;
// module.exports.PrefixParser = PrefixParser;
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
/**
* Set the logger level for module debugging.
2020-09-29 15:11:43 +00:00
*/
2020-11-02 14:06:09 +00:00
/* istambul ignore next */
2021-02-27 14:33:41 +00:00
export var loggerLevel = shared.logger.level;
shared.logger.level = "warn"; // By default log only the warn messages
2021-02-15 21:26:18 +00:00
2020-10-02 15:43:14 +00:00
/**
* Indicates whether a user is logged in to the F95Zone platform or not.
*/
2021-02-15 21:26:18 +00:00
export function isLogged(): boolean {
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 Export methods
/**
* Log in to the F95Zone platform.
* This **must** be the first operation performed before accessing any other script functions.
* @returns {Promise<LoginResult>} Result of the operation
*/
2021-02-15 21:26:18 +00:00
export async function login(username: string, password: string): Promise<LoginResult> {
/* istanbul ignore next */
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}`);
2021-02-15 21:26:18 +00:00
const result = await authenticate(creds);
shared.setIsLogged(result.success);
2020-09-29 15:11:43 +00:00
2020-12-17 21:44:40 +00:00
// Load platform data
2021-03-02 19:58:45 +00:00
if (result.success) {
await fetchPlatformData();
shared.session.create(username, password, creds.token);
}
2020-12-17 21:44:40 +00:00
2020-12-15 14:22:10 +00:00
/* istambul ignore next */
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
/**
2021-02-27 14:33:41 +00:00
* Chek if exists a new version of the handiwork.
*
2020-09-29 15:11:43 +00:00
* You **must** be logged in to the portal before calling this method.
*/
2021-02-27 14:33:41 +00:00
export async function checkIfHandiworkHasUpdate(hw: HandiWork): Promise<boolean> {
// Local variables
let hasUpdate = false;
/* istanbul ignore next */
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
2021-02-27 14:33:41 +00:00
if (await urlExists(hw.url, true)) {
// Fetch the online handiwork
const onlineHw = await getHandiworkFromURL<HandiWork>(hw.url);
2021-02-27 14:33:41 +00:00
// Compare the versions
hasUpdate = onlineHw.version?.toUpperCase() !== hw.version?.toUpperCase();
}
return hasUpdate;
};
2020-09-29 15:11:43 +00:00
/**
* 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
*/
2021-02-27 14:33:41 +00:00
export async function getHandiwork<T extends IBasic>(query: HandiworkSearchQuery, limit: number = 30): Promise<T[]> {
/* istanbul ignore next */
if (!shared.isLogged) {
2020-10-29 21:14:40 +00:00
shared.logger.warn(USER_NOT_LOGGED);
return null;
}
2021-02-27 14:33:41 +00:00
return await search<T>(query, limit);
};
2020-10-10 09:45:43 +00:00
/**
2021-02-27 14:33:41 +00:00
* Starting from the url, it gets all the information
* about the handiwork requested.
*
2020-10-10 09:45:43 +00:00
* You **must** be logged in to the portal before calling this method.
*/
2021-02-27 14:33:41 +00:00
export async function getHandiworkFromURL<T extends IBasic>(url: string): Promise<T> {
/* istanbul ignore next */
if (!shared.isLogged) {
2020-10-29 21:14:40 +00:00
shared.logger.warn(USER_NOT_LOGGED);
return null;
}
// Check URL validity
2021-02-15 21:26:18 +00:00
const exists = await urlExists(url);
if (!exists) throw new URIError(`${url} is not a valid URL`);
2021-02-15 21:26:18 +00:00
if (!isF95URL(url)) throw new Error(`${url} is not a valid F95Zone URL`);
2020-10-29 21:14:40 +00:00
// Get game data
2021-02-27 14:33:41 +00:00
return await getHandiworkInformation<T>(url);
2020-10-10 09:45:43 +00:00
};
2020-09-29 15:11:43 +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.
* @returns {Promise<UserData>} Data of the user currently logged in
2020-09-29 15:11:43 +00:00
*/
2021-02-15 21:26:18 +00:00
export async function getUserData(): Promise<UserData> {
/* istanbul ignore next */
if (!shared.isLogged) {
2020-10-29 21:14:40 +00:00
shared.logger.warn(USER_NOT_LOGGED);
return null;
}
2021-02-15 21:26:18 +00:00
return await retrieveUserData();
};
2020-11-30 09:40:23 +00:00
/**
* Gets the latest updated games that match the specified parameters.
* You **must** be logged in to the portal before calling this method.
2021-02-27 14:33:41 +00:00
* @param {LatestSearchQuery} query
2020-11-30 09:40:23 +00:00
* Parameters used for the search.
* @param {Number} limit Maximum number of results
*/
2021-02-27 14:33:41 +00:00
export async function getLatestUpdates<T extends IBasic>(query: LatestSearchQuery, limit: number): Promise<T[]> {
2020-11-30 09:40:23 +00:00
// Check limit value
2021-02-27 14:33:41 +00:00
if (limit <= 0) throw new Error("limit must be greater than 0");
2020-11-30 09:40:23 +00:00
2021-02-27 14:33:41 +00:00
// Fetch the results
const urls = await fetchLatestHandiworkURLs(query, limit);
2020-11-30 09:40:23 +00:00
2021-02-27 14:33:41 +00:00
// Get the data from urls
const promiseList = urls.map((u: string) => getHandiworkInformation<T>(u));
2020-11-30 13:01:16 +00:00
return await Promise.all(promiseList);
2020-11-30 09:40:23 +00:00
};
2020-09-29 15:11:43 +00:00
//#endregion