F95API/src/index.ts

245 lines
7.7 KiB
TypeScript
Raw Normal View History

2021-03-04 12:19:49 +00:00
// Copyright (c) 2021 MillenniumEarl
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
"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-03-05 09:56:27 +00:00
import {
authenticate,
urlExists,
isF95URL,
send2faCode
} 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
2021-03-05 09:56:27 +00:00
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";
2021-03-05 09:56:27 +00:00
//#endregion Re-export classes
2020-09-29 15:11:43 +00:00
2020-10-03 16:16:45 +00:00
//#region Export properties
2021-03-05 09:56:27 +00:00
2020-09-29 15:11:43 +00:00
/**
* Set the logger level for module debugging.
2020-09-29 15:11:43 +00:00
*/
2021-03-04 12:15:01 +00:00
// eslint-disable-next-line prefer-const
export let loggerLevel = shared.logger.level;
2021-02-27 14:33:41 +00:00
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-03-04 11:26:45 +00:00
export function isLogged(): boolean {
return shared.isLogged;
}
2021-03-05 09:56:27 +00:00
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.
2021-03-04 09:42:41 +00:00
*
2020-09-29 15:11:43 +00:00
* This **must** be the first operation performed before accessing any other script functions.
2021-03-05 09:56:27 +00:00
*
* @param cb2fa
* Callback used if two-factor authentication is required for the profile.
* It must return he OTP code to use for the login.
2020-09-29 15:11:43 +00:00
*/
2021-03-04 11:26:45 +00:00
export async function login(
username: string,
2021-03-05 09:56:27 +00:00
password: string,
cb2fa?: () => Promise<number>
2021-03-04 11:26:45 +00:00
): Promise<LoginResult> {
// Try to load a previous session
await shared.session.load();
2021-03-04 11:26:45 +00:00
// If the session is valid, return
if (shared.session.isValid(username, password)) {
shared.logger.info(`Loading previous session for ${username}`);
2021-03-04 11:26:45 +00:00
// Load platform data
await fetchPlatformData();
2021-03-04 11:26:45 +00:00
shared.setIsLogged(true);
2021-03-05 10:20:46 +00:00
return new LoginResult(
true,
LoginResult.ALREADY_AUTHENTICATED,
`${username} already authenticated (session)`
);
2021-03-04 11:26:45 +00:00
}
2020-10-29 21:14:40 +00:00
2021-03-04 11:26:45 +00:00
// Creating credentials and fetch unique platform token
shared.logger.trace("Fetching token...");
const creds = new Credentials(username, password);
await creds.fetchToken();
2021-03-04 11:26:45 +00:00
shared.logger.trace(`Authentication for ${username}`);
2021-03-05 09:56:27 +00:00
let result = await authenticate(creds);
2021-03-04 11:26:45 +00:00
shared.setIsLogged(result.success);
2020-09-29 15:11:43 +00:00
2021-03-05 09:56:27 +00:00
// 2FA Authentication is required, fetch OTP
if (result.message === "Two-factor authentication is needed to continue") {
const code = await cb2fa();
const response2fa = await send2faCode(code, creds.token);
if (response2fa.isSuccess()) result = response2fa.value;
else throw response2fa.value;
}
2021-03-05 09:56:27 +00:00
if (result.success) {
2021-03-04 11:26:45 +00:00
// Recreate the session, overwriting the old one
shared.session.create(username, password, creds.token);
await shared.session.save();
2020-12-17 21:44:40 +00:00
2021-03-05 09:56:27 +00:00
// Load platform data
await fetchPlatformData();
2021-03-04 11:26:45 +00:00
shared.logger.info("User logged in through the platform");
} else shared.logger.warn(`Error during authentication: ${result.message}`);
2021-03-04 11:26:45 +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.
2021-03-04 09:42:41 +00:00
*
2020-09-29 15:11:43 +00:00
* You **must** be logged in to the portal before calling this method.
*/
2021-03-05 10:26:53 +00:00
export async function checkIfHandiworkHasUpdate(hw: HandiWork): Promise<boolean> {
2021-03-04 11:26:45 +00:00
// Local variables
let hasUpdate = false;
2021-02-27 14:33:41 +00:00
2021-03-04 11:26:45 +00:00
// Check if the user is logged
if (!shared.isLogged) throw new UserNotLogged(USER_NOT_LOGGED);
2020-09-29 15:11:43 +00:00
2021-03-04 11:26:45 +00:00
// F95 change URL at every game update,
// so if the URL is different an update is available
if (await urlExists(hw.url, true)) {
// Fetch the online handiwork
const onlineHw = await getHandiworkFromURL<HandiWork>(hw.url);
2021-03-04 11:26:45 +00:00
// Compare the versions
hasUpdate = onlineHw.version?.toUpperCase() !== hw.version?.toUpperCase();
}
2021-02-27 14:33:41 +00:00
2021-03-04 11:26:45 +00:00
return hasUpdate;
}
2020-09-29 15:11:43 +00:00
/**
* Search for one or more handiworks identified by a specific query.
2021-03-04 09:42:41 +00:00
*
2020-09-29 15:11:43 +00:00
* You **must** be logged in to the portal before calling this method.
2021-03-04 09:42:41 +00:00
*
* @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
*/
2021-03-04 11:26:45 +00:00
export async function searchHandiwork<T extends IBasic>(
query: HandiworkSearchQuery,
2021-03-05 09:56:27 +00:00
limit: number = 10
2021-03-04 11:26:45 +00:00
): 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-03-04 11:26:45 +00:00
return 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-03-04 09:42:41 +00:00
*
2020-10-10 09:45:43 +00:00
* You **must** be logged in to the portal before calling this method.
*/
2021-03-05 10:26:53 +00:00
export async function getHandiworkFromURL<T extends IBasic>(url: string): Promise<T> {
2021-03-04 11:26:45 +00:00
// Check if the user is logged
if (!shared.isLogged) throw new UserNotLogged(USER_NOT_LOGGED);
// Check URL validity
const exists = await urlExists(url);
if (!exists) throw new URIError(`${url} is not a valid URL`);
if (!isF95URL(url)) throw new Error(`${url} is not a valid F95Zone URL`);
// Get game data
return getHandiworkInformation<T>(url);
}
2020-09-29 15:11:43 +00:00
/**
* Gets the data of the currently logged in user.
2021-03-04 09:42:41 +00:00
*
2020-10-02 15:43:14 +00:00
* You **must** be logged in to the portal before calling this method.
2021-03-04 09:42:41 +00:00
*
* @returns {Promise<UserProfile>} Data of the user currently logged in
2020-09-29 15:11:43 +00:00
*/
export async function getUserData(): Promise<UserProfile> {
2021-03-04 11:26:45 +00:00
// Check if the user is logged
if (!shared.isLogged) throw new UserNotLogged(USER_NOT_LOGGED);
// Create and fetch profile data
const profile = new UserProfile();
await profile.fetch();
2020-10-29 21:14:40 +00:00
2021-03-04 11:26:45 +00:00
return profile;
}
2020-11-30 09:40:23 +00:00
/**
* Gets the latest updated games that match the specified parameters.
2021-03-04 09:42:41 +00:00
*
2020-11-30 09:40:23 +00:00
* You **must** be logged in to the portal before calling this method.
2021-03-04 09:42:41 +00:00
*
* @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
*/
2021-03-04 11:26:45 +00:00
export async function getLatestUpdates<T extends IBasic>(
query: LatestSearchQuery,
2021-03-05 09:56:27 +00:00
limit: number = 10
2021-03-04 11:26:45 +00:00
): Promise<T[]> {
// Check limit value
if (limit <= 0) throw new Error("limit must be greater than 0");
// Check if the user is logged
if (!shared.isLogged) throw new UserNotLogged(USER_NOT_LOGGED);
// Fetch the results
const urls = await fetchLatestHandiworkURLs(query, limit);
// Get the data from urls
const promiseList = urls.map((u: string) => getHandiworkInformation<T>(u));
return Promise.all(promiseList);
}
2020-09-29 15:11:43 +00:00
//#endregion