Implement elaborateResponse

pull/77/head
MillenniumEarl 2021-03-10 10:19:02 +01:00
parent 144547f3bf
commit c5d449d411
4 changed files with 86 additions and 58 deletions

View File

@ -157,15 +157,19 @@ export default class PlatformUser implements ILazy {
const url = new URL(this.id.toString(), `${urls.MEMBERS}/`).toString();
// Fetch the page
const htmlResponse = await fetchHTML(url);
const result = htmlResponse.applyOnSuccess(this.elaborateResponse);
if (result.isFailure()) throw htmlResponse.value;
const response = await fetchHTML(url);
const result = response.applyOnSuccess(this.elaborateResponse);
if (result.isFailure()) throw response.value;
}
//#endregion Public methods
//#region Private methods
/**
* Process the HTML code received as
* an answer and gets the data contained in it.
*/
private elaborateResponse(html: string): void {
// Prepare cheerio
const $ = cheerio.load(html);

View File

@ -103,11 +103,23 @@ export default class Post implements ILazy {
// Fetch HTML page containing the post
const url = new URL(this.id.toString(), urls.POSTS).toString();
const htmlResponse = await fetchHTML(url);
const response = await fetchHTML(url);
if (htmlResponse.isSuccess()) {
const result = response.applyOnSuccess(this.elaborateResponse);
if (result.isFailure()) throw response.value;
}
//#endregion Public methods
//#region Private methods
/**
* Process the HTML code received as
* an answer and gets the data contained in it.
*/
private async elaborateResponse(html: string) {
// Load cheerio and find post
const $ = cheerio.load(htmlResponse.value);
const $ = cheerio.load(html);
const post = $(THREAD.POSTS_IN_PAGE)
.toArray()
@ -121,13 +133,8 @@ export default class Post implements ILazy {
// Finally parse the post
await this.parsePost($, $(post));
} else throw htmlResponse.value;
}
//#endregion Public methods
//#region Private methods
private async parsePost($: cheerio.Root, post: cheerio.Cheerio): Promise<void> {
// Find post's ID
const sid: string = post.find(POST.ID).attr("id").replace("post-", "");

View File

@ -189,26 +189,13 @@ export default class Thread implements ILazy {
return name.trim();
}
//#endregion Private methods
//#region Public methods
/**
* Gets information about this thread.
* Process the HTML code received as
* an answer and gets the data contained in it.
*/
public async fetch(): Promise<void> {
// Check login
if (!shared.isLogged) throw new UserNotLogged(USER_NOT_LOGGED);
// Prepare the url
this._url = new URL(this.id.toString(), urls.THREADS).toString();
// Fetch the HTML source
const htmlResponse = await fetchHTML(this.url);
if (htmlResponse.isSuccess()) {
private async elaborateResponse(html: string) {
// Load the HTML
const $ = cheerio.load(htmlResponse.value);
const $ = cheerio.load(html);
// Fetch data from selectors
const ownerID = $(THREAD.OWNER_ID).attr("data-user-id");
@ -230,7 +217,26 @@ export default class Thread implements ILazy {
// Validate the dates
if (DateTime.fromISO(modified).isValid) this._modified = new Date(modified);
if (DateTime.fromISO(published).isValid) this._publication = new Date(published);
} else throw htmlResponse.value;
}
//#endregion Private methods
//#region Public methods
/**
* Gets information about this thread.
*/
public async fetch(): Promise<void> {
// Check login
if (!shared.isLogged) throw new UserNotLogged(USER_NOT_LOGGED);
// Prepare the url
this._url = new URL(this.id.toString(), urls.THREADS).toString();
// Fetch the HTML source
const response = await fetchHTML(this.url);
const result = response.applyOnSuccess(this.elaborateResponse);
if (result.isFailure()) throw result.value;
}
/**

View File

@ -112,31 +112,39 @@ export default class UserProfile extends PlatformUser {
//#region Private methods
/**
* Gets the ID of the user currently logged.
*/
private async fetchUserID(): Promise<number> {
// Local variables
const url = new URL(urls.BASE).toString();
// fetch and parse page
const htmlResponse = await fetchHTML(url);
if (htmlResponse.isSuccess()) {
// Fetch and parse page
const response = await fetchHTML(url);
const result = response.applyOnSuccess((html) => {
// Load page with cheerio
const $ = cheerio.load(htmlResponse.value);
const $ = cheerio.load(html);
const sid = $(GENERIC.CURRENT_USER_ID).attr("data-user-id").trim();
return parseInt(sid, 10);
} else throw htmlResponse.value;
});
if (result.isFailure()) throw result.value;
else return result.value;
}
/**
* Gets the list of threads followed by the user currently logged.
*/
private async fetchWatchedThread(): Promise<IWatchedThread[]> {
// Prepare and fetch URL
const url = new URL(urls.WATCHED_THREADS);
url.searchParams.set("unread", "0");
const htmlResponse = await fetchHTML(url.toString());
if (htmlResponse.isSuccess()) {
const response = await fetchHTML(url.toString());
const result = response.applyOnSuccess(async (html) => {
// Load page in cheerio
const $ = cheerio.load(htmlResponse.value);
const $ = cheerio.load(html);
// Fetch the pages
const lastPage = parseInt($(WATCHED_THREAD.LAST_PAGE).text().trim(), 10);
@ -148,7 +156,10 @@ export default class UserProfile extends PlatformUser {
});
return [].concat(...watchedThreads);
} else throw htmlResponse.value;
});
if (result.isFailure()) throw result.value;
else return result.value as Promise<IWatchedThread[]>;
}
/**