Add `arrived` attribute (file creation timestamp) to books table.
[quanlib.git] / book.rb
diff --git a/book.rb b/book.rb
index 93c3964d4a88939a9e1019cd1a5585411cb47404..2b93f4b574c9c45738a3a95a2c0b4f1697083d1c 100644 (file)
--- a/book.rb
+++ b/book.rb
@@ -10,11 +10,14 @@ require_relative 'store'
 
 class Book
   @@DC_NS_URL = 'http://purl.org/dc/elements/1.1/'
+  @@SERIES_AND_VOLUME_REGEX = /^([A-Z]+)([0-9]+(\.[0-9]+)?)$/
 
+  attr_accessor :arrived
   attr_accessor :author
   attr_accessor :classification_id
   attr_accessor :cover
   attr_accessor :description
+  attr_accessor :language
   attr_accessor :path
   attr_accessor :series_id
   attr_accessor :title
@@ -48,6 +51,18 @@ class Book
     return false
   end
 
+  def self.grouping_for_title(title)
+    result = title
+
+    '\'",!#'.split('').each do |c|
+      result = result.gsub(c, '-')
+    end
+    result = result.gsub(/: */, '--')
+    result = result.gsub(' ', '_')
+
+    result
+  end
+
   def heading
     result = []
 
@@ -158,7 +173,7 @@ class Book
     vol = nil
 
     first = arr[0]
-    matchData = (arr[0]).match(/^([A-Z]+)([0-9]+)$/)
+    matchData = (arr[0]).match(@@SERIES_AND_VOLUME_REGEX)
     if nil != matchData
       capt = matchData.captures
       series = capt[0]
@@ -173,6 +188,15 @@ class Book
 
     title = arr.join(' ')
 
+    bare_title_grouping = title_grouping
+      .split('_')
+      .reject { |part| part.match(@@SERIES_AND_VOLUME_REGEX) }
+      .join('_')
+
+    unless bare_title_grouping == Book.grouping_for_title(title)
+      puts "WARNING:  title_grouping mismatch:  #{bare_title_grouping.inspect} vs. #{Book.grouping_for_title(title).inspect}"
+    end
+
     return series, vol, title
   end
 
@@ -201,6 +225,8 @@ class Book
       scan_pdf!(file_name)
     end
 
+    @arrived = File.ctime(file_name)
+
     @classification_id = @store.find_classification(@author.grouping, File.basename(file_name, '.*'))
 
     # TODO:  Fix horrible hard-coded strings and paths
@@ -308,6 +334,17 @@ class Book
       end
     end
 
+    #---------------------------------------
+    # Language
+
+    langNodes = opfDoc.css('dc|language', 'dc' => @@DC_NS_URL)
+    if (langNodes.length > 0)
+      langNode = langNodes[0]
+      if langNode
+        @language = langNode.content
+      end
+    end
+
     #---------------------------------------
     # Other metadata:  series, volume, cover