forked from iMyon/pixivPicker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
153 lines (148 loc) · 5.42 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/usr/bin/env:node
// Javascript File
//Filename: app.js
//Created: 2014-04-19 02:26:07
//Desc: 主程序入口,执行 node app.js
//Author: Myon, [email protected]
var fs = require('fs');
var path = require('path');
var mkdirp = require('mkdirp');
var argv = require('optimist').argv;
var storage = require('./lib/storage.js');
var download = require('./lib/download.js');
var config = require('./lib/config.js');
var pixiv = require('./lib/pixivLogin.js');
var getImages = require('./lib/getImagesArray.js');
if(argv.username){
config.pixiv.login.form.pixiv_id = argv.username;
}
if(argv.passwd){
config.pixiv.login.form.pass = argv.passwd;
}
//获取cookie 成功后发送getCookie信号
pixiv.login(config.pixiv.login.form);
if(config.pixiv.fetchUrl.match("http://www.pixiv.net/ranking.php")){
pixiv.on("getCookie",getImages.rankImages);
}
else{
pixiv.on("getCookie",getImages.authorImages);
}
getImages.on("getImages",function(images){
//处理每个图片下载完成信号
(function(){
var ccount = 0; //已完成数量
var succount = 0; //下载成功
var failcount = 0; //下载失败
download.on("finishADownload",function(image){
succount++;
ccount++;
image.complete = true;
console.log(" 下载成功 -> " + image.filename + " 剩余 " + (images.length - ccount));
if(ccount >= images.length){
download.emit("allFinished");
}
image.path = path.join(image.basePath,image.filename);
storage.writeLog(logFile,images); //每下载完一个 写入日志
});
download.on("hasDownloaded",function(image){
succount++;
ccount++;
console.log("已经下载过 -> " + image.filename + " 剩余 " + (images.length - ccount));
if(ccount >= images.length){
download.emit("allFinished");
}
});
//404错误处理
download.on('res404',function(image){
console.warn(" png下载 -> " + image.filename);
image.url = image.url.replace(/\.jpg/,".png");
image.filename = image.filename.replace(/\.jpg/,".png");
download.gen(image,path.join(image.basePath, image.filename),pixiv);
});
//全部下载完成信号处理
download.on('allFinished',function(){
storage.writeLog(logFile,images);
console.log("下载结束,成功 " + succount + " ,失败 " + failcount);
//显式结束进程
setTimeout(function(){
process.exit()
},2000);
});
//处理下载失败/相册下载
download.on('failDownload',function(image){
if((image.retryTime||0) < config.pixiv.maxRetryTime){
//下载次数+1
image.retryTime = (image.retryTime||0) + 1;
console.warn(" 重新下载 -> " + image.filename);
download.gen(image,path.join(image.basePath, image.filename),pixiv);
}
else{
//删除临时文件
fs.unlinkSync(path.join(image.basePath,image.filename));
// 尝试使用图册方式下载图片
if(!image.is_xiangce){ //如果是一个顶级对象
download.emit("xiangceDownload",image); //发送相册下载信号
}
else{
ccount++;
console.warn(" 下载失败 -> " + image.filename + " -> 已达到下载次数限制");
if(image.is_xiangce && image.xiangce && image.xiangce.length > 1){
image.complete = true;
image.xiangce.pop();
succount++;
console.log(" 下载成功 -> " + image.basePath
+ " 剩余 " + (images.length - ccount));
storage.writeLog(logFile,images);
}
else{
image.complete = false;
failcount++;
}
if(ccount >= images.length){
download.emit("allFinished");
}
}
}
});
//开始相册下载
download.on("xiangceDownload",function(image){
if(! image.xiangce){ //没有则创建并初始化
image.xiangce = [];
console.log("画册下载 -> " + image.filename)
// image.url = image.url.replace(/(\.gif|\.jpg|\.jpeg|\.png)$/,"_p" + 0 + ".jpg");
image.filename = image.url.match(/\/[^\/]+_p([^\/]+)$/)[1];
image.is_xiangce = true;
image.xiangce_init = true;
//创建目录
image.basePath = path.join(image.basePath,storage.formatFilename(image,config.pixiv.filenameFormat));
mkdirp.sync(image.basePath);
// 开始下载
download.gen(image,path.join(image.basePath, image.filename),pixiv);
}
else{
console.log("下载成功 -> " + path.join(
storage.formatFilename(image,config.pixiv.filenameFormat)
,image.filename
));
if(image.filename != image.xiangce[image.xiangce.length-1])
image.xiangce.push(image.filename);
storage.writeLog(logFile,images);
image.filename = image.xiangce.length + ".jpg";
image.url = image.url.replace(/_p\d+(\.gif|\.jpg|\.jpeg|\.png)$/,"_p"
+ image.filename);
download.gen(image,path.join(image.basePath, image.filename),pixiv);
}
});
})();//(闭包)
//放在后边执行,以免上面的on来不及接受信号
for(var k=0;k<images.length;k++){
if(images[k].is_xiangce)
download.emit("xiangceDownload", images[k]);
else
download.gen(images[k],path.join(images[k].basePath, images[k].filename),pixiv);
}
});
//超过时间就结束进程
setTimeout(function(){
process.exit()
},config.timeout);