Add method for fetch current user ID

pull/73/head
MillenniumEarl 2021-03-03 11:32:50 +01:00
parent 3a41426207
commit b1a71ac617
1 changed files with 23 additions and 2 deletions

View File

@ -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<number> {
// 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<IWatchedThread[]> {
// Prepare and fetch URL
const url = new URL(urls.F95_WATCHED_THREADS);