Last active
January 7, 2023 07:34
-
-
Save cognitom/df07d602f9f372b9d7be7d05c0366843 to your computer and use it in GitHub Desktop.
openBDの全データを整形しつつ単一ファイルにダウンロード
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) // ファイルに保存 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
準備。Node v7.4.0 で、次のライブラリをインストールしておきます。
ダウンロード開始。10分かもうちょっとかかります。