概要
ホットペッパーAPIを利用して近隣店舗の情報を取得する。
ホットペッパーAPI
ホットペッパーAPIとはリクルートが提供するOPENAPI。ホットペッパーとホットペッパーBeautyの2種類のAPIが提供されているが、今回はホットペッパーのAPIを利用していきます。
※ホットペッパーAPIの公式リファレンス
準備
ホットペッパーAPIを利用するには利用申請が必要となります。
リンクより新規登録し、APIキーを取得してください。申請からAPIキー取得までのリードタイムは約10分程度です。
実装
ライブラリインストール
~/develop/study/node/hotpepper $ yarn add fs xml2json node-fetch
ディレクトリ構造
~/develop/study/node/hotpepper $ tree -I node_modules
.
├── hotpepper.js
├── package.json
└── yarn.lock
0 directories, 3 files
hotpepper.js
const fs = require('fs');
const parser = require('xml2json');
const fetch = require('node-fetch');
const API_KEY = 'HOTPEERのAPIキー'
/**APIのURL */
const URL = 'http://webservice.recruit.co.jp/hotpepper/gourmet/v1/?key=' + API_KEY + '&large_area=Z011'
/** 非同期処理 */
async function main() {
try {
const res = await fetch(URL);
if (!res.ok) {
throw new Error(`${res.status} ${res.statusText}`);
}
const data = await res.text();
var json = parser.toJson(data);
const obj = JSON.parse(json)
console.log('xmlns:' +obj.results.xmlns)
console.log('店舗名:' +obj.results.shop[0].name)
console.log('住所:' +obj.results.shop[0].address)
} catch (err) {
console.error(err);
}
}
main();
実行結果
~/develop/study/node/hotpepper $ node hotpepper.js
xmlns:http://webservice.recruit.co.jp/HotPepper/
店舗名:さーて 四ッ谷店
住所:東京都新宿区四谷1-7-15 山田ビル2F