From ca11a442ed0e33d696577fbdf3ed2c4f777dc837 Mon Sep 17 00:00:00 2001 From: MillenniumEarl Date: Fri, 5 Mar 2021 13:04:43 +0100 Subject: [PATCH] Update JSDOC and url names --- scripts/constants/url.d.ts | 28 ++++---- src/scripts/classes/mapping/platform-user.ts | 2 +- src/scripts/classes/mapping/post.ts | 2 +- src/scripts/classes/mapping/thread.ts | 4 +- src/scripts/classes/mapping/user-profile.ts | 9 +-- .../classes/query/latest-search-query.ts | 2 +- .../classes/query/thread-search-query.ts | 2 +- src/scripts/constants/url.ts | 69 ++++++++++++++----- src/scripts/fetch-data/fetch-latest.ts | 2 +- src/scripts/fetch-data/fetch-platform-data.ts | 2 +- src/scripts/fetch-data/fetch-thread.ts | 2 +- src/scripts/network-helper.ts | 14 ++-- 12 files changed, 86 insertions(+), 52 deletions(-) diff --git a/scripts/constants/url.d.ts b/scripts/constants/url.d.ts index fc06636..9bbe6e8 100644 --- a/scripts/constants/url.d.ts +++ b/scripts/constants/url.d.ts @@ -1,27 +1,27 @@ export declare const urls: { - F95_BASE_URL: string; - F95_2FA_LOGIN: string; - F95_SEARCH_URL: string; - F95_LATEST_UPDATES: string; - F95_THREADS: string; - F95_LOGIN_URL: string; - F95_WATCHED_THREADS: string; - F95_LATEST_PHP: string; - F95_BOOKMARKS: string; + BASE: string; + LOGIN_2FA: string; + SEARCH: string; + LATEST_UPDATES: string; + THREADS: string; + LOGIN: string; + WATCHED_THREADS: string; + LATEST_PHP: string; + BOOKMARKS: string; /** * Add the unique ID of the post to * get the thread page where the post * is present. */ - F95_POSTS: string; + POSTS: string; /** * @todo */ - F95_CONVERSATIONS: string; + CONVERSATIONS: string; /** * @todo */ - F95_ALERTS: string; - F95_POSTS_NUMBER: string; - F95_MEMBERS: string; + ALERTS: string; + POSTS_NUMBER: string; + MEMBERS: string; }; diff --git a/src/scripts/classes/mapping/platform-user.ts b/src/scripts/classes/mapping/platform-user.ts index 9114ef7..f14a3b2 100644 --- a/src/scripts/classes/mapping/platform-user.ts +++ b/src/scripts/classes/mapping/platform-user.ts @@ -148,7 +148,7 @@ export default class PlatformUser { if (!this.id && this.id < 1) throw new Error("Invalid user ID"); // Prepare the URL - const url = new URL(this.id.toString(), `${urls.F95_MEMBERS}/`).toString(); + const url = new URL(this.id.toString(), `${urls.MEMBERS}/`).toString(); // Fetch the page const htmlResponse = await fetchHTML(url); diff --git a/src/scripts/classes/mapping/post.ts b/src/scripts/classes/mapping/post.ts index aa9c683..a09ce4c 100644 --- a/src/scripts/classes/mapping/post.ts +++ b/src/scripts/classes/mapping/post.ts @@ -96,7 +96,7 @@ export default class Post { */ public async fetch(): Promise { // Fetch HTML page containing the post - const url = new URL(this.id.toString(), urls.F95_POSTS).toString(); + const url = new URL(this.id.toString(), urls.POSTS).toString(); const htmlResponse = await fetchHTML(url); if (htmlResponse.isSuccess()) { diff --git a/src/scripts/classes/mapping/thread.ts b/src/scripts/classes/mapping/thread.ts index 50ce62e..ed532dd 100644 --- a/src/scripts/classes/mapping/thread.ts +++ b/src/scripts/classes/mapping/thread.ts @@ -139,7 +139,7 @@ export default class Thread { }; // Send POST request - const response = await fetchPOSTResponse(urls.F95_POSTS_NUMBER, params); + const response = await fetchPOSTResponse(urls.POSTS_NUMBER, params); if (response.isFailure()) throw response.value; } @@ -238,7 +238,7 @@ export default class Thread { */ public async fetch(): Promise { // Prepare the url - this._url = new URL(this.id.toString(), urls.F95_THREADS).toString(); + this._url = new URL(this.id.toString(), urls.THREADS).toString(); // Fetch the HTML source const htmlResponse = await fetchHTML(this.url); diff --git a/src/scripts/classes/mapping/user-profile.ts b/src/scripts/classes/mapping/user-profile.ts index 7ee1698..267fd77 100644 --- a/src/scripts/classes/mapping/user-profile.ts +++ b/src/scripts/classes/mapping/user-profile.ts @@ -105,7 +105,7 @@ export default class UserProfile extends PlatformUser { private async fetchUserID(): Promise { // Local variables - const url = new URL(urls.F95_BASE_URL).toString(); + const url = new URL(urls.BASE).toString(); // fetch and parse page const htmlResponse = await fetchHTML(url); @@ -120,7 +120,7 @@ export default class UserProfile extends PlatformUser { private async fetchWatchedThread(): Promise { // Prepare and fetch URL - const url = new URL(urls.F95_WATCHED_THREADS); + const url = new URL(urls.WATCHED_THREADS); url.searchParams.set("unread", "0"); const htmlResponse = await fetchHTML(url.toString()); @@ -177,10 +177,7 @@ export default class UserProfile extends PlatformUser { .map((idx, el) => { // Parse the URL const partialURL = $(el).find(WATCHED_THREAD.URL).attr("href"); - const url = new URL( - partialURL.replace("unread", ""), - `${urls.F95_BASE_URL}` - ).toString(); + const url = new URL(partialURL.replace("unread", ""), `${urls.BASE}`).toString(); return { url: url.toString(), diff --git a/src/scripts/classes/query/latest-search-query.ts b/src/scripts/classes/query/latest-search-query.ts index 12a87db..a93e2e8 100644 --- a/src/scripts/classes/query/latest-search-query.ts +++ b/src/scripts/classes/query/latest-search-query.ts @@ -110,7 +110,7 @@ export default class LatestSearchQuery implements IQuery { */ private prepareGETurl(): URL { // Create the URL - const url = new URL(urls.F95_LATEST_PHP); + const url = new URL(urls.LATEST_PHP); url.searchParams.set("cmd", "list"); // Set the category diff --git a/src/scripts/classes/query/thread-search-query.ts b/src/scripts/classes/query/thread-search-query.ts index 83d583c..6b9e801 100644 --- a/src/scripts/classes/query/thread-search-query.ts +++ b/src/scripts/classes/query/thread-search-query.ts @@ -88,7 +88,7 @@ export default class ThreadSearchQuery implements IQuery { const params = this.preparePOSTParameters(); // Return the POST response - return fetchPOSTResponse(urls.F95_SEARCH_URL, params); + return fetchPOSTResponse(urls.SEARCH, params); } //#endregion Public methods diff --git a/src/scripts/constants/url.ts b/src/scripts/constants/url.ts index 5b45a3c..014e98d 100644 --- a/src/scripts/constants/url.ts +++ b/src/scripts/constants/url.ts @@ -4,29 +4,66 @@ // https://opensource.org/licenses/MIT export const urls = { - F95_BASE_URL: "https://f95zone.to", - F95_2FA_LOGIN: "https://f95zone.to/login/two-step", - F95_SEARCH_URL: "https://f95zone.to/search/search/", - F95_LATEST_UPDATES: "https://f95zone.to/latest", - F95_THREADS: "https://f95zone.to/threads/", - F95_LOGIN_URL: "https://f95zone.to/login/login", - F95_WATCHED_THREADS: "https://f95zone.to/watched/threads", - F95_LATEST_PHP: "https://f95zone.to/new_latest.php", - F95_BOOKMARKS: "https://f95zone.to/account/bookmarks", + /** + * Page with the list of alerts for the user currently logged. + */ + ALERTS: "https://f95zone.to/account/alerts", + /** + * Basic URL of the platform. + */ + BASE: "https://f95zone.to", + /** + * Page with the list of favorite posts of the user currently logged. + */ + BOOKMARKS: "https://f95zone.to/account/bookmarks", + /** + * Page with the list of conversations of the currently logged user. + */ + CONVERSATIONS: "https://f95zone.to/conversations/", + /** + * URL of the script used for searching for content + * in the "Latest Updates" section of the platform. + */ + LATEST_PHP: "https://f95zone.to/new_latest.php", + /** + * Page with the latest updated platform content. + */ + LATEST_UPDATES: "https://f95zone.to/latest", + /** + * Page used for user login. + */ + LOGIN: "https://f95zone.to/login/login", + /** + * Page used for entering the OTP code in the case of two-factor authentication. + */ + LOGIN_2FA: "https://f95zone.to/login/two-step", + /** + * Summary page of users registered on the platform. + * Used for the search for a specific member through ID. + */ + MEMBERS: "https://f95zone.to/members", /** * Add the unique ID of the post to * get the thread page where the post * is present. */ - F95_POSTS: "https://f95zone.to/posts/", + POSTS: "https://f95zone.to/posts/", /** - * @todo + * URL used to send a POST request and change + * the number of posts that can be viewed per + * page of a specific thread. */ - F95_CONVERSATIONS: "https://f95zone.to/conversations/", + POSTS_NUMBER: "https://f95zone.to/account/dpp-update", /** - * @todo + * URL used to search the platform by POST request. */ - F95_ALERTS: "https://f95zone.to/account/alerts", - F95_POSTS_NUMBER: "https://f95zone.to/account/dpp-update", - F95_MEMBERS: "https://f95zone.to/members" + SEARCH: "https://f95zone.to/search/search/", + /** + * Add the unique ID of the thread to get it's page. + */ + THREADS: "https://f95zone.to/threads/", + /** + * Page with the list of watched threads of the currently logged user. + */ + WATCHED_THREADS: "https://f95zone.to/watched/threads" }; diff --git a/src/scripts/fetch-data/fetch-latest.ts b/src/scripts/fetch-data/fetch-latest.ts index 729e87b..ca89d04 100644 --- a/src/scripts/fetch-data/fetch-latest.ts +++ b/src/scripts/fetch-data/fetch-latest.ts @@ -41,7 +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.THREADS).href; resultURLs.push(gameURL); fetchedResults += 1; diff --git a/src/scripts/fetch-data/fetch-platform-data.ts b/src/scripts/fetch-data/fetch-platform-data.ts index 0c1095a..8105d5c 100644 --- a/src/scripts/fetch-data/fetch-platform-data.ts +++ b/src/scripts/fetch-data/fetch-platform-data.ts @@ -58,7 +58,7 @@ export default async function fetchPlatformData(): Promise { // Check if the data are cached if (!readCache(shared.cachePath)) { // Load the HTML - const html = await fetchHTML(f95url.F95_LATEST_UPDATES); + const html = await fetchHTML(f95url.LATEST_UPDATES); // Parse data if (html.isSuccess()) { diff --git a/src/scripts/fetch-data/fetch-thread.ts b/src/scripts/fetch-data/fetch-thread.ts index be2f035..317bfe8 100644 --- a/src/scripts/fetch-data/fetch-thread.ts +++ b/src/scripts/fetch-data/fetch-thread.ts @@ -77,7 +77,7 @@ function extractLinkFromResult(selector: cheerio.Cheerio): string { const partialLink = selector.find(THREAD_SEARCH.THREAD_TITLE).attr("href").trim(); // Compose and return the URL - return new URL(partialLink, f95urls.F95_BASE_URL).toString(); + return new URL(partialLink, f95urls.BASE).toString(); } //#endregion Private methods diff --git a/src/scripts/network-helper.ts b/src/scripts/network-helper.ts index ccbade8..92f45f9 100644 --- a/src/scripts/network-helper.ts +++ b/src/scripts/network-helper.ts @@ -98,7 +98,7 @@ export async function authenticate( throw new InvalidF95Token(`Invalid token for auth: ${credentials.token}`); // Secure the URL - const secureURL = enforceHttpsUrl(urls.F95_LOGIN_URL); + const secureURL = enforceHttpsUrl(urls.LOGIN); // Prepare the parameters to send to the platform to authenticate const params = { @@ -117,7 +117,7 @@ export async function authenticate( let authResult: LoginResult = null; try { // Fetch the response to the login request - const response = await fetchPOSTResponse(urls.F95_LOGIN_URL, params, force); + const response = await fetchPOSTResponse(urls.LOGIN, params, force); // Parse the response const result = response.applyOnSuccess((r) => manageLoginPOSTResponse(r)); @@ -149,7 +149,7 @@ export async function send2faCode( ): Promise> { // Prepare the parameters to send via POST request const params = { - _xfRedirect: urls.F95_BASE_URL, + _xfRedirect: urls.BASE, _xfRequestUri: "/login/two-step?_xfRedirect=https%3A%2F%2Ff95zone.to%2F&remember=1", _xfResponseType: "json", _xfToken: token, @@ -162,7 +162,7 @@ export async function send2faCode( }; // Send 2FA params - const response = await fetchPOSTResponse(urls.F95_2FA_LOGIN, params); + const response = await fetchPOSTResponse(urls.LOGIN_2FA, params); return response.applyOnSuccess((r: AxiosResponse) => { // r.data.status is 'ok' if the authentication is successful const result = r.data.status === "ok"; @@ -182,7 +182,7 @@ export async function send2faCode( */ export async function getF95Token(): Promise { // Fetch the response of the platform - const response = await fetchGETResponse(urls.F95_LOGIN_URL); + const response = await fetchGETResponse(urls.LOGIN); if (response.isSuccess()) { // The response is a HTML page, we need to find the with name "_xfToken" @@ -274,7 +274,7 @@ export function enforceHttpsUrl(url: string): string { * Check if the url belongs to the domain of the F95 platform. */ export function isF95URL(url: string): boolean { - return url.toString().startsWith(urls.F95_BASE_URL); + return url.toString().startsWith(urls.BASE); } /** @@ -358,7 +358,7 @@ function manageLoginPOSTResponse(response: AxiosResponse) { const $ = cheerio.load(response.data as string); // Check if 2 factor authentication is required - if (response.config.url.startsWith(urls.F95_2FA_LOGIN)) { + if (response.config.url.startsWith(urls.LOGIN_2FA)) { return new LoginResult( false, LoginResult.REQUIRE_2FA,