성준영

download files to project root directory

...@@ -9,8 +9,6 @@ const readline = require('readline'); ...@@ -9,8 +9,6 @@ const readline = require('readline');
9 var https = require('https'); 9 var https = require('https');
10 var querystring = require('querystring'); 10 var querystring = require('querystring');
11 11
12 -var Iconv = require('iconv').Iconv;
13 -var iconv = new Iconv('euc-kr', 'utf-8//translit//ignore');
14 12
15 13
16 var j = request.jar(); 14 var j = request.jar();
...@@ -21,11 +19,13 @@ exports.login = function (id, pw) { ...@@ -21,11 +19,13 @@ exports.login = function (id, pw) {
21 request({ 19 request({
22 url: "https://klas.khu.ac.kr/user/loginUser.do", 20 url: "https://klas.khu.ac.kr/user/loginUser.do",
23 method: "POST", 21 method: "POST",
24 - form: {USER_ID: id, PASSWORD: pw} 22 + form: {USER_ID: id, PASSWORD: pw},
23 + timeout: 3000
25 }, function (err, res, body) { 24 }, function (err, res, body) {
26 -
27 if (err) { 25 if (err) {
28 - reject('ERROR'); 26 + reject('timeout');
27 + } else if (j.getCookies("https://klas.khu.ac.kr").length === 0) {
28 + reject('login_error');
29 } else { 29 } else {
30 resolve(body); 30 resolve(body);
31 } 31 }
...@@ -137,10 +137,50 @@ exports.findFiles = function (classPageBody) { ...@@ -137,10 +137,50 @@ exports.findFiles = function (classPageBody) {
137 137
138 return new Promise(function (resolve, reject) { 138 return new Promise(function (resolve, reject) {
139 var $ = cheerio.load(classPageBody); 139 var $ = cheerio.load(classPageBody);
140 - var fileDownAnchors = $('.file-downbox-list > ul > li > a'); 140 + var fileDownAnchors = $('.mycl_listbox.today');
141 +
142 + var fileArr = [];
143 +
141 144
142 fileDownAnchors.each(function (i) { 145 fileDownAnchors.each(function (i) {
143 - console.log($(this).attr('href')); 146 + var tempFileArr = [];
147 + $(this).find('.mycl_veiw_learnig').find('.file-downbox-list').find('a').each(function (j) {
148 + var link = 'https://klas.khu.ac.kr' + $(this).attr('href').split('..')[1];
149 +
150 + tempFileArr.push(link);
151 + });
152 + fileArr.push(tempFileArr);
144 }); 153 });
154 +
155 + resolve(fileArr);
156 + })
157 +};
158 +
159 +exports.getSelectedFiles = function (fileArr, lectureBefore) {
160 + return new Promise(function (resolve, reject) {
161 + var downloadIndex = fileArr.length - lectureBefore - 1;
162 +
163 + if (downloadIndex < 0) {
164 + reject('no reference files');
165 + }
166 +
167 + resolve(fileArr[downloadIndex]);
168 + })
169 +};
170 +
171 +exports.downloadSelectedFile = function (selectedFile, path) {
172 +
173 + return new Promise(function (resolve, reject) {
174 +
175 + selectedFile.forEach(function (value, index) {
176 + request = https.get(value, function (response) {
177 + // console.log(response);
178 + var file = fs.createWriteStream("./file_" + index + ".pdf");
179 + response.pipe(file);
180 + });
181 + if (index === count - 1) {
182 + resolve('done');
183 + }
184 + })
145 }) 185 })
146 }; 186 };
...\ No newline at end of file ...\ No newline at end of file
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
2 2
3 var args = require('args'); 3 var args = require('args');
4 var functions = require('./functions'); 4 var functions = require('./functions');
5 +var os = require('os');
5 6
6 args 7 args
7 .option('id', 'Your Student ID of KHU, Required') 8 .option('id', 'Your Student ID of KHU, Required')
...@@ -26,8 +27,30 @@ functions.login(flags.id, flags.pw) ...@@ -26,8 +27,30 @@ functions.login(flags.id, flags.pw)
26 .then(functions.selectLecture) 27 .then(functions.selectLecture)
27 .then(functions.getClassPageBody) 28 .then(functions.getClassPageBody)
28 .then(functions.findFiles) 29 .then(functions.findFiles)
29 - .then(function(result){ 30 + .then(function (fileArr) {
30 - console.log(result); 31 + var lectureBefore;
32 + if (!flags.lectureBefore) {
33 + lectureBefore = 0;
34 + } else {
35 + lectureBefore = flags.lectureBefore;
36 + }
37 + return functions.getSelectedFiles(fileArr, lectureBefore);
38 + })
39 + .then(function (selectedFile) {
40 +
41 + // var path;
42 + // if (!flags.downloadPath) {
43 + // path = os.homedir() + '/downloads/klasFileDownloader'
44 + // } else {
45 + // path = flags.downloadPath;
46 + // }
47 + //
48 + // if (path[path.length - 1] === '/') {
49 + // path = path.substr(0, path.length - 1);
50 + // }
51 +
52 +
53 + functions.downloadSelectedFile(selectedFile, path)
31 }) 54 })
32 .catch(function (err) { 55 .catch(function (err) {
33 console.log(err); 56 console.log(err);
......
...@@ -28,7 +28,7 @@ ...@@ -28,7 +28,7 @@
28 "dependencies": { 28 "dependencies": {
29 "args": "^2.4.1", 29 "args": "^2.4.1",
30 "cheerio": "^0.22.0", 30 "cheerio": "^0.22.0",
31 - "iconv": "^2.2.1", 31 + "fs": "0.0.1-security",
32 "promise": "^7.1.1", 32 "promise": "^7.1.1",
33 "querystring": "^0.2.0", 33 "querystring": "^0.2.0",
34 "request": "^2.81.0" 34 "request": "^2.81.0"
......