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/'
15 attr_accessor :classification_id
17 attr_accessor :description
18 attr_accessor :language
20 attr_accessor :series_id
28 def load_from_file!(fileName)
30 parse_file_name!(fileName)
33 def self.can_handle?(fileName)
38 #puts "Filename: " + fileName.to_s
39 lowerName = fileName.downcase()
41 if lowerName.end_with?(".epub")
45 if lowerName.end_with?(".pdf")
56 result.push('<b>' + @title + '</b>')
58 result.push('<i>(Unknown title)</i>')
61 result.push('<i>by ' + @author.reading_order + '</i>')
65 series = @store.load_series(@series_id)
66 if nil != series and nil != series.descr
67 seriesInfo.push(series.descr.to_s)
70 seriesInfo.push(@volume.to_s)
72 if seriesInfo.length > 0
73 result.push(seriesInfo.join(' '))
77 if nil != @classification_id
78 classification = @store.load_classification(@classification_id)
80 if nil != classification
81 if nil != classification.ddc
82 result.push('Dewey: ' + classification.ddc.to_s)
84 if nil != classification.lcc
85 result.push('LCC: ' + classification.lcc.to_s)
89 return result.join('<br/>')
95 data.push('author="' + @author.inspect + '"')
98 data.push('series_id="' + @series_id.to_s() + '"')
101 data.push('volume="' + @volume + '"')
104 data.push('title="' + @title + '"')
107 data.push(@cover.inspect())
110 data.push('path="' + @path + '"')
112 return '(Book:' + data.join(',') + ')'
124 return File.basename(@path, '.*')
129 return /[[:upper:]]/.match(c)
133 def massage_author(input)
139 input.each_char do |c|
140 if isUpper?(c) and (reading_order.length > 0)
149 # Returns (series, volumeNo, titleText)
151 def processTitle(input)
156 arr = input.split('_')
162 matchData = (arr[0]).match(/^([A-Z]+)([0-9]+)$/)
164 capt = matchData.captures
170 pos = arr[-1].rindex('.')
172 arr[-1] = arr[-1].slice(0, pos)
175 title = arr.join(' ')
177 return series, vol, title
181 def parse_file_name!(file_name)
182 category = nil # e.g., non-fiction, fan-fiction
185 parts = file_name.split('/')
186 (series_code, @volume, @title) = processTitle(parts[-1])
189 reading_order = massage_author(grouping)
191 @author = Author.new(grouping, reading_order, sort_order)
192 @series_id = @store.get_series(grouping, series_code)
198 lc_file_name = file_name.downcase
199 if lc_file_name.end_with?(".epub")
201 elsif lc_file_name.end_with?(".pdf")
205 @classification_id = @store.find_classification(@author.grouping, File.basename(file_name, '.*'))
207 # TODO: Fix horrible hard-coded strings and paths
208 if ('01_nonfic' == category) && (nil == classification_id)
209 open(Store.unclassified_csv, 'a') do |fd|
210 fd.puts('"' + grouping.to_s + '","' + path + '"')
216 def scanEpub!(fileName)
217 #puts 'Scanning "' + fileName.to_s + '"...'
219 Zip.warn_invalid_date = false
220 Zip::File.open(fileName) do |zipfile|
221 entry = zipfile.find_entry('META-INF/container.xml')
223 puts 'No META-INF/container.xml, skipping book ' + fileName
226 contXml = zipfile.read('META-INF/container.xml')
227 contDoc = Nokogiri::XML(contXml)
228 opfPath = contDoc.css("container rootfiles rootfile")[0]['full-path']
230 scanOpf!(zipfile, opfPath)
232 rescue Zip::Error => exc
233 puts 'ERROR processing file "' + fileName + '":'
240 def scan_pdf!(file_name)
241 #puts 'Scanning "' + file_name.to_s + '"...'
243 pdf_path = File.expand_path(file_name).to_s
244 if ! pdf_path.end_with?('.pdf')
245 puts 'Unexpected internal error: path "' + file_name.to_s + '" does not end with ".pdf".'
249 jpeg_path = pdf_path[0..-5] + '.jpeg'
250 if File.file?(jpeg_path)
251 File.open(jpeg_path, 'r') do |is|
252 @cover = Cover.new(is, jpeg_path, 'image/jpeg')
259 def scanOpf!(zipfile, opfPath)
262 opfXml = zipfile.read(opfPath)
263 opfDoc = Nokogiri::XML(opfXml)
268 grouping = @author.grouping
269 reading_order = @author.reading_order
270 sort_order = @author.sort_order
272 creators = opfDoc.css('dc|creator', 'dc' => @@DC_NS_URL)
273 if (creators.length > 0)
274 creator = creators[0]
276 role = creator['opf:role']
278 reading_order = creator.content
280 file_as = creator['opf:file-as']
286 @author = Author.new(grouping, reading_order, sort_order)
290 #---------------------------------------
293 titles = opfDoc.css('dc|title', 'dc' => @@DC_NS_URL)
297 @title = title.content
301 #---------------------------------------
304 descrNodes = opfDoc.css('dc|description', 'dc' => @@DC_NS_URL)
305 if (descrNodes.length > 0)
306 descrNode = descrNodes[0]
308 @description = descrNode.content
312 #---------------------------------------
315 langNodes = opfDoc.css('dc|language', 'dc' => @@DC_NS_URL)
316 if (langNodes.length > 0)
317 langNode = langNodes[0]
319 @language = langNode.content
323 #---------------------------------------
324 # Other metadata: series, volume, cover
326 metas = opfDoc.css('package metadata meta')
329 content = m['content']
331 if 'calibre:series' == name
332 # TODO: Dynamically create a new series?
333 # @series_id = content
334 elsif 'calibre:series-index' == name
336 elsif 'cover' == name
338 #puts 'File ' + @path + ' coverId ' + coverId
345 @cover = load_cover(zipfile, opfPath, opfDoc, coverId)
349 def load_cover(zipfile, opfPath, opfDoc, coverId)
352 coverId = "cover-image"
355 items = opfDoc.css('package manifest item')
359 mimeType = i['media-type']
362 entry = zipfile.find_entry(href)
365 # Although the epub standard requires the path to be relative
366 # to the base of the epub (zip), some books encountered in the
367 # wild have been found to use a bath relative to the location
369 parts = opfPath.split('/')
370 opfBasePath = opfPath.split('/')[0..-2].join('/')
371 coverPath = opfBasePath + '/' + href
372 entry = zipfile.find_entry(coverPath)
376 # Another case found in the wild: cover image is at the root, but path is '../cover.jpeg'
377 if href.start_with? '../'
378 coverPath = href[3..-1]
379 entry = zipfile.find_entry(coverPath)
384 puts 'WARNING! Cover image "' + href + '" not found in file "' + @path + '".'
387 entry.get_input_stream() do |is|
388 return Cover.new(is, href, mimeType)