-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
211 lines (204 loc) · 7.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
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
211
const fs = require('fs');
const path = require('path');
const https = require('https');
const puppeteer = require('puppeteer');
(async () => {
let userId = '';
if (process.argv.length >= 3) {
userId = process.argv[2];
}
if (!userId) return;
const browser = await puppeteer.launch({
headless: false,
defaultViewport: null,
userDataDir: path.join(__dirname, 'profile'),
// args: ['--proxy-server=127.0.0.1:7890']
});
const page = await browser.newPage();
await page.goto(`https://${userId}.fanbox.cc`);
const cookies = await page.cookies('https://fanbox.cc/');
const cookieObj = {};
for (let i = 0; i < cookies.length; i++) {
const cookie = cookies[i];
cookieObj[cookie.name] = cookie.value;
}
let cookieValue = '';
for (let key in cookieObj) {
if (cookieValue !== '') cookieValue += '; ';
cookieValue += `${key}=${cookieObj[key]}`;
}
const list = await page.evaluate(async (id) => {
try {
return (await (await fetch(`https://api.fanbox.cc/post.listCreator?creatorId=${id}&limit=300`)).json()).body;
} catch (e) {
return null;
}
}, userId);
if (list && list.items) {
const dir = path.join(__dirname, 'data', userId);
if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true });
const items = list.items;
fs.writeFileSync(path.join(dir, `list.json`), JSON.stringify(items, null, 2));
for (let i = 0; i < items.length; i++) {
const item = items[i];
const jsonPath = path.join(dir, `${item.id}_${item.updatedDatetime.substr(0, 10)}.json`);
let data = null;
if (fs.existsSync(jsonPath)) {
data = JSON.parse(fs.readFileSync(jsonPath, 'utf8'));
} else {
data = await page.evaluate(async (id) => {
try {
return (await (await fetch(`https://api.fanbox.cc/post.info?postId=${id}`, {
credentials: "include"
})).json()).body;
} catch (e) {
return null;
}
}, item.id);
fs.writeFileSync(jsonPath, JSON.stringify(data, null, 2));
await sleep(3 * 1000);
}
if (data) {
if (data.body) {
const textPath = path.join(dir, `${item.id}_${item.updatedDatetime.substr(0, 10)}_${item.title.replace(/[?\\\/\*|]/g, '')}.txt`);
if (!fs.existsSync(textPath)) {
// TEXT
let text = '';
if (data.type === 'video' && data.body.video) {
if (data.body.video.serviceProvider === 'youtube') {
text += `[https://www.youtube.com/watch?v=${data.body.video.videoId}]\n`;
} else {
console.log(`${item.id}: UNKNOWN VIDEO PROVIDER: ${data.body.video.serviceProvider}`);
}
}
if (data.type === 'image' && data.body.images) {
for (let key in data.body.images) {
const image = data.body.images[key];
text += `[image][${image.id}]\n`;
}
}
if (data.body.text) {
text += data.body.text;
} else if (data.body.blocks) {
for (let i = 0; i < data.body.blocks.length; i++) {
const block = data.body.blocks[i];
if (text != '') text += '\n';
if (block.type === 'p' || block.type === 'header') {
text += block.text;
} else if (block.type === 'image') {
text += `[${block.type}][${block.imageId}]`;
} else if (block.type === 'file') {
text += `[${block.type}][${block.fileId}]`;
} else if (block.type === 'embed') {
if (data.body.embedMap) {
const embed = data.body.embedMap[block.embedId];
if (embed) {
if (embed.serviceProvider === 'youtube') {
text += `[${block.type}][https://www.youtube.com/watch?v=${embed.contentId}]`;
} else if (embed.serviceProvider === 'twitter') {
text += `[${block.type}][https://twitter.com/user/status/${embed.contentId}]`;
} else if (embed.serviceProvider === 'fanbox') {
text += `[${block.type}][https://www.pixiv.net/fanbox/${embed.contentId}]`;
} else {
console.log(`${item.id}: UNKNOWN EMBED PROVIDER: ${embed.serviceProvider}`);
}
} else {
console.log(`${item.id}: UNKNOWN EMBED: ${block.embedId}`);
}
}
} else {
text += `[${block.type}]`;
console.log(`${item.id}: UNKNOWN BLOCK TYPE: ${block.type}`);
}
}
}
fs.writeFileSync(textPath, text);
}
// IMAGE
const images = data.body.images || data.body.imageMap;
if (images) {
for (let key in images) {
const obj = images[key];
const filePath = path.join(dir, `${item.id}_${obj.id}.${obj.extension}`);
if (!fs.existsSync(filePath)) {
const buffer = await rdownload(obj.originalUrl, cookieValue);
fs.writeFileSync(filePath, buffer);
}
}
}
// FILE
const files = data.body.files || data.body.fileMap;
if (files) {
for (let key in files) {
const obj = files[key];
const filePath = path.join(dir, `${item.id}_${obj.name}.${obj.extension}`);
if (!fs.existsSync(filePath)) {
const buffer = await rdownload(obj.url, cookieValue);
fs.writeFileSync(filePath, buffer);
}
}
}
} else {
fs.unlinkSync(jsonPath);
console.log(`${item.id}: NO DATA BODY: ${item.feeRequired}_${item.title}`);
}
} else {
console.log(`${item.id}: NO DATA`);
}
// COVER
if (data.coverImageUrl) {
const extname = path.extname(data.coverImageUrl);
const filePath = path.join(dir, `${item.id}_cover${extname}`);
if (!fs.existsSync(filePath)) {
const buffer = await rdownload(data.coverImageUrl);
fs.writeFileSync(filePath, buffer);
}
}
}
}
await page.close();
await browser.close();
})();
function sleep(timeout) {
return new Promise(resolve => {
setTimeout(resolve, timeout);
});
}
async function rdownload(url, cookie) {
while (true) {
try {
return await download(url, cookie);
} catch (e) {
console.log(e.message);
}
}
}
function download(url, cookie) {
return new Promise((resolve, reject) => {
const req = https.request(url, {
headers: {
'accept': '*/*',
'accept-encoding': 'gzip, deflate, br',
'accept-language': 'ja',
'cookie': cookie || null,
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36 Edg/88.0.705.63'
}
}, (res) => {
if (res.statusCode === 200) {
const arr = [];
res.on('data', (chunk) => {
arr.push(chunk);
});
res.on('end', () => {
resolve(Buffer.concat(arr));
});
} else {
reject(new Error(`STATUSCODE: ${res.statusCode}`));
}
});
req.on('error', (e) => {
reject(e);
});
req.end();
});
};