CLOVER🍀

That was when it all began.

Google Books APIで、軽く書籍検索

Googleが提供している、Google Books APIを使うことである程度簡単に書籍検索が行えるように
なるらしいというお話。

APIは、こちら。

Using the API  |  Google Books APIs  |  Google Developers

実際のアクセス先URLやパラメーターの説明は、こちらを参照のこと。

https://developers.google.com/books/docs/v1/using?hl=ja#PerformingSearch

で、こちらを利用する簡単なGroovyスクリプトを書いてみました。
google-books-api-search.groovy

import groovy.json.JsonSlurper

def searchKeyword = args[0]

def searchType = 'intitle'

def apiUrl = 'https://www.googleapis.com/books/v1/volumes'

def searchParameter = URLEncoder.encode("${searchType}=${searchKeyword}", 'UTF-8')

def json = new URL("${apiUrl}?q=${searchParameter}").getText('UTF-8')

def result = new JsonSlurper()
    .parseText(json)

def items = result.items

if (!items) {
  System.out.println('search result, not found!!')
}

def books =
  items.collect { item ->
    def v = item.volumeInfo

    def isbn
    def isbn13 = v.industryIdentifiers.grep { it.type == 'ISBN_13' }


    if (!isbn13) {
      return  // ignore, if there is no ISBN13
    } else {
      isbn = isbn13.get(0).identifier
    }

    [
      title: v.title,
      subtitle: v.subtitle,
      authors: v.authors,
      publishedDate: v.publishedDate,
      description: v.description,
      isbn: isbn,
      pageCount: v.pageCount
    ]
  }

books.each { println(it) }

このスクリプトだと、「intitle」で検索を行います。

例えば、こんな感じ。

$ groovy google-books-api-search.groovy 'Spring Framework'
[title:SpringFramework4プログラミング入門, subtitle:null, authors:[掌田津耶乃], publishedDate:2014-08-01, description:Spring Tool Suite(STS)(統合開発ツール)、Spring DI(Springの核=DIフレームワーク)、Spring AOP(アスペクト指向プログラミング)、Resources & Properties(Springのコア技術)、Spring Data(データアクセス(JDBCおよびJPA))、Spring Boot(簡単!高速開発フレームワーク)。6つだけ覚えれば、君の武器になる!, isbn:9784798041568, pageCount:477]
[title:ホイットマン詩集(対訳), subtitle:アメリカ詩人選 2, authors:[ホイットマン,W.(ウォルト)], publishedDate:1997-03, description:null, isbn:9784003230978, pageCount:250]
[title:Spring3入門, subtitle:Javaフレームワーク・より良い設計とアーキテクチャ, authors:[長谷川裕一, 大野渉, 土岐孝平], publishedDate:2012-12-05, description:DIコンテナ、アスペクト指向プログラミング、凹型レイヤ、MVC、トランザクション管理、データアクセス...Javaによるソフトウェア開発のアイデアを実装し、エンジニアの想像力と技術力を増幅させるSpring Frameworkの魂を受け取れ。, isbn:9784774153803, pageCount:487]
[title:Head First SQL, subtitle:atama to karada de oboeru esukyūeru no kihon, authors:null, publishedDate:2008-06, description:イラストで理解するデータベースとSQL, isbn:9784873113692, pageCount:571]
[title:Cyber Warfare, subtitle:Building the Scientific Foundation, authors:[Sushil Jajodia, Paulo Shakarian, V.S. Subrahmanian, Vipin Swarup, Cliff Wang], publishedDate:2015-04-09, description:This book features a wide spectrum of the latest computer science research relating to cyber warfare, including military and policy dimensions. It is the first book to explore the scientific foundation of cyber warfare and features research from the areas of artificial intelligence, game theory, programming languages, graph theory and more. The high-level approach and emphasis on scientific rigor provides insights on ways to improve cyber warfare defense worldwide. Cyber Warfare: Building the Scientific Foundation targets researchers and practitioners working in cyber security, especially government employees or contractors. Advanced-level students in computer science and electrical engineering with an interest in security will also find this content valuable as a secondary textbook or reference., isbn:9783319140391, pageCount:321]
[title:パラレルワールド, subtitle:11次元の宇宙から超空間へ, authors:[ミチオカク], publishedDate:2006-01, description:ブラックホールへの決死の旅や、タイムマシン、もうひとつの宇宙、そして多次元空間―本書は宇宙論の世界を席捲する革新的な宇宙の姿を鮮やかに描き出す。今日、ひも理論とその発展理論であるM理論は圧倒的な支持を得て、世界の名だたる物理学者や天文学者が、最先端の波検出器、重力レンズ、衛星、天体望遠鏡を動員し、多宇宙(マルチバース)理論の検証に取り組んでいる。もしパラレルワールドが存在するのなら、いつかこの宇宙が暗く凍ったビッグフリーズを迎えるとき、われわれの未来の先進文明は、次元の「救命ボート」によってこの宇宙を脱出し、パラレルワールドへと至る方法を見つけるであろう。, isbn:9784140810866, pageCount:480]
[title:複製技術時代の芸術, subtitle:null, authors:[ヴァルター ベンヤミン], publishedDate:1999-11, description:映像文化論の古典として読み継がれる名著, isbn:9784794912664, pageCount:187]
[title:Clean Code, subtitle:アジャイルソフトウェア達人の技, authors:[ロバート・C. マーチン], publishedDate:2009-05, description:コードを書き、読み、洗練するときにどう考えるべきか?原則、パターン、実践、そして経験則を身につける。ケーススタディを注意深く読むことで、コードを洗練していく過程で行うべき判断について学ぶ。, isbn:9784048676885, pageCount:527]

直接のタイトル以外にも検索キーワードが効いているようですが、まあいいでしょう…。