LoginSignup
7
12

More than 5 years have passed since last update.

ど素人がAmazon読み放題のkindle本だけ探すRailsアプリケーション作った!

Last updated at Posted at 2017-05-10

最初に謝っておきます。
90%程が以下の情報をパクって作りました。
http://qiita.com/tatsuki-m/items/043f14e7d4504277c947

本当にありがとうございます!

私が作ったサイトはこちらです。
https://murmuring-woodland-50630.herokuapp.com/

SearchAmazon.png

見た目も同じ。

ごめんなさい。

画像付きでめちゃくちゃ分かりやすいので動くRailsアプリを作って「俺天才!」を感じたい人は凄くオススメです!

今回は、パクって無い検索部分についてまとめてみたいと思います。

      KINDLE_UMLIMITED_BROWSE_NODE_ID = 4486610051

      # Amazon::Ecs::Responceオブジェクトの取得
      books = Amazon::Ecs.item_search(
        params[:keyword],
        search_index:  'KindleStore',
        browse_node:  KINDLE_UMLIMITED_BROWSE_NODE_ID,
        response_group: 'ItemAttributes, Images, SalesRank, Small',
        country:  'jp',
        item_page: '1'
      )

      # ページ数取得
      total_pages = books.total_pages

      # 本のタイトル,画像URL, 詳細ページURLの取得
      @books = []
      books.items.each do |item|
        book = Book.new(
          item.get('ItemAttributes/Title'),
          item.get('LargeImage/URL'),
          item.get('DetailPageURL'),
          item.get('ItemAttributes/Author'),
          item.get('ItemAttributes/PublicationDate'),
          item.get('SalesRank')
        )
        @books << book
      end

Kindle Unlimitedの対象のものだけ

ごめんなさい。これも実はパクりました。
どこかは忘れてしまったのですが、IDを渡すと良いそうなので定数を作って検索条件の一つとして渡しています。

KINDLE_UMLIMITED_BROWSE_NODE_ID = 4486610051
books = Amazon::Ecs.item_search(
  browse_node:  KINDLE_UMLIMITED_BROWSE_NODE_ID
)

ページ数の取得

AmazonAPIの仕様だと最大10件までしか一回に取得できないそうです。
なので、10件以上取得したい場合はページ数を取得して、その数分繰り返し問い合わせるみたいな事をするそうなのですが、単純に検索処理の部分をeachすると「時間を空けて問い合わせろ!」みたいに怒られたので諦めました。

ページ数の取得はこの部分です

total_pages = books.total_pages

タイトルとか著者とかランキングとか

詳細な情報はこちらのサイトにまとまっています。
https://www.ajaxtower.jp/ecs/

欲しい情報をこのサイトで見つけて、pryで止めながら何処で取得できるのかチェックしてみたいな事をやれば欲しい情報は取れます。

例えば、画像のURLなどはresponse_groupImagesをすることでその情報も引っ張ってこれるよ
うになります。
この状態でLargeImage/URLを指定してやるばURLゲットできます。

その他も同じ感じで追加していきます。

        books = Amazon::Ecs.item_search(
            response_group: 'ItemAttributes, Images, SalesRank, Small',
        )

        book = Book.new(
          item.get('ItemAttributes/Title'),
          item.get('LargeImage/URL'),
          item.get('DetailPageURL'),
          item.get('ItemAttributes/Author'),
          item.get('ItemAttributes/PublicationDate'),
          item.get('SalesRank')
        )

ちなみに取得できる情報はこんな感じです

   #<Amazon::Element:0x007f88abc02d18
    @element=
     #(Element:0x3fc455e016a0 {
       name = "Item",
       children = [
         #(Element:0x3fc458bca988 { name = "ASIN", children = [ #(Text "B01N5GBEFX")] }),
         #(Element:0x3fc45a195538 {
           name = "DetailPageURL",
           children = [
             #(Text "https://www.amazon.co.jp/Ruby-Rails-5-0-%E5%88%9D%E7%B4%9A%E2%91%A2-HTML%E3%83%95%E3%82%A9%E3%83%BC%E3%83%A0%EF%BC%88%E5%89%8D%E7%B7%A8%EF%BC%89-ebook/dp/B01N5GBEFX?SubscriptionId=AKIAIA3BP3GG6D4WFSCQ&tag=0piyo0804-22&linkCode=xm2&camp=2025&creative=165953&creativeASIN=B01N5GBEFX")]
           }),
         #(Element:0x3fc45a42cc38 {
           name = "ItemLinks",
           children = [
             #(Element:0x3fc45a428b60 {
               name = "ItemLink",
               children = [
                 #(Element:0x3fc45a415100 { name = "Description", children = [ #(Text "Add To Wishlist")] }),
                 #(Element:0x3fc4563f5458 {
                   name = "URL",
                   children = [
                     #(Text "https://www.amazon.co.jp/gp/registry/wishlist/add-item.html?asin.0=B01N5GBEFX&SubscriptionId=AKIAIA3BP3GG6D4WFSCQ&tag=0piyo0804-22&linkCode=xm2&camp=2025&creative=5143&creativeASIN=B01N5GBEFX")]
                   })]
               }),
             #(Element:0x3fc455dbfc00 {
               name = "ItemLink",
               children = [
                 #(Element:0x3fc45a27db58 { name = "Description", children = [ #(Text "Tell A Friend")] }),
                 #(Element:0x3fc45a248728 {
                   name = "URL",
                   children = [
                     #(Text "https://www.amazon.co.jp/gp/pdp/taf/B01N5GBEFX?SubscriptionId=AKIAIA3BP3GG6D4WFSCQ&tag=0piyo0804-22&linkCode=xm2&camp=2025&creative=5143&creativeASIN=B01N5GBEFX")]
                   })]
               }),
             #(Element:0x3fc45acae078 {
               name = "ItemLink",
               children = [
                 #(Element:0x3fc45a21dc1c { name = "Description", children = [ #(Text "All Customer Reviews")] }),
                 #(Element:0x3fc455e0226c {
                   name = "URL",
                   children = [
                     #(Text "https://www.amazon.co.jp/review/product/B01N5GBEFX?SubscriptionId=AKIAIA3BP3GG6D4WFSCQ&tag=0piyo0804-22&linkCode=xm2&camp=2025&creative=5143&creativeASIN=B01N5GBEFX")]
                   })]
               }),
             #(Element:0x3fc45acbad8c {
               name = "ItemLink",
               children = [
                 #(Element:0x3fc45ac8accc { name = "Description", children = [ #(Text "All Offers")] }),
                 #(Element:0x3fc455d857bc {
                   name = "URL",
                   children = [
                     #(Text "https://www.amazon.co.jp/gp/offer-listing/B01N5GBEFX?SubscriptionId=AKIAIA3BP3GG6D4WFSCQ&tag=0piyo0804-22&linkCode=xm2&camp=2025&creative=5143&creativeASIN=B01N5GBEFX")]
                   })]
               })]
           }),
         #(Element:0x3fc45ac75854 { name = "SalesRank", children = [ #(Text "17549")] }),
         #(Element:0x3fc45ac25cc8 {
           name = "SmallImage",
           children = [
             #(Element:0x3fc458b78c50 { name = "URL", children = [ #(Text "https://images-fe.ssl-images-amazon.com/images/I/517%2BXd99-gL._SL75_.jpg")] }),
             #(Element:0x3fc45a206f80 { name = "Height", attributes = [ #(Attr:0x3fc45ac0cf5c { name = "Units", value = "pixels" })], children = [ #(Text "75")] }),
             #(Element:0x3fc4563e1278 { name = "Width", attributes = [ #(Attr:0x3fc4563dc570 { name = "Units", value = "pixels" })], children = [ #(Text "56")] })]
           }),
         #(Element:0x3fc45a203d58 {
           name = "MediumImage",
           children = [
             #(Element:0x3fc45a1efc7c { name = "URL", children = [ #(Text "https://images-fe.ssl-images-amazon.com/images/I/517%2BXd99-gL._SL160_.jpg")] }),
             #(Element:0x3fc458bedfa0 { name = "Height", attributes = [ #(Attr:0x3fc458be9f18 { name = "Units", value = "pixels" })], children = [ #(Text "160")] }),
             #(Element:0x3fc45a1db4fc { name = "Width", attributes = [ #(Attr:0x3fc45a4a1164 { name = "Units", value = "pixels" })], children = [ #(Text "120")] })]
           }),
         #(Element:0x3fc455ddb4b4 {
           name = "LargeImage",
           children = [
             #(Element:0x3fc458be49a0 { name = "URL", children = [ #(Text "https://images-fe.ssl-images-amazon.com/images/I/517%2BXd99-gL.jpg")] }),
             #(Element:0x3fc45a1d0a98 { name = "Height", attributes = [ #(Attr:0x3fc45a1d014c { name = "Units", value = "pixels" })], children = [ #(Text "500")] }),
             #(Element:0x3fc45a1bd178 { name = "Width", attributes = [ #(Attr:0x3fc45a1b8088 { name = "Units", value = "pixels" })], children = [ #(Text "375")] })]
           }),
         #(Element:0x3fc45a1b084c {
           name = "ImageSets",
           children = [
             #(Element:0x3fc455dd6554 {
               name = "ImageSet",
               attributes = [ #(Attr:0x3fc455dcfe5c { name = "Category", value = "primary" })],
               children = [
                 #(Element:0x3fc455dcb1f4 {
                   name = "SwatchImage",
                   children = [
                     #(Element:0x3fc45a48b828 { name = "URL", children = [ #(Text "https://images-fe.ssl-images-amazon.com/images/I/517%2BXd99-gL._SL30_.jpg")] }),
                     #(Element:0x3fc458bcd804 { name = "Height", attributes = [ #(Attr:0x3fc45a482070 { name = "Units", value = "pixels" })], children = [ #(Text "30")] }),
                     #(Element:0x3fc458bc3124 { name = "Width", attributes = [ #(Attr:0x3fc458bc003c { name = "Units", value = "pixels" })], children = [ #(Text "22")] })]
                   }),
                 #(Element:0x3fc45a19da08 {
                   name = "SmallImage",
                   children = [
                     #(Element:0x3fc45a199458 { name = "URL", children = [ #(Text "https://images-fe.ssl-images-amazon.com/images/I/517%2BXd99-gL._SL75_.jpg")] }),
                     #(Element:0x3fc45a449478 { name = "Height", attributes = [ #(Attr:0x3fc45a444fa4 { name = "Units", value = "pixels" })], children = [ #(Text "75")] }),
                     #(Element:0x3fc458b7ce54 { name = "Width", attributes = [ #(Attr:0x3fc45a4340f0 { name = "Units", value = "pixels" })], children = [ #(Text "56")] })]
                   }),
                 #(Element:0x3fc45a4110c8 {
                   name = "ThumbnailImage",
                   children = [
                     #(Element:0x3fc45a400fac { name = "URL", children = [ #(Text "https://images-fe.ssl-images-amazon.com/images/I/517%2BXd99-gL._SL75_.jpg")] }),
                     #(Element:0x3fc4563db288 { name = "Height", attributes = [ #(Attr:0x3fc455db2f64 { name = "Units", value = "pixels" })], children = [ #(Text "75")] }),
                     #(Element:0x3fc458b605ec { name = "Width", attributes = [ #(Attr:0x3fc455dab5fc { name = "Units", value = "pixels" })], children = [ #(Text "56")] })]
                   }),
                 #(Element:0x3fc455d9aa40 {
                   name = "TinyImage",
                   children = [
                     #(Element:0x3fc455d968a0 { name = "URL", children = [ #(Text "https://images-fe.ssl-images-amazon.com/images/I/517%2BXd99-gL._SL110_.jpg")] }),
                     #(Element:0x3fc4563d28cc { name = "Height", attributes = [ #(Attr:0x3fc4563b04c0 { name = "Units", value = "pixels" })], children = [ #(Text "110")] }),
                     #(Element:0x3fc4563a82c0 { name = "Width", attributes = [ #(Attr:0x3fc458b4f1ac { name = "Units", value = "pixels" })], children = [ #(Text "82")] })]
                   }),
                 #(Element:0x3fc45780784c {
                   name = "MediumImage",
                   children = [
                     #(Element:0x3fc458b376c4 { name = "URL", children = [ #(Text "https://images-fe.ssl-images-amazon.com/images/I/517%2BXd99-gL._SL160_.jpg")] }),
                     #(Element:0x3fc456371860 { name = "Height", attributes = [ #(Attr:0x3fc455d734cc { name = "Units", value = "pixels" })], children = [ #(Text "160")] }),
                     #(Element:0x3fc458b2f690 { name = "Width", attributes = [ #(Attr:0x3fc45a18c76c { name = "Units", value = "pixels" })], children = [ #(Text "120")] })]
                   }),
                 #(Element:0x3fc45a155ed8 {
                   name = "LargeImage",
                   children = [
                     #(Element:0x3fc45a14ce14 { name = "URL", children = [ #(Text "https://images-fe.ssl-images-amazon.com/images/I/517%2BXd99-gL.jpg")] }),
                     #(Element:0x3fc45a1395bc { name = "Height", attributes = [ #(Attr:0x3fc45a13023c { name = "Units", value = "pixels" })], children = [ #(Text "500")] }),
                     #(Element:0x3fc45a110e64 { name = "Width", attributes = [ #(Attr:0x3fc45a1015b8 { name = "Units", value = "pixels" })], children = [ #(Text "375")] })]
                   })]
               })]
           }),
         #(Element:0x3fc45a0c9ce4 {
           name = "ItemAttributes",
           children = [
             #(Element:0x3fc45a0c09c8 { name = "Author", children = [ #(Text "黒田 努")] }),
             #(Element:0x3fc45a0b0938 { name = "Binding", children = [ #(Text "Kindle版")] }),
             #(Element:0x3fc45a09cbcc { name = "Edition", children = [ #(Text "4")] }),
             #(Element:0x3fc45a079924 { name = "EISBN", children = [ #(Text "9784908829130")] }),
             #(Element:0x3fc45a061d38 { name = "Format", children = [ #(Text "Kindle本")] }),
             #(Element:0x3fc45a04c168 { name = "IsAdultProduct", children = [ #(Text "0")] }),
             #(Element:0x3fc45a034720 { name = "Label", children = [ #(Text "株式会社オイアクス")] }),
             #(Element:0x3fc45a01ce54 {
               name = "Languages",
               children = [
                 #(Element:0x3fc45a00d65c {
                   name = "Language",
                   children = [ #(Element:0x3fc45a005614 { name = "Name", children = [ #(Text "日本語")] }), #(Element:0x3fc455c75fc0 { name = "Type", children = [ #(Text "Published")] })]
                   })]
               }),
             #(Element:0x3fc458b137b0 { name = "Manufacturer", children = [ #(Text "株式会社オイアクス")] }),
             #(Element:0x3fc458aff6ac { name = "NumberOfPages", children = [ #(Text "237")] }),
             #(Element:0x3fc458aeb940 { name = "ProductGroup", children = [ #(Text "eBooks")] }),
             #(Element:0x3fc458ad3d2c { name = "ProductTypeName", children = [ #(Text "ABIS_EBOOKS")] }),
             #(Element:0x3fc458abe83c { name = "PublicationDate", children = [ #(Text "2016-12-15")] }),
             #(Element:0x3fc458aa30f0 { name = "Publisher", children = [ #(Text "株式会社オイアクス")] }),
             #(Element:0x3fc458a8ad0c { name = "ReleaseDate", children = [ #(Text "2016-12-15")] }),
             #(Element:0x3fc458a73788 { name = "Studio", children = [ #(Text "株式会社オイアクス")] }),
             #(Element:0x3fc458a5be44 { name = "Title", children = [ #(Text "Ruby on Rails 5.0 初級③: HTMLフォーム(前編) (OIAX BOOKS)")] })]
           })]
       })>,
7
12
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
7
12