F95API/src/index.ts

203 lines
7.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 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 UserProfile from "./scripts/classes/mapping/user-profile.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";
import { UserNotLogged } from "./scripts/classes/errors.js";
2020-09-29 15:11:43 +00:00
2020-12-26 14:23:41 +00:00
//#region Global variables
2020-12-26 14:23:41 +00:00
const USER_NOT_LOGGED = "User not authenticated, unable to continue";
2020-12-26 14:23:41 +00:00
//#endregion
//#region Re-export classes
export { default as Animation } from "./scripts/classes/handiwork/animation.js";
export { default as Asset } from "./scripts/classes/handiwork/asset.js";
export { default as Comic } from "./scripts/classes/handiwork/comic.js";
export { default as Game } from "./scripts/classes/handiwork/game.js";
export { default as Handiwork } from "./scripts/classes/handiwork/handiwork.js";
export { default as PlatformUser } from "./scripts/classes/mapping/platform-user.js";
export { default as Post } from "./scripts/classes/mapping/post.js";
export { default as Thread } from "./scripts/classes/mapping/thread.js";
export { default as UserProfile } from "./scripts/classes/mapping/user-profile.js";
export { default as HandiworkSearchQuery } from "./scripts/classes/query/handiwork-search-query.js";
export { default as LatestSearchQuery } from "./scripts/classes/query/latest-search-query.js";
export { default as ThreadSearchQuery } from "./scripts/classes/query/thread-search-query.js";
//#endregion Re-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.
*/
export function isLogged(): boolean { return shared.isLogged; };
2020-10-03 16:16:45 +00:00
//#endregion Export properties
2020-09-29 15:11:43 +00:00
//#region Export methods
2020-09-29 15:11:43 +00:00
/**
* Log in to the F95Zone platform.
*
2020-09-29 15:11:43 +00:00
* This **must** be the first operation performed before accessing any other script functions.
*/
2021-02-15 21:26:18 +00:00
export async function login(username: string, password: string): Promise<LoginResult> {
// Try to load a previous session
await shared.session.load();
// If the session is valid, return
if (shared.session.isValid(username, password)) {
shared.logger.info(`Loading previous session for ${username}`);
// Load platform data
await fetchPlatformData();
shared.setIsLogged(true);
return new LoginResult(true, `${username} already authenticated (session)`);
2020-10-29 21:14:40 +00:00
}
// Creating credentials and fetch unique platform token
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
2021-03-02 19:58:45 +00:00
if (result.success) {
// Load platform data
2021-03-02 19:58:45 +00:00
await fetchPlatformData();
// Recreate the session, overwriting the old one
2021-03-02 19:58:45 +00:00
shared.session.create(username, password, creds.token);
await shared.session.save();
2020-12-17 21:44:40 +00:00
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;
// Check if the user is logged
if (!shared.isLogged) throw new UserNotLogged(USER_NOT_LOGGED);
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
/**
* Search for one or more handiworks identified by a specific query.
*
2020-09-29 15:11:43 +00:00
* You **must** be logged in to the portal before calling this method.
*
* @param {HandiworkSearchQuery} query Parameters used for the search.
* @param {Number} limit Maximum number of results. Default: 10
2020-09-29 15:11:43 +00:00
*/
export async function searchHandiwork<T extends IBasic>(query: HandiworkSearchQuery, limit: number = 10): Promise<T[]> {
// Check if the user is logged
if (!shared.isLogged) throw new UserNotLogged(USER_NOT_LOGGED);
2020-10-29 21:14:40 +00:00
2021-02-27 14:33:41 +00:00
return await search<T>(query, limit);
};
2020-10-10 09:45:43 +00:00
/**
* Given the url, it gets all the information about the handiwork requested.
2021-02-27 14:33:41 +00:00
*
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> {
// Check if the user is logged
if (!shared.isLogged) throw new UserNotLogged(USER_NOT_LOGGED);
2020-10-29 21:14:40 +00:00
// 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<UserProfile>} Data of the user currently logged in
2020-09-29 15:11:43 +00:00
*/
export async function getUserData(): Promise<UserProfile> {
// Check if the user is logged
if (!shared.isLogged) throw new UserNotLogged(USER_NOT_LOGGED);
2020-10-29 21:14:40 +00:00
// Create and fetch profile data
const profile = new UserProfile();
await profile.fetch();
return profile;
};
2020-11-30 09:40:23 +00:00
/**
* Gets the latest updated games that match the specified parameters.
*
2020-11-30 09:40:23 +00:00
* You **must** be logged in to the portal before calling this method.
*
* @param {LatestSearchQuery} query Parameters used for the search.
* @param {Number} limit Maximum number of results. Default: 10
2020-11-30 09:40:23 +00:00
*/
export async function getLatestUpdates<T extends IBasic>(query: LatestSearchQuery, limit: number = 10): 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
// Check if the user is logged
if (!shared.isLogged) throw new UserNotLogged(USER_NOT_LOGGED);
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