Stores book metadata to PostgreSQL database.
[quanlib.git] / walkdir.rb
1 # Walk the directory (and subdirectories), identifying books.
2 #
3 # Expected format:
4 #   .../AuthorName/Title_of_the_Awesome_Book.ext
5 #
6 # Author is given as FirstLast.  For example, 
7 # Robert Anson Heinlein is RoberHeinlein, and 
8 # JKRowling is JoanneRowling.
9 #
10 # Book titles have spaces replaced with underscores,
11 # and punctuation [,!?'] replaced with hyphens.
12 #
13 # If the book forms part of a series, then an all-capitals 
14 # series designator, followed by a numeric volume number, 
15 # followed by an underscore, is prefixed to the name.
16 # For example, Hardy Boys' volume 1, The Tower Treasure, 
17 # is rendered as .../FranklinDixon/HB001_The_Tower_Treasure.epub
18 # and Mrs. Pollifax volume 6, On the China Station, is
19 # .../DorothyGilman/P06_On_the_China_Station.epub.
20
21 require 'book'
22 require 'store'
23
24 class WalkDir
25   def initialize(store, root)
26     @root = root
27     @store = store
28     @files = walk(@root)
29   end
30
31   def books
32     result = []
33     for file in @files.sort
34       if Book.canHandle?(file)
35         book = Book.new(file)
36         @store.store_book(book)
37         result.push(book)
38       end
39     end
40     return result
41   end
42
43   def walk(path)
44     result = []
45     children = Dir.entries(path)
46     for child in children
47       fullName = (path.chomp("/")) + "/" + child
48       if (File.directory?(fullName)) and (child != ".") and (child != "..") and (!File.symlink?(fullName))
49         sub = walk(fullName)
50         if (sub != nil) and (sub.length > 0)
51           result.concat(sub)
52         end
53       elsif (! File.directory?(fullName))
54         result.push(fullName)
55       end
56     end
57     #puts result
58     return result
59   end
60 end