-
Notifications
You must be signed in to change notification settings - Fork 2
/
guten_mongo.rb
65 lines (53 loc) · 1.54 KB
/
guten_mongo.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
require 'rubygems' # not necessary for Ruby 1.9
require 'mongo'
include Mongo
def validate_encoding(str, encoding='UTF-8')
str.force_encoding(encoding).chars.collect do |c|
(c.valid_encoding?) ? c:'?'
end.join
end
db = MongoClient.new('localhost', 27017).db('gutenberg')
books = Grid.new(db)
count = 0
Dir.foreach("gutenberg") {|book|
if File.file?("gutenberg/#{book}") && !book.start_with?(".")
title = " "
author = " "
release_date = " "
language = " "
encoding = " "
content = ""
puts book
count = count + 1
puts "Count: #{count}"
File.foreach("gutenberg/#{book}") { |line|
line = validate_encoding(line)
content << line
# begin
if !line.empty?
if line.start_with?("Title: ")
title = line.sub("Title: ", "")
elsif line.start_with?("Author: ")
author = line.sub("Author: ", "")
elsif line.start_with?("Release Date: ")
release_date = line.sub("Release Date: ", "")
elsif line.start_with?("Language: ")
language = line.sub("Language: ", "")
elsif line.start_with?("Character set encoding: ")
encoding = line.sub("Character set encoding: ", "")
end
end
}
puts "*****"
new_book = { :title => title,
:content_type => "text",
:author => author,
:release_date => release_date,
:language => language,
:encoding => encoding,
:original_file_name => book,
:time => Time.now
}
books.put(content, new_book)
end
}