From b1a71ac6174baa4148c903f84988e844fbe75c9e Mon Sep 17 00:00:00 2001 From: MillenniumEarl Date: Wed, 3 Mar 2021 11:32:50 +0100 Subject: [PATCH] Add method for fetch current user ID --- src/scripts/classes/mapping/user-profile.ts | 25 +++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/src/scripts/classes/mapping/user-profile.ts b/src/scripts/classes/mapping/user-profile.ts index 008b421..9a1b5b2 100644 --- a/src/scripts/classes/mapping/user-profile.ts +++ b/src/scripts/classes/mapping/user-profile.ts @@ -7,7 +7,7 @@ import cheerio from "cheerio"; import Post from "./post.js"; import PlatformUser from "./platform-user.js"; import { urls } from "../../constants/url.js"; -import { WATCHED_THREAD } from "../../constants/css-selector.js"; +import { GENERIC, WATCHED_THREAD } from "../../constants/css-selector.js"; import { fetchHTML } from "../../network-helper.js"; import { GenericAxiosError, UnexpectedResponseContentType } from "../errors.js"; import { Result } from "../result.js"; @@ -69,10 +69,16 @@ export default class UserProfile extends PlatformUser { //#endregion Getters + constructor() { super(); } + //#region Public methods public async fetch() { - // First fetch the basic data + // First get the user ID and set it + const id = await this.fetchUserID(); + super.setID(id); + + // Than fetch the basic data await super.fetch(); // Now fetch the watched threads @@ -83,6 +89,21 @@ export default class UserProfile extends PlatformUser { //#region Private methods + private async fetchUserID(): Promise { + // Local variables + const url = new URL(urls.F95_BASE_URL).toString(); + + // fetch and parse page + const htmlResponse = await fetchHTML(url); + if (htmlResponse.isSuccess()) { + // Load page with cheerio + const $ = cheerio.load(htmlResponse.value); + + const sid = $(GENERIC.CURRENT_USER_ID).attr("data-user-id").trim(); + return parseInt(sid); + } else throw htmlResponse.value; + } + private async fetchWatchedThread(): Promise { // Prepare and fetch URL const url = new URL(urls.F95_WATCHED_THREADS);