Class: Libri::Scraper

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#awardObject

Returns the value of attribute award.



3
4
5
# File 'lib/libri/scraper.rb', line 3

def award
  @award
end

#bookObject

Returns the value of attribute book.



3
4
5
# File 'lib/libri/scraper.rb', line 3

def book
  @book
end

#urlObject

Returns the value of attribute url.



3
4
5
# File 'lib/libri/scraper.rb', line 3

def url
  @url
end

Instance Method Details

#scrape_award(award) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/libri/scraper.rb', line 22

def scrape_award(award)
    html = award.url
    books_page = Nokogiri::HTML(open(html))

    books = {}

    books_array = books_page.css("div.product-shelf-info").take(20).map { |book|
        books = {
            :title => book.css("div.product-shelf-title").text.strip,
            :author => book.css("div.product-shelf-author").text.strip,
            :url => "https://www.barnesandnoble.com" + book.css("a").attribute("href").value
        } 
    }.uniq

    Libri::Books.create_from_collection(books_array)
end

#scrape_barnes_nobleObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/libri/scraper.rb', line 5

def scrape_barnes_noble
    html = "https://www.barnesandnoble.com/b/books/awards/_/N-8q8Z1d6q?showMoreIds=10008"
    awards_page = Nokogiri::HTML(open(html))

    awards = {}

    # Chosen .take(28) because without it, our awards list will include a 'Show Less'
    awards_array = awards_page.css("ul#sidebar-section-0 li a").take(28).map { |award|
        awards = {
            :name => award.text.chomp,
            :url => "https://www.barnesandnoble.com" + award.attribute("href").value
        }
    }

    Libri::Awards.create_from_collection(awards_array)
end

#scrape_book(book) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/libri/scraper.rb', line 39

def scrape_book(book)
    html = book.url
    book_page = Nokogiri::HTML(open(html))
    info_section = book_page.css("div.tabpanel")

    book_info_hash = {
        :title_by_author => info_section.css("div#productInfoOverview div.mb-m").text,
        :blurbs_and_plot => info_section.css("div#productInfoOverview p").map(&:text).join("\n").strip,
        :about_author => info_section.css("div#MeetTheAuthor div.text--medium").text.strip,
        :excerpt => info_section.css("div.read-an-excerpt p").text,
        :availability => book_page.css("button#pdp-marketplace-btn").text.chomp,
        :url => book.url
    }.delete_if { |key, val| val.to_s.strip.empty? }

    Libri::Book.create_from_collection(book_info_hash)
end

#scrape_quoteObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/libri/scraper.rb', line 56

def scrape_quote
    html = "https://www.goodreads.com/quotes/tag/books"
    quotes_page = Nokogiri::HTML(open(html))
    quote_section = quotes_page.css("div.quote")

    quote_hash = {}

    quotes_array = quote_section.map { |quote|
        quote_hash = {
            :quote => quote.css("div.quoteText").first.text.scan(/(“.+”)/).join(""),
            :author => quote.css("div.quoteText span.authorOrTitle").first.text
        }
    }
    
    Libri::Quote.create_from_collection(quotes_array)
end