Skip to content

Instantly share code, notes, and snippets.

@HakurouKen
Last active July 29, 2018 08:56
Show Gist options
  • Save HakurouKen/dcac044621bd525447c8688bd3ff1ed5 to your computer and use it in GitHub Desktop.
Save HakurouKen/dcac044621bd525447c8688bd3ff1ed5 to your computer and use it in GitHub Desktop.
Download bilibili 404 images.
const fs = require('fs');
const path = require('path');
const request = require('superagent');
const mkdirp = require('mkdirp');
async function getMetas() {
const response = await request.get('https://www.bilibili.com/activity/web/view/data/31');
const { body } = response;
if (body.code === 0) {
return body.data.list;
}
throw response;
}
async function download(url, dest) {
mkdirp.sync(path.dirname(dest));
const fullUrl = url.indexOf('//') === 0 ? `https:${url}` : url;
const buffer = await request.get(fullUrl).buffer();
fs.writeFileSync(dest, buffer.body);
return buffer.body;
}
async function wait(time = 1000) {
return await new Promise(resolve => {
setTimeout(() => { resolve() }, time);
});
}
async function main(folder = './bilibili-404s') {
const metas = await getMetas();
for (let i = 0; i < metas.length; i++) {
const meta = metas[i];
const url = meta.data.img;
const suffix = path.extname(url);
await download(url, path.resolve(folder, meta.id + suffix));
await wait(1000);
console.log(`${meta.id}${suffix} downloaded (${i + 1}/${metas.length}).`);
}
}
module.exports = main;
if (require.main === module) {
const folder = process.argv[2];
main(folder);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment