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/'
13 @@SERIES_AND_VOLUME_REGEX = /^([A-Z]+)([0-9]+(\.[0-9]+)?)$/
16 attr_accessor :classification_id
18 attr_accessor :description
19 attr_accessor :language
21 attr_accessor :series_id
29 def load_from_file!(fileName)
31 parse_file_name!(fileName)
34 def self.can_handle?(fileName)
39 #puts "Filename: " + fileName.to_s
40 lowerName = fileName.downcase()
42 if lowerName.end_with?(".epub")
46 if lowerName.end_with?(".pdf")
53 def self.grouping_for_title(title)
56 '\'",!#'.split('').each do |c|
57 result = result.gsub(c, '-')
59 result = result.gsub(/: */, '--')
60 result = result.gsub(' ', '_')
69 result.push('<b>' + @title + '</b>')
71 result.push('<i>(Unknown title)</i>')
74 result.push('<i>by ' + @author.reading_order + '</i>')
78 series = @store.load_series(@series_id)
79 if nil != series and nil != series.descr
80 seriesInfo.push(series.descr.to_s)
83 seriesInfo.push(@volume.to_s)
85 if seriesInfo.length > 0
86 result.push(seriesInfo.join(' '))
90 if nil != @classification_id
91 classification = @store.load_classification(@classification_id)
93 if nil != classification
94 if nil != classification.ddc
95 result.push('Dewey: ' + classification.ddc.to_s)
97 if nil != classification.lcc
98 result.push('LCC: ' + classification.lcc.to_s)
102 return result.join('<br/>')
108 data.push('author="' + @author.inspect + '"')
111 data.push('series_id="' + @series_id.to_s() + '"')
114 data.push('volume="' + @volume + '"')
117 data.push('title="' + @title + '"')
120 data.push(@cover.inspect())
123 data.push('path="' + @path + '"')
125 return '(Book:' + data.join(',') + ')'
137 return File.basename(@path, '.*')
142 return /[[:upper:]]/.match(c)
146 def massage_author(input)
152 input.each_char do |c|
153 if isUpper?(c) and (reading_order.length > 0)
162 # Returns (series, volumeNo, titleText)
164 def processTitle(input)
169 arr = input.split('_')
175 matchData = (arr[0]).match(@@SERIES_AND_VOLUME_REGEX)
177 capt = matchData.captures
183 pos = arr[-1].rindex('.')
185 arr[-1] = arr[-1].slice(0, pos)
188 title = arr.join(' ')
190 bare_title_grouping = title_grouping
192 .reject { |part| part.match(@@SERIES_AND_VOLUME_REGEX) }
195 unless bare_title_grouping == Book.grouping_for_title(title)
196 puts "WARNING: title_grouping mismatch: #{bare_title_grouping.inspect} vs. #{Book.grouping_for_title(title).inspect}"
199 return series, vol, title
203 def parse_file_name!(file_name)
204 category = nil # e.g., non-fiction, fan-fiction
207 parts = file_name.split('/')
208 (series_code, @volume, @title) = processTitle(parts[-1])
211 reading_order = massage_author(grouping)
213 @author = Author.new(grouping, reading_order, sort_order)
214 @series_id = @store.get_series(grouping, series_code)
220 lc_file_name = file_name.downcase
221 if lc_file_name.end_with?(".epub")
223 elsif lc_file_name.end_with?(".pdf")
227 @classification_id = @store.find_classification(@author.grouping, File.basename(file_name, '.*'))
229 # TODO: Fix horrible hard-coded strings and paths
230 if ('01_nonfic' == category) && (nil == classification_id)
231 open(Store.unclassified_csv, 'a') do |fd|
232 fd.puts('"' + grouping.to_s + '","' + path + '"')
238 def scanEpub!(fileName)
239 #puts 'Scanning "' + fileName.to_s + '"...'
241 Zip.warn_invalid_date = false
242 Zip::File.open(fileName) do |zipfile|
243 entry = zipfile.find_entry('META-INF/container.xml')
245 puts 'No META-INF/container.xml, skipping book ' + fileName
248 contXml = zipfile.read('META-INF/container.xml')
249 contDoc = Nokogiri::XML(contXml)
250 opfPath = contDoc.css("container rootfiles rootfile")[0]['full-path']
252 scanOpf!(zipfile, opfPath)
254 rescue Zip::Error => exc
255 puts 'ERROR processing file "' + fileName + '":'
262 def scan_pdf!(file_name)
263 #puts 'Scanning "' + file_name.to_s + '"...'
265 pdf_path = File.expand_path(file_name).to_s
266 if ! pdf_path.end_with?('.pdf')
267 puts 'Unexpected internal error: path "' + file_name.to_s + '" does not end with ".pdf".'
271 jpeg_path = pdf_path[0..-5] + '.jpeg'
272 if File.file?(jpeg_path)
273 File.open(jpeg_path, 'r') do |is|
274 @cover = Cover.new(is, jpeg_path, 'image/jpeg')
281 def scanOpf!(zipfile, opfPath)
284 opfXml = zipfile.read(opfPath)
285 opfDoc = Nokogiri::XML(opfXml)
290 grouping = @author.grouping
291 reading_order = @author.reading_order
292 sort_order = @author.sort_order
294 creators = opfDoc.css('dc|creator', 'dc' => @@DC_NS_URL)
295 if (creators.length > 0)
296 creator = creators[0]
298 role = creator['opf:role']
300 reading_order = creator.content
302 file_as = creator['opf:file-as']
308 @author = Author.new(grouping, reading_order, sort_order)
312 #---------------------------------------
315 titles = opfDoc.css('dc|title', 'dc' => @@DC_NS_URL)
319 @title = title.content
323 #---------------------------------------
326 descrNodes = opfDoc.css('dc|description', 'dc' => @@DC_NS_URL)
327 if (descrNodes.length > 0)
328 descrNode = descrNodes[0]
330 @description = descrNode.content
334 #---------------------------------------
337 langNodes = opfDoc.css('dc|language', 'dc' => @@DC_NS_URL)
338 if (langNodes.length > 0)
339 langNode = langNodes[0]
341 @language = langNode.content
345 #---------------------------------------
346 # Other metadata: series, volume, cover
348 metas = opfDoc.css('package metadata meta')
351 content = m['content']
353 if 'calibre:series' == name
354 # TODO: Dynamically create a new series?
355 # @series_id = content
356 elsif 'calibre:series-index' == name
358 elsif 'cover' == name
360 #puts 'File ' + @path + ' coverId ' + coverId
367 @cover = load_cover(zipfile, opfPath, opfDoc, coverId)
371 def load_cover(zipfile, opfPath, opfDoc, coverId)
374 coverId = "cover-image"
377 items = opfDoc.css('package manifest item')
381 mimeType = i['media-type']
384 entry = zipfile.find_entry(href)
387 # Although the epub standard requires the path to be relative
388 # to the base of the epub (zip), some books encountered in the
389 # wild have been found to use a bath relative to the location
391 parts = opfPath.split('/')
392 opfBasePath = opfPath.split('/')[0..-2].join('/')
393 coverPath = opfBasePath + '/' + href
394 entry = zipfile.find_entry(coverPath)
398 # Another case found in the wild: cover image is at the root, but path is '../cover.jpeg'
399 if href.start_with? '../'
400 coverPath = href[3..-1]
401 entry = zipfile.find_entry(coverPath)
406 puts 'WARNING! Cover image "' + href + '" not found in file "' + @path + '".'
409 entry.get_input_stream() do |is|
410 return Cover.new(is, href, mimeType)