6 require_relative 'author'
7 require_relative 'classification'
8 require_relative 'cover'
9 require_relative 'store'
12 @@DC_NS_URL = 'http://purl.org/dc/elements/1.1/'
16 @classification_id = nil
26 def load_from_file!(fileName)
28 parse_file_name!(fileName)
31 def self.can_handle?(fileName)
36 #puts "Filename: " + fileName.to_s
37 lowerName = fileName.downcase()
39 if lowerName.end_with?(".epub")
43 if lowerName.end_with?(".pdf")
62 def classification_id=(value)
63 @classification_id = value
78 def description=(value)
86 result.push('<b>' + @title + '</b>')
88 result.push('<i>(Unknown title)</i>')
91 result.push('<i>by ' + @author.reading_order + '</i>')
95 series = @store.load_series(@series_id)
96 if nil != series and nil != series.descr
97 seriesInfo.push(series.descr.to_s)
100 seriesInfo.push(@volume.to_s)
102 if seriesInfo.length > 0
103 result.push(seriesInfo.join(' '))
107 if nil != @classification_id
108 classification = @store.load_classification(@classification_id)
110 if nil != classification
111 if nil != classification.ddc
112 result.push('Dewey: ' + classification.ddc.to_s)
114 if nil != classification.lcc
115 result.push('LCC: ' + classification.lcc.to_s)
119 return result.join('<br/>')
125 data.push('author="' + @author.inspect + '"')
128 data.push('series_id="' + @series_id.to_s() + '"')
131 data.push('volume="' + @volume + '"')
134 data.push('title="' + @title + '"')
137 data.push(@cover.inspect())
140 data.push('path="' + @path + '"')
142 return '(Book:' + data.join(',') + ')'
157 def series_id=(value)
178 return File.basename(@path, '.*')
191 return /[[:upper:]]/.match(c)
195 def massage_author(input)
201 input.each_char do |c|
202 if isUpper?(c) and (reading_order.length > 0)
211 # Returns (series, volumeNo, titleText)
213 def processTitle(input)
218 arr = input.split('_')
224 matchData = (arr[0]).match(/^([A-Z]+)([0-9]+)$/)
226 capt = matchData.captures
232 pos = arr[-1].rindex('.')
234 arr[-1] = arr[-1].slice(0, pos)
237 title = arr.join(' ')
239 return series, vol, title
243 def parse_file_name!(file_name)
244 category = nil # e.g., non-fiction, fan-fiction
247 parts = file_name.split('/')
248 (series_code, @volume, @title) = processTitle(parts[-1])
251 reading_order = massage_author(grouping)
253 @author = Author.new(grouping, reading_order, sort_order)
254 @series_id = @store.get_series(grouping, series_code)
260 lc_file_name = file_name.downcase
261 if lc_file_name.end_with?(".epub")
263 elsif lc_file_name.end_with?(".pdf")
267 @classification_id = @store.find_classification(@author.grouping, File.basename(file_name, '.*'))
269 # TODO: Fix horrible hard-coded strings and paths
270 if ('01_nonfic' == category) && (nil == classification_id)
271 open(Store.unclassified_csv, 'a') do |fd|
272 fd.puts('"' + grouping.to_s + '","' + path + '"')
278 def scanEpub!(fileName)
279 #puts 'Scanning "' + fileName.to_s + '"...'
281 Zip.warn_invalid_date = false
282 Zip::File.open(fileName) do |zipfile|
283 entry = zipfile.find_entry('META-INF/container.xml')
285 puts 'No META-INF/container.xml, skipping book ' + fileName
288 contXml = zipfile.read('META-INF/container.xml')
289 contDoc = Nokogiri::XML(contXml)
290 opfPath = contDoc.css("container rootfiles rootfile")[0]['full-path']
292 scanOpf!(zipfile, opfPath)
294 rescue Zip::Error => exc
295 puts 'ERROR processing file "' + fileName + '":'
302 def scan_pdf!(file_name)
303 #puts 'Scanning "' + file_name.to_s + '"...'
305 pdf_path = File.expand_path(file_name).to_s
306 if ! pdf_path.end_with?('.pdf')
307 puts 'Unexpected internal error: path "' + file_name.to_s + '" does not end with ".pdf".'
311 jpeg_path = pdf_path[0..-5] + '.jpeg'
312 if File.file?(jpeg_path)
313 File.open(jpeg_path, 'r') do |is|
314 @cover = Cover.new(is, jpeg_path, 'image/jpeg')
321 def scanOpf!(zipfile, opfPath)
324 opfXml = zipfile.read(opfPath)
325 opfDoc = Nokogiri::XML(opfXml)
330 grouping = @author.grouping
331 reading_order = @author.reading_order
332 sort_order = @author.sort_order
334 creators = opfDoc.css('dc|creator', 'dc' => @@DC_NS_URL)
335 if (creators.length > 0)
336 creator = creators[0]
338 role = creator['opf:role']
340 reading_order = creator.content
342 file_as = creator['opf:file-as']
348 @author = Author.new(grouping, reading_order, sort_order)
352 #---------------------------------------
355 titles = opfDoc.css('dc|title', 'dc' => @@DC_NS_URL)
359 @title = title.content
363 #---------------------------------------
366 descrNodes = opfDoc.css('dc|description', 'dc' => @@DC_NS_URL)
367 if (descrNodes.length > 0)
368 descrNode = descrNodes[0]
370 @description = descrNode.content
374 #---------------------------------------
375 # Other metadata: series, volume, cover
377 metas = opfDoc.css('package metadata meta')
380 content = m['content']
382 if 'calibre:series' == name
383 # TODO: Dynamically create a new series?
384 # @series_id = content
385 elsif 'calibre:series-index' == name
387 elsif 'cover' == name
389 #puts 'File ' + @path + ' coverId ' + coverId
396 @cover = load_cover(zipfile, opfPath, opfDoc, coverId)
400 def load_cover(zipfile, opfPath, opfDoc, coverId)
403 coverId = "cover-image"
406 items = opfDoc.css('package manifest item')
410 mimeType = i['media-type']
413 entry = zipfile.find_entry(href)
416 # Although the epub standard requires the path to be relative
417 # to the base of the epub (zip), some books encountered in the
418 # wild have been found to use a bath relative to the location
420 parts = opfPath.split('/')
421 opfBasePath = opfPath.split('/')[0..-2].join('/')
422 coverPath = opfBasePath + '/' + href
423 entry = zipfile.find_entry(coverPath)
427 # Another case found in the wild: cover image is at the root, but path is '../cover.jpeg'
428 if href.start_with? '../'
429 coverPath = href[3..-1]
430 entry = zipfile.find_entry(coverPath)
435 puts 'WARNING! Cover image "' + href + '" not found in file "' + @path + '".'
438 entry.get_input_stream() do |is|
439 return Cover.new(is, href, mimeType)