6 require 'classification'
11 @@DC_NS_URL = 'http://purl.org/dc/elements/1.1/'
15 @classification_id = nil
25 def load_from_file!(fileName)
27 parse_file_name!(fileName)
30 def self.can_handle?(fileName)
35 #puts "Filename: " + fileName.to_s
36 lowerName = fileName.downcase()
38 if lowerName.end_with?(".epub")
42 if lowerName.end_with?(".pdf")
61 def classification_id=(value)
62 @classification_id = value
77 def description=(value)
85 result.push('<b>' + @title + '</b>')
87 result.push('<i>(Unknown title)</i>')
90 result.push('<i>by ' + @author.reading_order + '</i>')
94 series = @store.load_series(@series_id)
95 if nil != series and nil != series.descr
96 seriesInfo.push(series.descr.to_s)
99 seriesInfo.push(@volume.to_s)
101 if seriesInfo.length > 0
102 result.push(seriesInfo.join(' '))
106 if nil != @classification_id
107 classification = @store.load_classification(@classification_id)
109 if nil != classification
110 if nil != classification.ddc
111 result.push('Dewey: ' + classification.ddc.to_s)
113 if nil != classification.lcc
114 result.push('LCC: ' + classification.lcc.to_s)
118 return result.join('<br/>')
124 data.push('author="' + @author.inspect + '"')
127 data.push('series_id="' + @series_id.to_s() + '"')
130 data.push('volume="' + @volume + '"')
133 data.push('title="' + @title + '"')
136 data.push(@cover.inspect())
139 data.push('path="' + @path + '"')
141 return '(Book:' + data.join(',') + ')'
156 def series_id=(value)
177 return File.basename(@path, '.*')
190 return /[[:upper:]]/.match(c)
194 def massage_author(input)
200 input.each_char do |c|
201 if isUpper?(c) and (reading_order.length > 0)
210 # Returns (series, volumeNo, titleText)
212 def processTitle(input)
217 arr = input.split('_')
223 matchData = (arr[0]).match(/^([A-Z]+)([0-9]+)$/)
225 capt = matchData.captures
231 pos = arr[-1].rindex('.')
233 arr[-1] = arr[-1].slice(0, pos)
236 title = arr.join(' ')
238 return series, vol, title
242 def parse_file_name!(file_name)
243 category = nil # e.g., non-fiction, fan-fiction
246 parts = file_name.split('/')
247 (series_code, @volume, @title) = processTitle(parts[-1])
250 reading_order = massage_author(grouping)
252 @author = Author.new(grouping, reading_order, sort_order)
253 @series_id = @store.get_series(grouping, series_code)
259 lc_file_name = file_name.downcase
260 if lc_file_name.end_with?(".epub")
262 elsif lc_file_name.end_with?(".pdf")
266 @classification_id = @store.find_classification(@author.grouping, File.basename(file_name, '.*'))
268 # TODO: Fix horrible hard-coded strings and paths
269 if ('01_nonfic' == category) && (nil == classification_id)
270 open(Store.unclassified_csv, 'a') do |fd|
271 fd.puts('"' + grouping.to_s + '","' + path + '"')
277 def scanEpub!(fileName)
278 #puts 'Scanning "' + fileName.to_s + '"...'
280 Zip::File.open(fileName) do |zipfile|
281 entry = zipfile.find_entry('META-INF/container.xml')
283 puts 'No META-INF/container.xml, skipping book ' + fileName
286 contXml = zipfile.read('META-INF/container.xml')
287 contDoc = Nokogiri::XML(contXml)
288 opfPath = contDoc.css("container rootfiles rootfile")[0]['full-path']
290 scanOpf!(zipfile, opfPath)
292 rescue Zip::Error => exc
293 puts 'ERROR processing file "' + fileName + '":'
300 def scan_pdf!(file_name)
301 #puts 'Scanning "' + file_name.to_s + '"...'
303 pdf_path = File.expand_path(file_name).to_s
304 if ! pdf_path.end_with?('.pdf')
305 puts 'Unexpected internal error: path "' + file_name.to_s + '" does not end with ".pdf".'
309 jpeg_path = pdf_path[0..-5] + '.jpeg'
310 if File.file?(jpeg_path)
311 File.open(jpeg_path, 'r') do |is|
312 @cover = Cover.new(is, jpeg_path, 'image/jpeg')
319 def scanOpf!(zipfile, opfPath)
322 opfXml = zipfile.read(opfPath)
323 opfDoc = Nokogiri::XML(opfXml)
328 grouping = @author.grouping
329 reading_order = @author.reading_order
330 sort_order = @author.sort_order
332 creators = opfDoc.css('dc|creator', 'dc' => @@DC_NS_URL)
333 if (creators.length > 0)
334 creator = creators[0]
336 role = creator['opf:role']
338 reading_order = creator.content
340 file_as = creator['opf:file-as']
346 @author = Author.new(grouping, reading_order, sort_order)
350 #---------------------------------------
353 titles = opfDoc.css('dc|title', 'dc' => @@DC_NS_URL)
357 @title = title.content
361 #---------------------------------------
364 descrNodes = opfDoc.css('dc|description', 'dc' => @@DC_NS_URL)
365 if (descrNodes.length > 0)
366 descrNode = descrNodes[0]
368 @description = descrNode.content
372 #---------------------------------------
373 # Other metadata: series, volume, cover
375 metas = opfDoc.css('package metadata meta')
378 content = m['content']
380 if 'calibre:series' == name
381 # TODO: Dynamically create a new series?
382 # @series_id = content
383 elsif 'calibre:series-index' == name
385 elsif 'cover' == name
387 #puts 'File ' + @path + ' coverId ' + coverId
394 @cover = load_cover(zipfile, opfPath, opfDoc, coverId)
398 def load_cover(zipfile, opfPath, opfDoc, coverId)
401 coverId = "cover-image"
404 items = opfDoc.css('package manifest item')
408 mimeType = i['media-type']
411 entry = zipfile.find_entry(href)
414 # Although the epub standard requires the path to be relative
415 # to the base of the epub (zip), some books encountered in the
416 # wild have been found to use a bath relative to the location
418 parts = opfPath.split('/')
419 opfBasePath = opfPath.split('/')[0..-2].join('/')
420 coverPath = opfBasePath + '/' + href
421 entry = zipfile.find_entry(coverPath)
425 puts 'WARNING! Cover image "' + href + '" not found in file "' + @path + '".'
428 entry.get_input_stream() do |is|
429 return Cover.new(is, href, mimeType)