This repository has been archived by the owner on Apr 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
sigaa-web-content-student.ts
executable file
·134 lines (120 loc) · 3.16 KB
/
sigaa-web-content-student.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import { Parser } from '@helpers/sigaa-parser';
import { UpdatableResourceData } from '@resources/sigaa-resource-manager';
import { HTTP } from '@session/sigaa-http';
import { SigaaForm } from '@session/sigaa-page';
import {
AbstractUpdatableResource,
UpdatableResource,
UpdatableResourceCallback
} from '@resources/updatable-resource';
/**
* @category Internal
*/
export interface WebContentData extends UpdatableResourceData {
title: string;
id: string;
form: SigaaForm;
date?: Date;
}
/**
* @category Internal
*/
export interface WebContent extends UpdatableResource<WebContentData> {
readonly type: 'webcontent';
getDate(): Promise<Date>;
getContent(): Promise<string>;
readonly title: string;
}
/**
* @category Internal
*/
export class SigaaWebContent
extends AbstractUpdatableResource
implements WebContent {
readonly type = 'webcontent';
private _title!: string;
private form!: SigaaForm;
private _content!: string;
private _date!: Date;
constructor(
private http: HTTP,
private parser: Parser,
options: WebContentData,
updater: UpdatableResourceCallback
) {
super(options.instanceIndentifier, updater);
this.update(options);
}
update(options: WebContentData): void {
this._title = options.title;
if (options.date) this._date = options.date;
this.form = options.form;
this.isClosed = false;
}
async getDate(): Promise<Date> {
if (!this._date) {
await this.loadWebContentPage();
}
return this._date;
}
get title(): string {
this.checkIfItWasClosed();
return this._title;
}
async getContent(): Promise<string> {
if (!this._content) {
await this.loadWebContentPage();
}
return this._content;
}
private async loadWebContentPage(retry = true): Promise<void> {
this.checkIfItWasClosed();
try {
const page = await this.http.post(
this.form.action.href,
this.form.postValues
);
if (page.statusCode === 200) {
const rows = page.$('table.formAva tr').toArray();
for (const row of rows) {
const rowLabel = this.parser.removeTagsHtml(
page.$(row).find('th').html()
);
const rowContent = this.parser.removeTagsHtml(
page.$(row).find('td').html()
);
switch (rowLabel) {
case 'Título:': {
this._title = rowContent;
break;
}
case 'Conteúdo:': {
this._content = rowContent;
break;
}
case 'Data Cadastro:': {
const date = this.parser.parseDates(rowContent, 1);
this._date = date[0];
break;
}
}
}
} else if (page.statusCode === 302) {
throw new Error('SIGAA: Webcontent expired.');
} else {
throw new Error('SIGAA: Unexpected webcontent page status code.');
}
} catch (err) {
if (retry) {
await this.updateInstance();
return this.loadWebContentPage(false);
} else {
throw err;
}
}
}
get id(): string {
this.checkIfItWasClosed();
return this.form.postValues.id;
}
}