Add DDC and LCC info to book headers in page display.
authorChris Jaekl <cejaekl@yahoo.com>
Tue, 4 Jul 2017 13:22:34 +0000 (22:22 +0900)
committerChris Jaekl <cejaekl@yahoo.com>
Tue, 4 Jul 2017 13:22:34 +0000 (22:22 +0900)
Also, drop duplicate PDF and EPUB holdings, preferring EPUB.

book.rb
classification.rb [new file with mode: 0644]
page.rb
store.rb
walkdir.rb

diff --git a/book.rb b/book.rb
index 5b698e7a474e2909433c0b6c893dc13af802c193..b0a1bbff66de2851b9bab47b8ad6eb0d16cc65f3 100644 (file)
--- a/book.rb
+++ b/book.rb
@@ -3,6 +3,7 @@ require 'nokogiri'
 require 'zip'
 
 require 'author'
+require 'classification'
 require 'cover'
 require 'store'
 
@@ -101,6 +102,19 @@ class Book
       result.push(seriesInfo.join(' '))
     end
 
+    classification = nil
+    if nil != @classification_id
+      classification = @store.load_classification(@classification_id)
+    end
+    if nil != classification
+      if nil != classification.ddc
+        result.push('Dewey: ' + classification.ddc.to_s)
+      end
+      if nil != classification.lcc
+        result.push('LCC: ' + classification.lcc.to_s)
+      end
+    end
+
     return result.join('<br/>')
   end
 
diff --git a/classification.rb b/classification.rb
new file mode 100644 (file)
index 0000000..2061e46
--- /dev/null
@@ -0,0 +1,75 @@
+
+class Classification
+  def initialize(ddc, lcc, author_grouping, author, title_grouping, title)
+    @id = nil
+    @ddc = ddc
+    @lcc = lcc
+    @author_grouping = author_grouping
+    @author = author
+    @title_grouping = title_grouping
+    @title = title
+  end
+
+  def id
+    @id
+  end
+  def id=(value)
+    @id = value
+  end
+
+  def ddc
+    @ddc
+  end
+  def lcc
+    @lcc
+  end
+  def author_grouping
+    @author_grouping
+  end
+  def author
+    @author
+  end
+  def 
+
+  def inspect
+    data = []
+    if nil != @ddc
+      data.push('Dewey=' + @ddc.to_s)
+    end
+    if nil != @lcc
+      data.push('LCC=' + @lcc.to_s)
+    end
+    if nil != @author_grouping
+      data.push('author_grouping=' + @author_grouping.to_s)
+    end
+    if nil != @author
+      data.push('author=' + @author.to_s)
+    end
+    if nil != @title_grouping
+      data.push('title_grouping=' + @title_grouping.to_s)
+    end
+    if nil != @title
+      data.push('title=' + @title)
+    end
+
+    return '(Classification:' + data.join(',') + ')'
+  end
+
+  def to_s
+    inspect
+  end
+
+  protected
+  def reading_to_sort_order(reading_order)
+    sort_order = reading_order
+
+    parts = reading_order.split(' ')
+    if parts.length > 1
+      sort_order = parts[-1] + ', ' + parts[0..-2].join(' ')
+    end
+
+    return sort_order
+  end
+end
+
diff --git a/page.rb b/page.rb
index ac1c90738769d5032c5794f5eb4e4bd627c3be4f..a94c90f08109d99f95feed709c8a14d0494a6b40 100644 (file)
--- a/page.rb
+++ b/page.rb
@@ -1,6 +1,6 @@
 require 'fileutils'
-require 'store'
 
+require 'store'
 
 class Page
   def initialize(store)
index 660fc80bb64464fcb9aa20e18aa4c9a797666294..b1e3d7f7bb1e06b2540689d02a8742c957efc333 100644 (file)
--- a/store.rb
+++ b/store.rb
@@ -41,8 +41,8 @@ class Store
       CREATE TABLE Authors (
         id          INTEGER PRIMARY KEY,
         grouping    VARCHAR(64),
-        reading     VARCHAR(128),
-        sort        VARCHAR(128)
+        reading     VARCHAR(196),
+        sort        VARCHAR(196)
       );
 EOS
 
@@ -289,6 +289,28 @@ EOS
     return nil
   end
 
+  def load_classification(id)
+    sql  = "SELECT ddc, lcc, author_grouping, author_sort, title_grouping, title "
+    sql += " FROM Classifications WHERE id=$1"
+    @conn.exec_params(sql, [id]) do |rs|
+      if rs.ntuples > 0
+        row = rs[0]
+        ddc = row['ddc']
+        lcc = row['lcc']
+        author_grouping = row['author_grouping']
+        author = row['author_sort']
+        title_grouping = row['title_grouping']
+        title = row['title']
+
+        result = Classification.new(ddc, lcc, author_grouping, author, title_grouping, title)
+        result.id = id
+        return result
+      end
+    end
+
+    return nil
+  end
+
   def load_cover(id)
     if nil == id
       return nil
index 6ef98c242de8d285ff8479e65d961b6911cbe7ea..fb23fcf56d44d1fd8bbbad76153aabd2debb6898 100644 (file)
@@ -30,7 +30,8 @@ class WalkDir
 
   def books
     result = []
-    for file in @files.sort
+    @files = remove_duplicates(@files)
+    for file in @files.sort()
       if Book.can_handle?(file) && (!is_duplicate?(file))
         book = Book.new(@store)
         book.load_from_file!(file)
@@ -57,6 +58,30 @@ class WalkDir
     return false
   end
 
+  def remove_duplicates(files)
+    unique = {}
+    for file in files
+      if Book.can_handle?(file)
+        key = File.dirname(file) + '/' + File.basename(file, '.*')
+        if unique.has_key?(key)
+          new_ext = File.extname(file)
+          old_ext = File.extname(unique[key])
+          if ('.pdf' == old_ext) && ('.epub' == new_ext)
+            # Prefer EPUB over PDF
+            puts 'REPLACED ' + unique[key].to_s + ' with ' + file.to_s
+            unique[key] = file
+          else
+            puts 'DROPPED ' + file.to_s + " because it's superceded by " + unique[key].to_s
+          end
+        else
+          unique[key] = file
+        end
+      end
+    end
+
+    return unique.values
+  end
+
   def walk(path)
     result = []
     children = Dir.entries(path)