スポンサードリンク

PythonでAmazonのKindle Unlimited情報を取得してみるに続いて、
あらかじめテキストファイルに記載していた書名を取得して
Amazon Product Advertising APIに対して検索を行います。

Pythonでテキストファイルを開く

テキストファイルは、以下のようなソースで読み込みます。

#!/usr/bin/env python

f = open('text.txt', 'r')

for line in f:
    print line
    
f.close()

読み込みたいのは以下のようなテキストファイルです。

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?

英語多読用の本のリストが欲しい

これは何かというと、英語多読用の本のリストです。

SSS英語多読研究会がすすめる「非常にやさしい本からはじめ、100万語単位で読む多読」をやってみたいと思っていたのですが、
多読をするためにはまず教材となる英語の本を用意する必要があります。

SSS英語多読研究会では、レベル別に読むべき本を紹介してくれています

問題は、多読をするためには大量の本を読まなければいけないのですが、
目安である100万語まで到達するまでにはかなりの本が必要になります。

特にレベルの低いころに読む本は、1冊あたりの字数が少ないために
どうしても本の数が多くなってしまいます。

1冊500円~600円程度で購入できるので、数冊買う分にはいいのですが、
多読となると数十冊単位で読む必要があります。
となると、数万円は軽くかかります。

しかもそのレベルを通り過ぎてしまえば、必要がなくなってしまうという
悲しい運命が待っています。
簡単なものから始めるがゆえに、ほぼ読み捨てしなければならないということです。

そこで目を付けたのが、Kindle Unlimitedです。

読み放題のKindle Unlimitedであれば、お金を気にせず読み進められます。
洋書もかなりの冊数あります(先ほど洋書一覧で見ると137万冊以上あるようです)ので、
きっと多読に使える本もあるはずということです。

ただ、ここで問題になるのは対象になる本を見つける方法です。
リストにあがっている本をいちいち検索するのはメンドくさい、
そもそもKindle Unlimitedにない本もある(ないことが多い)、
効率よく多読するには本を効率よく見つけることも大切だということで
リストからKindle Unlimitedにあるのかを探すプログラムを作ることにしました。

今回検索するリストは、SSS英語多読研究会がレベル0として推薦しているものです。

書名リストからAmazon Product Advertising APIを呼び出すプログラム

書名リストのテキストをlevel0.txtとして、Pythonプログラムと同じディレクトリに置きます。

前回の「PythonでAmazonのKindle Unlimited情報を取得してみる」に、ファイルの読み込み処理を組み込みます。

修正したプログラムが以下です。

# -*- 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

file_readというメソッドを作って、ここでテキストファイルを読み込み
1行ずつを配列の各要素に入れて返します。

file_readの呼び出し元では、返り値でループさせます。
その際に、sleep(5)を入れています。

これで実行してみると、
「AttributeError: no such child: {http://webservices.amazon.com/AWSECommerceService/2013-08-01}Item」
というエラーで終了します。

APIの結果がない場合にエラーで落ちてしまっています。
今度はこれの対策を考えます。

以下のように書き換えてみました。

# -*- 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

調べてみると、「root.Items.find(‘Item’)」は、
Itemが空の時Noneを返すということなので、
「items.find(‘Item’)」をitem_findに放り込んで
これがNoneの場合は処理しないようにしました。

結果はというと、以下のようになりました。

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 

結果は全滅・・・。
一つもKindle Unlimitedにはないという結果に。

そしてなぜか「if (item is not None):」のelse部分を通ってくれなかったという謎も。

リストを広げて再取得してみることにします。

スポンサードリンク