-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathart-scraper.js
executable file
·315 lines (263 loc) · 9.19 KB
/
art-scraper.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
"use strict";
const { mkdirp } = require('mkdirp');
const ALBUM = 1;
const ALBUMS = 'Albums';
const fs = require('fs'),
RoonApi = require('node-roon-api'),
RoonApiSettings = require('node-roon-api-settings'),
RoonApiStatus = require('node-roon-api-status'),
RoonApiBrowse = require('node-roon-api-browse'),
RoonApiImage = require('node-roon-api-image');
var core = undefined;
var album_index = 0;
var album_list = [];
var skip_counter = 0;
var total_albums = 0;
var roon = new RoonApi({
extension_id: 'com.epochaudio.coverwall',
display_name: 'Roon封面艺术墙',
display_version: '2.0.5',
publisher: '门耳朵',
email: '[email protected]',
website: 'https://www.epochaudio.cn',
core_paired: function(core_) {
core = core_;
console.log('Core paired:', core.display_name);
},
core_unpaired: function(core_) {
core = undefined;
console.log('Core unpaired');
}
});
var scrape_settings = roon.load_config("settings") || {
image_size: 'MEDIUM',
max_images: 1000
};
const IMAGE_SIZES = {
SMALL: {
width: 225,
height: 225
},
MEDIUM: {
width: 500,
height: 500
},
LARGE: {
width: 1000,
height: 1000
}
};
function makelayout(settings) {
let l = {
values: settings,
layout: [],
has_error: false
};
if (album_index == album_list.length) {
l.layout.push({
type: "dropdown",
title: "Image Size",
values: [
{ title: "Small (225x225)", value: "SMALL" },
{ title: "Medium (500x500)", value: "MEDIUM" },
{ title: "Large (1000x1000)", value: "LARGE" }
],
setting: "image_size"
});
l.layout.push({
type: "integer",
title: "最大图片数量",
min: 1,
max: 10000,
setting: "max_images"
});
} else {
l.layout.push({
type: 'group',
title: 'There is currently an action in progress, try again later',
items: []
});
}
return l;
}
function refresh_browse(opts, path, cb) {
opts = Object.assign({ hierarchy: "browse" }, opts);
core.services.RoonApiBrowse.browse(opts, (err, r) => {
if (err == false) {
if (r.action == "list") {
let list_offset = r.list.display_offset > 0 ? r.list.display_offset : 0;
console.log(`正在加载专辑列表... 偏移量: ${list_offset}`);
load_browse(list_offset, path, cb);
}
}
});
}
function load_browse(list_offset, path, cb) {
let opts = {
hierarchy: "browse",
offset: list_offset,
set_display_offset: list_offset
};
core.services.RoonApiBrowse.load(opts, (err, r) => {
if (err == false && path) {
if (!r.list.level || !path[r.list.level - 1] || r.list.title == path[r.list.level - 1]) {
if (!path[r.list.level]) {
if (cb) {
console.log(`已加载 ${r.items.length} 张专辑,总共 ${r.list.count} 张`);
if (r.offset + r.items.length < r.list.count) {
load_browse(r.offset + r.items.length, path, cb);
}
cb(r.items, r.offset + r.items.length == r.list.count);
}
} else {
for (let i = 0; i < r.items.length; i++) {
if (r.items[i].title == path[r.list.level]) {
refresh_browse({ item_key: r.items[i].item_key }, path, cb);
break;
}
}
}
}
}
});
}
function scrape_library() {
console.log(`Starting scrape for albums`);
scrape_albums();
}
function scrape_albums() {
const ALBUM_DIR = process.env.ALBUM_DIR || '/app/art/Albums';
try {
fs.accessSync(ALBUM_DIR, fs.constants.W_OK);
startScraping();
} catch (err) {
console.error(`错误: 目录 ${ALBUM_DIR} 不存在或没有写入权限`);
svc_status.set_status("目录访问错误", true);
return;
}
}
function startScraping() {
const opts = {pop_all: true};
const path = ['Library', ALBUMS, ''];
refresh_browse(opts, path, (items, done) => {
const remaining = scrape_settings.max_images - album_list.length;
if (remaining > 0) {
const itemsToAdd = items.slice(0, remaining);
album_list = album_list.concat(itemsToAdd);
}
if (done || album_list.length >= scrape_settings.max_images) {
total_albums = album_list.length;
console.log(`总共将处理 ${total_albums} 张专辑(最大限制:${scrape_settings.max_images})`);
if (album_list.length > 0) {
store_image(ALBUMS, album_list[album_index].title, album_list[album_index].image_key, store_next_image);
}
}
});
}
function store_next_image(type, error) {
let entry;
let fraction;
console.log(`Processing ${type}, error: ${error}`);
if (album_index >= album_list.length) {
if (skip_counter) {
svc_status.set_status(`Scraping done! (${skip_counter} skipped)`, false);
} else {
svc_status.set_status('Scraping done!', false);
}
reset_state();
return;
}
entry = album_list[album_index];
fraction = album_index / total_albums;
console.log(`Album progress: ${album_index + 1}/${total_albums}`);
album_index++;
if (entry) {
set_progress(type, Math.round(fraction * 100));
store_image(type, entry.title, entry.image_key, store_next_image);
}
}
function store_image(type, name, image_key, cb) {
if (image_key) {
const size = IMAGE_SIZES[scrape_settings.image_size] || IMAGE_SIZES.MEDIUM;
const opts = {
scale: 'fill',
width: size.width,
height: size.height,
format: 'image/jpeg'
};
name = name.replace(/[\?\/\"<>:]/g, '_');
const MAX_RETRIES = 3;
let retryCount = 0;
const tryDownload = () => {
core.services.RoonApiImage.get_image(image_key, opts, (error, content_type, image) => {
if (error) {
if (retryCount < MAX_RETRIES) {
retryCount++;
console.log(`Retry ${retryCount} for ${name}`);
setTimeout(tryDownload, 1000 * retryCount);
return;
}
console.error(`Failed after ${MAX_RETRIES} retries:`, name, error);
skip_counter++;
cb && cb(type, error);
} else {
fs.writeFile(`${process.cwd()}/art/${type}/${name}.jpg`, image, () => {
cb && cb(type);
});
}
});
};
tryDownload();
} else {
skip_counter++;
cb && cb(type);
}
}
function set_progress(type, progress) {
svc_status.set_status(`Scraping library for ${type}... [${progress}%]`, false);
}
function init_signal_handlers() {
const handle = function(signal) {
process.exit(0);
};
// Register signal handlers to enable a graceful stop of the container
process.on('SIGTERM', handle);
process.on('SIGINT', handle);
}
function reset_state() {
album_index = 0;
album_list = [];
skip_counter = 0;
svc_status.set_status("Ready", false);
}
var svc_settings = new RoonApiSettings(roon, {
get_settings: function(cb) {
cb(makelayout(scrape_settings));
},
save_settings: function(req, isdryrun, settings) {
let l = makelayout(settings.values);
req.send_complete(l.has_error ? "NotValid" : "Success", { settings: l });
if (!isdryrun && !l.has_error) {
scrape_settings = l.values;
svc_settings.update_settings(l);
roon.save_config("settings", scrape_settings);
// 同时保存到.roondata目录
const fs = require('fs').promises;
const path = require('path');
const roonSettingsPath = path.join(process.cwd(), '.roondata/settings.json');
fs.mkdir(path.dirname(roonSettingsPath), { recursive: true })
.then(() => fs.writeFile(roonSettingsPath, JSON.stringify(scrape_settings, null, 2)))
.then(() => console.log('设置已保存到.roondata目录'))
.catch(err => console.error('保存设置到.roondata目录失败:', err));
scrape_library();
}
}
});
var svc_status = new RoonApiStatus(roon);
roon.init_services({
required_services: [ RoonApiBrowse, RoonApiImage ],
provided_services: [ svc_settings, svc_status ]
});
reset_state();
init_signal_handlers();
roon.start_discovery();