2020-10-16 08:06:03 +00:00
|
|
|
"use strict";
|
2020-10-01 19:13:23 +00:00
|
|
|
|
|
|
|
// 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";
|
2021-03-03 19:18:01 +00:00
|
|
|
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";
|
2021-03-03 19:18:01 +00:00
|
|
|
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
|
2021-03-03 19:18:01 +00:00
|
|
|
|
2020-12-26 14:23:41 +00:00
|
|
|
const USER_NOT_LOGGED = "User not authenticated, unable to continue";
|
2021-03-03 19:18:01 +00:00
|
|
|
|
2020-12-26 14:23:41 +00:00
|
|
|
//#endregion
|
|
|
|
|
2021-03-03 19:18:01 +00:00
|
|
|
//#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
|
|
|
/**
|
2020-11-02 14:26:06 +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-03-03 19:18:01 +00:00
|
|
|
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
|
2021-03-03 19:18:01 +00:00
|
|
|
|
2020-09-29 15:11:43 +00:00
|
|
|
/**
|
|
|
|
* Log in to the F95Zone platform.
|
2021-03-03 19:18:01 +00:00
|
|
|
*
|
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> {
|
2021-03-03 19:18:01 +00:00
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2021-03-03 19:18:01 +00:00
|
|
|
// Creating credentials and fetch unique platform token
|
2020-11-01 21:23:03 +00:00
|
|
|
shared.logger.trace("Fetching token...");
|
|
|
|
const creds = new Credentials(username, password);
|
|
|
|
await creds.fetchToken();
|
2020-10-02 12:01:51 +00:00
|
|
|
|
2020-11-01 21:23:03 +00:00
|
|
|
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) {
|
2021-03-03 19:18:01 +00:00
|
|
|
// Load platform data
|
2021-03-02 19:58:45 +00:00
|
|
|
await fetchPlatformData();
|
2021-03-03 19:18:01 +00:00
|
|
|
|
|
|
|
// Recreate the session, overwriting the old one
|
2021-03-02 19:58:45 +00:00
|
|
|
shared.session.create(username, password, creds.token);
|
2021-03-03 19:18:01 +00:00
|
|
|
await shared.session.save();
|
2020-12-17 21:44:40 +00:00
|
|
|
|
2021-03-03 19:18:01 +00:00
|
|
|
shared.logger.info("User logged in through the platform");
|
|
|
|
} else shared.logger.warn(`Error during authentication: ${result.message}`);
|
2020-11-01 21:23:03 +00:00
|
|
|
|
2020-10-29 21:14:40 +00:00
|
|
|
return result;
|
2020-10-02 15:52:59 +00:00
|
|
|
};
|
|
|
|
|
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;
|
|
|
|
|
2021-03-03 19:18:01 +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
|
|
|
|
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);
|
2020-10-12 12:11:36 +00:00
|
|
|
|
2021-02-27 14:33:41 +00:00
|
|
|
// Compare the versions
|
|
|
|
hasUpdate = onlineHw.version?.toUpperCase() !== hw.version?.toUpperCase();
|
|
|
|
}
|
|
|
|
|
|
|
|
return hasUpdate;
|
2020-10-02 15:52:59 +00:00
|
|
|
};
|
2020-11-01 21:23:03 +00:00
|
|
|
|
2020-09-29 15:11:43 +00:00
|
|
|
/**
|
2021-03-03 19:18:01 +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.
|
2021-03-03 19:18:01 +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-03 19:18:01 +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-02 15:52:59 +00:00
|
|
|
};
|
2020-11-01 21:23:03 +00:00
|
|
|
|
2020-10-10 09:45:43 +00:00
|
|
|
/**
|
2021-03-03 19:18:01 +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> {
|
2021-03-03 19:18:01 +00:00
|
|
|
// Check if the user is logged
|
|
|
|
if (!shared.isLogged) throw new UserNotLogged(USER_NOT_LOGGED);
|
2020-10-29 21:14:40 +00:00
|
|
|
|
2020-11-01 21:23:03 +00:00
|
|
|
// Check URL validity
|
2021-02-15 21:26:18 +00:00
|
|
|
const exists = await urlExists(url);
|
2020-11-01 21:23:03 +00:00
|
|
|
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-11-01 21:23:03 +00:00
|
|
|
|
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-11-01 21:23:03 +00:00
|
|
|
|
2020-09-29 15:11:43 +00:00
|
|
|
/**
|
2020-09-29 21:38:08 +00:00
|
|
|
* Gets the data of the currently logged in user.
|
2021-03-03 19:18:01 +00:00
|
|
|
*
|
2020-10-02 15:43:14 +00:00
|
|
|
* You **must** be logged in to the portal before calling this method.
|
2021-03-03 19:18:01 +00:00
|
|
|
*
|
|
|
|
* @returns {Promise<UserProfile>} Data of the user currently logged in
|
2020-09-29 15:11:43 +00:00
|
|
|
*/
|
2021-03-03 19:18:01 +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
|
|
|
|
2021-03-03 19:18:01 +00:00
|
|
|
// Create and fetch profile data
|
|
|
|
const profile = new UserProfile();
|
|
|
|
await profile.fetch();
|
|
|
|
|
|
|
|
return profile;
|
2020-10-02 15:52:59 +00:00
|
|
|
};
|
2020-11-30 09:40:23 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the latest updated games that match the specified parameters.
|
2021-03-03 19:18:01 +00:00
|
|
|
*
|
2020-11-30 09:40:23 +00:00
|
|
|
* You **must** be logged in to the portal before calling this method.
|
2021-03-03 19:18:01 +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-03 19:18:01 +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
|
|
|
|
2021-03-03 19:18:01 +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
|
|
|
};
|
2021-03-03 19:18:01 +00:00
|
|
|
|
2020-09-29 15:11:43 +00:00
|
|
|
//#endregion
|