comichandler.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. var $ = require("cheerio");
  2. var request = require('request');
  3. var agent = require("socks5-http-client/lib/Agent");
  4. var fs = require('fs');
  5. var ListHandler = require('./listhandler.js');
  6. require('./config.js');
  7. var ComicHandler = {
  8. NowIndex:0,
  9. ListTree:[],
  10. start:function(fromComic){
  11. // console.log(this);
  12. // return;
  13. if(!this.ListTree.length) this.ListTree = ListLib.tree;
  14. var fromEp = 1;
  15. if(fromComic){
  16. var fromComicID = fromComic.id;
  17. if(fromComicID){
  18. for(var i in this.ListTree){
  19. if(this.ListTree[i]==fromComicID){
  20. this.NowIndex = i;
  21. break;
  22. }
  23. }
  24. }
  25. if(fromComic.ep) fromEp = fromComic.ep;
  26. }
  27. if(this.NowIndex==this.ListTree.length){
  28. console.log('Comic Download Complete');
  29. }else{
  30. var comicID = this.ListTree[this.NowIndex];
  31. new ComicSaver(comicID,fromEp);
  32. this.NowIndex++;
  33. }
  34. }
  35. }
  36. var ComicSaver = function(comicID,fromEp){
  37. var _this=this;
  38. _this.ep = 0;
  39. _this.nowEp = 0;
  40. _this.imgLength = 0;
  41. _this.imgDownloaded = 0;
  42. _this.retryTimes = {};
  43. _this.imgLib = [];
  44. // console.log(this);
  45. // return;
  46. _this.comicOBJ = ListLib[comicID];
  47. if(!_this.comicOBJ){
  48. console.log('ComicID '+comicID+' wrong!');
  49. return;
  50. }
  51. console.log('----------------------------------------------');
  52. console.log('[BEGIN] Start Download Comic [ '+comicID+' ]');
  53. console.log('Creating directory for [ '+_this.comicOBJ.name+' ]');
  54. _this.comicPath = './save/'+_this.comicOBJ.id+'-'+_this.comicOBJ.name+'/';
  55. if(!fs.existsSync(_this.comicPath)){
  56. fs.mkdirSync(_this.comicPath);
  57. }
  58. var options = {
  59. url:ApiURL.comicEp.replace('{ComicID}',_this.comicOBJ.id)
  60. }
  61. console.log('Analysing......');
  62. request(options,function(error,response,body){
  63. if(!error && body && response.statusCode==200){
  64. var data;
  65. try{
  66. data = JSON.parse(body);
  67. }catch(e){
  68. console.log(e,body);
  69. new ComicSaver(ComicID,fromEp);
  70. delete _this;
  71. return;
  72. // downloadEP();
  73. }
  74. _this.ep = data['ep_count'];
  75. _this.nowEp = fromEp?fromEp:1;
  76. console.log('Analysie Success!');
  77. console.log('Comic [ '+_this.comicOBJ.id+' ] has [ '+_this.ep+' ] EP!!!');
  78. downloadEP();
  79. }else{
  80. console.log('Comic [ '+_this.comicOBJ.id+' ] analysie error!!! Retry!!!');
  81. new ComicSaver(ComicID,fromEp);
  82. delete _this;
  83. return;
  84. }
  85. });
  86. function downloadEP(){
  87. if(_this.nowEp>_this.ep){
  88. finished();
  89. return;
  90. }
  91. var opt = {
  92. url:ApiURL.comic.replace('{ComicID}',_this.comicOBJ.id).replace('{EP}',_this.nowEp)
  93. }
  94. console.log('Comic [ '+_this.comicOBJ.id+' ] EP [ '+_this.nowEp+' ]start download!!!');
  95. var epPath = _this.comicPath+_this.nowEp+'/';
  96. if(!fs.existsSync(epPath)){
  97. fs.mkdirSync(epPath);
  98. }
  99. request(opt,function(error,response,body){
  100. // console.log(body);
  101. if(!error && body && response.statusCode==200){
  102. var data;
  103. try{
  104. data = JSON.parse(body);
  105. }catch(e){
  106. console.log(e,body);
  107. downloadEP();
  108. return;
  109. }
  110. _this.imgLength = data.length;
  111. _this.imgDownloaded = 0;
  112. console.log('Comic [ '+_this.comicOBJ.id+' ] EP [ '+_this.nowEp+' ] has [ '+_this.imgLength+' ] images!!!');
  113. console.log('Comic [ '+_this.comicOBJ.id+' ] EP [ '+_this.nowEp+' ] images start download!!!');
  114. for(var i in data){
  115. _this.retryTimes[data[i]['url']] = 0;
  116. // saveImg(data[i]['url']);
  117. _this.imgLib.push(data[i]['url']);
  118. }
  119. saveImg();
  120. }else{
  121. console.log('Comic [ '+_this.comicOBJ.id+' ] EP [ '+_this.nowEp+' ] download error!!!Retry!!!');
  122. downloadEP();
  123. }
  124. });
  125. }
  126. function saveImg(){
  127. var imgSrc = _this.imgLib[_this.imgDownloaded];
  128. if(!imgSrc){
  129. console.log('Comic [ '+_this.comicOBJ.id+' ] image download error!!!');
  130. console.log('Image [ '+imgSrc+' ] error');
  131. }
  132. var splitURL = imgSrc.split('/');
  133. var opt = {
  134. url:imgSrc,
  135. encoding:'binary',
  136. timeout:6000
  137. }
  138. request(opt,function(error,response,body){
  139. if(!error && body){
  140. var data = body;
  141. var name = splitURL[splitURL.length-1];
  142. var savePath = _this.comicPath+_this.nowEp+'/'+name;
  143. fs.writeFileSync(savePath,data,'binary');
  144. data=null;
  145. _this.imgDownloaded++;
  146. if(_this.imgDownloaded==_this.imgLength){
  147. _this.nowEp++;
  148. downloadEP();
  149. }else{
  150. saveImg();
  151. }
  152. }else{
  153. if(_this.retryTimes[imgSrc]==3){
  154. console.log('Comic [ '+_this.comicOBJ.id+' ] image download error!!!');
  155. console.log('Image [ '+imgSrc+' ] error');
  156. }else{
  157. _this.retryTimes[imgSrc]++;
  158. saveImg();
  159. console.log('Image [ '+imgSrc+' ] redownload!');
  160. }
  161. }
  162. });
  163. }
  164. function finished(){
  165. console.log('[FINISH] Comic [ '+_this.comicOBJ.id+' ] All File Downloaded!!!');
  166. console.log('----------------------------------------------');
  167. delete _this;
  168. ComicHandler.start();
  169. }
  170. }
  171. module.exports = ComicHandler;