1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

hubotでぐるなびAPIからお店を検索する

Posted at

別サイト

に書いていたのだけどサ終してしまったので移行した記事その3
2019/03/07のもの

ぐるなびAPI

ぐるなびAPI
を使うとお店の検索をしたり口コミ情報を取得したりできる

ランダムで指定の地域のお店を出すhubotスクリプト

動いたのでメモ

API_URL     = 'https://api.gnavi.co.jp/RestSearchAPI/v3/?'
API_KEY       = '[APIキー]'
address     = '[デフォルトの地名(東京都渋谷区とか)]'

module.exports = (robot) ->
  robot.respond /お店 ?(.*)$/i, (msg) ->
    attr = msg.match[1].trim()
    address = attr if attr != ""
    url = "#{API_URL}keyid=#{API_KEY}&hit_per_page=1&offset_page=1&address=#{encodeURIComponent(address)}"

    # 対象の件数をとるために2回通信する
    robot.http(url)
      .get() (err, response, body) ->
        data1 = JSON.parse body
        totalCount = data1.total_hit_count
        offsetPage = Math.floor Math.random() * totalCount
        # v3のAPIだとoffsetが1001以上だとエラーになるので調整
        if totalCount > 1000
          offsetPage = Math.floor Math.random() * 1000

        url = "#{API_URL}keyid=#{API_KEY}&hit_per_page=1&offset_page=#{offsetPage}&address=#{encodeURIComponent(address)}"
        robot.http(url)
          .get() (err, response, body) ->
            data2 = JSON.parse body
            items = data2.rest[0]
            message = """
                      おすすめ: #{items.name}
                      カテゴリ: #{items.category}
                      住所: #{items.address}
                      URL: #{items.url}
                      """
            msg.send message

感想

APIも結構あるしパラメータがかなり細かく設定できるのでカスタムしがいはありそう

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?