-
Notifications
You must be signed in to change notification settings - Fork 0
/
update-favorites-delta.js
51 lines (40 loc) · 2.01 KB
/
update-favorites-delta.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
// ${host}-favorites-delta.json is holding all the posts from your favorite boards but the images are NOT downloaded yet
// this will update ${host}-favorites-delta.json after you downloaded the images from your favorite boards
const axios = require('axios').default
const fs = require('fs');
const path = require('path');
const { exit } = require('process');
const { promisify } = require('util');
const exec = promisify(require('child_process').exec);
if (!process.argv[2]) {
console.error('Moebooru host required! I need to know which <host>-favorites-delta.json file to get posts.');
console.error('Example: https://konachan.com');
exit(1)
}
const dir = process.argv[3];
if (!dir) {
console.error('Input directory required! I will check all the images in that directory and remove them from <host>-favorites-delta.json.');
exit(1)
}
const host = new URL(process.argv[2]);
const newPostsFilename = path.join('data', `${host.hostname}-favorites-delta.json`);
const newlyAddedPosts = JSON.parse(fs.readFileSync(newPostsFilename));
const oldLength = Object.keys(newlyAddedPosts).length;
// list files in directory
let filenames = fs.readdirSync(dir).filter(file => file.match(/\d+/));
const filteredOut = filenames.filter(filePath => !(filePath.endsWith(".jpg") || filePath.endsWith(".jpeg") || filePath.endsWith(".png"))).map(filePath => filePath.match(/\d+/)[0]);
filenames = filenames.filter(filePath => filePath.endsWith(".jpg") || filePath.endsWith(".jpeg") || filePath.endsWith(".png"))
const files = filenames.map(file => ({
filePath: path.format({ base: file, dir }),
id: file.match(/\d+/)[0],
filename: `${file}`
}));
files.forEach(file => {
const post = newlyAddedPosts[file.id];
if (post) {
delete newlyAddedPosts[file.id];
}
})
const newLength = Object.keys(newlyAddedPosts).length;
console.log(`${host.hostname}-favorites-delta.json updated: deleted`, oldLength - newLength, 'posts', 'with', newLength, 'left');
fs.writeFileSync(newPostsFilename, JSON.stringify(newlyAddedPosts));