Skip to content

Instantly share code, notes, and snippets.

@cognitom
Last active January 7, 2023 07:34
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cognitom/df07d602f9f372b9d7be7d05c0366843 to your computer and use it in GitHub Desktop.
Save cognitom/df07d602f9f372b9d7be7d05c0366843 to your computer and use it in GitHub Desktop.
openBDの全データを整形しつつ単一ファイルにダウンロード
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 = 'https://api.openbd.jp/v1'
const cwd = process.cwd()
const distFile = join(cwd, 'all.json')
highland([`${apiRoot}/coverage`])
.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
Copy link
Author

cognitom commented Jan 28, 2017

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

$ npm i JSONStream highland lodash.get request reify

ダウンロード開始。10分かもうちょっとかかります。

$ node --require reify download.js

@cognitom
Copy link
Author

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