Post is now a lazy class

pull/73/head
MillenniumEarl 2021-03-02 17:08:34 +01:00
parent 63d27febfa
commit 4a16642540
1 changed files with 18 additions and 23 deletions

View File

@ -65,38 +65,33 @@ export default class Post {
//#endregion Getters //#endregion Getters
constructor(id: number) { this._id = id; }
//#region Public methods //#region Public methods
public async fetchData(id: number): Promise<void>;
public async fetchData(article: cheerio.Cheerio): Promise<void>;
/** /**
* Gets the post data starting from its unique ID for the entire platform. * Gets the post data starting from its unique ID for the entire platform.
*/ */
public async fetchData(arg: number | cheerio.Cheerio): Promise<void> { public async fetch() {
if (typeof arg === "number") { // Fetch HTML page containing the post
// Fetch HTML page containing the post const url = new URL(this.id.toString(), urls.F95_POSTS).toString();
const url = new URL(arg.toString(), urls.F95_POSTS).toString(); const htmlResponse = await fetchHTML(url);
const htmlResponse = await fetchHTML(url);
if (htmlResponse.isSuccess()) { if (htmlResponse.isSuccess()) {
// Load cheerio and find post // Load cheerio and find post
const $ = cheerio.load(htmlResponse.value); const $ = cheerio.load(htmlResponse.value);
const post = $(THREAD.POSTS_IN_PAGE).toArray().find((el, idx) => { const post = $(THREAD.POSTS_IN_PAGE).toArray().find((el, idx) => {
// Fetch the ID and check if it is what we are searching // 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); const id = parseInt(sid);
if (id === arg) return el; if (id === this.id) return el;
}); });
// Finally parse the post // Finally parse the post
this.parsePost($(post)); this.parsePost($(post));
} else throw htmlResponse.value; } else throw htmlResponse.value;
} else this.parsePost(arg);
} }
//#endregion Public methods //#endregion Public methods