-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
191 lines (154 loc) · 5.9 KB
/
index.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
const fs = require('fs');
const request = require('request');
const program = require('commander');
const RSS = require('rss');
const util = require('./util');
program
.command('run <out-file>')
.option('-s --session_id <session_id>', 'Session ID.')
.option('-r --rank_type <rank_type>', 'Rank type: daily(Default), weekly, monthly, rookie, original, male, female')
.option('-t --content_type <content_type>', 'Content type: all(Default), illust, manga, ugoira, novel')
.option('-x --with_r18', 'With R18 content; Default off.')
.option('-d --date <date>', 'YYYY-MM-DD; Default to today.')
.option('-c --count <count>', 'Count to fetch; Default to 50.')
.option('--title <title>', 'Define title of generated RSS feed; Default is "Pixiv RSS".')
.option('--image_src_prefix <image_src_prefix>', 'Prefix for image src; Default is empty.')
.action((outFile, options) => {
options = regularizeOptions(options);
getEntries(options, (error, entries) => {
if (error) {
console.error(error);
return;
}
save(entries, outFile, options);
});
});
const getHeaders = (options) => {
const headers = {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/602.2.14 (KHTML, like Gecko) Version/10.0.1 Safari/602.2.14',
'Origin': 'http://www.pixiv.net',
'Referer': 'http://www.pixiv.net',
};
if (options && options.session_id) {
headers['Cookie'] = `PHPSESSID=${options.session_id}`;
}
return headers;
};
const regularizeOptions = (options) => {
const now = new Date();
const result = Object.assign({}, {
rank_type: 'daily',
content_type: 'all',
with_r18: false,
date: `${now.getFullYear()}-${now.getMonth() + 1}-${now.getDate()}`,
count: 50,
title: 'Pixiv RSS',
image_src_prefix: '',
}, options);
result.count = parseInt(result.count, 10);
return result;
};
const extractEntries = (html, limit) => {
const pattern = /data-entry-id="(.+?)".+?data-src="(.+?)".+?data-user-name="(.+?)".+?data-caption="(.+?)".+?data-permalink="(.+?)"/g;
const entries = [];
while (match = pattern.exec(html)) {
let src = match[2].replace(/&/g, '&');
src = src.replace('400x400_80_a2', '540x540_70');
src = src.replace('square1200', 'master1200');
const dateMatch = src.match(/\/img\/(\d+)\/(\d+)\/(\d+)\/(\d+)\/(\d+)\/(\d+)\//);
entries.push({
id: match[1],
src,
userName: match[3],
caption: match[4],
link: match[5].replace(/&/g, '&'),
date: new Date(
parseInt(dateMatch[1], 10),
parseInt(dateMatch[2], 10) - 1,
parseInt(dateMatch[3], 10),
parseInt(dateMatch[4], 10),
parseInt(dateMatch[5], 10),
parseInt(dateMatch[6], 10)
),
});
if (limit && entries.length >= limit) {
break;
}
}
return entries;
};
const getEntries = (options, callback) => {
const urlBase = `https://www.pixiv.net/rpc/whitecube/index.php`;
const headers = getHeaders(options);
once = (result, page, total) => {
const paramString = util.getParamString({
mode: 'ranking',
rank_type: options.rank_type,
content_type: options.content_type,
date: options.date,
page_x_restrict: options.with_r18 ? 1 : 0,
page,
});
request({
url: `${urlBase}?${paramString}`,
headers,
}, ((error, response, body) => {
if (error) {
callback(error);
return;
}
if (response.statusCode !== 200) {
callback(new Error(`Request failed with status code: ${response.statusCode}\n${body}`));
return;
}
const json = JSON.parse(body);
const entries = extractEntries(json.body.html, options.count === 0 ? undefined : options.count - total);
result.push.apply(result, entries);
total += entries.length;
if (entries.length === 0 || (options.count !== 0 && total >= options.count)) {
result.splice(options.count);
callback(null, result);
return;
}
once(result, page + 1, total);
}));
};
once([], 1, 0);
};
const save = (entries, outFilePath, options) => {
const outPathMath = outFilePath.match(/^(.+)\//);
const outPath = outPathMath ? outPathMath[1] : './';
const feed = new RSS({
title: options.title,
site_url: 'http://pixiv.net',
image_url: 'http://www.pixiv.net/favicon.ico',
language: 'jp',
pubDate: new Date(),
ttl: 60,
});
const headers = getHeaders();
const total = entries.length;
let count = 0;
entries.forEach((entry, i) => {
const imageFilePath = `images/` + entry.src.replace(/^.+\/img\/(.+)$/, '$1');
util.downloadImage(entry.src, headers, `${outPath}/${imageFilePath}`, () => {
feed.item({
guid: entry.id,
title: entry.caption,
description: `<img src="${options.image_src_prefix}${imageFilePath}" /><br /><br />${entry.caption} by ${entry.userName}`,
url: entry.link,
author: entry.userName,
date: entry.date
});
count++;
console.log(`Entry saved(${count}/${total}): ${entry.id}: ${entry.caption}.`);
if (count >= total) {
fs.writeFileSync(outFilePath, feed.xml({ indent: true }));
console.log(`Done.
XML file saved in ${outFilePath}.
Images saved in ${outPath}/images.`);
}
});
});
};
program.parse(process.argv);