Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
import {join} from 'path'
import {createWriteStream} from 'fs'
import request from 'request'
import {parse, stringify} from 'JSONStream'
import find from 'lodash.get'
import highland from 'highland'
const apiRoot = ''
const cwd = process.cwd()
const distFile = join(cwd, 'all.json')
.flatMap(url => highland(request(url))) // openBDに問い合わせ
.through(parse('*')) // jsonから、ISBNを取得
.batch(10000) // 10000件ごとにまとめる
.map(isbns => ({method: 'POST', url: `${apiRoot}/get`, form: {isbn: isbns.join(',')}}))
.flatMap(opts => highland(request(opts))) // openBDに問い合わせ
.through(parse('*')) // jsonから、書誌情報を取得
.map(book => Object.assign({}, book.summary, {
description: find(book, 'onix.CollateralDetail.TextContent[0].Text', ''),
price: find(book, 'onix.ProductSupply.SupplyDetail.Price[0].PriceAmount', '')
})) // 書誌情報を整形
.through(stringify()) // オブジェクトからjsonに
.pipe(createWriteStream(distFile)) // ファイルに保存

cognitom commented Jan 28, 2017

準備。Node v7.4.0 で、次のライブラリをインストールしておきます。

$ npm i JSONStream highland lodash.get request reify


$ node --require reify download.js

cognitom commented Jan 29, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment