Implement elaborateResponse
parent
144547f3bf
commit
c5d449d411
|
@ -157,15 +157,19 @@ export default class PlatformUser implements ILazy {
|
||||||
const url = new URL(this.id.toString(), `${urls.MEMBERS}/`).toString();
|
const url = new URL(this.id.toString(), `${urls.MEMBERS}/`).toString();
|
||||||
|
|
||||||
// Fetch the page
|
// Fetch the page
|
||||||
const htmlResponse = await fetchHTML(url);
|
const response = await fetchHTML(url);
|
||||||
const result = htmlResponse.applyOnSuccess(this.elaborateResponse);
|
const result = response.applyOnSuccess(this.elaborateResponse);
|
||||||
if (result.isFailure()) throw htmlResponse.value;
|
if (result.isFailure()) throw response.value;
|
||||||
}
|
}
|
||||||
|
|
||||||
//#endregion Public methods
|
//#endregion Public methods
|
||||||
|
|
||||||
//#region Private methods
|
//#region Private methods
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Process the HTML code received as
|
||||||
|
* an answer and gets the data contained in it.
|
||||||
|
*/
|
||||||
private elaborateResponse(html: string): void {
|
private elaborateResponse(html: string): void {
|
||||||
// Prepare cheerio
|
// Prepare cheerio
|
||||||
const $ = cheerio.load(html);
|
const $ = cheerio.load(html);
|
||||||
|
|
|
@ -103,11 +103,23 @@ export default class Post implements ILazy {
|
||||||
|
|
||||||
// Fetch HTML page containing the post
|
// Fetch HTML page containing the post
|
||||||
const url = new URL(this.id.toString(), urls.POSTS).toString();
|
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
|
// Load cheerio and find post
|
||||||
const $ = cheerio.load(htmlResponse.value);
|
const $ = cheerio.load(html);
|
||||||
|
|
||||||
const post = $(THREAD.POSTS_IN_PAGE)
|
const post = $(THREAD.POSTS_IN_PAGE)
|
||||||
.toArray()
|
.toArray()
|
||||||
|
@ -121,13 +133,8 @@ export default class Post implements ILazy {
|
||||||
|
|
||||||
// Finally parse the post
|
// Finally parse the post
|
||||||
await this.parsePost($, $(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> {
|
private async parsePost($: cheerio.Root, post: cheerio.Cheerio): Promise<void> {
|
||||||
// Find post's ID
|
// Find post's ID
|
||||||
const sid: string = post.find(POST.ID).attr("id").replace("post-", "");
|
const sid: string = post.find(POST.ID).attr("id").replace("post-", "");
|
||||||
|
|
|
@ -189,26 +189,13 @@ export default class Thread implements ILazy {
|
||||||
return name.trim();
|
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> {
|
private async elaborateResponse(html: string) {
|
||||||
// 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()) {
|
|
||||||
// Load the HTML
|
// Load the HTML
|
||||||
const $ = cheerio.load(htmlResponse.value);
|
const $ = cheerio.load(html);
|
||||||
|
|
||||||
// Fetch data from selectors
|
// Fetch data from selectors
|
||||||
const ownerID = $(THREAD.OWNER_ID).attr("data-user-id");
|
const ownerID = $(THREAD.OWNER_ID).attr("data-user-id");
|
||||||
|
@ -230,7 +217,26 @@ export default class Thread implements ILazy {
|
||||||
// Validate the dates
|
// Validate the dates
|
||||||
if (DateTime.fromISO(modified).isValid) this._modified = new Date(modified);
|
if (DateTime.fromISO(modified).isValid) this._modified = new Date(modified);
|
||||||
if (DateTime.fromISO(published).isValid) this._publication = new Date(published);
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -112,31 +112,39 @@ export default class UserProfile extends PlatformUser {
|
||||||
|
|
||||||
//#region Private methods
|
//#region Private methods
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the ID of the user currently logged.
|
||||||
|
*/
|
||||||
private async fetchUserID(): Promise<number> {
|
private async fetchUserID(): Promise<number> {
|
||||||
// Local variables
|
// Local variables
|
||||||
const url = new URL(urls.BASE).toString();
|
const url = new URL(urls.BASE).toString();
|
||||||
|
|
||||||
// fetch and parse page
|
// Fetch and parse page
|
||||||
const htmlResponse = await fetchHTML(url);
|
const response = await fetchHTML(url);
|
||||||
if (htmlResponse.isSuccess()) {
|
const result = response.applyOnSuccess((html) => {
|
||||||
// Load page with cheerio
|
// Load page with cheerio
|
||||||
const $ = cheerio.load(htmlResponse.value);
|
const $ = cheerio.load(html);
|
||||||
|
|
||||||
const sid = $(GENERIC.CURRENT_USER_ID).attr("data-user-id").trim();
|
const sid = $(GENERIC.CURRENT_USER_ID).attr("data-user-id").trim();
|
||||||
return parseInt(sid, 10);
|
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[]> {
|
private async fetchWatchedThread(): Promise<IWatchedThread[]> {
|
||||||
// Prepare and fetch URL
|
// Prepare and fetch URL
|
||||||
const url = new URL(urls.WATCHED_THREADS);
|
const url = new URL(urls.WATCHED_THREADS);
|
||||||
url.searchParams.set("unread", "0");
|
url.searchParams.set("unread", "0");
|
||||||
|
|
||||||
const htmlResponse = await fetchHTML(url.toString());
|
const response = await fetchHTML(url.toString());
|
||||||
|
const result = response.applyOnSuccess(async (html) => {
|
||||||
if (htmlResponse.isSuccess()) {
|
|
||||||
// Load page in cheerio
|
// Load page in cheerio
|
||||||
const $ = cheerio.load(htmlResponse.value);
|
const $ = cheerio.load(html);
|
||||||
|
|
||||||
// Fetch the pages
|
// Fetch the pages
|
||||||
const lastPage = parseInt($(WATCHED_THREAD.LAST_PAGE).text().trim(), 10);
|
const lastPage = parseInt($(WATCHED_THREAD.LAST_PAGE).text().trim(), 10);
|
||||||
|
@ -148,7 +156,10 @@ export default class UserProfile extends PlatformUser {
|
||||||
});
|
});
|
||||||
|
|
||||||
return [].concat(...watchedThreads);
|
return [].concat(...watchedThreads);
|
||||||
} else throw htmlResponse.value;
|
});
|
||||||
|
|
||||||
|
if (result.isFailure()) throw result.value;
|
||||||
|
else return result.value as Promise<IWatchedThread[]>;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in New Issue