今回、技術評論社のSoftware Design 2010年7月号の第三特集「iPadとKindleに見る 電子書籍の未来」の原稿を書かせていただきました。
[429] [429] Client error: `POST https://webservices.amazon.co.jp/paapi5/getitems` resulted in a `429 Too Many Requests` response: {"__type":"com.amazon.paapi5#TooManyRequestsException","Errors":[{"Code":"TooManyRequests","Message":"The request was de (truncated...)
誌面にソースコードを載せるのは昔懐かしいことになってしまうので、こちらのエントリに記載をさせていただきます。
このコードは、EPUBの生成にgepub、Twitterへのアクセスにrubytterというライブラリをそれぞれ利用させてもらっています。このため、最初にgemでライブラリをインストールする必要があります。
$ gem install rubytter
$ gem install gepub
$ gem install -r libxml-ruby
下記のコードは、TwitterのAPIへのアクセスにBasic認証を用いています。TwitterのBasic認証は6月30日8月16日から8月末にかけて廃止されるため、OAuth認証かxAuth認証を利用するようにしなければいけません。手直ししてから掲載する予定だったのですが、仕事がたてこんでいるので、Basic認証のバージョンのままですみません。近日中にOAuth対応版に差し替えをします。
require 'rubygems'
# EPUBのライブラリ作成には、
# http://github.com/skoji/gepub
# gepubを使うには、Libxml-Rubyも必要です。
require 'gepub'
# TwitterのAPIにアクセスするため、rubytterを利用します。
# http://github.com/jugyo/rubytter
require 'rubytter'
require 'parsedate'
require 'fileutils'
# Twitterのアカウントを設定
# USERIDとPASSWORDを書き換えてください。
t = Rubytter.new('USERID', 'PASSWORD')
# 書名など
epubdir = "fav_example" # epubのコンテンツを置くディレクトリ
title = "サンプルスクリプト" # epubのファイル名
# epubのメタデータ作成
epub = GEPUB::Generator.new(title)
epub.author="Yoshihiro TSUBOI" # 著者の名前
epub.publisher="技術評論社" # 出版社の名前
epub.date = "2010-01-01"
epub.identifier = "https://www.ytsuboi.org/wp/epub/" # identifierはURLのみ
FileUtils.rm_rf(epubdir)
FileUtils.mkdir(epubdir)
# 表紙のデータとサンプルの別ページを生成
[ 'coverpage', 'chapter1' ].each {
|name|
File.open(epubdir + "/#{name}.html", 'w') {
|file|
file << <<EOF
<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja" lang="ja">
<head>
<title>sample #{name} </title>
</head>
<body>
<h1>#{name}</h1>
<p>here comes the contents for #{name}</p>
</body>
</html>
EOF
}
}
# Favoriteを書いたページの生成
page = 1
ids = []
n = "\n"
# 頭の部分
contents = <<-EOF
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja" lang="ja">
<head>
<title>My Fav</title>
</head>
<body>
<h1>私のfav</h1>
EOF
# Twitterにアクセスして、favをxhtmlに書き出す。
loop do
favs = t.favorites("", :page => page)
favs.each do |fav|
contents += '<p><b>@' + fav.user.screen_name + '</b><br />' + n
favtext = fav.text
p favtext.sub('&', '&')
p favtext.sub('"', '"')
p favtext.sub('<', '<')
p favtext.sub('>', '<')
contents += favtext + '<br />' + n
date = ParseDate::parsedate(fav.created_at)
date = Time::local(*date[0..-3])
contents += (date + 60 * 60 * 9).strftime("%Y/%m/%d %H:%M:%S") + '<br />' + n
contents += 'http://twitter.com/' + fav.user.screen_name + '/status/' + fav.id.to_s + '</p>' + n
ids.push(fav.id)
end
if favs.count < 20
break
else
page += 1
end
end
# ファイルの最後の部分
contents += <<-EOF
</body>
</html>
EOF
# 書き出し
File.open(epubdir + "/contents.html", "w") do |file|
file.puts contents
end
# "coverpage"は表紙をなので目次のデータには入れない。
epub.addManifest('cover', "coverpage.html", 'application/xhtml+xml')
epub.spine.push('cover')
epub.addManifest('Favorites', "contents.html", 'application/xhtml+xml')
epub.spine.push('Favorites')
epub.addNav('chap1', 'Favorites', "contents.html")
epub.addManifest('chap1', "chapter1.html", 'application/xhtml+xml')
epub.spine.push('chap1')
epub.addNav('chap1', 'Chapter 1', "chapter1.html")
# container.xml/contents.opf/toc.ncxなどのファイルを生成
epub.create(epubdir)
# 生成したディレクトリから、EPUBファイル作成
epub.create_epub(epubdir, ".")

1 Comments.