From 3014ce350a9164192a28d52d7b56b94962adc60f Mon Sep 17 00:00:00 2001 From: MillenniumEarl Date: Fri, 5 Mar 2021 11:26:53 +0100 Subject: [PATCH] Run prettier --- src/example.ts | 11 +++----- src/index.ts | 8 ++---- src/scripts/classes/handiwork/game.ts | 9 +------ src/scripts/classes/mapping/platform-user.ts | 6 ++--- src/scripts/classes/mapping/post.ts | 20 +++----------- src/scripts/classes/mapping/thread.ts | 10 +++---- src/scripts/classes/mapping/user-profile.ts | 11 ++------ src/scripts/classes/prefix-parser.ts | 15 +++-------- .../classes/query/handiwork-search-query.ts | 21 +++------------ .../classes/query/latest-search-query.ts | 13 +++------ .../classes/query/thread-search-query.ts | 11 +++----- src/scripts/classes/session.ts | 5 ++-- src/scripts/constants/css-selector.ts | 18 +++++-------- src/scripts/fetch-data/fetch-latest.ts | 8 ++---- src/scripts/fetch-data/fetch-thread.ts | 3 +-- src/scripts/network-helper.ts | 10 +++---- src/scripts/scrape-data/handiwork-parse.ts | 27 +++++-------------- src/scripts/scrape-data/post-parse.ts | 8 ++---- 18 files changed, 53 insertions(+), 161 deletions(-) diff --git a/src/example.ts b/src/example.ts index c54ce1e..a4fe78e 100644 --- a/src/example.ts +++ b/src/example.ts @@ -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(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`); } } diff --git a/src/index.ts b/src/index.ts index 71d8c2b..65a7637 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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 { +export async function checkIfHandiworkHasUpdate(hw: HandiWork): Promise { // Local variables let hasUpdate = false; @@ -186,9 +184,7 @@ export async function searchHandiwork( * * You **must** be logged in to the portal before calling this method. */ -export async function getHandiworkFromURL( - url: string -): Promise { +export async function getHandiworkFromURL(url: string): Promise { // Check if the user is logged if (!shared.isLogged) throw new UserNotLogged(USER_NOT_LOGGED); diff --git a/src/scripts/classes/handiwork/game.ts b/src/scripts/classes/handiwork/game.ts index 3cc6690..d422668 100644 --- a/src/scripts/classes/handiwork/game.ts +++ b/src/scripts/classes/handiwork/game.ts @@ -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 diff --git a/src/scripts/classes/mapping/platform-user.ts b/src/scripts/classes/mapping/platform-user.ts index 2355220..9114ef7 100644 --- a/src/scripts/classes/mapping/platform-user.ts +++ b/src/scripts/classes/mapping/platform-user.ts @@ -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("$", ""); diff --git a/src/scripts/classes/mapping/post.ts b/src/scripts/classes/mapping/post.ts index 8a7c3c4..aa9c683 100644 --- a/src/scripts/classes/mapping/post.ts +++ b/src/scripts/classes/mapping/post.ts @@ -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 { + private async parsePost($: cheerio.Root, post: cheerio.Cheerio): Promise { // 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(); diff --git a/src/scripts/classes/mapping/thread.ts b/src/scripts/classes/mapping/thread.ts index bb222cd..50ce62e 100644 --- a/src/scripts/classes/mapping/thread.ts +++ b/src/scripts/classes/mapping/thread.ts @@ -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 { // 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; diff --git a/src/scripts/classes/mapping/user-profile.ts b/src/scripts/classes/mapping/user-profile.ts index 9a7c3b2..7ee1698 100644 --- a/src/scripts/classes/mapping/user-profile.ts +++ b/src/scripts/classes/mapping/user-profile.ts @@ -34,10 +34,7 @@ interface IWatchedThread { } // Types -type TFetchResult = Result< - GenericAxiosError | UnexpectedResponseContentType, - string ->; +type TFetchResult = Result; /** * 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 { + private async fetchPages(url: URL, n: number, s = 1): Promise { // Local variables const responsePromiseList: Promise[] = []; diff --git a/src/scripts/classes/prefix-parser.ts b/src/scripts/classes/prefix-parser.ts index 69d7eb7..0f64776 100644 --- a/src/scripts/classes/prefix-parser.ts +++ b/src/scripts/classes/prefix-parser.ts @@ -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; diff --git a/src/scripts/classes/query/handiwork-search-query.ts b/src/scripts/classes/query/handiwork-search-query.ts index 21854f4..2bed0ae 100644 --- a/src/scripts/classes/query/handiwork-search-query.ts +++ b/src/scripts/classes/query/handiwork-search-query.ts @@ -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>; 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" - ).execute(); - else - response = await this.cast( - "ThreadSearchQuery" - ).execute(); + response = await this.cast("LatestSearchQuery").execute(); + else response = await this.cast("ThreadSearchQuery").execute(); return response; } diff --git a/src/scripts/classes/query/latest-search-query.ts b/src/scripts/classes/query/latest-search-query.ts index 4944a01..12a87db 100644 --- a/src/scripts/classes/query/latest-search-query.ts +++ b/src/scripts/classes/query/latest-search-query.ts @@ -72,14 +72,10 @@ export default class LatestSearchQuery implements IQuery { return validator.validateSync(this).length === 0; } - public async execute(): Promise< - Result> - > { + public async execute(): Promise>> { // 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; }); diff --git a/src/scripts/classes/query/thread-search-query.ts b/src/scripts/classes/query/thread-search-query.ts index 07ed497..83d583c 100644 --- a/src/scripts/classes/query/thread-search-query.ts +++ b/src/scripts/classes/query/thread-search-query.ts @@ -78,14 +78,10 @@ export default class ThreadSearchQuery implements IQuery { return validator.validateSync(this).length === 0; } - public async execute(): Promise< - Result> - > { + public async execute(): Promise>> { // 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) diff --git a/src/scripts/classes/session.ts b/src/scripts/classes/session.ts index a09c687..a837aef 100644 --- a/src/scripts/classes/session.ts +++ b/src/scripts/classes/session.ts @@ -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; } diff --git a/src/scripts/constants/css-selector.ts b/src/scripts/constants/css-selector.ts index 31b01c6..2308fef 100644 --- a/src/scripts/constants/css-selector.ts +++ b/src/scripts/constants/css-selector.ts @@ -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]" }; diff --git a/src/scripts/fetch-data/fetch-latest.ts b/src/scripts/fetch-data/fetch-latest.ts index 75034d9..729e87b 100644 --- a/src/scripts/fetch-data/fetch-latest.ts +++ b/src/scripts/fetch-data/fetch-latest.ts @@ -24,10 +24,7 @@ export default async function fetchLatestHandiworkURLs( limit = 30 ): Promise { // 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; diff --git a/src/scripts/fetch-data/fetch-thread.ts b/src/scripts/fetch-data/fetch-thread.ts index b02faea..3020ec0 100644 --- a/src/scripts/fetch-data/fetch-thread.ts +++ b/src/scripts/fetch-data/fetch-thread.ts @@ -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; } diff --git a/src/scripts/network-helper.ts b/src/scripts/network-helper.ts index b41d951..cf4a689 100644 --- a/src/scripts/network-helper.ts +++ b/src/scripts/network-helper.ts @@ -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) => { // 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; diff --git a/src/scripts/scrape-data/handiwork-parse.ts b/src/scripts/scrape-data/handiwork-parse.ts index 65969f1..e569616 100644 --- a/src/scripts/scrape-data/handiwork-parse.ts +++ b/src/scripts/scrape-data/handiwork-parse.ts @@ -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( - url: string -): Promise; +export async function getHandiworkInformation(url: string): Promise; -export async function getHandiworkInformation( - url: string -): Promise; +export async function getHandiworkInformation(url: string): Promise; /** * 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 = diff --git a/src/scripts/scrape-data/post-parse.ts b/src/scripts/scrape-data/post-parse.ts index 3fee722..b2c2f9b 100644 --- a/src/scripts/scrape-data/post-parse.ts +++ b/src/scripts/scrape-data/post-parse.ts @@ -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); } });