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-search-teacher-result.ts
111 lines (100 loc) · 2.55 KB
/
sigaa-search-teacher-result.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
import { Parser } from '@helpers/sigaa-parser';
import { HTTP, ProgressCallback } from '@session/sigaa-http';
import { URL } from 'url';
/**
* @category Internal
*/
export interface TeacherResultData {
name: string;
department: string;
photoURL?: URL;
pageURL: URL;
}
/**
* @category Public
*/
export interface TeacherResult {
readonly name: string;
readonly department: string;
readonly pageURL: URL;
readonly profilePictureURL?: URL;
/**
* May return undefined if the teacher has no registered email
*/
getEmail(): Promise<string | undefined>;
/**
* Download user profile picture, save in basepath
* Returns the destination of the file on the file system
* Throws an exception if the teacher does not have a photo
* @param basepath path to save file
* @param callback
*/
downloadProfilePicture(
basepath: string,
callback: ProgressCallback
): Promise<string>;
}
/**
* @category Internal
*/
export class SigaaSearchTeacherResult implements TeacherResult {
private _name: string;
private _department: string;
private _pageURL: URL;
private _photoURL?: URL;
constructor(
private http: HTTP,
private parser: Parser,
options: TeacherResultData
) {
this._name = options.name;
this._department = options.department;
this._pageURL = options.pageURL;
this._photoURL = options.photoURL;
}
async getEmail(): Promise<string | undefined> {
const page = await this.http.get(this.pageURL.href);
const contactElements = page.$('#contato').children().toArray();
let email;
for (const contactElement of contactElements) {
const name = this.parser.removeTagsHtml(
page.$(contactElement).find('dt').html()
);
if (name === 'Endereço eletrônico') {
email = this.parser.removeTagsHtml(
page.$(contactElement).find('dd').html()
);
break;
}
}
if (email && email !== 'não informado') {
return email;
} else {
return undefined;
}
}
get name(): string {
return this._name;
}
get profilePictureURL(): URL | undefined {
return this._photoURL;
}
downloadProfilePicture(
basepath: string,
callback: ProgressCallback
): Promise<string> {
if (!this.profilePictureURL)
throw new Error("SIGAA: This teacher doesn't have profile picture");
return this.http.downloadFileByGet(
this.profilePictureURL.href,
basepath,
callback
);
}
get department(): string {
return this._department;
}
get pageURL(): URL {
return this._pageURL;
}
}