Run prettier

pull/81/head
MillenniumEarl 2021-03-05 11:26:53 +01:00
parent 48885a8604
commit 3014ce350a
18 changed files with 53 additions and 161 deletions

View File

@ -74,8 +74,7 @@ async function main() {
// Get user data
console.log("Fetching user data...");
const userdata = await getUserData();
const gameThreads = userdata.watched.filter((e) => e.forum === "Games")
.length;
const gameThreads = userdata.watched.filter((e) => e.forum === "Games").length;
console.log(
`${userdata.name} follows ${userdata.watched.length} threads of which ${gameThreads} are games\n`
);
@ -87,9 +86,7 @@ async function main() {
const latestUpdates = await getLatestUpdates<Game>(latestQuery, 1);
console.log(
`"${
latestUpdates.shift().name
}" was the last "3d game" tagged game to be updated\n`
`"${latestUpdates.shift().name}" was the last "3d game" tagged game to be updated\n`
);
// Get game data
@ -114,8 +111,6 @@ async function main() {
// Extract first game
const gamedata = searchResult.shift();
const authors = gamedata.authors.map((a, idx) => a.name).join(", ");
console.log(
`Found: ${gamedata.name} (${gamedata.version}) by ${authors}\n`
);
console.log(`Found: ${gamedata.name} (${gamedata.version}) by ${authors}\n`);
}
}

View File

@ -141,9 +141,7 @@ export async function login(
*
* You **must** be logged in to the portal before calling this method.
*/
export async function checkIfHandiworkHasUpdate(
hw: HandiWork
): Promise<boolean> {
export async function checkIfHandiworkHasUpdate(hw: HandiWork): Promise<boolean> {
// Local variables
let hasUpdate = false;
@ -186,9 +184,7 @@ export async function searchHandiwork<T extends IBasic>(
*
* You **must** be logged in to the portal before calling this method.
*/
export async function getHandiworkFromURL<T extends IBasic>(
url: string
): Promise<T> {
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);

View File

@ -6,14 +6,7 @@
"use strict";
// Modules from files
import {
TAuthor,
TEngine,
IGame,
TRating,
TStatus,
TCategory
} from "../../interfaces";
import { TAuthor, TEngine, IGame, TRating, TStatus, TCategory } from "../../interfaces";
export default class Game implements IGame {
//#region Properties

View File

@ -180,12 +180,10 @@ export default class PlatformUser {
// Parse date
const joined = $(MEMBER.JOINED)?.attr("datetime");
if (luxon.DateTime.fromISO(joined).isValid)
this._joined = new Date(joined);
if (luxon.DateTime.fromISO(joined).isValid) this._joined = new Date(joined);
const lastSeen = $(MEMBER.LAST_SEEN)?.attr("datetime");
if (luxon.DateTime.fromISO(lastSeen).isValid)
this._joined = new Date(lastSeen);
if (luxon.DateTime.fromISO(lastSeen).isValid) this._joined = new Date(lastSeen);
// Parse donation
const donation = $(MEMBER.AMOUNT_DONATED)?.text().replace("$", "");

View File

@ -10,10 +10,7 @@ import cheerio from "cheerio";
// Modules from file
import PlatformUser from "./platform-user.js";
import {
IPostElement,
parseF95ThreadPost
} from "../../scrape-data/post-parse.js";
import { IPostElement, parseF95ThreadPost } from "../../scrape-data/post-parse.js";
import { POST, THREAD } from "../../constants/css-selector.js";
import { urls } from "../../constants/url.js";
import { fetchHTML } from "../../network-helper.js";
@ -110,10 +107,7 @@ export default class Post {
.toArray()
.find((el, idx) => {
// Fetch the ID and check if it is what we are searching
const sid: string = $(el)
.find(POST.ID)
.attr("id")
.replace("post-", "");
const sid: string = $(el).find(POST.ID).attr("id").replace("post-", "");
const id = parseInt(sid, 10);
if (id === this.id) return el;
@ -128,10 +122,7 @@ export default class Post {
//#region Private methods
private async parsePost(
$: cheerio.Root,
post: cheerio.Cheerio
): Promise<void> {
private async parsePost($: cheerio.Root, post: cheerio.Cheerio): Promise<void> {
// Find post's ID
const sid: string = post.find(POST.ID).attr("id").replace("post-", "");
this._id = parseInt(sid, 10);
@ -149,10 +140,7 @@ export default class Post {
this._lastEdit = new Date(sLastEdit);
// Find post's owner
const sOwnerID: string = post
.find(POST.OWNER_ID)
.attr("data-user-id")
.trim();
const sOwnerID: string = post.find(POST.OWNER_ID).attr("data-user-id").trim();
this._owner = new PlatformUser(parseInt(sOwnerID, 10));
await this._owner.fetch();

View File

@ -195,9 +195,7 @@ export default class Thread {
}
// Sorts the list of posts
return fetchedPosts.sort((a, b) =>
a.id > b.id ? 1 : b.id > a.id ? -1 : 0
);
return fetchedPosts.sort((a, b) => (a.id > b.id ? 1 : b.id > a.id ? -1 : 0));
}
/**
@ -267,8 +265,7 @@ export default class Thread {
this._category = JSONLD["articleSection"] as TCategory;
// Validate the dates
if (luxon.DateTime.fromISO(modified).isValid)
this._modified = new Date(modified);
if (luxon.DateTime.fromISO(modified).isValid) this._modified = new Date(modified);
if (luxon.DateTime.fromISO(published).isValid)
this._publication = new Date(published);
} else throw htmlResponse.value;
@ -282,8 +279,7 @@ export default class Thread {
*/
public async getPost(index: number): Promise<Post | null> {
// Validate parameters
if (index < 1)
throw new ParameterError("Index must be greater or equal than 1");
if (index < 1) throw new ParameterError("Index must be greater or equal than 1");
// Local variables
let returnValue = null;

View File

@ -34,10 +34,7 @@ interface IWatchedThread {
}
// Types
type TFetchResult = Result<
GenericAxiosError | UnexpectedResponseContentType,
string
>;
type TFetchResult = Result<GenericAxiosError | UnexpectedResponseContentType, string>;
/**
* Class containing the data of the user currently connected to the F95Zone platform.
@ -151,11 +148,7 @@ export default class UserProfile extends PlatformUser {
* @param n Total number of pages
* @param s Page to start from
*/
private async fetchPages(
url: URL,
n: number,
s = 1
): Promise<TFetchResult[]> {
private async fetchPages(url: URL, n: number, s = 1): Promise<TFetchResult[]> {
// Local variables
const responsePromiseList: Promise<TFetchResult>[] = [];

View File

@ -19,10 +19,7 @@ export default class PrefixParser {
* @param {Any} value Value associated with the key
* @returns {String|undefined} Key found or undefined
*/
private getKeyByValue(
object: TPrefixDict,
value: string
): string | undefined {
private getKeyByValue(object: TPrefixDict, value: string): string | undefined {
return Object.keys(object).find((key) => object[key] === value);
}
@ -54,9 +51,7 @@ export default class PrefixParser {
* desired element and return the dictionary that contains it.
* @param element Element to search in the prefixes as a key or as a value
*/
private searchElementInPrefixes(
element: string | number
): TPrefixDict | null {
private searchElementInPrefixes(element: string | number): TPrefixDict | null {
// Local variables
let dictName = null;
@ -64,13 +59,11 @@ export default class PrefixParser {
for (const [key, subdict] of Object.entries(shared.prefixes)) {
// Check if the element is a value in the sub-dict
const valueInDict =
typeof element === "string" &&
this.valueInDict(subdict, element as string);
typeof element === "string" && this.valueInDict(subdict, element as string);
// Check if the element is a key in the subdict
const keyInDict =
typeof element === "number" &&
Object.keys(subdict).includes(element.toString());
typeof element === "number" && Object.keys(subdict).includes(element.toString());
if (valueInDict || keyInDict) {
dictName = key;

View File

@ -35,13 +35,7 @@ import ThreadSearchQuery, { TThreadOrder } from "./thread-search-query.js";
*
* `views`: Order based on the number of visits. Replacement: `replies`.
*/
type THandiworkOrder =
| "date"
| "likes"
| "relevance"
| "replies"
| "title"
| "views";
type THandiworkOrder = "date" | "likes" | "relevance" | "replies" | "title" | "views";
type TExecuteResult = Result<GenericAxiosError, AxiosResponse<any>>;
export default class HandiworkSearchQuery implements IQuery {
@ -118,20 +112,13 @@ export default class HandiworkSearchQuery implements IQuery {
// Check if the query is valid
if (!this.validate()) {
throw new Error(
`Invalid query: ${validator.validateSync(this).join("\n")}`
);
throw new Error(`Invalid query: ${validator.validateSync(this).join("\n")}`);
}
// Convert the query
if (this.selectSearchType() === "latest")
response = await this.cast<LatestSearchQuery>(
"LatestSearchQuery"
).execute();
else
response = await this.cast<ThreadSearchQuery>(
"ThreadSearchQuery"
).execute();
response = await this.cast<LatestSearchQuery>("LatestSearchQuery").execute();
else response = await this.cast<ThreadSearchQuery>("ThreadSearchQuery").execute();
return response;
}

View File

@ -72,14 +72,10 @@ export default class LatestSearchQuery implements IQuery {
return validator.validateSync(this).length === 0;
}
public async execute(): Promise<
Result<GenericAxiosError, AxiosResponse<any>>
> {
public async execute(): Promise<Result<GenericAxiosError, AxiosResponse<any>>> {
// Check if the query is valid
if (!this.validate()) {
throw new Error(
`Invalid query: ${validator.validateSync(this).join("\n")}`
);
throw new Error(`Invalid query: ${validator.validateSync(this).join("\n")}`);
}
// Prepare the URL
@ -98,10 +94,7 @@ export default class LatestSearchQuery implements IQuery {
const diff = this.dateDiffInDays(new Date(), d);
// Find the closest valid value in the array
const closest = [365, 180, 90, 30, 14, 7, 3, 1].reduce(function (
prev,
curr
) {
const closest = [365, 180, 90, 30, 14, 7, 3, 1].reduce(function (prev, curr) {
return Math.abs(curr - diff) < Math.abs(prev - diff) ? curr : prev;
});

View File

@ -78,14 +78,10 @@ export default class ThreadSearchQuery implements IQuery {
return validator.validateSync(this).length === 0;
}
public async execute(): Promise<
Result<GenericAxiosError, AxiosResponse<any>>
> {
public async execute(): Promise<Result<GenericAxiosError, AxiosResponse<any>>> {
// Check if the query is valid
if (!this.validate()) {
throw new Error(
`Invalid query: ${validator.validateSync(this).join("\n")}`
);
throw new Error(`Invalid query: ${validator.validateSync(this).join("\n")}`);
}
// Define the POST parameters
@ -131,8 +127,7 @@ export default class ThreadSearchQuery implements IQuery {
// Set included and excluded tags (joined with a comma)
if (this.includedTags) params["c[tags]"] = this.includedTags.join(",");
if (this.excludedTags)
params["c[excludeTags]"] = this.excludedTags.join(",");
if (this.excludedTags) params["c[excludeTags]"] = this.excludedTags.join(",");
// Set minimum reply number
if (this.minimumReplies > 0)

View File

@ -210,9 +210,8 @@ export default class Session {
// Search for expired cookies
const jarValid =
this._cookieJar
.getCookiesSync("https://f95zone.to")
.filter((el) => el.TTL() === 0).length === 0;
this._cookieJar.getCookiesSync("https://f95zone.to").filter((el) => el.TTL() === 0)
.length === 0;
return dateValid && hashValid && jarValid;
}

View File

@ -15,8 +15,7 @@ export const selectors = {
GET_REQUEST_TOKEN: 'input[name="_xfToken"]',
UD_USERNAME_ELEMENT: 'a[href="/account/"] > span.p-navgroup-linkText',
UD_AVATAR_PIC: 'a[href="/account/"] > span.avatar > img[class^="avatar"]',
LOGIN_MESSAGE_ERROR:
"div.blockMessage.blockMessage--error.blockMessage--iconic",
LOGIN_MESSAGE_ERROR: "div.blockMessage.blockMessage--error.blockMessage--iconic",
LU_TAGS_SCRIPT: "script:contains('latestUpdates')",
BK_RESULTS: "ol.listPlain > * div.contentRow-main",
BK_POST_URL: "div.contentRow-title > a",
@ -117,8 +116,7 @@ export const POST = {
*
* For use within a `THREAD.POSTS_IN_PAGE` selector.
*/
NUMBER:
'* ul.message-attribution-opposite > li > a:not([id])[rel="nofollow"]',
NUMBER: '* ul.message-attribution-opposite > li > a:not([id])[rel="nofollow"]',
/**
* Unique ID of the post in the F95Zone platform in the `id` attribute.
*
@ -154,8 +152,7 @@ export const POST = {
*
* For use within a `THREAD.POSTS_IN_PAGE` selector.
*/
BOOKMARKED:
'* ul.message-attribution-opposite >li > a[title="Bookmark"].is-bookmarked'
BOOKMARKED: '* ul.message-attribution-opposite >li > a[title="Bookmark"].is-bookmarked'
};
export const MEMBER = {
@ -188,15 +185,13 @@ export const MEMBER = {
*
* The date is contained in the `datetime` attribute as an ISO string.
*/
JOINED:
"div.uix_memberHeader__extra > div.memberHeader-blurb:nth-child(1) > * time",
JOINED: "div.uix_memberHeader__extra > div.memberHeader-blurb:nth-child(1) > * time",
/**
* Last time the user connected to the platform.
*
* The date is contained in the `datetime` attribute as an ISO string.
*/
LAST_SEEN:
"div.uix_memberHeader__extra > div.memberHeader-blurb:nth-child(2) > * time",
LAST_SEEN: "div.uix_memberHeader__extra > div.memberHeader-blurb:nth-child(2) > * time",
MESSAGES: "div.pairJustifier > dl:nth-child(1) > * a",
REACTION_SCORE: "div.pairJustifier > dl:nth-child(2) > dd",
POINTS: "div.pairJustifier > dl:nth-child(3) > * a",
@ -216,6 +211,5 @@ export const MEMBER = {
* If the text is `Unignore` then the user is ignored.
* If the text is `Ignore` then the user is not ignored.
*/
IGNORED:
"div.memberHeader-buttons > div.buttonGroup:first-child > a[data-sk-ignore]"
IGNORED: "div.memberHeader-buttons > div.buttonGroup:first-child > a[data-sk-ignore]"
};

View File

@ -24,10 +24,7 @@ export default async function fetchLatestHandiworkURLs(
limit = 30
): Promise<string[]> {
// Local variables
const shallowQuery: LatestSearchQuery = Object.assign(
new LatestSearchQuery(),
query
);
const shallowQuery: LatestSearchQuery = Object.assign(new LatestSearchQuery(), query);
const resultURLs = [];
let fetchedResults = 0;
let noMorePages = false;
@ -44,8 +41,7 @@ export default async function fetchLatestHandiworkURLs(
data.map((e, idx) => {
if (fetchedResults < limit) {
const gameURL = new URL(e.thread_id.toString(), urls.F95_THREADS)
.href;
const gameURL = new URL(e.thread_id.toString(), urls.F95_THREADS).href;
resultURLs.push(gameURL);
fetchedResults += 1;

View File

@ -34,8 +34,7 @@ export default async function fetchThreadHandiworkURLs(
const response = await query.execute();
// Fetch the results from F95 and return the handiwork urls
if (response.isSuccess())
return fetchResultURLs(response.value.data as string, limit);
if (response.isSuccess()) return fetchResultURLs(response.value.data as string, limit);
else throw response.value;
}

View File

@ -150,8 +150,7 @@ export async function send2faCode(
// Prepare the parameters to send via POST request
const params = {
_xfRedirect: urls.F95_BASE_URL,
_xfRequestUri:
"/login/two-step?_xfRedirect=https%3A%2F%2Ff95zone.to%2F&remember=1",
_xfRequestUri: "/login/two-step?_xfRedirect=https%3A%2F%2Ff95zone.to%2F&remember=1",
_xfResponseType: "json",
_xfToken: token,
_xfWithData: "1",
@ -167,9 +166,7 @@ export async function send2faCode(
return response.applyOnSuccess((r: AxiosResponse<any>) => {
// r.data.status is 'ok' if the authentication is successful
const result = r.data.status === "ok";
const message = result
? "Authentication successful"
: r.data.errors.join(",");
const message = result ? "Authentication successful" : r.data.errors.join(",");
const code = result
? LoginResult.AUTH_SUCCESSFUL_2FA
: message ===
@ -239,8 +236,7 @@ export async function fetchPOSTResponse(
// Prepare the parameters for the POST request
const urlParams = new URLSearchParams();
for (const [key, value] of Object.entries(params))
urlParams.append(key, value);
for (const [key, value] of Object.entries(params)) urlParams.append(key, value);
// Shallow copy of the common configuration object
commonConfig.jar = shared.session.cookieJar;

View File

@ -11,23 +11,13 @@ import luxon from "luxon";
// Modules from files
import HandiWork from "../classes/handiwork/handiwork.js";
import Thread from "../classes/mapping/thread.js";
import {
IBasic,
TAuthor,
TEngine,
TExternalPlatform,
TStatus
} from "../interfaces.js";
import { IBasic, TAuthor, TEngine, TExternalPlatform, TStatus } from "../interfaces.js";
import shared, { TPrefixDict } from "../shared.js";
import { ILink, IPostElement } from "./post-parse.js";
export async function getHandiworkInformation<T extends IBasic>(
url: string
): Promise<T>;
export async function getHandiworkInformation<T extends IBasic>(url: string): Promise<T>;
export async function getHandiworkInformation<T extends IBasic>(
url: string
): Promise<T>;
export async function getHandiworkInformation<T extends IBasic>(url: string): Promise<T>;
/**
* Gets information of a particular handiwork from its thread.
@ -175,8 +165,7 @@ function fillWithPrefixes(hw: HandiWork, prefixes: string[]) {
const prefix = item.replace("[", "").replace("]", "");
// Check what the prefix indicates
if (stringInDict(prefix, shared.prefixes["engines"]))
engine = prefix as TEngine;
if (stringInDict(prefix, shared.prefixes["engines"])) engine = prefix as TEngine;
else if (stringInDict(prefix, shared.prefixes["statuses"]))
status = prefix as TStatus;
else if (stringInDict(prefix, fakeModDict)) mod = true;
@ -212,10 +201,7 @@ function fillWithPostData(hw: HandiWork, elements: IPostElement[]) {
?.text?.split(",")
.map((s) => s.trim());
hw.version = getPostElementByName(elements, "version")?.text;
hw.installation = getPostElementByName(
elements,
"installation"
)?.content.shift()?.text;
hw.installation = getPostElementByName(elements, "installation")?.content.shift()?.text;
hw.pages = getPostElementByName(elements, "pages")?.text;
hw.resolution = getPostElementByName(elements, "resolution")
?.text?.split(",")
@ -240,8 +226,7 @@ function fillWithPostData(hw: HandiWork, elements: IPostElement[]) {
// Fill the dates
const releaseDate = getPostElementByName(elements, "release date")?.text;
if (luxon.DateTime.fromISO(releaseDate).isValid)
hw.lastRelease = new Date(releaseDate);
if (luxon.DateTime.fromISO(releaseDate).isValid) hw.lastRelease = new Date(releaseDate);
//#region Convert the author
const authorElement =

View File

@ -173,8 +173,7 @@ function reducePostElement(element: IPostElement): IPostElement {
const content = element.content[0] as IPostElement;
const nullValues =
(!element.name || !content.name) && (!element.text || !content.text);
const sameValues =
element.name === content.name || element.text === content.text;
const sameValues = element.name === content.name || element.text === content.text;
if (nullValues || sameValues) {
element.name = element.name || content.name;
@ -245,10 +244,7 @@ function parseCheerioNode(
const childElement = parseCheerioNode($, el);
// If the children is valid (not empty) push it
if (
(childElement.text || childElement.content.length !== 0) &&
!isTextNode(el)
) {
if ((childElement.text || childElement.content.length !== 0) && !isTextNode(el)) {
content.content.push(childElement);
}
});