forked from calvinf/Bible-API-Thing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbibles-api.rb
executable file
·130 lines (102 loc) · 3.78 KB
/
bibles-api.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/usr/bin/env ruby
# using bundler up in here, up in here
require 'rubygems'
require 'bundler/setup'
Bundler.require(:default)
# all the other things we want to use
require 'cgi' #escaping
require 'pp' #prettyprint (for errors and testing)
# other includes
require './api-key.rb' # BIBLE_KEY
require './config.rb' # configuration
require './models/Pack.rb' # Pack model
require './models/Verse.rb' # Verse model
MongoMapper.connection = Mongo::Connection.new('localhost', 27017)
MongoMapper.database = "versemachine"
# TODO move to BibleSearch class
def get_search_url(verses)
url = PASSAGES_API + '?&q[]=' + CGI.escape(verses.join(','))
return url
end
# TODO move to BibleSearch class
def get_search_result(url)
c = Curl::Easy.new(url)
c.userpwd = BIBLE_KEY + ':X'
c.perform
return c.body_str
end
#TODO look adding this method to pack
def get_pack_data(pack)
versesNeeded = check_for_verses(pack)
if versesNeeded > 0
puts "Verses needed for #{pack.get_title}"
url = get_search_url(pack.verses)
data = get_search_result(url)
@passages = get_passages(data)
# print passages
@passages.each_entry do |passage|
verse = distill(passage)
#puts verse.to_s
end
else
puts "Pack #{pack.get_title}: all verses found in MongoDB"
end
end
def check_for_verses(pack)
# see if the verses are in db or if we need to fetch them
versesNeeded = 0
pack.verses.each do |reference|
cache_key = 'esv::' + reference.downcase.gsub(/\s+/, "")
curVerse = Verse.find_by_cache_key(cache_key)
if curVerse.nil?
versesNeeded += 1
end
end
return versesNeeded
end
def get_passages(data)
# prepare to parse
@doc = Nokogiri::XML(data) do |config|
config.nocdata
end
# grab passages
return @doc.css('passages passage')
end
def distill(passage)
coder = HTMLEntities.new
passage.css('sup').remove
translation = passage.at_css('version').content
reference = passage.at_css('display').content
text_html = Nokogiri::HTML(passage.at_css('text_preview').content)
text_html.xpath("//sup").remove
#trim the leading/trailing whitespace, remove linebreaks, remove tabs, remove excessive whitespace
text = text_html.content
text = coder.encode(text)
text.strip!.gsub!(/[\n\t]/, ' ')
text.gsub!(/\s+/, " ")
verse = Verse.create({
:reference => reference,
:text => text,
:translation => translation
})
verse.save!
puts verse.to_s
return verse
end
#TMS specific stuff to refactor later
a = Pack.new('a')
b = Pack.new('b')
c = Pack.new('c')
d = Pack.new('d')
e = Pack.new('e')
#packs w/ arrays of bible verse references
a.verses = ["2 Corinthians 5:17", "Galatians 2:20", "Romans 12:1", "John 14:21", "2 Timothy 3:16", "Joshua 1:8", "John 15:7", "Philippians 4:6-7", "Matthew 18:20", "Hebrews 10:24-25", "Matthew 4:19", "Romans 1:16"]
b.verses = ["Romans 3:23", "Isaiah 53:6", "Romans 6:23", "Hebrews 9:27", "Romans 5:8", "1 Peter 3:18", "Ephesians 2:8-9", "Titus 3:5", "John 1:12", "Revelation 3:20", "1 John 5:13", "John 5:24"]
c.verses = ["1 Corinthians 3:16", "1 Corinthians 2:12", "Isaiah 41:10", "Philippians 4:13", "Lamentations 3:22-23", "Numbers 23:19", "Isaiah 26:3", "1 Peter 4:7", "Romans 8:32", "Philippians 4:19", "Hebrews 2:18", "Psalms 119:9-11"]
d.verses = ["Matthew 6:33", "Luke 9:23", "1 John 2:15-16", "Romans 12:2", "1 Corinthians 15:58", "Hebrews 12:3", "Mark 10:45", "2 Corinthians 4:5", "Proverbs 3:9-10", "2 Corinthians 9:6-7", "Acts 1:8", "Matthew 28:19-20"]
e.verses = ["John 13:34-35", "1 John 3:18", "Philippians 2:3-4", "1 Peter 5:5-6", "Ephesians 5:3", "1 Peter 2:11", "Leviticus 19:11", "Acts 24:16", "Hebrews 11:6", "Romans 4:20-21", "Galatians 6:9-10", "Matthew 5:16"]
packs = [a, b, c, d, e]
# loop through each pack
packs.each do |pack|
get_pack_data(pack)
end