123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184 |
- var $ = require("cheerio");
- var request = require('request');
- var agent = require("socks5-http-client/lib/Agent");
- var fs = require('fs');
- var ListHandler = require('./listhandler.js');
- require('./config.js');
- var ComicHandler = {
- NowIndex:0,
- ListTree:[],
- start:function(fromComic){
- // console.log(this);
- // return;
- if(!this.ListTree.length) this.ListTree = ListLib.tree;
- var fromEp = 1;
- if(fromComic){
- var fromComicID = fromComic.id;
- if(fromComicID){
- for(var i in this.ListTree){
- if(this.ListTree[i]==fromComicID){
- this.NowIndex = i;
- break;
- }
- }
- }
- if(fromComic.ep) fromEp = fromComic.ep;
- }
- if(this.NowIndex==this.ListTree.length){
- console.log('Comic Download Complete');
- }else{
- var comicID = this.ListTree[this.NowIndex];
- new ComicSaver(comicID,fromEp);
- this.NowIndex++;
- }
- }
- }
- var ComicSaver = function(comicID,fromEp){
- var _this=this;
- _this.ep = 0;
- _this.nowEp = 0;
- _this.imgLength = 0;
- _this.imgDownloaded = 0;
- _this.retryTimes = {};
- _this.imgLib = [];
- // console.log(this);
- // return;
- _this.comicOBJ = ListLib[comicID];
- if(!_this.comicOBJ){
- console.log('ComicID '+comicID+' wrong!');
- return;
- }
- console.log('----------------------------------------------');
- console.log('[BEGIN] Start Download Comic [ '+comicID+' ]');
- console.log('Creating directory for [ '+_this.comicOBJ.name+' ]');
- _this.comicPath = './save/'+_this.comicOBJ.id+'-'+_this.comicOBJ.name+'/';
- if(!fs.existsSync(_this.comicPath)){
- fs.mkdirSync(_this.comicPath);
- }
- var options = {
- url:ApiURL.comicEp.replace('{ComicID}',_this.comicOBJ.id)
- }
- console.log('Analysing......');
- request(options,function(error,response,body){
- if(!error && body && response.statusCode==200){
- var data;
- try{
- data = JSON.parse(body);
- }catch(e){
- console.log(e,body);
- new ComicSaver(ComicID,fromEp);
- delete _this;
- return;
- // downloadEP();
- }
- _this.ep = data['ep_count'];
- _this.nowEp = fromEp?fromEp:1;
- console.log('Analysie Success!');
- console.log('Comic [ '+_this.comicOBJ.id+' ] has [ '+_this.ep+' ] EP!!!');
- downloadEP();
- }else{
- console.log('Comic [ '+_this.comicOBJ.id+' ] analysie error!!! Retry!!!');
- new ComicSaver(ComicID,fromEp);
- delete _this;
- return;
- }
- });
- function downloadEP(){
- if(_this.nowEp>_this.ep){
- finished();
- return;
- }
- var opt = {
- url:ApiURL.comic.replace('{ComicID}',_this.comicOBJ.id).replace('{EP}',_this.nowEp)
- }
- console.log('Comic [ '+_this.comicOBJ.id+' ] EP [ '+_this.nowEp+' ]start download!!!');
- var epPath = _this.comicPath+_this.nowEp+'/';
- if(!fs.existsSync(epPath)){
- fs.mkdirSync(epPath);
- }
- request(opt,function(error,response,body){
- // console.log(body);
- if(!error && body && response.statusCode==200){
- var data;
- try{
- data = JSON.parse(body);
- }catch(e){
- console.log(e,body);
- downloadEP();
- return;
- }
- _this.imgLength = data.length;
- _this.imgDownloaded = 0;
- console.log('Comic [ '+_this.comicOBJ.id+' ] EP [ '+_this.nowEp+' ] has [ '+_this.imgLength+' ] images!!!');
- console.log('Comic [ '+_this.comicOBJ.id+' ] EP [ '+_this.nowEp+' ] images start download!!!');
- for(var i in data){
- _this.retryTimes[data[i]['url']] = 0;
- // saveImg(data[i]['url']);
- _this.imgLib.push(data[i]['url']);
- }
- saveImg();
- }else{
- console.log('Comic [ '+_this.comicOBJ.id+' ] EP [ '+_this.nowEp+' ] download error!!!Retry!!!');
- downloadEP();
- }
- });
- }
- function saveImg(){
- var imgSrc = _this.imgLib[_this.imgDownloaded];
- if(!imgSrc){
- console.log('Comic [ '+_this.comicOBJ.id+' ] image download error!!!');
- console.log('Image [ '+imgSrc+' ] error');
- }
- var splitURL = imgSrc.split('/');
- var opt = {
- url:imgSrc,
- encoding:'binary',
- timeout:6000
- }
- request(opt,function(error,response,body){
- if(!error && body){
- var data = body;
- var name = splitURL[splitURL.length-1];
- var savePath = _this.comicPath+_this.nowEp+'/'+name;
- fs.writeFileSync(savePath,data,'binary');
- data=null;
- _this.imgDownloaded++;
- if(_this.imgDownloaded==_this.imgLength){
- _this.nowEp++;
- downloadEP();
- }else{
- saveImg();
- }
- }else{
- if(_this.retryTimes[imgSrc]==3){
- console.log('Comic [ '+_this.comicOBJ.id+' ] image download error!!!');
- console.log('Image [ '+imgSrc+' ] error');
- }else{
- _this.retryTimes[imgSrc]++;
- saveImg();
- console.log('Image [ '+imgSrc+' ] redownload!');
- }
-
- }
- });
- }
- function finished(){
- console.log('[FINISH] Comic [ '+_this.comicOBJ.id+' ] All File Downloaded!!!');
- console.log('----------------------------------------------');
- delete _this;
- ComicHandler.start();
- }
- }
- module.exports = ComicHandler;
|