From: Chris Jaekl Date: Tue, 4 Jul 2017 13:22:34 +0000 (+0900) Subject: Add DDC and LCC info to book headers in page display. X-Git-Url: https://jaekl.net/gitweb/?a=commitdiff_plain;h=b8fe580f73b094e366643388ecd2d184b643616a;p=quanlib.git Add DDC and LCC info to book headers in page display. Also, drop duplicate PDF and EPUB holdings, preferring EPUB. --- diff --git a/book.rb b/book.rb index 5b698e7..b0a1bbf 100644 --- 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('
') end diff --git a/classification.rb b/classification.rb new file mode 100644 index 0000000..2061e46 --- /dev/null +++ b/classification.rb @@ -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 ac1c907..a94c90f 100644 --- a/page.rb +++ b/page.rb @@ -1,6 +1,6 @@ require 'fileutils' -require 'store' +require 'store' class Page def initialize(store) diff --git a/store.rb b/store.rb index 660fc80..b1e3d7f 100644 --- 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 diff --git a/walkdir.rb b/walkdir.rb index 6ef98c2..fb23fcf 100644 --- a/walkdir.rb +++ b/walkdir.rb @@ -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)