Toggle navigation
Toggle navigation
This project
Loading...
Sign in
성준영
/
klas-file-downloader
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Snippets
Network
Create a new issue
Builds
Commits
Issue Boards
Authored by
성준영
2017-05-31 14:22:41 +0900
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
762c3a1241d03d4f117ed02b9fa710645d2608b9
762c3a12
1 parent
06fb58f4
폴더 예외처리
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
38 additions
and
17 deletions
functions.js
index.js
functions.js
View file @
762c3a1
...
...
@@ -248,10 +248,11 @@ exports.selectChapter = function (chapterFilesArr) {
* @author sungjunyoung
* @description
* @param selectedFiles - 다운로드할 챕터 오브젝트
* @param selectLecture - 선택된 강의명
* @param downloadPath - 다운로드 경로
* @returns {*|Promise}
*/
exports
.
downloadSelectedFiles
=
function
(
selectedFiles
,
downloadPath
)
{
exports
.
downloadSelectedFiles
=
function
(
selectedFiles
,
selectLecture
,
downloadPath
)
{
return
new
Promise
(
function
(
resolve
,
reject
)
{
...
...
@@ -262,6 +263,16 @@ exports.downloadSelectedFiles = function (selectedFiles, downloadPath) {
}
// 다운로드 경로 설정
if
(
!
fs
.
existsSync
(
downloadPath
+
selectLecture
+
'/'
))
fs
.
mkdirSync
(
downloadPath
+
selectLecture
+
'/'
);
downloadPath
+=
selectLecture
+
'/'
;
if
(
!
fs
.
existsSync
(
downloadPath
+
selectedFiles
.
chapter
+
'/'
))
fs
.
mkdirSync
(
downloadPath
+
selectedFiles
.
chapter
+
'/'
);
downloadPath
+=
selectedFiles
.
chapter
+
'/'
;
let
count
=
0
;
selectedFiles
.
files
.
forEach
(
function
(
value
,
index
)
{
request
=
https
.
get
(
value
.
link
,
function
(
response
)
{
...
...
@@ -279,8 +290,12 @@ exports.downloadSelectedFiles = function (selectedFiles, downloadPath) {
});
};
exports
.
downloadAllFiles
=
function
(
chapterFilesArr
,
selectLecture
,
downloadPath
)
{
/**
* @author sungjunyoung
* @description 해당 강의에 대한 모든 강의자료를 다운로드합니다.
* @returns {*|Promise}
*/
exports
.
downloadAllFiles
=
function
(
chapterFilesArr
,
selectLecture
,
downloadPath
)
{
return
new
Promise
(
function
(
resolve
,
reject
)
{
...
...
@@ -290,26 +305,29 @@ exports.downloadAllFiles = function (chapterFilesArr,selectLecture, downloadPath
downloadPath
=
require
(
'path'
).
resolve
(
downloadPath
)
+
'/'
;
}
fs
.
mkdirSync
(
downloadPath
+
selectLecture
+
'/'
);
if
(
!
fs
.
existsSync
(
downloadPath
+
selectLecture
+
'/'
))
fs
.
mkdirSync
(
downloadPath
+
selectLecture
+
'/'
);
downloadPath
+=
selectLecture
+
'/'
;
console
.
log
(
''
);
let
count
=
0
;
chapterFilesArr
.
forEach
(
function
(
chapterObj
,
index
)
{
chapterFilesArr
.
forEach
(
function
(
chapterObj
,
index
)
{
var
asyncDownloadPath
=
downloadPath
+
chapterObj
.
chapter
+
'/'
;
fs
.
mkdirSync
(
asyncDownloadPath
);
if
(
!
fs
.
existsSync
(
asyncDownloadPath
))
fs
.
mkdirSync
(
asyncDownloadPath
);
var
subCount
=
0
;
chapterObj
.
files
.
forEach
(
function
(
value
,
index
)
{
request
=
https
.
get
(
value
.
link
,
function
(
response
)
{
chapterObj
.
files
.
forEach
(
function
(
value
,
index
)
{
request
=
https
.
get
(
value
.
link
,
function
(
response
)
{
let
file
=
fs
.
createWriteStream
(
asyncDownloadPath
+
value
.
fileName
);
response
.
pipe
(
file
);
subCount
++
;
if
(
subCount
===
chapterObj
.
files
.
length
)
{
count
++
;
console
.
log
(
' 다운로드중... ('
+
asyncDownloadPath
+
')'
);
if
(
count
===
chapterFilesArr
.
length
)
{
subCount
++
;
if
(
subCount
===
chapterObj
.
files
.
length
)
{
count
++
;
console
.
log
(
' 다운로드중... ('
+
asyncDownloadPath
+
')'
);
if
(
count
===
chapterFilesArr
.
length
)
{
resolve
(
'\n 파일이 '
+
downloadPath
+
' 에 저장되었어요! 열공 :)'
);
}
}
...
...
index.js
View file @
762c3a1
...
...
@@ -30,11 +30,14 @@ if (require.main === module) {
.
then
(
functions
.
getLecture
)
.
then
(
functions
.
getLectureLink
)
.
then
(
functions
.
selectLecture
)
.
then
(
functions
.
getClassPageBody
)
.
then
(
function
(
lectureObject
)
{
selectLecture
=
lectureObject
.
lectureName
;
return
functions
.
getClassPageBody
(
lectureObject
);
})
.
then
(
functions
.
findFiles
)
.
then
(
functions
.
selectChapter
)
.
then
(
function
(
selectedFiles
)
{
return
functions
.
downloadSelectedFiles
(
selectedFiles
,
flags
.
downloadPath
);
return
functions
.
downloadSelectedFiles
(
selectedFiles
,
selectLecture
,
flags
.
downloadPath
);
})
.
then
(
function
(
result
)
{
console
.
log
(
result
);
...
...
@@ -58,11 +61,11 @@ if (require.main === module) {
.
then
(
function
(
chapterFilesArr
)
{
return
functions
.
downloadAllFiles
(
chapterFilesArr
,
selectLecture
,
flags
.
downloadPath
);
})
.
then
(
function
(
result
)
{
.
then
(
function
(
result
)
{
console
.
log
(
result
);
process
.
exit
();
})
.
catch
(
function
(
err
)
{
.
catch
(
function
(
err
)
{
console
.
log
(
err
);
process
.
exit
();
})
...
...
Please
register
or
login
to post a comment