class BookFinder::Bok

b-ok api interface for simple use with web scrapping

Constants

BASEURL

Public Instance Methods

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_authors() click to toggle source
# File lib/bok.rb, line 112
def book_authors
  @doc.search('[@class="authors"]').each do |node|
    authors = {}
    node.search("a").each do |author|
      authors[author.text.to_s] = (BASEURL.to_s + author["href"].to_s)
    end
    @authors_by_book.push(authors)
  end
end
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