From 4a166425405e4d0062dc14116af1c3a4781487ac Mon Sep 17 00:00:00 2001 From: MillenniumEarl Date: Tue, 2 Mar 2021 17:08:34 +0100 Subject: [PATCH] Post is now a lazy class --- src/scripts/classes/post.ts | 41 ++++++++++++++++--------------------- 1 file changed, 18 insertions(+), 23 deletions(-) diff --git a/src/scripts/classes/post.ts b/src/scripts/classes/post.ts index 08b59d7..53662d5 100644 --- a/src/scripts/classes/post.ts +++ b/src/scripts/classes/post.ts @@ -65,38 +65,33 @@ export default class Post { //#endregion Getters + constructor(id: number) { this._id = id; } + //#region Public methods - public async fetchData(id: number): Promise; - - public async fetchData(article: cheerio.Cheerio): Promise; - /** * Gets the post data starting from its unique ID for the entire platform. */ - public async fetchData(arg: number | cheerio.Cheerio): Promise { - if (typeof arg === "number") { - // Fetch HTML page containing the post - const url = new URL(arg.toString(), urls.F95_POSTS).toString(); - const htmlResponse = await fetchHTML(url); + public async fetch() { + // Fetch HTML page containing the post + const url = new URL(this.id.toString(), urls.F95_POSTS).toString(); + const htmlResponse = await fetchHTML(url); - if (htmlResponse.isSuccess()) { - // Load cheerio and find post - const $ = cheerio.load(htmlResponse.value); + if (htmlResponse.isSuccess()) { + // Load cheerio and find post + const $ = cheerio.load(htmlResponse.value); - const post = $(THREAD.POSTS_IN_PAGE).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 id = parseInt(sid); + const post = $(THREAD.POSTS_IN_PAGE).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 id = parseInt(sid); - if (id === arg) return el; - }); + if (id === this.id) return el; + }); - // Finally parse the post - this.parsePost($(post)); - } else throw htmlResponse.value; - - } else this.parsePost(arg); + // Finally parse the post + this.parsePost($(post)); + } else throw htmlResponse.value; } //#endregion Public methods