성준영

폴더 예외처리

...@@ -248,10 +248,11 @@ exports.selectChapter = function (chapterFilesArr) { ...@@ -248,10 +248,11 @@ exports.selectChapter = function (chapterFilesArr) {
248 * @author sungjunyoung 248 * @author sungjunyoung
249 * @description 249 * @description
250 * @param selectedFiles - 다운로드할 챕터 오브젝트 250 * @param selectedFiles - 다운로드할 챕터 오브젝트
251 + * @param selectLecture - 선택된 강의명
251 * @param downloadPath - 다운로드 경로 252 * @param downloadPath - 다운로드 경로
252 * @returns {*|Promise} 253 * @returns {*|Promise}
253 */ 254 */
254 -exports.downloadSelectedFiles = function (selectedFiles, downloadPath) { 255 +exports.downloadSelectedFiles = function (selectedFiles, selectLecture, downloadPath) {
255 256
256 return new Promise(function (resolve, reject) { 257 return new Promise(function (resolve, reject) {
257 258
...@@ -262,6 +263,16 @@ exports.downloadSelectedFiles = function (selectedFiles, downloadPath) { ...@@ -262,6 +263,16 @@ exports.downloadSelectedFiles = function (selectedFiles, downloadPath) {
262 } 263 }
263 264
264 265
266 +
267 + // 다운로드 경로 설정
268 + if (!fs.existsSync(downloadPath + selectLecture + '/'))
269 + fs.mkdirSync(downloadPath + selectLecture + '/');
270 + downloadPath += selectLecture + '/';
271 +
272 + if (!fs.existsSync(downloadPath + selectedFiles.chapter + '/'))
273 + fs.mkdirSync(downloadPath + selectedFiles.chapter + '/');
274 + downloadPath += selectedFiles.chapter + '/';
275 +
265 let count = 0; 276 let count = 0;
266 selectedFiles.files.forEach(function (value, index) { 277 selectedFiles.files.forEach(function (value, index) {
267 request = https.get(value.link, function (response) { 278 request = https.get(value.link, function (response) {
...@@ -279,8 +290,12 @@ exports.downloadSelectedFiles = function (selectedFiles, downloadPath) { ...@@ -279,8 +290,12 @@ exports.downloadSelectedFiles = function (selectedFiles, downloadPath) {
279 }); 290 });
280 }; 291 };
281 292
282 - 293 +/**
283 -exports.downloadAllFiles = function (chapterFilesArr,selectLecture, downloadPath) { 294 + * @author sungjunyoung
295 + * @description 해당 강의에 대한 모든 강의자료를 다운로드합니다.
296 + * @returns {*|Promise}
297 + */
298 +exports.downloadAllFiles = function (chapterFilesArr, selectLecture, downloadPath) {
284 299
285 return new Promise(function (resolve, reject) { 300 return new Promise(function (resolve, reject) {
286 301
...@@ -290,26 +305,29 @@ exports.downloadAllFiles = function (chapterFilesArr,selectLecture, downloadPath ...@@ -290,26 +305,29 @@ exports.downloadAllFiles = function (chapterFilesArr,selectLecture, downloadPath
290 downloadPath = require('path').resolve(downloadPath) + '/'; 305 downloadPath = require('path').resolve(downloadPath) + '/';
291 } 306 }
292 307
293 - fs.mkdirSync(downloadPath + selectLecture + '/'); 308 + if (!fs.existsSync(downloadPath + selectLecture + '/'))
309 + fs.mkdirSync(downloadPath + selectLecture + '/');
294 downloadPath += selectLecture + '/'; 310 downloadPath += selectLecture + '/';
295 console.log(''); 311 console.log('');
296 312
297 let count = 0; 313 let count = 0;
298 - chapterFilesArr.forEach(function(chapterObj, index){ 314 + chapterFilesArr.forEach(function (chapterObj, index) {
299 var asyncDownloadPath = downloadPath + chapterObj.chapter + '/'; 315 var asyncDownloadPath = downloadPath + chapterObj.chapter + '/';
300 - fs.mkdirSync(asyncDownloadPath); 316 +
317 + if (!fs.existsSync(asyncDownloadPath))
318 + fs.mkdirSync(asyncDownloadPath);
301 319
302 var subCount = 0; 320 var subCount = 0;
303 - chapterObj.files.forEach(function(value, index){ 321 + chapterObj.files.forEach(function (value, index) {
304 - request = https.get(value.link, function(response){ 322 + request = https.get(value.link, function (response) {
305 let file = fs.createWriteStream(asyncDownloadPath + value.fileName); 323 let file = fs.createWriteStream(asyncDownloadPath + value.fileName);
306 response.pipe(file); 324 response.pipe(file);
307 325
308 - subCount ++; 326 + subCount++;
309 - if(subCount === chapterObj.files.length){ 327 + if (subCount === chapterObj.files.length) {
310 - count ++; 328 + count++;
311 - console.log(' 다운로드중... ('+ asyncDownloadPath +')'); 329 + console.log(' 다운로드중... (' + asyncDownloadPath + ')');
312 - if(count === chapterFilesArr.length){ 330 + if (count === chapterFilesArr.length) {
313 resolve('\n 파일이 ' + downloadPath + ' 에 저장되었어요! 열공 :)'); 331 resolve('\n 파일이 ' + downloadPath + ' 에 저장되었어요! 열공 :)');
314 } 332 }
315 } 333 }
......
...@@ -30,11 +30,14 @@ if (require.main === module) { ...@@ -30,11 +30,14 @@ if (require.main === module) {
30 .then(functions.getLecture) 30 .then(functions.getLecture)
31 .then(functions.getLectureLink) 31 .then(functions.getLectureLink)
32 .then(functions.selectLecture) 32 .then(functions.selectLecture)
33 - .then(functions.getClassPageBody) 33 + .then(function (lectureObject) {
34 + selectLecture = lectureObject.lectureName;
35 + return functions.getClassPageBody(lectureObject);
36 + })
34 .then(functions.findFiles) 37 .then(functions.findFiles)
35 .then(functions.selectChapter) 38 .then(functions.selectChapter)
36 .then(function (selectedFiles) { 39 .then(function (selectedFiles) {
37 - return functions.downloadSelectedFiles(selectedFiles, flags.downloadPath); 40 + return functions.downloadSelectedFiles(selectedFiles, selectLecture, flags.downloadPath);
38 }) 41 })
39 .then(function (result) { 42 .then(function (result) {
40 console.log(result); 43 console.log(result);
...@@ -58,11 +61,11 @@ if (require.main === module) { ...@@ -58,11 +61,11 @@ if (require.main === module) {
58 .then(function (chapterFilesArr) { 61 .then(function (chapterFilesArr) {
59 return functions.downloadAllFiles(chapterFilesArr, selectLecture, flags.downloadPath); 62 return functions.downloadAllFiles(chapterFilesArr, selectLecture, flags.downloadPath);
60 }) 63 })
61 - .then(function(result){ 64 + .then(function (result) {
62 console.log(result); 65 console.log(result);
63 process.exit(); 66 process.exit();
64 }) 67 })
65 - .catch(function(err){ 68 + .catch(function (err) {
66 console.log(err); 69 console.log(err);
67 process.exit(); 70 process.exit();
68 }) 71 })
......