X-Git-Url: http://jaekl.net/gitweb/?a=blobdiff_plain;f=book.rb;h=370093da92964413102b286792ab27a4e8ed6415;hb=061091d1fc2bb2351afc695a5fcbdbc19e48e03b;hp=5f14aed14790523eaa012e07857d650bfb43b1e2;hpb=0104e6084e78bc47f39ad73698bd8d63090d5eef;p=quanlib.git
diff --git a/book.rb b/book.rb
index 5f14aed..370093d 100644
--- a/book.rb
+++ b/book.rb
@@ -1,10 +1,15 @@
-require './author.rb'
+require 'nokogiri'
+require 'zip'
+
+require 'author'
+require 'cover'
class Book
def initialize(fileName)
@author = nil
@cover = nil
+ @path = fileName
@series = nil
@title = nil
@volume = nil
@@ -26,8 +31,41 @@ class Book
return false
end
+ def cover
+ return @cover
+ end
+
+ def describe
+ result = []
+
+ if nil != @title
+ result.push('' + @title + '')
+ else
+ result.push('(Unknown title)')
+ end
+ if nil != @author
+ result.push(@author.to_s())
+ end
+
+ seriesInfo = []
+ if nil != @series
+ seriesInfo.push(@series.to_s)
+ end
+ if nil != @volume
+ seriesInfo.push(@volume.to_s)
+ end
+ if seriesInfo.length > 0
+ result.push(seriesInfo.join(' '))
+ end
+
+ return result.join('
')
+ end
+
def inspect
data = []
+ if nil != @author
+ data.push('author="' + @author.to_s + '"')
+ end
if nil != @series
data.push('series="' + @series + '"')
end
@@ -37,8 +75,11 @@ class Book
if nil != @title
data.push('title="' + @title + '"')
end
- if nil != @author
- data.push('author="' + @author + '"')
+ if nil != @cover
+ data.push(@cover.inspect())
+ end
+ if nil != @path
+ data.push('path="' + @path + '"')
end
return '(Book:' + data.join(',') + ')'
end
@@ -107,5 +148,89 @@ class Book
if parts.length > 1
@author = massageAuthor(parts[-2])
end
+
+ if fileName.downcase.end_with?(".epub")
+ scanEpub!(fileName)
+ end
+ end
+
+ protected
+ def scanEpub!(fileName)
+ puts 'Scanning "' + fileName.to_s + '"...'
+ Zip::File.open(fileName) do |zipfile|
+ contXml = zipfile.read('META-INF/container.xml')
+ contDoc = Nokogiri::XML(contXml)
+ opfPath = contDoc.css("container rootfiles rootfile")[0]['full-path']
+
+ scanOpf!(zipfile, opfPath)
+ end
+ end
+
+ protected
+ def scanOpf!(zipfile, opfPath)
+ coverId = nil
+
+ opfXml = zipfile.read(opfPath)
+ opfDoc = Nokogiri::XML(opfXml)
+
+ #-------
+ # Author
+
+ creator = opfDoc.css('dc|creator', 'dc' => 'http://purl.org/dc/elements/1.1/')
+ if nil != creator
+ roleNode = creator.attr('role')
+ if nil != roleNode
+ role = roleNode.value
+ if 'aut' == role
+ name = creator.children[0].content
+ parts = name.split(' ')
+ if parts.length > 1
+ surname = parts[-1]
+ givenNames = parts[0..-2].join(' ')
+ @author = Author.new(surname, givenNames)
+ else
+ @author = Author.new(name, '')
+ end
+ end
+ end
+ end
+
+ #---------------------------------------
+ # Other metadata: series, volume, cover
+
+ metas = opfDoc.css('package metadata meta')
+ for m in metas
+ name = m['name']
+ content = m['content']
+
+ if 'calibre:series' == name
+ @series = content
+ elsif 'calibre:series-index' == name
+ @volume = content
+ elsif 'cover' == name
+ coverId = content
+ end
+ end
+
+ #---------------
+ # Load the cover
+
+ coverFile = nil
+ if nil != coverId
+ items = opfDoc.css('package manifest item')
+ for i in items
+ href = i['href']
+ id = i['id']
+ mimeType = i['media-type']
+
+ if coverId == id
+ entry = zipfile.find_entry(href)
+ entry.get_input_stream() do |is|
+ @cover = Cover.new(is, href, mimeType)
+ end
+ end
+ end
+ end
end
end
+