<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>bottlenose - イチゾーのブログ</title>
	<atom:link href="https://ichizo.biz/tag/bottlenose/feed" rel="self" type="application/rss+xml" />
	<link>https://ichizo.biz</link>
	<description>システム開発やWordPressについてなど</description>
	<lastBuildDate>Thu, 29 Dec 2016 08:29:30 +0000</lastBuildDate>
	<language>ja</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.8.1</generator>
<site xmlns="com-wordpress:feed-additions:1">120380898</site>	<item>
		<title>英語多読用リストがKindle Unlimitedに存在するか自動チェックしてみる</title>
		<link>https://ichizo.biz/2016/12/20/english-kindle-unlimited.html</link>
					<comments>https://ichizo.biz/2016/12/20/english-kindle-unlimited.html?noamp=mobile#respond</comments>
		
		<dc:creator><![CDATA[一蔵]]></dc:creator>
		<pubDate>Tue, 20 Dec 2016 08:50:13 +0000</pubDate>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[英語]]></category>
		<category><![CDATA[Amazon Product Advertising API]]></category>
		<category><![CDATA[bottlenose]]></category>
		<category><![CDATA[Kindle Unlimited]]></category>
		<guid isPermaLink="false">http://ichizo.biz/?p=73</guid>

					<description><![CDATA[<p>PythonでAmazonのKindle Unlimited [&#8230;]</p>
<p>The post <a href="https://ichizo.biz/2016/12/20/english-kindle-unlimited.html">英語多読用リストがKindle Unlimitedに存在するか自動チェックしてみる</a> first appeared on <a href="https://ichizo.biz">イチゾーのブログ</a>.</p>]]></description>
										<content:encoded><![CDATA[<p><a href="http://ichizo.biz/2016/12/20/python-kindleunlimited.html">PythonでAmazonのKindle Unlimited情報を取得してみる</a>に続いて、<br />
あらかじめテキストファイルに記載していた書名を取得して<br />
Amazon Product Advertising APIに対して検索を行います。</p>
<h2>Pythonでテキストファイルを開く</h2>
<p>テキストファイルは、以下のようなソースで読み込みます。</p>
<pre class="lang:python decode:true " >
#!/usr/bin/env python

f = open('text.txt', 'r')

for line in f:
    print line
    
f.close()
</pre>
<p>読み込みたいのは以下のようなテキストファイルです。</p>
<pre class="lang:default decode:true " >
Foundations Reading Library
Penguin Readers Easystarts
Oxford Bookworms Starters
Macmillan Readers Starter
Macmillan New Wave Readers Level 1
Oxford Reading Tree
Longman Literacy Land Story Street
Macmillan Springboard
Rookie Readers Fiction
Cambridge Storybooks
Longman Shared Reading
Oxford Start with English Readers
Longman Big and Little Books
CTP Emergent Readers
100 English
Sight Word Readers
Time-to-Discover Readers
Step into Reading Step 1
Step into Reading Step 2
Scholastic Reader Level1
Scholastic Reader Level2
My First I Can Read Books
I Can Read Books Level 1
Penguin Young Readers Level 1
Ready-to-Read Pre-Level 1
Ready-to-Read Level 1
Ready-to-Read Level 2
All Aboard Reading Picture Books
All Aboard Reading Level 1
Puffin Easy-to-Read Level 1
Addison Wesley Big and Little Books
Oxford Classic Tales
Curious George Short Stories
Dorling Kindersley Readers Level 1
Dorling Kindersley Readers Level 2
Oxford Dominoes Stage 0
Penguin Young Readers Level 2
Longman Chatterbox
TL PM Readers
HM Leveled Reading Grade 1
The Very Hungry Caterpillar
Good Night Moon
Brown Bear, Brown Bear, What Do You See?
</pre>
<h3>英語多読用の本のリストが欲しい</h3>
<p>これは何かというと、英語多読用の本のリストです。</p>
<p>SSS英語多読研究会がすすめる「<a href="https://www.seg.co.jp/sss/learning/" target="_blank">非常にやさしい本からはじめ、１００万語単位で読む多読</a>」をやってみたいと思っていたのですが、<br />
多読をするためにはまず教材となる英語の本を用意する必要があります。</p>
<p>SSS英語多読研究会では、<a href="https://www.seg.co.jp/sss/review/osusume.html" target="_blank">レベル別に読むべき本を紹介してくれています</a>。</p>
<p>問題は、多読をするためには大量の本を読まなければいけないのですが、<br />
目安である100万語まで到達するまでにはかなりの本が必要になります。</p>
<p>特にレベルの低いころに読む本は、1冊あたりの字数が少ないために<br />
どうしても本の数が多くなってしまいます。</p>
<p>１冊500円～600円程度で購入できるので、数冊買う分にはいいのですが、<br />
多読となると数十冊単位で読む必要があります。<br />
となると、数万円は軽くかかります。</p>
<p>しかもそのレベルを通り過ぎてしまえば、必要がなくなってしまうという<br />
悲しい運命が待っています。<br />
簡単なものから始めるがゆえに、ほぼ読み捨てしなければならないということです。</p>
<p>そこで目を付けたのが、Kindle Unlimitedです。</p>
<p>読み放題のKindle Unlimitedであれば、お金を気にせず読み進められます。<br />
洋書もかなりの冊数あります（先ほど洋書一覧で見ると137万冊以上あるようです）ので、<br />
きっと多読に使える本もあるはずということです。</p>
<p>ただ、ここで問題になるのは対象になる本を見つける方法です。<br />
リストにあがっている本をいちいち検索するのはメンドくさい、<br />
そもそもKindle Unlimitedにない本もある（ないことが多い）、<br />
効率よく多読するには本を効率よく見つけることも大切だということで<br />
リストからKindle Unlimitedにあるのかを探すプログラムを作ることにしました。</p>
<p>今回検索するリストは、SSS英語多読研究会がレベル0として推薦しているものです。</p>
<h3>書名リストからAmazon Product Advertising APIを呼び出すプログラム</h3>
<p>書名リストのテキストをlevel0.txtとして、Pythonプログラムと同じディレクトリに置きます。</p>
<p>前回の「<a href="http://ichizo.biz/2016/12/20/python-kindleunlimited.html">PythonでAmazonのKindle Unlimited情報を取得してみる</a>」に、ファイルの読み込み処理を組み込みます。</p>
<p>修正したプログラムが以下です。</p>
<pre class="lang:python decode:true " >
# -*- coding: utf-8 -*-
"""
Created on Mon Dec 19 14:49:19 2016

@author: 
"""

from bottlenose import api
from lxml import objectify
import time

AMAZON_ACCESS_KEY_ID = "アクセスキー"
AMAZON_SECRET_KEY = "シークレットキー"
AMAZON_ASSOC_TAG = "アソシエイトタグ"

KINDLE_UMLIMITED_NODE_ID = "4486610051"

amazon = api.Amazon(AMAZON_ACCESS_KEY_ID, AMAZON_SECRET_KEY, AMAZON_ASSOC_TAG, Region = "JP")

def file_read (file):
    f = open(file, 'r')
    data = f.read()
    f.close()
    lines = data.split('\n')
    return lines

def item_search (keywords, searchIndex="KindleStore", browseNode=KINDLE_UMLIMITED_NODE_ID):
    response = amazon.ItemSearch(Keywords=keywords, SearchIndex=searchIndex, BrowseNode=browseNode, ResponseGroup="Large")
    root = objectify.fromstring(response)
    return root.Items.Item

if __name__ == '__main__':

    lines = file_read('level0.txt')
    for line in lines:
        for item in item_search(line):
            title = item.ItemAttributes.Title
            author = item.ItemAttributes.Author
            url = item.DetailPageURL
            print ("%s\n%s\n%s\n%s\n\n" % (line, title, author, url))
        time.sleep(5)   # Amazon API呼び出しは1秒に1回以内に収めるためsleep
</pre>
<p>file_readというメソッドを作って、ここでテキストファイルを読み込み<br />
１行ずつを配列の各要素に入れて返します。</p>
<p>file_readの呼び出し元では、返り値でループさせます。<br />
その際に、sleep(5)を入れています。</p>
<p>これで実行してみると、<br />
「AttributeError: no such child: {http://webservices.amazon.com/AWSECommerceService/2013-08-01}Item」<br />
というエラーで終了します。</p>
<p>APIの結果がない場合にエラーで落ちてしまっています。<br />
今度はこれの対策を考えます。</p>
<p>以下のように書き換えてみました。</p>
<pre class="lang:python decode:true " >
# -*- coding: utf-8 -*-
"""
Created on Mon Dec 19 14:49:19 2016

@author: 
"""

from bottlenose import api
from lxml import objectify
import time

AMAZON_ACCESS_KEY_ID = "アクセスキー"
AMAZON_SECRET_KEY = "シークレットキー"
AMAZON_ASSOC_TAG = "アソシエイトタグ"

KINDLE_UMLIMITED_NODE_ID = "4486610051"

amazon = api.Amazon(AMAZON_ACCESS_KEY_ID, AMAZON_SECRET_KEY, AMAZON_ASSOC_TAG, Region = "JP")

def file_read (file):
    f = open(file, 'r')
    data = f.read()
    f.close()
    lines = data.split('\n')
    return lines

def item_search (keywords, searchIndex="KindleStore", browseNode=KINDLE_UMLIMITED_NODE_ID):
    response = amazon.ItemSearch(Keywords=keywords, SearchIndex=searchIndex, BrowseNode=browseNode, ResponseGroup="Large")
    root = objectify.fromstring(response)
    return root.Items

if __name__ == '__main__':

    lines = file_read('level0.txt')
    for line in lines:
        items = item_search(line)
        item_find = items.find('Item')
        if (item_find is not None):
            for item in item_find:
                if (item is not None):
                    title = item.ItemAttributes.Title
                    author = item.ItemAttributes.Author
                    url = item.DetailPageURL
                    print ("%s\n%s\n%s\n%s\n\n" % (line, title, author, url))
                else:
                    print ("%s : Book is None" % (line))
        print ("END %s" % (line))
        time.sleep(5)   # Amazon API呼び出しは1秒に1回以内に収めるためsleep
</pre>
<p>調べてみると、「root.Items.find(&#8216;Item&#8217;)」は、<br />
Itemが空の時Noneを返すということなので、<br />
「items.find(&#8216;Item&#8217;)」をitem_findに放り込んで<br />
これがNoneの場合は処理しないようにしました。</p>
<p>結果はというと、以下のようになりました。</p>
<pre class="lang:default decode:true " >
END Foundations Reading Library
END Penguin Readers Easystarts
END Oxford Bookworms Starters
END Macmillan Readers Starter
END Macmillan New Wave Readers Level 1
END Oxford Reading Tree
END Longman Literacy Land Story Street
END Macmillan Springboard
END Rookie Readers Fiction
END Cambridge Storybooks
END Longman Shared Reading
END Oxford Start with English Readers
END Longman Big and Little Books
END CTP Emergent Readers
END 100 English
END Sight Word Readers
END Time-to-Discover Readers
END Step into Reading Step 1
END Step into Reading Step 2
END Scholastic Reader Level1
END Scholastic Reader Level2
END My First I Can Read Books
END I Can Read Books Level 1
END Penguin Young Readers Level 1
END Ready-to-Read Pre-Level 1
END Ready-to-Read Level 1
END Ready-to-Read Level 2
END All Aboard Reading Picture Books
END All Aboard Reading Level 1
END Puffin Easy-to-Read Level 1
END Addison Wesley Big and Little Books
END Oxford Classic Tales
END Curious George Short Stories
END Dorling Kindersley Readers Level 1
END Dorling Kindersley Readers Level 2
END Oxford Dominoes Stage 0
END Penguin Young Readers Level 2
END Longman Chatterbox
END TL PM Readers
END HM Leveled Reading Grade 1
END The Very Hungry Caterpillar
END Good Night Moon
END Brown Bear, Brown Bear, What Do You See?
END 
</pre>
<p>結果は全滅・・・。<br />
一つもKindle Unlimitedにはないという結果に。</p>
<p>そしてなぜか「if (item is not None):」のelse部分を通ってくれなかったという謎も。</p>
<p>リストを広げて再取得してみることにします。</p><p>The post <a href="https://ichizo.biz/2016/12/20/english-kindle-unlimited.html">英語多読用リストがKindle Unlimitedに存在するか自動チェックしてみる</a> first appeared on <a href="https://ichizo.biz">イチゾーのブログ</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://ichizo.biz/2016/12/20/english-kindle-unlimited.html/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">73</post-id>	</item>
		<item>
		<title>PythonでAmazonのKindle Unlimited情報を取得してみる</title>
		<link>https://ichizo.biz/2016/12/20/python-kindleunlimited.html</link>
					<comments>https://ichizo.biz/2016/12/20/python-kindleunlimited.html?noamp=mobile#respond</comments>
		
		<dc:creator><![CDATA[一蔵]]></dc:creator>
		<pubDate>Tue, 20 Dec 2016 03:10:39 +0000</pubDate>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[Amazon Product Advertising API]]></category>
		<category><![CDATA[bottlenose]]></category>
		<category><![CDATA[Kindle Unlimited]]></category>
		<guid isPermaLink="false">http://ichizo.biz/?p=53</guid>

					<description><![CDATA[<p>Kindle Unlimitedって検索しずらい Kindl [&#8230;]</p>
<p>The post <a href="https://ichizo.biz/2016/12/20/python-kindleunlimited.html">PythonでAmazonのKindle Unlimited情報を取得してみる</a> first appeared on <a href="https://ichizo.biz">イチゾーのブログ</a>.</p>]]></description>
										<content:encoded><![CDATA[<h2>Kindle Unlimitedって検索しずらい</h2>
<p>Kindle Unlimitedを利用しているのですが、いまいち検索しずらいので<br />
該当の本がKindle Unlimitedで読めるのかを取得するプログラムを作ってみます。</p>
<p>PythonからAmazon Product Advertising APIを呼び出すプログラムを実装してみます。</p>
<p>環境は、「<a href="http://ichizo.biz/2016/12/13/anaconda.html">AnacondaでWindows上にPython環境を構築</a>」で構築したものを使用します。</p>
<p>Windows上でインストールしたAnacondaで一緒に入っているSyderを使用します。</p>
<p>Syderを立ち上げて、まずはプロジェクトを作成します。</p>
<p>メニューのProjectsから「New Project&#8230;」をクリックします。</p>
<p>プロジェクト名とプロジェクトで使用するフォルダを指定します。</p>
<h2>作成したいPythonプログラム</h2>
<p>作成したいプログラムは、書名で検索してその本がKindle Unlimitedに<br />
登録されているかを返すというものです。</p>
<p>現在、Kindle Unlimitedを利用中なのですが、<br />
Kindle Unlimitedだけを対象とした検索って<br />
いまいちやりずらいんです。</p>
<p>そこで、書名から検索してそれがKindle Unlimitedの対象になっているかを<br />
調べるプログラムを作ろうと思った次第です。</p>
<p>書名は1冊ではなく複数調べたいので、<br />
入力はテキストファイルにリストで書いておいて<br />
それを読み込むようにしたいと思います。</p>
<p>出力は、書名とそれがKindle Unlimitedに存在するかの真偽値を<br />
記載するようにすればよいでしょう。</p>
<h3>Amazon Product Advertising API用のライブラリを利用する</h3>
<p>Pythonには、Amazon Product Advertising API用のライブラリがあるようなので、それを利用させてもらいます。</p>
<p>まずはbottlenoseというライブラリをインストールします。</p>
<p>コマンドプロンプトを立ち上げる際に右クリックして、<br />
「その他」から「管理者として実行」をクリックします。</p>
<p>以下のコマンドを実行します。</p>
<pre class="lang:python decode:true " >pip install bottlenose</pre>
<p>これでインストールできました。</p>
<p>pipのバージョンが古いというメッセージが出ましたので、<br />
ついでにpipのバージョンも上げておきます。</p>
<pre class="lang:python decode:true " >python -m pip install --upgrade pip</pre>
<h3>Pythonプログラムのコーディング</h3>
<p>準備ができたので、さっそくPythonプログラムを実装します。<br />
今度はSpyderのエディタでコーディングを行います。</p>
<p>まずは簡単に、キーワードを指定して該当する書籍の 書名、著者名、URLを返すプログラムを書きます。</p>
<pre class="lang:python decode:true " >
# -*- coding: utf-8 -*-
"""
Created on Mon Dec 19 14:49:19 2016

@author:
"""

from bottlenose import api
from lxml import objectify

AMAZON_ACCESS_KEY_ID = "アクセスキー"
AMAZON_SECRET_KEY = "シークレットキー"
AMAZON_ASSOC_TAG = "アソシエイトタグ"

amazon = api.Amazon(AMAZON_ACCESS_KEY_ID, AMAZON_SECRET_KEY, AMAZON_ASSOC_TAG, Region = "JP")

def item_search (keywords, searchIndex="Books"):
    response = amazon.ItemSearch(Keywords=keywords, SearchIndex=searchIndex, ResponseGroup="Large")
    root = objectify.fromstring(response)
    return root.Items.Item

if __name__ == '__main__':

    for item in item_search('python'):
        title = item.ItemAttributes.Title
        author = item.ItemAttributes.Author
        url = item.DetailPageURL
        print ("%s\n%s\n%s\n\n" % (title, author, url))
</pre>
<p>lxmlはAnacondaに含まれていましたので、インストールする必要はありませんでした。</p>
<p>これで、「python」というキーワードに該当する本の<br />
タイトル、著者名、URLを取得することができました。</p>
<h3>Kindle Unlimitedを対象に検索する</h3>
<p>上記では、「searchIndex=&#8221;Books&#8221;」として、本のカテゴリから検索しましたが、<br />
次はKindle Unlimitedカテゴリ内を検索するようにします。</p>
<p>すでに実装されている方がいらっしゃったので、参考にさせていただきます。</p>
<p><a href="http://stonebeach-dakar.hatenablog.com/entry/Ruby%E3%81%A7Amazon%E3%81%AEAPI%E3%82%92%E4%BD%BF%E3%81%A3%E3%81%A6Kindle_Unlimited%E6%9C%AC%E3%81%AE%E4%B8%80%E8%A6%A7%E3%82%92%E5%8F%96%E5%BE%97%E3%81%99%E3%82%8B%E3%80%82" target="_blank">RubyでAmazonのAPIを使ってKindle Unlimited本の一覧を取得する。</a></p>
<p>こちらの方の記事によると、searchIndexを「KindleStore」にして<br />
browseNodeを追加してKindle UnlimitedのノードID「4486610051」を指定してやればよいようです。</p>
<p>で、実装してみました。</p>
<pre class="lang:default decode:true " >
# -*- coding: utf-8 -*-
"""
Created on Mon Dec 19 14:49:19 2016

@author:
"""

from bottlenose import api
from lxml import objectify

AMAZON_ACCESS_KEY_ID = "アクセスキー"
AMAZON_SECRET_KEY = "シークレットキー"
AMAZON_ASSOC_TAG = "アソシエイトタグ"

KINDLE_UMLIMITED_NODE_ID = "4486610051"

amazon = api.Amazon(AMAZON_ACCESS_KEY_ID, AMAZON_SECRET_KEY, AMAZON_ASSOC_TAG, Region = "JP")

def item_search (keywords, searchIndex="KindleStore", browseNode=KINDLE_UMLIMITED_NODE_ID):
    response = amazon.ItemSearch(Keywords=keywords, SearchIndex=searchIndex, BrowseNode=browseNode, ResponseGroup="Large")
    root = objectify.fromstring(response)
    return root.Items.Item

if __name__ == '__main__':

    for item in item_search('python'):
        title = item.ItemAttributes.Title
        author = item.ItemAttributes.Author
        url = item.DetailPageURL
        print ("%s\n%s\n%s\n\n" % (title, author, url))
</pre>
<p>変更した個所は、item_searchでsearchIndexを「KindleStore」に変更して<br />
browseNodeを追加しただけです。</p>
<p>これを実行してみたところ、きちんとKindle Unlimited対象商品がリストに上がってきます。</p>
<p>これで、Amazon Product Advertising APIからKindle Unlimitedの電子書籍を取得することができました。</p>
<p>次回からは、いよいよ書名のリストからそれがKindle Unlimitedにあるかを取得するプログラムに進みます。</p><p>The post <a href="https://ichizo.biz/2016/12/20/python-kindleunlimited.html">PythonでAmazonのKindle Unlimited情報を取得してみる</a> first appeared on <a href="https://ichizo.biz">イチゾーのブログ</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://ichizo.biz/2016/12/20/python-kindleunlimited.html/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">53</post-id>	</item>
	</channel>
</rss>
