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-main.ts
executable file
·344 lines (300 loc) · 9.44 KB
/
sigaa-main.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
import { Account } from '@account/sigaa-account';
import { BondFactory, SigaaBondFactory } from '@bonds/sigaa-bond-factory';
import { Parser, SigaaParser } from '@helpers/sigaa-parser';
import { FileData, SigaaFile } from '@resources/sigaa-file';
import { SigaaSearch } from '@search/sigaa-search';
import { HTTPSession, SigaaHTTPSession } from '@session/sigaa-http-session';
import { HTTP } from '@session/sigaa-http';
import { HTTPFactory, SigaaHTTPFactory } from '@session/sigaa-http-factory';
import { Login } from '@session/login/sigaa-login';
import { SigaaLoginIFSC } from '@session/login/sigaa-login-ifsc';
import { SigaaLoginUFPB } from '@session/login/sigaa-login-ufpb';
import { InstitutionType, Session, SigaaSession } from '@session/sigaa-session';
import { SigaaCookiesController } from '@session/sigaa-cookies-controller';
import { SigaaPageCacheWithBond } from '@session/sigaa-page-cache-with-bond';
import { SigaaPageCacheFactory } from '@session/sigaa-page-cache-factory';
import {
AccountFactory,
SigaaAccountFactory
} from '@account/sigaa-account-factory';
import {
BondController,
SigaaBondController
} from '@session/sigaa-bond-controller';
import {
CourseFactory,
SigaaCourseFactory
} from '@courses/sigaa-course-student-factory';
import {
CourseResourcesManagerFactory,
SigaaCourseResourceManagerFactory
} from '@courses/sigaa-course-resources-manager-factory';
import {
CourseResourcesFactory,
SigaaCourseResourcesFactory
} from '@courses/sigaa-course-resources-factory';
import {
LessonParserFactory,
SigaaLessonParserFactory
} from '@courses/sigaa-lesson-parser-factory';
import { XOR } from './sigaa-types';
import {
ActivityFactory,
SigaaActivityFactory
} from '@activity/sigaa-activity-factory';
import { RequestStackController } from '@helpers/sigaa-request-stack';
import { Request } from '@session/sigaa-http-session';
import { Page } from '@session/sigaa-page';
import { CookiesController } from '@session/sigaa-cookies-controller';
import { SigaaRequestStack } from '@helpers/sigaa-request-stack';
/**
* @category Internal
*/
interface SigaaCommonConstructorOptions {
login?: Login;
parser?: Parser;
}
interface SigaaConstructorURL {
url: string;
bondController?: BondController;
cookiesController?: CookiesController;
requestStackController?: RequestStackController<Request, Page>;
}
interface WithAccountFactory {
accountFactory?: AccountFactory;
}
interface WithBondFactory {
bondFactory: BondFactory;
}
interface WithCourseFactory {
courseFactory: CourseFactory;
}
interface WithActivityFactory {
activityFactory?: ActivityFactory;
}
type WithoutCourseFactory = {
lessonParserFactory?: LessonParserFactory;
} & XOR<
{ courseResourcesManagerFactory?: CourseResourcesManagerFactory },
{ courseResourcesFactory?: CourseResourcesFactory }
>;
/**
* @category Internal
*/
interface SigaaConstructorHTTP {
httpFactory: HTTPFactory;
httpSession: HTTPSession;
}
/**
* @category Public
*/
export type SigaaOptionsConstructor = SigaaCommonConstructorOptions &
XOR<{ institution?: InstitutionType }, { session?: Session }> &
XOR<SigaaConstructorURL, SigaaConstructorHTTP> &
XOR<
WithAccountFactory,
XOR<
WithBondFactory,
WithActivityFactory & XOR<WithCourseFactory, WithoutCourseFactory>
>
>;
/**
* Main class, used to instantiate other classes in standard use.
* @category Public
*/
export class Sigaa {
/**
* Instance of login class.
*/
readonly loginInstance: Login;
/**
* Instance of http factory.
*/
readonly httpFactory: HTTPFactory;
/**
* Instance of parser.
*/
readonly parser: Parser;
/**
* Instance of session.
*/
readonly session: Session;
/**
* Instance of account factory.
*/
readonly accountFactory: AccountFactory;
/**
* Instance of http session.
*/
readonly httpSession: HTTPSession;
/**
* Instance of http.
*/
private http: HTTP;
constructor(options: SigaaOptionsConstructor) {
const pageCacheFactory = new SigaaPageCacheFactory();
const pageCache = new SigaaPageCacheWithBond(pageCacheFactory);
if ('parser' in options && options.parser) {
this.parser = options.parser;
} else {
this.parser = new SigaaParser();
}
if ('session' in options && options.session) {
this.session = options.session;
} else {
this.session = new SigaaSession(options.institution);
}
if ('url' in options && options.url) {
let cookiesController: CookiesController;
if ('cookiesController' in options && options.cookiesController) {
cookiesController = options.cookiesController;
} else {
cookiesController = new SigaaCookiesController();
}
let requestStackController: RequestStackController<Request, Page>;
if (
'requestStackController' in options &&
options.requestStackController
) {
requestStackController = options.requestStackController;
} else {
requestStackController = new SigaaRequestStack<Request, Page>();
}
this.httpSession = new SigaaHTTPSession(
options.url,
cookiesController,
pageCache,
requestStackController
);
const bondController =
options.bondController || new SigaaBondController();
this.httpFactory = new SigaaHTTPFactory(
this.httpSession,
pageCache,
bondController
);
} else {
if ('httpFactory' in options && options.httpFactory) {
this.httpFactory = options.httpFactory;
} else {
throw new Error(
'SIGAA: Invalid httpFactory. It may be that you have forgotten the URL'
);
}
if ('httpSession' in options && options.httpSession) {
this.httpSession = options.httpSession;
} else {
throw new Error('SIGAA: Invalid httpSession.');
}
}
this.http = this.httpFactory.createHttp();
if ('accountFactory' in options && options.accountFactory) {
this.accountFactory = options.accountFactory;
} else {
let bondFactory: BondFactory;
if ('bondFactory' in options && options.bondFactory) {
bondFactory = options.bondFactory;
} else {
let activityFactory: ActivityFactory;
if ('activityFactory' in options && options.activityFactory) {
activityFactory = options.activityFactory;
} else {
activityFactory = new SigaaActivityFactory();
}
let courseFactory: CourseFactory;
if ('courseFactory' in options && options.courseFactory) {
courseFactory = options.courseFactory;
} else {
let courseResourcesManagerFactory: CourseResourcesManagerFactory;
if (
'courseResourcesManagerFactory' in options &&
options.courseResourcesManagerFactory
) {
courseResourcesManagerFactory =
options.courseResourcesManagerFactory;
} else {
let courseResourcesFactory: CourseResourcesFactory;
if (
'courseResourcesFactory' in options &&
options.courseResourcesFactory
) {
courseResourcesFactory = options.courseResourcesFactory;
} else {
courseResourcesFactory = new SigaaCourseResourcesFactory(
this.parser
);
}
courseResourcesManagerFactory =
new SigaaCourseResourceManagerFactory(courseResourcesFactory);
}
let lessonParserFactory: LessonParserFactory;
if ('lessonParserFactory' in options && options.lessonParserFactory) {
lessonParserFactory = options.lessonParserFactory;
} else {
lessonParserFactory = new SigaaLessonParserFactory(this.parser);
}
courseFactory = new SigaaCourseFactory(
this.http,
this.parser,
courseResourcesManagerFactory,
lessonParserFactory
);
}
bondFactory = new SigaaBondFactory(
this.httpFactory,
this.parser,
courseFactory,
activityFactory
);
}
this.accountFactory = new SigaaAccountFactory(
this.http,
this.parser,
this.session,
bondFactory
);
}
this.loginInstance =
options.login || options.institution === 'UFPB'
? new SigaaLoginUFPB(this.http, this.session)
: new SigaaLoginIFSC(this.http, this.session);
}
/**
* User authentication.
* @param username
* @param password
*/
async login(username: string, password: string): Promise<Account> {
const page = await this.loginInstance.login(username, password);
try {
return await this.accountFactory.getAccount(page);
} catch (err) {
const retryPage = await this.http.followAllRedirect(
await this.http.get(page.url.href, { noCache: true }),
{ noCache: true }
);
return this.accountFactory.getAccount(retryPage);
}
}
/**
* Load file to download.
* @param options
* @param options.id file id
* @param options.key file key
*/
loadFile(options: FileData): SigaaFile {
return new SigaaFile(this.http, options);
}
/**
* Returns instance of SigaaSearch.
*/
get search(): SigaaSearch {
return new SigaaSearch(this.http, this.parser);
}
/**
* Close the instance, it just clears the session data, if you want to log off the system you must use Account.logoff().
*/
close(): void {
this.httpSession.close();
}
}