class BookFinder::Bok
b-ok api interface for simple use with web scrapping
Constants
- BASEURL
Public Instance Methods
search(params)
click to toggle source
# File lib/bok.rb, line 23 def search(params) setup query, page, from, to, language, order_by = params.values return :QUERY_MISSING unless query @page = page || 1 url = BASEURL + "/s/?q=#{query}&page=#{page}" if from && to url += "&yearFrom=#{from}&yearTo=#{to}" end if language url += "&language=#{language}" end if order_by url += "&order=#{order_by}" end html = URI.parse(url).open @doc = Nokogiri::HTML(html) run structure end
setup()
click to toggle source
# File lib/bok.rb, line 47 def setup @page = nil @img_by_book = [] @title_by_book = [] @mirrors_by_book = [] @authors_by_book = [] @year_by_book = [] @language_by_book = [] @extension_by_book = [] @size_by_book = [] end
Private Instance Methods
book_file()
click to toggle source
# File lib/bok.rb, line 134 def book_file @doc.search("div.bookProperty.property__file").each do |node| extension, size = node.text.to_s.gsub!(/\s+/, "") .gsub!(/\w+:/, "").split(",") @extension_by_book.push(extension) @size_by_book.push(size) end end
book_language()
click to toggle source
# File lib/bok.rb, line 128 def book_language @doc.search("div.bookProperty.property_language").each do |node| @language_by_book.push(node.text.to_s.gsub!(/\s+/, "").gsub!(/\w+:/, "")) end end
book_titles()
click to toggle source
# File lib/bok.rb, line 101 def book_titles @doc.search('[@itemprop="name"] a').each do |node| @title_by_book.push(node.text.to_s) url = BASEURL.to_s + node["href"].to_s doc = Nokogiri::HTML(URI.parse(url).open) doc.search("a.dlButton").each do |nodes| @mirrors_by_book.push(BASEURL.to_s + nodes["href"].to_s) end end end
book_year()
click to toggle source
# File lib/bok.rb, line 122 def book_year @doc.search("div.bookProperty.property_year").each do |node| @year_by_book.push(node.text.to_s.gsub!(/\s+/, "").gsub!(/\w+:/, "")) end end
img_urls()
click to toggle source
# File lib/bok.rb, line 95 def img_urls @doc.search("img.cover.lazy").each do |node| @img_by_book.push("https:" + node["data-src"].to_s) end end
run()
click to toggle source
# File lib/bok.rb, line 61 def run img_urls book_titles book_authors book_year book_language book_file end
structure()
click to toggle source
# File lib/bok.rb, line 70 def structure books = [] @img_by_book.zip( @title_by_book, @mirrors_by_book, @authors_by_book, @year_by_book, @language_by_book, @extension_by_book, @size_by_book ).each do |book| books.push({ img: book[0], title: book[1], mirrors: [book[2]], authors: book[3], year: book[4], language: book[5], extension: book[6], size: book[7] }.stringify_keys) end books end