-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathillust.js
210 lines (189 loc) · 5.98 KB
/
illust.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import 'babel-polyfill';
import login from './login';
import { loginRequired } from './login';
import cheerio from 'cheerio';
import Promise from 'Bluebird';
import _ from 'lodash';
import path from 'path';
import { cachedProperty,replacePlaceholder } from './utility';
import extend from 'extend';
const request = Promise.promisifyAll(require('request'));
const fs = Promise.promisifyAll(require('fs'));
const PAGE = 'http://www.pixiv.net/member_illust.php';
const TYPE = {
NORMAL: 0,
UGOIRA: 1,
MULTIPLE: 2
};
function validType(...types) {
const VALID_TYPES = Object.values(TYPE);
types.forEach(type => {
if (!VALID_TYPES.includes(type)) {
throw new Error('Unvalid types.');
}
});
return (target, prop, descriptor) => {
const method = descriptor.value;
descriptor.value = async function(...args){
let type = await this._type();
if (!types.includes(type)){
throw new Error('Not allowed in this type of illust.');
}
return method.call(this,args);
};
return descriptor;
};
}
class Illust {
constructor(id){
this.id = id;
}
getPageUrl(mode='medium'){
return `${PAGE}?mode=${mode}&illust_id=${this.id}`;
}
get url(){
return this.getPageUrl();
}
@cachedProperty
@loginRequired
async getContent(){
let response = await request.getAsync({
url: this.url,
headers: {
'Host': 'www.pixiv.net',
'Referer': 'http://www.pixiv.net/',
},
jar: login.cookieJar
});
return cheerio.load(response.body);
}
@cachedProperty
async getInfo(){
let $ = await this.getContent();
let $workInfo = $('.work-info');
let $metas = $workInfo.find('.meta li');
return {
id: this.id,
title: $workInfo.find('.title').text(),
author: $('.user').text().trim(),
tools: $workInfo.find('.tools li').map((i,elem) => {
return $(elem).text().trim();
}).get(),
page: this.url,
date: $metas.eq(0).text()
};
}
@cachedProperty
async _type(){
let $ = await this.getContent();
if ($('.works_display .multiple').length) {
return TYPE.MULTIPLE;
} else if ($('._ugoku-illust-player-container').length) {
return TYPE.UGOIRA;
} else {
return TYPE.NORMAL;
}
}
@cachedProperty
@validType(TYPE.NORMAL)
async _getImageUrl(){
let $ = await this.getContent();
return $('.original-image').attr('data-src');
}
@cachedProperty
@validType(TYPE.MULTIPLE)
async _getNum(){
let $ = await this.getContent();
let text= $('.meta li').eq(1).text();
return +(text.match(/(\d+)P/) || [])[1] || 0;
}
@cachedProperty
@validType(TYPE.MULTIPLE)
async _getImageUrls(){
let num = await this._getNum();
return Promise.all(_.range(num).map(index =>
request.getAsync({
url: `${this.getPageUrl('manga_big')}&page=${index}`,
jar: login.cookieJar
})
)).then(responses => {
return responses.map(response => {
let $ = cheerio.load(response.body);
return $('img').attr('src');
});
});
}
@cachedProperty
@validType(TYPE.MULTIPLE)
async _guessImageUrls(){
let num = await this._getNum();
let response = await request.getAsync({
url: `${this.getPageUrl('manga_big')}&page=0`,
jar: login.cookieJar
});
let $ = cheerio.load(response.body);
let img0 = $('img').attr('src');
return _.range(num).map(index => img0.replace(/_p(\d+)\..*?$/,($,$1) => {
return $.replace(`_p${$1}`,`_p${index}`);
}));
}
@cachedProperty
@validType(TYPE.UGOIRA)
async _getPackUrl(){
let $ = await this.getContent();
let $script = $('script').filter((i,elem) => {
return $(elem).text().includes('pixiv.context.ugokuIllustFullscreenData');
});
let pixiv = (new Function('var pixiv={context:{}};' + $script.text() +';return pixiv;'))();
return pixiv.context.ugokuIllustFullscreenData.src;
}
async _getDownloadQueue(){
let type = await this._type();
let info = await this.getInfo();
if (type === TYPE.MULTIPLE) {
let urls = this._guessImageUrls();
return urls.map((url,index) => {
return extend({},info,{
title: `${info.title} - ${_.padStart(index+1, 2, '0')}`,
url: url,
suffix: path.extname(url)
});
});
}
let url;
if (type === TYPE.NORMAL) {
url = await this._getImageUrl();
}
if (type === TYPE.UGOIRA) {
url = await this._getPackUrl();
}
extend(info,{
url: url,
suffix: path.extname(url)
});
return [info];
}
async download(filepath='{{author}} - {{title}}{{suffix}}'){
let infos = await this._getDownloadQueue();
for (let info of infos) {
try {
let file = replacePlaceholder(filepath,info);
console.log(`Downloading ${path.basename(file)} ...`);
let resp = await request.getAsync({
url: info.url,
headers: {
'Referer': this.url
},
// do not encode the response data
// directly dumps to the files.
encoding: null,
jar: login.cookieJar
});
await fs.writeFileAsync(file,resp.body);
} catch(e) {
// do nothing
}
}
}
}
export default Illust;