Skip to content

Commit cd7ea40

Browse files
committed
update: 新しいダウンロード方式に対応
1 parent 4fa2f47 commit cd7ea40

File tree

4 files changed

+164
-72
lines changed

4 files changed

+164
-72
lines changed

package.json

+6-4
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,18 @@
3030
"eslint": "^6.0.1",
3131
"eslint-config-prettier": "^6.0.0",
3232
"eslint-plugin-prettier": "^3.1.0",
33-
"prettier": "^1.18.2",
34-
"ts-node": "^8.3.0",
35-
"typescript": "^3.5.3"
33+
"prettier": "^2.2.1",
34+
"typescript": "^4.1.3"
3635
},
3736
"dependencies": {
3837
"axios": "^0.19.0",
38+
"axios-cookiejar-support": "^1.0.1",
3939
"cli-progress": "^2.1.1",
4040
"colors": "^1.3.3",
4141
"command-line-args": "^5.1.1",
4242
"csv": "^5.1.1",
43-
"iconv-lite": "^0.5.0"
43+
"iconv-lite": "^0.5.0",
44+
"tough-cookie": "^4.0.0",
45+
"ts-node": "^9.1.1"
4446
}
4547
}

src/kdbDownloader.ts

+80-43
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,95 @@
11
import _axios from 'axios'
2+
import _axiosCookiejarSupport from 'axios-cookiejar-support'
23
import * as iconv from 'iconv-lite'
34
import * as fs from 'fs'
45
import * as colors from 'colors'
56

7+
_axiosCookiejarSupport(_axios)
8+
69
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
10+
responseType: 'arraybuffer',
11+
withCredentials: true,
12+
jar: true,
1213
})
1314

15+
const postBody = (obj: any) => {
16+
const urlParams = new URLSearchParams()
17+
Object.keys(obj).forEach((k) => urlParams.append(k, obj[k]))
18+
return urlParams
19+
}
20+
21+
const extractFlowExecutionKey = (html: string) =>
22+
html.match(/&_flowExecutionKey=(.*?)"/m)[1]
23+
24+
const grantSession = async (): Promise<string> => {
25+
const res = await axios.get<Buffer>('https://kdb.tsukuba.ac.jp/')
26+
return extractFlowExecutionKey(iconv.decode(res.data, 'utf8'))
27+
}
28+
29+
const searchAll = async (flowExecutionKey: string): Promise<string> => {
30+
const res = await axios.post<Buffer>(
31+
'https://kdb.tsukuba.ac.jp/campusweb/campussquare.do',
32+
postBody({
33+
_flowExecutionKey: flowExecutionKey,
34+
_eventId: 'searchOpeningCourse',
35+
index: '',
36+
locale: '',
37+
nendo: 2020,
38+
termCode: '',
39+
dayCode: '',
40+
periodCode: '',
41+
campusCode: '',
42+
hierarchy1: '',
43+
hierarchy2: '',
44+
hierarchy3: '',
45+
hierarchy4: '',
46+
hierarchy5: '',
47+
freeWord: '',
48+
_gaiyoFlg: 1,
49+
_risyuFlg: 1,
50+
_excludeFukaikoFlg: 1,
51+
outputFormat: 0,
52+
})
53+
)
54+
return extractFlowExecutionKey(iconv.decode(res.data, 'utf8'))
55+
}
56+
57+
const downloadCSV = async (flowExecutionKey: string): Promise<string> => {
58+
const res = await axios.post<Buffer>(
59+
'https://kdb.tsukuba.ac.jp/campusweb/campussquare.do',
60+
postBody({
61+
_flowExecutionKey: flowExecutionKey,
62+
_eventId: 'output',
63+
index: '',
64+
locale: '',
65+
nendo: 2020,
66+
termCode: '',
67+
dayCode: '',
68+
periodCode: '',
69+
campusCode: '',
70+
hierarchy1: '',
71+
hierarchy2: '',
72+
hierarchy3: '',
73+
hierarchy4: '',
74+
hierarchy5: '',
75+
freeWord: '',
76+
_gaiyoFlg: 1,
77+
_risyuFlg: 1,
78+
_excludeFukaikoFlg: 1,
79+
outputFormat: 0,
80+
})
81+
)
82+
return iconv.decode(res.data, 'Shift_JIS')
83+
}
84+
1485
/**
1586
* KDBからCSVを取得
1687
*/
1788
export default async (
1889
year: number = new Date().getFullYear()
1990
): 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
91+
let flowExecutionKey = ''
92+
flowExecutionKey = await grantSession()
93+
flowExecutionKey = await searchAll(flowExecutionKey)
94+
return downloadCSV(flowExecutionKey)
5895
}

tsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"compilerOptions": {
3-
"target": "esnext",
3+
"target": "es2017",
44
"module": "commonjs",
55
"moduleResolution": "node",
66
"skipLibCheck": true,

yarn.lock

+77-24
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,9 @@ ansi-styles@^3.2.0, ansi-styles@^3.2.1:
103103
color-convert "^1.9.0"
104104

105105
arg@^4.1.0:
106-
version "4.1.0"
107-
resolved "https://registry.yarnpkg.com/arg/-/arg-4.1.0.tgz#583c518199419e0037abb74062c37f8519e575f0"
106+
version "4.1.3"
107+
resolved "https://registry.yarnpkg.com/arg/-/arg-4.1.3.tgz#269fc7ad5b8e42cb63c896d5666017261c144089"
108+
integrity sha512-58S9QDqG0Xx27YwPSt9fJxivjYl432YCwfDMfZ+71RAqUrZef7LrKQZ3LHLOwCS4FLNBplP533Zx895SeOCHvA==
108109

109110
argparse@^1.0.7:
110111
version "1.0.10"
@@ -121,6 +122,14 @@ astral-regex@^1.0.0:
121122
version "1.0.0"
122123
resolved "https://registry.yarnpkg.com/astral-regex/-/astral-regex-1.0.0.tgz#6c8c3fb827dd43ee3918f27b82782ab7658a6fd9"
123124

125+
axios-cookiejar-support@^1.0.1:
126+
version "1.0.1"
127+
resolved "https://registry.yarnpkg.com/axios-cookiejar-support/-/axios-cookiejar-support-1.0.1.tgz#7b32af7d932508546c68b1fc5ba8f562884162e1"
128+
integrity sha512-IZJxnAJ99XxiLqNeMOqrPbfR7fRyIfaoSLdPUf4AMQEGkH8URs0ghJK/xtqBsD+KsSr3pKl4DEQjCn834pHMig==
129+
dependencies:
130+
is-redirect "^1.0.0"
131+
pify "^5.0.0"
132+
124133
axios@^0.19.0:
125134
version "0.19.0"
126135
resolved "https://registry.yarnpkg.com/axios/-/axios-0.19.0.tgz#8e09bff3d9122e133f7b8101c8fbdd00ed3d2ab8"
@@ -142,6 +151,7 @@ brace-expansion@^1.1.7:
142151
buffer-from@^1.0.0:
143152
version "1.1.1"
144153
resolved "https://registry.yarnpkg.com/buffer-from/-/buffer-from-1.1.1.tgz#32713bc028f75c02fdb710d7c7bcec1f2c6070ef"
154+
integrity sha512-MQcXEUbCKtEo7bhqEs6560Hyd4XaovZlO/k9V3hjVUF/zwW7KBVdSK4gIt/bzwS9MbR5qob+F5jusZsb0YQK2A==
145155

146156
callsites@^3.0.0:
147157
version "3.1.0"
@@ -209,6 +219,11 @@ [email protected]:
209219
version "0.0.1"
210220
resolved "https://registry.yarnpkg.com/concat-map/-/concat-map-0.0.1.tgz#d8a96bd77fd68df7793a73036a3ba0d5405d477b"
211221

222+
create-require@^1.1.0:
223+
version "1.1.1"
224+
resolved "https://registry.yarnpkg.com/create-require/-/create-require-1.1.1.tgz#c1d7e8f1e5f6cfc9ff65f9cd352d37348756c333"
225+
integrity sha512-dcKFX3jn0MpIaXjisoRvexIJVEKzaq7z2rZKxf+MSr9TkdmHmsU4m2lcLojrj/FHl8mk5VxMmYA+ftRkP/3oKQ==
226+
212227
cross-spawn@^6.0.5:
213228
version "6.0.5"
214229
resolved "https://registry.yarnpkg.com/cross-spawn/-/cross-spawn-6.0.5.tgz#4a5ec7c64dfae22c3a14124dbacdee846d80cbc4"
@@ -269,8 +284,9 @@ defaults@^1.0.3:
269284
clone "^1.0.2"
270285

271286
diff@^4.0.1:
272-
version "4.0.1"
273-
resolved "https://registry.yarnpkg.com/diff/-/diff-4.0.1.tgz#0c667cb467ebbb5cea7f14f135cc2dba7780a8ff"
287+
version "4.0.2"
288+
resolved "https://registry.yarnpkg.com/diff/-/diff-4.0.2.tgz#60f3aecb89d5fae520c11aa19efc2bb982aade7d"
289+
integrity sha512-58lmxKSA4BNyLz+HHMUzlOEpg09FV+ev6ZMe3vJihgdxzgcwZ8VoEEPmALCZG9LmqfVoNMMKpttIYTVG6uDY7A==
274290

275291
doctrine@^3.0.0:
276292
version "3.0.0"
@@ -571,6 +587,11 @@ is-promise@^2.1.0:
571587
version "2.1.0"
572588
resolved "https://registry.yarnpkg.com/is-promise/-/is-promise-2.1.0.tgz#79a2a9ece7f096e80f36d2b2f3bc16c1ff4bf3fa"
573589

590+
is-redirect@^1.0.0:
591+
version "1.0.0"
592+
resolved "https://registry.yarnpkg.com/is-redirect/-/is-redirect-1.0.0.tgz#1d03dded53bd8db0f30c26e4f95d36fc7c87dc24"
593+
integrity sha1-HQPd7VO9jbDzDCbk+V02/HyH3CQ=
594+
574595
isexe@^2.0.0:
575596
version "2.0.0"
576597
resolved "https://registry.yarnpkg.com/isexe/-/isexe-2.0.0.tgz#e8fbf374dc556ff8947a10dcb0572d633f2cfa10"
@@ -619,8 +640,9 @@ lodash@^4.17.11, lodash@^4.17.12:
619640
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.14.tgz#9ce487ae66c96254fe20b599f21b6816028078ba"
620641

621642
make-error@^1.1.1:
622-
version "1.3.5"
623-
resolved "https://registry.yarnpkg.com/make-error/-/make-error-1.3.5.tgz#efe4e81f6db28cadd605c70f29c831b58ef776c8"
643+
version "1.3.6"
644+
resolved "https://registry.yarnpkg.com/make-error/-/make-error-1.3.6.tgz#2eb2e37ea9b67c4891f684a1394799af484cf7a2"
645+
integrity sha512-s8UhlNe7vPKomQhC1qFelMokr/Sc3AgNbso3n74mVPA5LTZwkB9NlXf4XPamLxJE8h0gh73rM94xvwRT2CVInw==
624646

625647
mimic-fn@^1.0.0:
626648
version "1.2.0"
@@ -714,6 +736,11 @@ path-key@^2.0.1:
714736
version "2.0.1"
715737
resolved "https://registry.yarnpkg.com/path-key/-/path-key-2.0.1.tgz#411cadb574c5a140d3a4b1910d40d80cc9f40b40"
716738

739+
pify@^5.0.0:
740+
version "5.0.0"
741+
resolved "https://registry.yarnpkg.com/pify/-/pify-5.0.0.tgz#1f5eca3f5e87ebec28cc6d54a0e4aaf00acc127f"
742+
integrity sha512-eW/gHNMlxdSP6dmG6uJip6FXN0EQBwm2clYYd8Wul42Cwu/DK8HEftzsapcNdYe2MfLiIwZqsDk2RDEsTE79hA==
743+
717744
prelude-ls@~1.1.2:
718745
version "1.1.2"
719746
resolved "https://registry.yarnpkg.com/prelude-ls/-/prelude-ls-1.1.2.tgz#21932a549f5e52ffd9a827f570e04be62a97da54"
@@ -724,15 +751,21 @@ prettier-linter-helpers@^1.0.0:
724751
dependencies:
725752
fast-diff "^1.1.2"
726753

727-
prettier@^1.18.2:
728-
version "1.18.2"
729-
resolved "https://registry.yarnpkg.com/prettier/-/prettier-1.18.2.tgz#6823e7c5900017b4bd3acf46fe9ac4b4d7bda9ea"
754+
prettier@^2.2.1:
755+
version "2.2.1"
756+
resolved "https://registry.yarnpkg.com/prettier/-/prettier-2.2.1.tgz#795a1a78dd52f073da0cd42b21f9c91381923ff5"
757+
integrity sha512-PqyhM2yCjg/oKkFPtTGUojv7gnZAoG80ttl45O6x2Ug/rMJw4wcc9k6aaf2hibP7BGVCCM33gZoGjyvt9mm16Q==
730758

731759
progress@^2.0.0:
732760
version "2.0.3"
733761
resolved "https://registry.yarnpkg.com/progress/-/progress-2.0.3.tgz#7e8cf8d8f5b8f239c1bc68beb4eb78567d572ef8"
734762

735-
punycode@^2.1.0:
763+
psl@^1.1.33:
764+
version "1.8.0"
765+
resolved "https://registry.yarnpkg.com/psl/-/psl-1.8.0.tgz#9326f8bcfb013adcc005fdff056acce020e51c24"
766+
integrity sha512-RIdOzyoavK+hA18OGGWDqUTsCLhtA7IcZ/6NCs4fFJaHBDab+pDDmDIByWFRQJq2Cd7r1OoQxBGKOaztq+hjIQ==
767+
768+
punycode@^2.1.0, punycode@^2.1.1:
736769
version "2.1.1"
737770
resolved "https://registry.yarnpkg.com/punycode/-/punycode-2.1.1.tgz#b58b010ac40c22c5657616c8d2c2c02c7bf479ec"
738771

@@ -803,16 +836,18 @@ slice-ansi@^2.1.0:
803836
astral-regex "^1.0.0"
804837
is-fullwidth-code-point "^2.0.0"
805838

806-
source-map-support@^0.5.6:
807-
version "0.5.12"
808-
resolved "https://registry.yarnpkg.com/source-map-support/-/source-map-support-0.5.12.tgz#b4f3b10d51857a5af0138d3ce8003b201613d599"
839+
source-map-support@^0.5.17:
840+
version "0.5.19"
841+
resolved "https://registry.yarnpkg.com/source-map-support/-/source-map-support-0.5.19.tgz#a98b62f86dcaf4f67399648c085291ab9e8fed61"
842+
integrity sha512-Wonm7zOCIJzBGQdB+thsPar0kYuCIzYvxZwlBa87yi/Mdjv7Tip2cyVbLj5o0cFPN4EVkuTwb3GDDyUx2DGnGw==
809843
dependencies:
810844
buffer-from "^1.0.0"
811845
source-map "^0.6.0"
812846

813847
source-map@^0.6.0:
814848
version "0.6.1"
815849
resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.6.1.tgz#74722af32e9614e9c287a8d0bbde48b5e2f1a263"
850+
integrity sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==
816851

817852
sprintf-js@~1.0.2:
818853
version "1.0.3"
@@ -882,15 +917,26 @@ tmp@^0.0.33:
882917
dependencies:
883918
os-tmpdir "~1.0.2"
884919

885-
ts-node@^8.3.0:
886-
version "8.3.0"
887-
resolved "https://registry.yarnpkg.com/ts-node/-/ts-node-8.3.0.tgz#e4059618411371924a1fb5f3b125915f324efb57"
920+
tough-cookie@^4.0.0:
921+
version "4.0.0"
922+
resolved "https://registry.yarnpkg.com/tough-cookie/-/tough-cookie-4.0.0.tgz#d822234eeca882f991f0f908824ad2622ddbece4"
923+
integrity sha512-tHdtEpQCMrc1YLrMaqXXcj6AxhYi/xgit6mZu1+EDWUn+qhUf8wMQoFIy9NXuq23zAwtcB0t/MjACGR18pcRbg==
924+
dependencies:
925+
psl "^1.1.33"
926+
punycode "^2.1.1"
927+
universalify "^0.1.2"
928+
929+
ts-node@^9.1.1:
930+
version "9.1.1"
931+
resolved "https://registry.yarnpkg.com/ts-node/-/ts-node-9.1.1.tgz#51a9a450a3e959401bda5f004a72d54b936d376d"
932+
integrity sha512-hPlt7ZACERQGf03M253ytLY3dHbGNGrAq9qIHWUY9XHYl1z7wYngSr3OQ5xmui8o2AaxsONxIzjafLUiWBo1Fg==
888933
dependencies:
889934
arg "^4.1.0"
935+
create-require "^1.1.0"
890936
diff "^4.0.1"
891937
make-error "^1.1.1"
892-
source-map-support "^0.5.6"
893-
yn "^3.0.0"
938+
source-map-support "^0.5.17"
939+
yn "3.1.1"
894940

895941
tslib@^1.8.1, tslib@^1.9.0:
896942
version "1.10.0"
@@ -908,15 +954,21 @@ type-check@~0.3.2:
908954
dependencies:
909955
prelude-ls "~1.1.2"
910956

911-
typescript@^3.5.3:
912-
version "3.5.3"
913-
resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.5.3.tgz#c830f657f93f1ea846819e929092f5fe5983e977"
957+
typescript@^4.1.3:
958+
version "4.1.3"
959+
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.1.3.tgz#519d582bd94cba0cf8934c7d8e8467e473f53bb7"
960+
integrity sha512-B3ZIOf1IKeH2ixgHhj6la6xdwR9QrLC5d1VKeCSY4tvkqhF2eqd9O7txNlS0PO3GrBAFIdr3L1ndNwteUbZLYg==
914961

915962
typical@^4.0.0:
916963
version "4.0.0"
917964
resolved "https://registry.yarnpkg.com/typical/-/typical-4.0.0.tgz#cbeaff3b9d7ae1e2bbfaf5a4e6f11eccfde94fc4"
918965
integrity sha512-VAH4IvQ7BDFYglMd7BPRDfLgxZZX4O4TFcRDA6EN5X7erNJJq+McIEp8np9aVtxrCJ6qx4GTYVfOWNjcqwZgRw==
919966

967+
universalify@^0.1.2:
968+
version "0.1.2"
969+
resolved "https://registry.yarnpkg.com/universalify/-/universalify-0.1.2.tgz#b646f69be3942dabcecc9d6639c80dc105efaa66"
970+
integrity sha512-rBJeI5CXAlmy1pV+617WB9J63U6XcazHHF2f2dbJix4XzpUF0RS3Zbj0FGIOCAva5P/d/GBOYaACQ1w+0azUkg==
971+
920972
uri-js@^4.2.2:
921973
version "4.2.2"
922974
resolved "https://registry.yarnpkg.com/uri-js/-/uri-js-4.2.2.tgz#94c540e1ff772956e2299507c010aea6c8838eb0"
@@ -950,6 +1002,7 @@ [email protected]:
9501002
dependencies:
9511003
mkdirp "^0.5.1"
9521004

953-
yn@^3.0.0:
954-
version "3.1.0"
955-
resolved "https://registry.yarnpkg.com/yn/-/yn-3.1.0.tgz#fcbe2db63610361afcc5eb9e0ac91e976d046114"
1005+
1006+
version "3.1.1"
1007+
resolved "https://registry.yarnpkg.com/yn/-/yn-3.1.1.tgz#1e87401a09d767c1d5eab26a6e4c185182d2eb50"
1008+
integrity sha512-Ux4ygGWsu2c7isFWe8Yu1YluJmqVhxqK2cLXNQA5AcC3QfbGNpM7fu0Y8b/z16pXLnFxZYvWhd3fhBY9DLmC6Q==

0 commit comments

Comments
 (0)