提交 8f8c9bf5 编写于 作者: W wizardforcel

2019-06-28 11:17:45

上级 75964221
// 需要 Image Magick 和 pngquant /*
apt install imagemagick
apt install pngquant
*/
var chp = require('child_process') var chp = require('child_process')
var fs = require('fs') var fs = require('fs')
var path = require('path') var path = require('path')
var os = require('os') var os = require('os')
function betterImg(img) { function betterImg(img, tmpDir) {
var tmpDir = os.tmpdir() tmpDir = tmpDir || os.tmpdir()
var fname = new Date().getTime().toString() + '.jpg' var fname = new Date().getTime().toString() + '.jpg'
fname = path.join(tmpDir, fname) fname = path.join(tmpDir, fname)
fs.writeFileSync(fname, img) fs.writeFileSync(fname, img)
......
...@@ -46,7 +46,7 @@ function processImg(html, pageUrl, imgs) { ...@@ -46,7 +46,7 @@ function processImg(html, pageUrl, imgs) {
if(!imgs.has(picname)) { if(!imgs.has(picname)) {
var data = request('GET', url).getBody(); var data = request('GET', url).getBody();
data = betterImg(data) data = betterImg(data, 'tmp')
imgs.set(picname, data); imgs.set(picname, data);
} }
...@@ -89,6 +89,11 @@ function compToId(name) { ...@@ -89,6 +89,11 @@ function compToId(name) {
return id return id
} }
function safeMkDir(dir) {
try {fs.mkdirSync(dir)}
catch(ex) {}
}
function main() { function main() {
var name = process.argv[2] var name = process.argv[2]
...@@ -98,6 +103,7 @@ function main() { ...@@ -98,6 +103,7 @@ function main() {
var toc = getToc(id) var toc = getToc(id)
var articles = [] var articles = []
var imgs = new Map() var imgs = new Map()
safeMkDir('tmp')
for(var it of toc) { for(var it of toc) {
var prefix = 'https://www.kaggle.com' var prefix = 'https://www.kaggle.com'
...@@ -123,6 +129,7 @@ function main() { ...@@ -123,6 +129,7 @@ function main() {
articles.splice(0, 0, {title: `Kaggle Kernel - ${name}`, content: ''}) articles.splice(0, 0, {title: `Kaggle Kernel - ${name}`, content: ''})
genEpub(articles, imgs) genEpub(articles, imgs)
} }
if(module == require.main) main() if(module == require.main) main()
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册