1
+ const request = require ( 'request' ) ;
2
+ const fs = require ( 'fs' ) ;
3
+ const async = require ( 'async' ) ;
4
+
5
+ require ( 'dotenv' ) . config ( )
6
+ const env = process . env ;
7
+
8
+ function downloadImage ( address ) {
9
+ var r = request ( address ) ;
10
+
11
+ r . on ( 'response' , function ( res ) {
12
+ var filepath = './dl/' + res . headers . date + '.' + res . headers [ 'content-type' ] . split ( '/' ) [ 1 ] ;
13
+ console . log ( filepath ) ;
14
+ res . pipe ( fs . createWriteStream ( filepath ) ) ;
15
+ } ) ;
16
+ }
17
+
18
+ function getAllImages ( filename ) {
19
+ request ( `${ env . BASE_URL } /command.cgi?op=100&DIR=/DCIM/${ filename } ` , function ( error , response , body ) {
20
+ if ( error ) {
21
+ return console . log ( 'error:' , error ) ; // Print the error if one occurred
22
+ }
23
+
24
+ var lines = body . split ( '\r\n' )
25
+ var q = async . queue ( function ( task , done ) {
26
+ downloadImage ( task . url ) ;
27
+ } ) ;
28
+ for ( var i = 0 , len = lines . length ; i < len ; i ++ ) {
29
+ var line = lines [ i ] ;
30
+
31
+ if ( line !== 'WLANSD_FILELIST' ) {
32
+
33
+ var splitLine = line . split ( ',' ) ;
34
+ var directory = splitLine [ 0 ] ;
35
+ var filename = splitLine [ 1 ] ;
36
+
37
+ if ( filename ) {
38
+ console . log ( `\nStarting download on ${ directory } /${ filename } ` ) ;
39
+ var fullPath = `${ env . BASE_URL } ${ directory } /${ filename } ` ;
40
+ console . log ( fullPath ) ;
41
+ q . push ( {
42
+ url : fullPath
43
+ } , function ( err ) {
44
+ if ( err ) {
45
+ console . log ( `ERROR: ${ err } ` ) ;
46
+ }
47
+ console . log ( `Finished downloading ${ fullPath } \n` ) ;
48
+ } ) ;
49
+ }
50
+ }
51
+
52
+ }
53
+ } )
54
+ }
55
+
56
+
57
+ request ( `${ env . BASE_URL } /command.cgi?op=100&DIR=/DCIM` , function ( error , response , body ) {
58
+ if ( error ) {
59
+ return console . log ( 'error:' , error ) ; // Print the error if one occurred
60
+ }
61
+
62
+ var lines = body . split ( '\r\n' )
63
+
64
+ for ( var i = 0 , len = lines . length ; i < len ; i ++ ) {
65
+ var line = lines [ i ] . trim ( ) ;
66
+ if ( line !== 'WLANSD_FILELIST' ) {
67
+
68
+ var splitLine = line . split ( ',' ) ;
69
+ var directory = splitLine [ 0 ] ;
70
+ var filename = splitLine [ 1 ] ;
71
+ var size = splitLine [ 2 ] ;
72
+ var attribute = splitLine [ 3 ] ;
73
+ var date = splitLine [ 4 ] ;
74
+ var time = splitLine [ 5 ] ;
75
+
76
+ if ( attribute === '16' && filename !== 'EOSMISC' ) {
77
+ console . log ( `Folder ${ filename } ` ) ;
78
+ getAllImages ( filename ) ;
79
+ }
80
+ }
81
+ }
82
+ } ) ;
0 commit comments