|
1 | 1 | import _axios from 'axios'
|
| 2 | +import _axiosCookiejarSupport from 'axios-cookiejar-support' |
2 | 3 | import * as iconv from 'iconv-lite'
|
3 | 4 | import * as fs from 'fs'
|
4 | 5 | import * as colors from 'colors'
|
| 6 | +import * as assert from 'assert' |
| 7 | + |
| 8 | +export class NoCoursesFoundError extends Error { |
| 9 | + public constructor() { |
| 10 | + super('Search results are empty') |
| 11 | + Object.defineProperty(this, 'name', { |
| 12 | + configurable: true, |
| 13 | + enumerable: false, |
| 14 | + value: this.constructor.name, |
| 15 | + writable: true, |
| 16 | + }) |
| 17 | + Error.captureStackTrace(this, NoCoursesFoundError) |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +_axiosCookiejarSupport(_axios) |
5 | 22 |
|
6 | 23 | const axios = _axios.create({
|
7 |
| - responseType: 'arraybuffer' //Shift_JIS のデータを受け取る都合でbufferで受け取る |
8 |
| -}) |
9 |
| -axios.interceptors.response.use(function(response) { |
10 |
| - response.data = iconv.decode(response.data, 'Shift_JIS') // Shift_JIS to UTF-8 |
11 |
| - return response |
| 24 | + responseType: 'arraybuffer', |
| 25 | + withCredentials: true, |
| 26 | + jar: true, |
12 | 27 | })
|
13 | 28 |
|
| 29 | +const postBody = (obj: any) => { |
| 30 | + const urlParams = new URLSearchParams() |
| 31 | + Object.keys(obj).forEach((k) => urlParams.append(k, obj[k])) |
| 32 | + return urlParams |
| 33 | +} |
| 34 | + |
| 35 | +const extractFlowExecutionKey = (html: string) => |
| 36 | + html.match(/&_flowExecutionKey=(.*?)"/m)[1] |
| 37 | + |
| 38 | +const grantSession = async (): Promise<string> => { |
| 39 | + const res = await axios.get<Buffer>('https://kdb.tsukuba.ac.jp/') |
| 40 | + return extractFlowExecutionKey(iconv.decode(res.data, 'utf8')) |
| 41 | +} |
| 42 | + |
| 43 | +const searchAll = async ( |
| 44 | + flowExecutionKey: string, |
| 45 | + year: number |
| 46 | +): Promise<string> => { |
| 47 | + const res = await axios.post<Buffer>( |
| 48 | + 'https://kdb.tsukuba.ac.jp/campusweb/campussquare.do', |
| 49 | + postBody({ |
| 50 | + _flowExecutionKey: flowExecutionKey, |
| 51 | + _eventId: 'searchOpeningCourse', |
| 52 | + index: '', |
| 53 | + locale: '', |
| 54 | + nendo: year, |
| 55 | + termCode: '', |
| 56 | + dayCode: '', |
| 57 | + periodCode: '', |
| 58 | + campusCode: '', |
| 59 | + hierarchy1: '', |
| 60 | + hierarchy2: '', |
| 61 | + hierarchy3: '', |
| 62 | + hierarchy4: '', |
| 63 | + hierarchy5: '', |
| 64 | + freeWord: '', |
| 65 | + _gaiyoFlg: 1, |
| 66 | + _risyuFlg: 1, |
| 67 | + _excludeFukaikoFlg: 1, |
| 68 | + outputFormat: 0, |
| 69 | + }) |
| 70 | + ) |
| 71 | + const html = iconv.decode(res.data, 'utf8') |
| 72 | + if (html.includes('(全部で 0件あります)')) throw new NoCoursesFoundError() |
| 73 | + return extractFlowExecutionKey(html) |
| 74 | +} |
| 75 | + |
| 76 | +const downloadExcel = async ( |
| 77 | + flowExecutionKey: string, |
| 78 | + year |
| 79 | +): Promise<Buffer> => { |
| 80 | + const res = await axios.post<Buffer>( |
| 81 | + 'https://kdb.tsukuba.ac.jp/campusweb/campussquare.do', |
| 82 | + postBody({ |
| 83 | + _flowExecutionKey: flowExecutionKey, |
| 84 | + _eventId: 'outputOpeningCourseExcel', |
| 85 | + index: '', |
| 86 | + locale: '', |
| 87 | + nendo: year, |
| 88 | + termCode: '', |
| 89 | + dayCode: '', |
| 90 | + periodCode: '', |
| 91 | + campusCode: '', |
| 92 | + hierarchy1: '', |
| 93 | + hierarchy2: '', |
| 94 | + hierarchy3: '', |
| 95 | + hierarchy4: '', |
| 96 | + hierarchy5: '', |
| 97 | + freeWord: '', |
| 98 | + _gaiyoFlg: 1, |
| 99 | + _risyuFlg: 1, |
| 100 | + _excludeFukaikoFlg: 1, |
| 101 | + outputFormat: 1, |
| 102 | + }) |
| 103 | + ) |
| 104 | + assert( |
| 105 | + res.headers.contentType !== |
| 106 | + 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet .xlsx; charset=UTF-8' |
| 107 | + ) |
| 108 | + return res.data |
| 109 | +} |
| 110 | + |
14 | 111 | /**
|
15 | 112 | * KDBからCSVを取得
|
16 | 113 | */
|
17 | 114 | export default async (
|
18 | 115 | year: number = new Date().getFullYear()
|
19 |
| -): Promise<string> => { |
20 |
| - console.log('Downloading csv from kdb...'.cyan) |
21 |
| - const params = { |
22 |
| - pageId: 'SB0070', |
23 |
| - action: 'downloadList', |
24 |
| - hdnFy: year, |
25 |
| - hdnTermCode: '', |
26 |
| - hdnDayCode: '', |
27 |
| - hdnPeriodCode: '', |
28 |
| - hdnAgentName: '', |
29 |
| - hdnOrg: '', |
30 |
| - hdnIsManager: '', |
31 |
| - hdnReq: '', |
32 |
| - hdnFac: '', |
33 |
| - hdnDepth: '', |
34 |
| - hdnChkSyllabi: false, |
35 |
| - hdnChkAuditor: false, |
36 |
| - hdnCourse: '', |
37 |
| - hdnKeywords: '', |
38 |
| - hdnFullname: '', |
39 |
| - hdnDispDay: '', |
40 |
| - hdnDispPeriod: '', |
41 |
| - hdnOrgName: '', |
42 |
| - hdnReqName: '', |
43 |
| - cmbDwldtype: 'csv' |
44 |
| - } |
45 |
| - const urlParams = new URLSearchParams() |
46 |
| - Object.keys(params).forEach(k => urlParams.append(k, params[k])) |
47 |
| - const res = await axios({ |
48 |
| - method: 'post', |
49 |
| - url: 'https://kdb.tsukuba.ac.jp', |
50 |
| - data: urlParams, |
51 |
| - headers: { |
52 |
| - 'Accept-Encoding': '', |
53 |
| - 'Accept-Language': 'ja,ja-JP;q=0.9,en;q=0.8' |
54 |
| - } |
55 |
| - }) |
56 |
| - console.log('✔ Done'.cyan.bold) |
57 |
| - return res.data |
| 116 | +): Promise<Buffer> => { |
| 117 | + let flowExecutionKey = '' |
| 118 | + flowExecutionKey = await grantSession() |
| 119 | + flowExecutionKey = await searchAll(flowExecutionKey, year) |
| 120 | + return downloadExcel(flowExecutionKey, year) |
58 | 121 | }
|
0 commit comments