Skip to content

Instantly share code, notes, and snippets.

@eternalphane
Created October 5, 2019 14:22
Show Gist options
  • Save eternalphane/032cd39d1391cb0eeb59c6fa62be0add to your computer and use it in GitHub Desktop.
Save eternalphane/032cd39d1391cb0eeb59c6fa62be0add to your computer and use it in GitHub Desktop.
xiaozhan_toefl_tpo resource downloader (小站托福TPO资源下载)
import fs from 'fs';
import sqlite from 'sqlite';
import fetch from 'node-fetch';
(async () => {
const db = await sqlite.open('C:\\Program Files (x86)\\xiaozhan_toefl_tpo\\resources\\cache\\ylk.db');
const tags = (await db.all('SELECT DISTINCT tag FROM t_download_info WHERE tag LIKE "tpo%"')).map(row => row.tag);
for (const tag of tags) {
if (fs.existsSync(tag)) {
continue;
}
fs.mkdirSync(tag);
const rows = await db.all(`SELECT name, url FROM t_download_info WHERE tag = '${tag}'`);
await Promise.all(rows.map(({ name, url }) => {
if (fs.existsSync(`${tag}\\${name}`)) {
return;
}
const file = fs.createWriteStream(`${tag}\\${name}`);
fetch(url, { headers: { Referer: 'http://app.zhan.com/' } }).then(r => r.body.pipe(file));
return new Promise((resolve) => {
file.on('finish', () => {
file.close();
console.log(`${tag}\\${name}`);
resolve();
});
});
}));
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment