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
19 attr_accessor :series_id
27 def load_from_file!(fileName)
29 parse_file_name!(fileName)
32 def self.can_handle?(fileName)
37 #puts "Filename: " + fileName.to_s
38 lowerName = fileName.downcase()
40 if lowerName.end_with?(".epub")
44 if lowerName.end_with?(".pdf")
55 result.push('<b>' + @title + '</b>')
57 result.push('<i>(Unknown title)</i>')
60 result.push('<i>by ' + @author.reading_order + '</i>')
64 series = @store.load_series(@series_id)
65 if nil != series and nil != series.descr
66 seriesInfo.push(series.descr.to_s)
69 seriesInfo.push(@volume.to_s)
71 if seriesInfo.length > 0
72 result.push(seriesInfo.join(' '))
76 if nil != @classification_id
77 classification = @store.load_classification(@classification_id)
79 if nil != classification
80 if nil != classification.ddc
81 result.push('Dewey: ' + classification.ddc.to_s)
83 if nil != classification.lcc
84 result.push('LCC: ' + classification.lcc.to_s)
88 return result.join('<br/>')
94 data.push('author="' + @author.inspect + '"')
97 data.push('series_id="' + @series_id.to_s() + '"')
100 data.push('volume="' + @volume + '"')
103 data.push('title="' + @title + '"')
106 data.push(@cover.inspect())
109 data.push('path="' + @path + '"')
111 return '(Book:' + data.join(',') + ')'
123 return File.basename(@path, '.*')
128 return /[[:upper:]]/.match(c)
132 def massage_author(input)
138 input.each_char do |c|
139 if isUpper?(c) and (reading_order.length > 0)
148 # Returns (series, volumeNo, titleText)
150 def processTitle(input)
155 arr = input.split('_')
161 matchData = (arr[0]).match(/^([A-Z]+)([0-9]+)$/)
163 capt = matchData.captures
169 pos = arr[-1].rindex('.')
171 arr[-1] = arr[-1].slice(0, pos)
174 title = arr.join(' ')
176 return series, vol, title
180 def parse_file_name!(file_name)
181 category = nil # e.g., non-fiction, fan-fiction
184 parts = file_name.split('/')
185 (series_code, @volume, @title) = processTitle(parts[-1])
188 reading_order = massage_author(grouping)
190 @author = Author.new(grouping, reading_order, sort_order)
191 @series_id = @store.get_series(grouping, series_code)
197 lc_file_name = file_name.downcase
198 if lc_file_name.end_with?(".epub")
200 elsif lc_file_name.end_with?(".pdf")
204 @classification_id = @store.find_classification(@author.grouping, File.basename(file_name, '.*'))
206 # TODO: Fix horrible hard-coded strings and paths
207 if ('01_nonfic' == category) && (nil == classification_id)
208 open(Store.unclassified_csv, 'a') do |fd|
209 fd.puts('"' + grouping.to_s + '","' + path + '"')
215 def scanEpub!(fileName)
216 #puts 'Scanning "' + fileName.to_s + '"...'
218 Zip.warn_invalid_date = false
219 Zip::File.open(fileName) do |zipfile|
220 entry = zipfile.find_entry('META-INF/container.xml')
222 puts 'No META-INF/container.xml, skipping book ' + fileName
225 contXml = zipfile.read('META-INF/container.xml')
226 contDoc = Nokogiri::XML(contXml)
227 opfPath = contDoc.css("container rootfiles rootfile")[0]['full-path']
229 scanOpf!(zipfile, opfPath)
231 rescue Zip::Error => exc
232 puts 'ERROR processing file "' + fileName + '":'
239 def scan_pdf!(file_name)
240 #puts 'Scanning "' + file_name.to_s + '"...'
242 pdf_path = File.expand_path(file_name).to_s
243 if ! pdf_path.end_with?('.pdf')
244 puts 'Unexpected internal error: path "' + file_name.to_s + '" does not end with ".pdf".'
248 jpeg_path = pdf_path[0..-5] + '.jpeg'
249 if File.file?(jpeg_path)
250 File.open(jpeg_path, 'r') do |is|
251 @cover = Cover.new(is, jpeg_path, 'image/jpeg')
258 def scanOpf!(zipfile, opfPath)
261 opfXml = zipfile.read(opfPath)
262 opfDoc = Nokogiri::XML(opfXml)
267 grouping = @author.grouping
268 reading_order = @author.reading_order
269 sort_order = @author.sort_order
271 creators = opfDoc.css('dc|creator', 'dc' => @@DC_NS_URL)
272 if (creators.length > 0)
273 creator = creators[0]
275 role = creator['opf:role']
277 reading_order = creator.content
279 file_as = creator['opf:file-as']
285 @author = Author.new(grouping, reading_order, sort_order)
289 #---------------------------------------
292 titles = opfDoc.css('dc|title', 'dc' => @@DC_NS_URL)
296 @title = title.content
300 #---------------------------------------
303 descrNodes = opfDoc.css('dc|description', 'dc' => @@DC_NS_URL)
304 if (descrNodes.length > 0)
305 descrNode = descrNodes[0]
307 @description = descrNode.content
311 #---------------------------------------
312 # Other metadata: series, volume, cover
314 metas = opfDoc.css('package metadata meta')
317 content = m['content']
319 if 'calibre:series' == name
320 # TODO: Dynamically create a new series?
321 # @series_id = content
322 elsif 'calibre:series-index' == name
324 elsif 'cover' == name
326 #puts 'File ' + @path + ' coverId ' + coverId
333 @cover = load_cover(zipfile, opfPath, opfDoc, coverId)
337 def load_cover(zipfile, opfPath, opfDoc, coverId)
340 coverId = "cover-image"
343 items = opfDoc.css('package manifest item')
347 mimeType = i['media-type']
350 entry = zipfile.find_entry(href)
353 # Although the epub standard requires the path to be relative
354 # to the base of the epub (zip), some books encountered in the
355 # wild have been found to use a bath relative to the location
357 parts = opfPath.split('/')
358 opfBasePath = opfPath.split('/')[0..-2].join('/')
359 coverPath = opfBasePath + '/' + href
360 entry = zipfile.find_entry(coverPath)
364 # Another case found in the wild: cover image is at the root, but path is '../cover.jpeg'
365 if href.start_with? '../'
366 coverPath = href[3..-1]
367 entry = zipfile.find_entry(coverPath)
372 puts 'WARNING! Cover image "' + href + '" not found in file "' + @path + '".'
375 entry.get_input_stream() do |is|
376 return Cover.new(is, href, mimeType)