forked from bambooom/douban-backup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb-notes-img-dl.js
62 lines (56 loc) · 2.24 KB
/
db-notes-img-dl.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
const fs = require('fs');
const path = require('path');
const {promisify} = require('util');
const download = require('image-downloader');
const {sleep} = require('./util');
// const pinyin = require("pinyin"); // no need to convert Chinese titles now
const writeFile = promisify(fs.writeFile);
const readFile = promisify(fs.readFile);
const readDir = promisify(fs.readdir);
const mkDir = promisify(fs.mkdir);
const DIR = './douban-notes-20210716';
// const DIR = './douban-notes-test'; // for test
const IMG_DIR = './assets/images';
const IMG_PREFIX = '/assets/images/'; // for my blog setting
(async () => {
const notesFiles = await readDir(DIR);
let COUNT = 0, FAILED_URLS = [];
for (const notesFile of notesFiles) {
let basename = path.basename(notesFile); // 2020-07-08-filename.md
basename = basename.match(/^\d{4}-\d{2}-\d{2}-(.+)\.md$/);
basename = basename[1]; // filename, no .md
basename = basename.replace(/\s/g, '-'); // `filename`, use as image folder name
let file = await readFile(path.resolve(__dirname, `${DIR}/${notesFile}`), 'utf8');
let imgs = [];
file = file.replace(/!\[(.*?)\]\((.+?)\)/g, function (whole, desc, url) {
imgs.push(url);
let name = url.match(/\/([^/]*\.(?:jpg|webp))$/); // last p12344.jpg
return `![${desc}](${IMG_PREFIX}${basename}/${name[1]})`; // last is pic name
});
if (imgs.length > 0) {
console.log(basename, ' has ', imgs.length, ' images.');
const imgDir = path.resolve(__dirname, path.join(IMG_DIR, basename));
await mkDir(imgDir, {recursive: true}); // mkdir for images
for (const url of imgs) {
await download.image({
url,
dest: imgDir,
})
.then(({filename}) => {
console.log('Saved to', filename);
COUNT++;
})
.catch(() => {
let msg = 'Failed to download ' + url + ' which belongs to ' + basename;
FAILED_URLS.push(msg);
});
if (COUNT % 100 === 0) {
console.log(`Already ${COUNT} images, wait for 3 seconds...`);
await sleep(3000);
}
}
await writeFile(path.resolve(__dirname, `${DIR}/${notesFile}`), file, 'utf8'); // write to the file
}
}
console.log(FAILED_URLS);
})();