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,31 +103,38 @@ 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()) {
// 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, 10);
if (id === this.id) return el;
});
// Finally parse the post
await this.parsePost($, $(post));
} else throw htmlResponse.value;
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(html);
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, 10);
if (id === this.id) return el;
});
// Finally parse the post
await this.parsePost($, $(post));
}
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,6 +189,36 @@ export default class Thread implements ILazy {
return name.trim();
}
/**
* Process the HTML code received as
* an answer and gets the data contained in it.
*/
private async elaborateResponse(html: string) {
// Load the HTML
const $ = cheerio.load(html);
// Fetch data from selectors
const ownerID = $(THREAD.OWNER_ID).attr("data-user-id");
const tagArray = $(THREAD.TAGS).toArray();
const prefixArray = $(THREAD.PREFIXES).toArray();
const JSONLD = getJSONLD($("body"));
const published = JSONLD["datePublished"] as string;
const modified = JSONLD["dateModified"] as string;
// Parse the thread's data
this._title = this.cleanHeadline(JSONLD["headline"] as string);
this._tags = tagArray.map((el) => $(el).text().trim());
this._prefixes = prefixArray.map((el) => $(el).text().trim());
this._owner = new PlatformUser(parseInt(ownerID, 10));
await this._owner.fetch();
this._rating = this.parseRating(JSONLD);
this._category = JSONLD["articleSection"] as TCategory;
// Validate the dates
if (DateTime.fromISO(modified).isValid) this._modified = new Date(modified);
if (DateTime.fromISO(published).isValid) this._publication = new Date(published);
}
//#endregion Private methods
//#region Public methods
@ -204,33 +234,9 @@ export default class Thread implements ILazy {
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
const $ = cheerio.load(htmlResponse.value);
// Fetch data from selectors
const ownerID = $(THREAD.OWNER_ID).attr("data-user-id");
const tagArray = $(THREAD.TAGS).toArray();
const prefixArray = $(THREAD.PREFIXES).toArray();
const JSONLD = getJSONLD($("body"));
const published = JSONLD["datePublished"] as string;
const modified = JSONLD["dateModified"] as string;
// Parse the thread's data
this._title = this.cleanHeadline(JSONLD["headline"] as string);
this._tags = tagArray.map((el) => $(el).text().trim());
this._prefixes = prefixArray.map((el) => $(el).text().trim());
this._owner = new PlatformUser(parseInt(ownerID, 10));
await this._owner.fetch();
this._rating = this.parseRating(JSONLD);
this._category = JSONLD["articleSection"] as TCategory;
// 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;
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[]>;
}
/**