From 0104e6084e78bc47f39ad73698bd8d63090d5eef Mon Sep 17 00:00:00 2001 From: Chris Jaekl Date: Thu, 23 Feb 2017 22:01:04 +0900 Subject: [PATCH] Initial commit --- author.rb | 7 ++++ book.rb | 111 +++++++++++++++++++++++++++++++++++++++++++++++++++++ main.rb | 10 +++++ walkdir.rb | 56 +++++++++++++++++++++++++++ 4 files changed, 184 insertions(+) create mode 100644 author.rb create mode 100644 book.rb create mode 100644 main.rb create mode 100644 walkdir.rb diff --git a/author.rb b/author.rb new file mode 100644 index 0000000..6bfe610 --- /dev/null +++ b/author.rb @@ -0,0 +1,7 @@ + +class Author + def initialize(surname, givenNames) + @surname = surname + @givenNames = givenNames + end +end diff --git a/book.rb b/book.rb new file mode 100644 index 0000000..5f14aed --- /dev/null +++ b/book.rb @@ -0,0 +1,111 @@ + +require './author.rb' + +class Book + def initialize(fileName) + @author = nil + @cover = nil + @series = nil + @title = nil + @volume = nil + + parseFileName!(fileName) + end + + def self.canHandle?(fileName) + if nil == fileName + return false + end + + lowerName = fileName.downcase() + + if lowerName.end_with?(".epub") + return true + end + + return false + end + + def inspect + data = [] + if nil != @series + data.push('series="' + @series + '"') + end + if nil != @volume + data.push('volume="' + @volume + '"') + end + if nil != @title + data.push('title="' + @title + '"') + end + if nil != @author + data.push('author="' + @author + '"') + end + return '(Book:' + data.join(',') + ')' + end + + def to_s + return inspect() + end + + protected + def isUpper?(c) + return /[[:upper:]]/.match(c) + end + + protected + def massageAuthor(input) + if nil == input + return nil + end + + result = "" + input.each_char do |c| + if isUpper?(c) and (result.length > 0) + result += " " + end + result += c + end + + return result + end + + # Returns (series, volumeNo, titleText) + protected + def processTitle(input) + if nil == input + return nil + end + + arr = input.split('_') + + series = nil + vol = nil + + first = arr[0] + matchData = (arr[0]).match(/^([A-Z]+)([0-9]+)$/) + if nil != matchData + capt = matchData.captures + series = capt[0] + vol = capt[1] + arr.shift + end + + pos = arr[-1].rindex('.') + if nil != pos + arr[-1] = arr[-1].slice(0, pos) + end + + title = arr.join(' ') + + return series, vol, title + end + + protected + def parseFileName!(fileName) + parts = fileName.split('/') + (@series, @volume, @title) = processTitle(parts[-1]) + if parts.length > 1 + @author = massageAuthor(parts[-2]) + end + end +end diff --git a/main.rb b/main.rb new file mode 100644 index 0000000..98f5a35 --- /dev/null +++ b/main.rb @@ -0,0 +1,10 @@ +require './walkdir.rb' + +books = [] + +for arg in ARGV + w = WalkDir.new(arg) + books.push(w.books) +end + +puts books diff --git a/walkdir.rb b/walkdir.rb new file mode 100644 index 0000000..b54d0a2 --- /dev/null +++ b/walkdir.rb @@ -0,0 +1,56 @@ +# Walk the directory (and subdirectories), identifying books. +# +# Expected format: +# .../AuthorName/Title_of_the_Awesome_Book.ext +# +# Author is given as FirstLast. For example, +# Robert Anson Heinlein is RoberHeinlein, and +# JKRowling is JoanneRowling. +# +# Book titles have spaces replaced with underscores, +# and punctuation [,!?'] replaced with hyphens. +# +# If the book forms part of a series, then an all-capitals +# series designator, followed by a numeric volume number, +# followed by an underscore, is prefixed to the name. +# For example, Hardy Boys' volume 1, The Tower Treasure, +# is rendered as .../FranklinDixon/HB001_The_Tower_Treasure.epub +# and Mrs. Pollifax volume 6, On the China Station, is +# .../DorothyGilman/P06_On_the_China_Station.epub. + +require './book.rb' + +class WalkDir + def initialize(root) + @root = root + @files = walk(@root) + end + + def books + result = [] + for file in @files + if Book.canHandle?(file) + book = Book.new(file) + result.push(book) + end + end + return result + end + + def walk(path) + result = [] + children = Dir.entries(path) + for child in children + fullName = (path.chomp("/")) + "/" + child + if (File.directory?(fullName)) and (child != ".") and (child != "..") + sub = walk(fullName) + if (sub != nil) and (sub.length > 0) + result.concat(sub) + end + elsif (! File.directory?(fullName)) + result.push(fullName) + end + end + return result + end +end -- 2.30.2