Class: BibleTools::Scraper

Inherits:
Object
  • Object
show all
Defined in:
lib/bibletools.rb

Instance Method Summary collapse

Constructor Details

#initialize(book: 'Exodus', url: '', debug: false) ⇒ Scraper

Returns a new instance of Scraper.

Raises:



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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/bibletools.rb', line 96

def initialize(book: 'Exodus', url: '', debug: false)
  
  raise ScraperErr, 'Please specify the URL' if url.empty?
  
  baseurl = url

  id = if book.is_a? String then
    i = BOOKS.index(book.gsub(/\b./){|x| x.upcase})
    puts 'book *' + book + '* not found' unless i
    (i + 1).to_s
  else
    book.to_s
  end
  
  puts 'id: ' + id.inspect if @debug

  url2 = baseurl + '?id=' + id
  doc = Nokorexi.new(url2).to_doc
  e = doc.root.at_css('.pagination')
  endchapter = e.xpath('li')[-2].text('a')

  book = Rexle.new('<book/>')

  (1..endchapter.to_i).each do |chp|

    c = chp.to_s
    puts 'reading chapter ' + c

    sleep 1
    url2 = baseurl + ("?id=%s&bible_chapter=%s" % [id, c])
    doc = Nokorexi.new(url2).to_doc
    a = doc.root.xpath('//div/p')
    a2 = a.select {|e| e2 = e.element('a'); e2.attributes[:name] if e2}

    chapter = Rexle::Element.new('chapter', attributes: {no: c})

    a2.each.with_index do |e, i|

      puts 'processing verse ' + i.to_s
      txt = e.plaintext
      n = txt.slice!(0,2).to_i.to_s
      chapter.add Rexle::Element.new('verse', attributes: {no: n}).add_text(txt)

    end

    book.root.add chapter

  end

  @doc = book

end

Instance Method Details

#save(title) ⇒ Object



153
154
155
156
157
158
159
# File 'lib/bibletools.rb', line 153

def save(title)
  
  filename = title.downcase.gsub(/\s+/,'-') + '.xml'      
  File.write filename, @doc.root.xml(pretty: true)
  puts filename +' saved!'
  
end

#to_docObject



149
150
151
# File 'lib/bibletools.rb', line 149

def to_doc
  @doc
end