2020
2121const fs = require ( 'fs' ) ;
2222const https = require ( 'https' ) ;
23+ const { resolve } = require ( 'path' ) ;
2324
2425const term = require ( 'terminal-kit' ) . terminal ;
2526
@@ -36,6 +37,7 @@ let empty = false;
3637let closing = false ;
3738
3839let index = 0 ;
40+ let tempIndex = 0 ;
3941let dlComplete = 0 ;
4042const total = {
4143 total : 0 ,
@@ -56,6 +58,7 @@ catch (error) {
5658 term ( 'No config file detected.' ) ;
5759
5860 const defaultConfig = {
61+ batch : 128 ,
5962 selectedArchive : 0 ,
6063 archives : [
6164 {
@@ -100,19 +103,31 @@ function setFiles() {
100103 return ;
101104 }
102105
106+ // check for bad config file
107+ if ( ! config . hasOwnProperty ( 'batch' ) || ! config . hasOwnProperty ( 'archives' ) || ! config . hasOwnProperty ( 'selectedArchive' ) ) {
108+ term . red ( '\n[ CONFIG ERROR ] ' ) ;
109+ term ( 'Invalid config file.\n └─[ Please fix it, or try deleting it so that it may be re-generated. ]' ) ;
110+ term . inputField ( ) ;
111+ return ;
112+ }
103113 if ( config . archives . length == 0 ) {
104114 term . red ( '\n[ CONFIG ERROR ] ' ) ;
105- term ( 'No available archives' ) ;
115+ term ( 'No available archives found in "./config.json". ' ) ;
106116 term . inputField ( ) ;
107117 return ;
108118 }
109- else if ( config . selectedArchive >= config . archives . length ) {
119+ if ( config . selectedArchive >= config . archives . length ) {
110120 config . selectedArchive = 0 ;
111121 }
112-
113122 if ( ! fs . existsSync ( config . archives [ config . selectedArchive ] . dataJSON ) ) {
114123 fs . writeFileSync ( config . archives [ config . selectedArchive ] . dataJSON , '{"table":[]}' , 'utf-8' ) ;
115124 }
125+ if ( config . batch > 320 ) {
126+ config . batch = 320 ;
127+ }
128+ else if ( config . batch < 1 ) {
129+ config . batch = 1 ;
130+ }
116131
117132 // check for bad data file
118133 try {
@@ -336,7 +351,7 @@ async function downloadAllFolders(save) {
336351 // no need for an api key since links can be reconstructed if e6 says they are null
337352 // + (config.userName + config.APIKey != '' ? 'login=' + config.userName + '&api_key=' + config.APIKey + '&' : '')
338353 + 'tags=' + tags
339- + '&limit=320'
354+ + '&limit=' + config . batch
340355 + '&page=a' + save . table [ index ] . latestID ,
341356 { 'headers' : { 'User-Agent' : 'e6_auto_archive' } } ) ;
342357
@@ -350,8 +365,9 @@ async function downloadAllFolders(save) {
350365 }
351366
352367 // remember modified folder
353- if ( foldersModified [ foldersModified . length - 1 ] != tags ) {
354- foldersModified . push ( ( Object . keys ( data [ 'posts' ] ) . length == 320 ? '>' : '+' ) + Object . keys ( data [ 'posts' ] ) . length + ' ][ ' + tags ) ;
368+ if ( tempIndex != index ) {
369+ foldersModified . push ( ( Object . keys ( data [ 'posts' ] ) . length == config . batch ? '>' : '+' ) + Object . keys ( data [ 'posts' ] ) . length + ' ][ ' + tags ) ;
370+ tempIndex = index ;
355371 }
356372
357373 dlComplete = 0 ;
@@ -409,7 +425,7 @@ async function downloadAllFolders(save) {
409425
410426 // retry
411427 if ( attempt < 4 ) {
412- console . log ( ' ┬ ┬\n │ └─[ Attempt ' + ( attempt + 1 ) + ' Failed, Retrying... ]' ) ;
428+ console . log ( ' ┬ ┬\n │ └─[ Attempt ' + ( attempt + 1 ) + ' Failed, Retrying... ]' ) ;
413429 await new Promise ( resolve => setTimeout ( resolve , 200 ) ) ;
414430 await downloadFile ( attempt + 1 ) ;
415431 resolve ( ) ;
@@ -435,7 +451,7 @@ async function downloadAllFolders(save) {
435451 save . table [ index ] . latestID = data [ 'posts' ] [ 0 ] [ 'id' ] ;
436452
437453 // if this was a max fetch (320 files) then repeat
438- if ( data [ 'posts' ] . length == 320 ) {
454+ if ( data [ 'posts' ] . length == config . batch ) {
439455 // comply with rate limit (~1 API call per second)
440456 // shouldn't be needed since its 320 files but someone might have ridiculous internet speeds
441457 const rate = rateLimitDelay ( ) ;
@@ -504,7 +520,8 @@ async function downloadAllFolders(save) {
504520 catch ( err ) {
505521 term . red ( '\n[ ERROR :< ] ' ) ;
506522 console . error ( err ) ;
507- term . inputField ( ) ;
523+ await term . inputField ( ) . promise ;
524+ resolve ( ) ;
508525 }
509526}
510527
@@ -539,7 +556,7 @@ function exitHandler() {
539556 directories . push ( file . directory ) ;
540557 file . stream . close ( ) ;
541558 if ( fs . existsSync ( file . name ) ) fs . unlinkSync ( file . name ) ;
542- term . red ( '[ FILE DELETED ; ' + file . name + ' ]' ) ;
559+ term . red ( '[ FILE DELETED ; ' + file . name + ' ]\n ' ) ;
543560 }
544561 }
545562 fileStreams = [ ] ;
0 commit comments