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-04-04 02:47:19 +0900
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
d2a2c0d5a03a64a35ba22b034b6a5a1dee051e21
d2a2c0d5
1 parent
fba496bd
download files to project root directory
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
73 additions
and
10 deletions
functions.js
index.js
package.json
functions.js
View file @
d2a2c0d
...
...
@@ -9,8 +9,6 @@ const readline = require('readline');
var
https
=
require
(
'https'
);
var
querystring
=
require
(
'querystring'
);
var
Iconv
=
require
(
'iconv'
).
Iconv
;
var
iconv
=
new
Iconv
(
'euc-kr'
,
'utf-8//translit//ignore'
);
var
j
=
request
.
jar
();
...
...
@@ -21,11 +19,13 @@ exports.login = function (id, pw) {
request
({
url
:
"https://klas.khu.ac.kr/user/loginUser.do"
,
method
:
"POST"
,
form
:
{
USER_ID
:
id
,
PASSWORD
:
pw
}
form
:
{
USER_ID
:
id
,
PASSWORD
:
pw
},
timeout
:
3000
},
function
(
err
,
res
,
body
)
{
if
(
err
)
{
reject
(
'ERROR'
);
reject
(
'timeout'
);
}
else
if
(
j
.
getCookies
(
"https://klas.khu.ac.kr"
).
length
===
0
)
{
reject
(
'login_error'
);
}
else
{
resolve
(
body
);
}
...
...
@@ -137,10 +137,50 @@ exports.findFiles = function (classPageBody) {
return
new
Promise
(
function
(
resolve
,
reject
)
{
var
$
=
cheerio
.
load
(
classPageBody
);
var
fileDownAnchors
=
$
(
'.file-downbox-list > ul > li > a'
);
var
fileDownAnchors
=
$
(
'.mycl_listbox.today'
);
var
fileArr
=
[];
fileDownAnchors
.
each
(
function
(
i
)
{
console
.
log
(
$
(
this
).
attr
(
'href'
));
var
tempFileArr
=
[];
$
(
this
).
find
(
'.mycl_veiw_learnig'
).
find
(
'.file-downbox-list'
).
find
(
'a'
).
each
(
function
(
j
)
{
var
link
=
'https://klas.khu.ac.kr'
+
$
(
this
).
attr
(
'href'
).
split
(
'..'
)[
1
];
tempFileArr
.
push
(
link
);
});
fileArr
.
push
(
tempFileArr
);
});
resolve
(
fileArr
);
})
};
exports
.
getSelectedFiles
=
function
(
fileArr
,
lectureBefore
)
{
return
new
Promise
(
function
(
resolve
,
reject
)
{
var
downloadIndex
=
fileArr
.
length
-
lectureBefore
-
1
;
if
(
downloadIndex
<
0
)
{
reject
(
'no reference files'
);
}
resolve
(
fileArr
[
downloadIndex
]);
})
};
exports
.
downloadSelectedFile
=
function
(
selectedFile
,
path
)
{
return
new
Promise
(
function
(
resolve
,
reject
)
{
selectedFile
.
forEach
(
function
(
value
,
index
)
{
request
=
https
.
get
(
value
,
function
(
response
)
{
// console.log(response);
var
file
=
fs
.
createWriteStream
(
"./file_"
+
index
+
".pdf"
);
response
.
pipe
(
file
);
});
if
(
index
===
count
-
1
)
{
resolve
(
'done'
);
}
})
})
};
\ No newline at end of file
...
...
index.js
View file @
d2a2c0d
...
...
@@ -2,6 +2,7 @@
var
args
=
require
(
'args'
);
var
functions
=
require
(
'./functions'
);
var
os
=
require
(
'os'
);
args
.
option
(
'id'
,
'Your Student ID of KHU, Required'
)
...
...
@@ -26,8 +27,30 @@ functions.login(flags.id, flags.pw)
.
then
(
functions
.
selectLecture
)
.
then
(
functions
.
getClassPageBody
)
.
then
(
functions
.
findFiles
)
.
then
(
function
(
result
){
console
.
log
(
result
);
.
then
(
function
(
fileArr
)
{
var
lectureBefore
;
if
(
!
flags
.
lectureBefore
)
{
lectureBefore
=
0
;
}
else
{
lectureBefore
=
flags
.
lectureBefore
;
}
return
functions
.
getSelectedFiles
(
fileArr
,
lectureBefore
);
})
.
then
(
function
(
selectedFile
)
{
// var path;
// if (!flags.downloadPath) {
// path = os.homedir() + '/downloads/klasFileDownloader'
// } else {
// path = flags.downloadPath;
// }
//
// if (path[path.length - 1] === '/') {
// path = path.substr(0, path.length - 1);
// }
functions
.
downloadSelectedFile
(
selectedFile
,
path
)
})
.
catch
(
function
(
err
)
{
console
.
log
(
err
);
...
...
package.json
View file @
d2a2c0d
...
...
@@ -28,7 +28,7 @@
"dependencies"
:
{
"args"
:
"^2.4.1"
,
"cheerio"
:
"^0.22.0"
,
"
iconv"
:
"^2.2.1
"
,
"
fs"
:
"0.0.1-security
"
,
"promise"
:
"^7.1.1"
,
"querystring"
:
"^0.2.0"
,
"request"
:
"^2.81.0"
...
...
Please
register
or
login
to post a comment