-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathkupid.ts
More file actions
286 lines (258 loc) · 9.7 KB
/
Copy pathkupid.ts
File metadata and controls
286 lines (258 loc) · 9.7 KB
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
import type { NoticeInfo, KupidType } from "./types.ts";
import { getToken, trim } from "./utils.ts";
interface GrwResponse {
grwSessionId: string;
html: string;
}
interface NoticeViewParameters {
kind: string;
index: string;
message_id: string;
replyTop: string;
replyPos: string;
replyTo: string;
rowReply: string;
depth: string;
}
async function getNoticeListPage(
token: string,
session: string,
kind: string
): Promise<GrwResponse> {
const url = `https://grw.korea.ac.kr/GroupWare/user/NoticeList.jsp?kind=${kind}&compId=148&menuCd=340&language=ko&frame=&token=${token}&orgtoken=${token}`;
const cookieString = `ssotoken=${token}; PORTAL_SESSIONID=${session};`;
const response = await fetch(url, {
headers: {
referer: "https://portal.korea.ac.kr/",
cookie: cookieString,
},
});
const [cookies] = response.headers.getSetCookie();
if (!cookies) throw Error("Failed to get cookies");
const [grwSessionCookie] = cookies.split(";");
if (!grwSessionCookie) throw Error("Failed to get grwSessionCookie");
const [_, grwSessionId] = grwSessionCookie.split("=");
if (!grwSessionId) throw Error("Failed to get grwSessionId");
const stream = await response.arrayBuffer();
const decoder = new TextDecoder("euc-kr");
const html = decoder.decode(stream);
return { grwSessionId, html };
}
function parseNoticeParamsFromHTML(html: string): Array<NoticeViewParameters> {
const hrefs = html.match(/javascript\:view\((.+)\);/g);
if (!hrefs) throw Error("Failed to parse html");
return hrefs.map((href) => {
const matchArray = href.match(/\d+/g);
if (!matchArray) throw Error("Failed to parse href");
const [kind, index, message_id, replyTop, replyPos, replyTo, rowReply, depth] = matchArray;
return {
kind,
index,
message_id,
replyTop,
replyPos,
replyTo,
rowReply,
depth,
};
});
}
function parseScheduleParamsFromHTML(html: string): Array<NoticeViewParameters> {
const hrefs = html.match(/javascript\:view1\((.+)\);/g);
if (!hrefs) throw Error("Failed to parse html");
return hrefs.map((href) => {
const matchArray = href.match(/\d+/g);
if (!matchArray) throw Error("Failed to parse href");
const [_, kind, index, message_id, replyTop, replyPos, replyTo, rowReply, depth] = matchArray;
return {
kind,
index,
message_id,
replyTop,
replyPos,
replyTo,
rowReply,
depth,
};
});
}
function getNoticeUrl(token: string, params: NoticeViewParameters) {
return `https://grw.korea.ac.kr/GroupWare/user/NoticeView.jsp?language=ko&WhereSelect=all&KeyWord=&tab=&temp=&JOB_MODE=Q&JOBMODE=I&hdCurrPage=1&hdListCount=100&hdPageCount=5&hdPageList=&userid=&index=${params.index}&message_id=${params.message_id}&replyTop=${params.replyTop}&replyPos=${params.replyPos}&replyTo=${params.replyTo}&rowReply=${params.rowReply}&depth=${params.depth}&kind=${params.kind}&type=0&mode=0&flag=&access_type=Y&calIndex=&token=${token}`;
}
async function fetchNotice(
token: string,
session: string,
grwSession: string,
url: string,
kind: string
) {
const cookieString = `ssotoken=${token}; PORTAL_SESSIONID=${session}; GRW_SESSIONID=${grwSession};`;
const response = await fetch(url, {
headers: {
referer: `https://grw.korea.ac.kr/GroupWare/user/NoticeList.jsp?kind=${kind}`,
cookie: cookieString,
},
});
const stream = await response.arrayBuffer();
const decoder = new TextDecoder("euc-kr");
const html = decoder.decode(stream).replace(/euc\-kr/g, "utf-8");
return html.replace(/\<input type="button"/g, '<input type="button" style="display:none"');
}
function makeFilePathPublic(html: string) {
return html.replace(/javascript\:Download\((.+)\);/g, (match, p1: string) => {
const params = p1.split(",").map((p) => p.trim().replace(/'/g, ""));
return `https://portal.korea.ac.kr/common/Download.kpd?filePath=${params[0]}&fileName=${params[1]}`;
});
}
async function getNoticesFromKupid(id: string, password: string): Promise<string[]> {
const { token, sessionId } = await getToken(id, password);
const { grwSessionId, html } = await getNoticeListPage(token, sessionId, "11");
const params = parseNoticeParamsFromHTML(html);
const urls = params.map((param) => getNoticeUrl(token, param));
return Promise.all(
urls.map(async (url) => {
const html = await fetchNotice(token, sessionId, grwSessionId, url, "11");
return makeFilePathPublic(html);
})
);
}
async function getSchedulesFromKupid(id: string, password: string): Promise<string[]> {
const { token, sessionId } = await getToken(id, password);
const { grwSessionId, html } = await getNoticeListPage(token, sessionId, "89");
const params = parseScheduleParamsFromHTML(html);
const urls = params.map((param) => getNoticeUrl(token, param));
return Promise.all(
urls.map(async (url) => {
const html = await fetchNotice(token, sessionId, grwSessionId, url, "89");
return makeFilePathPublic(html);
})
);
}
async function getScholarFromKupid(id: string, password: string): Promise<string[]> {
const { token, sessionId } = await getToken(id, password);
const { grwSessionId, html } = await getNoticeListPage(token, sessionId, "88");
const params = parseNoticeParamsFromHTML(html);
const urls = params.map((param) => getNoticeUrl(token, param));
return Promise.all(
urls.map(async (url) => {
const html = await fetchNotice(token, sessionId, grwSessionId, url, "88");
return makeFilePathPublic(html);
})
);
}
function parseNoticeInfo(html: string): NoticeInfo {
const tableRows = html.split("<tr>").slice(1);
tableRows[tableRows.length - 1] = tableRows[tableRows.length - 1].split("</tr>")[0];
const rawWriter = tableRows[0].match(/\<td\>(.+)\<\/td\>/);
if (!rawWriter) throw Error("Failed to parse writer");
const writer = trim(rawWriter[1]);
const rawDate = tableRows[2].match(/\<td colspan="\d"\>(.+)\<\/td\>/);
if (!rawDate) throw Error("Failed to parse date");
const date = trim(rawDate[1]);
const rawTitle = tableRows[4].match(/\<td colspan="\d"\>(.+)\<\/td\>/);
if (!rawTitle) throw Error("Failed to parse title");
const title = trim(rawTitle[1]);
const rawContent = tableRows.slice(5).join("");
const content = `<tbody>${rawContent}</tbody>`;
const rawId = html.match(/\<input type="hidden" name="index" value="(.+)"\/\>/);
if (!rawId) throw Error("Failed to parse id");
const id = rawId[1].trim();
const url = `https://portal.korea.ac.kr/front/IntroNotice/NMainNoticeContent.kpd?idx=${id}&seq=`;
return {
id,
title,
date,
writer,
content,
url,
};
}
function parseScheduleInfo(html: string): NoticeInfo {
const tableRows = html.split("<tr>").slice(1);
tableRows[tableRows.length - 1] = tableRows[tableRows.length - 1].split("</tr>")[0];
const rawWriter = tableRows[0].match(/\<td\>(.+)\<\/td\>/);
if (!rawWriter) throw Error("Failed to parse writer");
const writer = trim(rawWriter[1]);
const rawDate = tableRows[1].split("<td>")[1].split("</td>")[0];
if (!rawDate) throw Error("Failed to parse date");
const date = trim(rawDate).trim().replace(/\n/g, "").replace(/\t/g, "").replace(/ /g, "");
const rawTitle = tableRows[4].match(/\<td colspan="\d"\>(.+)\<\/td\>/);
if (!rawTitle) throw Error("Failed to parse title");
const title = trim(rawTitle[1]);
const rawContent = tableRows.slice(5).join("");
const content = `<tbody>${rawContent}</tbody>`;
const rawId = html.match(/\<input type="hidden" name="index" value="(.+)"\/\>/);
if (!rawId) throw Error("Failed to parse id");
const id = rawId[1].trim();
const url = `https://portal.korea.ac.kr/front/IntroNotice/NMainNoticeContent.kpd?idx=${id}&seq=`;
return {
id,
title,
date,
writer,
content,
url,
};
}
function parseScholarInfo(html: string): NoticeInfo {
const tableRows = html.split("<tr>").slice(1);
tableRows[tableRows.length - 1] = tableRows[tableRows.length - 1].split("</tr>")[0];
const rawWriter = tableRows[0].match(/\<td\>(.+)\<\/td\>/);
if (!rawWriter) throw Error("Failed to parse writer");
const writer = trim(rawWriter[1]);
const rawDate = tableRows[2].match(/\<td colspan="\d"\>(.+)\<\/td\>/);
if (!rawDate) throw Error("Failed to parse date");
const date = trim(rawDate[1]);
const rawTitle = tableRows[4].match(/\<td colspan="\d"\>(.+)\<\/td\>/);
if (!rawTitle) throw Error("Failed to parse title");
const title = trim(rawTitle[1]);
const rawContent = tableRows.slice(5).join("");
const content = `<tbody>${rawContent}</tbody>`;
const rawId = html.match(/\<input type="hidden" name="index" value="(.+)"\/\>/);
if (!rawId) throw Error("Failed to parse id");
const id = rawId[1].trim();
const url = `https://portal.korea.ac.kr/front/IntroNotice/NMainNoticeContent.kpd?idx=${id}&seq=`;
return {
id,
title,
date,
writer,
content,
url,
};
}
/** notice content from KUPID. */
export function fetchKupidNotices(
id: string,
password: string,
type: KupidType
): Promise<string[]> {
switch (type) {
case "Scholar":
return getScholarFromKupid(id, password);
case "Notice":
return getNoticesFromKupid(id, password);
case "Schedule":
return getSchedulesFromKupid(id, password);
default:
throw new Error("Invalid Type");
}
}
/** return metadata and notice content from KUPID. */
export async function fetchParsedKupidNotices(
id: string,
password: string,
type: KupidType
): Promise<NoticeInfo[]> {
const htmls = await fetchKupidNotices(id, password, type);
switch (type) {
case "Scholar":
return htmls.map(parseScholarInfo);
case "Notice":
return htmls.map(parseNoticeInfo);
case "Schedule":
return htmls.map(parseScheduleInfo);
default:
throw new Error("Invalid Type");
}
}