libri 0.2.1 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8165ef7a5a7120d94748d87b6605b3118175d5f0013cb696ef7ca380f62c853d
4
- data.tar.gz: 662da9a7efbb5bfa42521b202918fe917739328d29e8fc7c66872cdbc54a0129
3
+ metadata.gz: 442cfcbbae40605ea64f8c70d335456ec514f594ef30291e9967bf048b8809e1
4
+ data.tar.gz: 8454365900c686f5e5362f17fe9547c9e9ac7f6b2e86ccef955e5dfdcd95ab52
5
5
  SHA512:
6
- metadata.gz: '09e526310264ce82aebd1c6eea928a920d376f4c5106bb02fcc8123d1dd6eced64654905be283725c40110b1094192e60ec62c8784ea0443a44912c343b6249c'
7
- data.tar.gz: f0b9d8ca5cee7f63db2bb9b6b68042339a55c2232d8f7fda8b123fb9eeaf5393c71c8441c1ce66ebaf1740727c8ddbc2cc59e42489183316d4c2b2f343e75700
6
+ metadata.gz: 2cfb3dce4c83c51271f0e8cc820ae9780b7c42792f5313f49808bc0157c17096e6ef70113d81aecb4099fbbadfedb8be0d58bf80ed4afc5b649438b312ae32bf
7
+ data.tar.gz: fdbff3d2478b97bbd61343a546615778d019ca05c0a77e081a7bff77746cefa4b2b6e66e63ef21d1a0c291edaf1f622c068d9a6991a2633e2fae2d409476df4b
data/.DS_Store CHANGED
Binary file
data/Gemfile.lock CHANGED
@@ -1,24 +1,19 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- libri (0.1.1)
4
+ libri (0.2.2)
5
5
  colorize
6
6
  nokogiri (~> 1.8.3)
7
7
 
8
8
  GEM
9
9
  remote: https://rubygems.org/
10
10
  specs:
11
- coderay (1.1.2)
12
11
  colorize (0.8.1)
13
12
  diff-lcs (1.3)
14
13
  gem-release (2.0.1)
15
- method_source (0.9.0)
16
14
  mini_portile2 (2.3.0)
17
15
  nokogiri (1.8.3)
18
16
  mini_portile2 (~> 2.3.0)
19
- pry (0.11.3)
20
- coderay (~> 1.1.0)
21
- method_source (~> 0.9.0)
22
17
  rake (10.5.0)
23
18
  rspec (3.7.0)
24
19
  rspec-core (~> 3.7.0)
@@ -41,7 +36,6 @@ DEPENDENCIES
41
36
  bundler (~> 1.16)
42
37
  gem-release
43
38
  libri!
44
- pry
45
39
  rake (~> 10.0)
46
40
  rspec (~> 3.0)
47
41
 
data/lib/libri.rb CHANGED
@@ -7,4 +7,5 @@ require_relative './libri/cli'
7
7
  require_relative './libri/awards.rb'
8
8
  require_relative './libri/books.rb'
9
9
  require_relative './libri/book.rb'
10
- require_relative './libri/quote.rb'
10
+ require_relative './libri/quote.rb'
11
+ require_relative './libri/scraper.rb'
data/lib/libri/awards.rb CHANGED
@@ -1,28 +1,23 @@
1
1
  class Libri::Awards
2
2
  attr_accessor :name, :url
3
-
4
- def self.all
5
- self.scrape_barnes_noble
6
- end
7
3
 
8
- def self.scrape_barnes_noble
9
- html = "https://www.barnesandnoble.com/b/books/awards/_/N-29Z8q8Z1d6q"
10
- awards_page = Nokogiri::HTML(open(html))
4
+ @@all = []
11
5
 
12
- # awards.name = awards_page.css("ul#sidebar-section-0 li a").text
13
- # awards.url = awards_page.css("ul#sidebar-section-0 li a").attribute("href").value
14
-
15
- awards_array = []
16
- awards = {}
6
+ def initialize(awards_hash)
7
+ awards_hash.each { |key, val|
8
+ send "#{key}=", val
9
+ }
10
+ @@all << self
11
+ end
17
12
 
18
- awards_page.css("ul#sidebar-section-0 li a").take(15).each { |award|
19
- awards = {
20
- :name => award.text.chomp,
21
- :url => "https://www.barnesandnoble.com" + award.attribute("href").value
22
- }
23
- awards_array << awards
13
+ def self.create_from_collection(awards_array)
14
+ awards_array.each { |awards_hash|
15
+ self.new(awards_hash)
24
16
  }
25
- awards_array
17
+ end
18
+
19
+ def self.all
20
+ @@all
26
21
  end
27
22
 
28
23
  end
data/lib/libri/book.rb CHANGED
@@ -1,31 +1,31 @@
1
1
  class Libri::Book
2
- attr_accessor :title_by_author, :about_author, :availability, :blurbs_and_plot, :url, :excerpt, :related_books, :book
2
+ attr_accessor :title_by_author, :blurbs_and_plot, :about_author,:excerpt, :availability, :url, :book
3
3
 
4
- def self.scrape_book(book)
5
- html = book[:url]
6
- book_page = Nokogiri::HTML(open(html))
7
- info_section = book_page.css("div.tabpanel")
4
+ @@all = []
8
5
 
9
- related_books_hash = {}
10
-
11
- book_info_hash = {
12
- :title_by_author => info_section.css("div#productInfoOverview div.mb-m").text,
13
- :blurbs_and_plot => info_section.css("div#productInfoOverview p").map(&:text).join("\n").strip,
14
- :about_author => info_section.css("div#MeetTheAuthor div.text--medium").text.strip,
15
- :excerpt => info_section.xpath("//div[@class='read-an-excerpt']/p[not(@class) and position()<7]").map(&:text).join("\n"),
16
- # :related_books => book_page.css("div.product-shelf-info").each { |book|
17
- # related_books_hash = {
18
- # :title => book.css("div.product-shelf-title").text.strip,
19
- # :author => book.css("div.product-shelf-author").text.strip,
20
- # :url => "https://www.barnesandnoble.com" + book.css("a").attribute("href").value
21
- # }
22
- # },
23
- :availability => book_page.css("button#pdp-marketplace-btn").text.chomp,
24
- :url => book[:url]
6
+ def initialize(book_info_hash)
7
+ book_info_hash.each { |key, val|
8
+ send "#{key}=", val
25
9
  }
10
+ @@all << self
11
+ end
26
12
 
27
- book_info_hash.delete_if { |key, val| val.to_s.strip.empty? }
13
+ def self.create_from_collection(book_info_array)
14
+ book_info_array.each { |book_info_hash|
15
+ self.new(book_info_hash)
16
+ }
17
+ end
28
18
 
19
+ def add_book_info(book_info_hash)
20
+ book_info_hash.each { |key, val|
21
+ send "#{key}=", val
22
+ }
23
+ self
29
24
  end
25
+
26
+ def self.all
27
+ @@all
28
+ end
29
+
30
30
 
31
31
  end
data/lib/libri/books.rb CHANGED
@@ -1,23 +1,24 @@
1
1
  class Libri::Books
2
- attr_accessor :award, :url
2
+ attr_accessor :title, :author, :url
3
3
 
4
- def self.scrape_award(award)
5
- html = award[:url]
6
- books_page = Nokogiri::HTML(open(html))
4
+ @@all = []
7
5
 
8
- books_array = []
9
- books = {}
10
-
11
- books_page.css("div.product-shelf-info").take(20).each { |book|
12
- books = {
13
- :title => book.css("div.product-shelf-title").text.strip,
14
- :author => book.css("div.product-shelf-author").text.strip,
15
- :url => "https://www.barnesandnoble.com" + book.css("a").attribute("href").value
16
- }
6
+ def initialize(books_hash)
7
+ books_hash.each { |key, val|
8
+ send "#{key}=", val
9
+ }
10
+ @@all << self
11
+ end
17
12
 
18
- books_array << books
13
+ def self.create_from_collection(books_array)
14
+ books_array.each { |books_hash|
15
+ self.new(books_hash)
19
16
  }
20
- books_array
21
17
  end
18
+
19
+ def self.all
20
+ @@all
21
+ end
22
+
22
23
  end
23
24
 
data/lib/libri/cli.rb CHANGED
@@ -1,47 +1,54 @@
1
1
  class Libri::CLI
2
- attr_accessor :awards, :name, :book, :url
2
+
3
+ attr_accessor :award, :awards_array, :books_array, :book_info_hash, :quotes_array, :title_by_author
3
4
 
4
5
  LINE = "----------------------------------------------------------"
5
6
  SPACE = " "
7
+ STRING = <<-EOS
8
+ . .
9
+ !!!!!!!
10
+ ..'''::::.. . [[[|]]] .
11
+ .::' ``::.. !!!!!!!!|--_--|!!!!!
12
+ '...::' `'':::. [[[[[[[[\\_(X)_/]]]]]
13
+ . . |=| ._/-__-__\\===========/-__\\_
14
+ !!!!!!!!!\\========[ /]]|[[\\ ]=====/
15
+ /_-_-| | |-_--|=| | | ||=|_|_|=||"|==|
16
+ /-__--|_|_|_-_-| |_|_|=||______=||_| =|
17
+ /-----------------------\\===========/-----/
18
+ ^^^\\^^^^^^^^^^^^^^^^^^^^^^[[|]]|[[|]]=====/
19
+ |.' ..==::'"'::==.. '.[ /~~~~~\\ ]]]]]]]
20
+ | .'=[[[|]]|[[|]]]=`._||== = || =\\ ]
21
+ ||= == ||:^s^:|| = == ||=| | | || |=||
22
+ _||_ = =||o---.|| = ==_||_= == =||==_||_
23
+ \\__/= = ||: :||= == \\__/[][][][][]\\__/
24
+ [||]= ==||:___:|| = = [||]\\//\\//\\[||]
25
+ } {---'"'-----'"'- --} {//\\//\\//} {
26
+ ____[==]__________________[==]\\//\\//\\[==]_____
27
+ |`|~~~~|================|~~~~|~~~~~~~~|~~~~||
28
+ jgs|^| ^ |================|^ | ^ ^^ ^ | ^ ||
29
+ EOS
6
30
 
7
31
  def call
8
- puts <<~HEREDOC
9
- . .
10
- !!!!!!!
11
- ..'''::::.. . [[[|]]] .
12
- .::' ``::.. !!!!!!!!|--_--|!!!!!
13
- '...::' `'':::. [[[[[[[[\\_(X)_/]]]]]
14
- . . |=| ._/-__-__\\===========/-__\\_
15
- !!!!!!!!!\\========[ /]]|[[\\ ]=====/
16
- /_-_-| | |-_--|=| | | ||=|_|_|=||"|==|
17
- /-__--|_|_|_-_-| |_|_|=||______=||_| =|
18
- /-----------------------\\===========/-----/
19
- ^^^\\^^^^^^^^^^^^^^^^^^^^^^[[|]]|[[|]]=====/
20
- |.' ..==::'"'::==.. '.[ /~~~~~\\ ]]]]]]]
21
- | .'=[[[|]]|[[|]]]=`._||== = || =\\ ]
22
- ||= == ||:^s^:|| = == ||=| | | || |=||
23
- _||_ = =||o---.|| = ==_||_= == =||==_||_
24
- \\__/= = ||: :||= == \\__/[][][][][]\\__/
25
- [||]= ==||:___:|| = = [||]\\//\\//\\[||]
26
- } {---'"'-----'"'- --} {//\\//\\//} {
27
- ____[==]__________________[==]\\//\\//\\[==]_____
28
- |`|~~~~|================|~~~~|~~~~~~~~|~~~~||
29
- jgs|^| ^ |================|^ | ^ ^^ ^ | ^ ||
30
- HEREDOC
32
+ puts STRING
31
33
  puts SPACE
32
34
  puts "Welcome to Libri, a chamber full of literary wonders.".blue
33
- puts "Come freely. This will take a few moments..".blue
34
35
  puts "Below are some of the most prized literary awards of our time.".blue
36
+ puts "Come freely. This will take a few moments..".blue
35
37
  puts LINE
38
+ make_awards
36
39
  list_awards
37
40
  puts LINE
38
41
  leave
39
42
  end
40
43
 
44
+ def make_awards
45
+ @awards_array = Scraper.scrape_barnes_noble
46
+ Libri::Awards.create_from_collection(awards_array)
47
+ end
48
+
41
49
  def list_awards
42
- @awards_array = Libri::Awards.all
43
- @awards_array.each.with_index(1) { |award, i|
44
- puts "#{i}. #{award[:name]}"
50
+ Libri::Awards.all.each.with_index(1) { |award, i|
51
+ puts "#{i}. #{award.name}"
45
52
  }
46
53
  puts LINE
47
54
  puts "Which award would you like to explore?".blue
@@ -50,32 +57,40 @@ class Libri::CLI
50
57
  menu_awards
51
58
  end
52
59
 
60
+ def make_books(award)
61
+ @books_array = Scraper.scrape_award(award)
62
+ Libri::Books.create_from_collection(books_array)
63
+ end
64
+
53
65
  def list_books(award)
54
- @books_array = Libri::Books.scrape_award(award)
55
- @books_array.each.with_index(1) { |book, i|
56
- puts "#{i}. #{book[:title]} #{book[:author]}. #{book[:rating]}"
66
+ Libri::Books.all.each.with_index(1) { |book, i|
67
+ puts "#{i}. #{book.title} #{book.author}"
57
68
  }
58
69
  puts LINE
59
70
  puts "Which book would you like to know more about?".blue
60
71
  puts LINE
61
72
 
62
73
  menu_books(award)
74
+
63
75
  end
64
76
 
65
77
  def list_details(book)
66
- @book_info_hash = Libri::Book.scrape_book(book)
78
+ @book_info_hash = Scraper.scrape_book(book)
79
+
67
80
  @book_info_hash.each { |key, val|
68
81
  puts SPACE
69
- puts "#{key.upcase}".red
82
+ puts "#{key}".upcase.red
70
83
  puts LINE
71
84
  puts "#{val}"
72
- }
85
+ }
73
86
  end
74
87
 
75
88
  def random_quote
76
- @quotes_array = Libri::Quote.scrape_quote
77
- @random = @quotes_array.sample
78
- @random.each { |key, val|
89
+ @quotes_array = Scraper.scrape_quote
90
+ Libri::Quote.create_from_collection(quotes_array)
91
+
92
+ random = @quotes_array.sample
93
+ random.each { |key, val|
79
94
  puts SPACE
80
95
  puts "#{val}"
81
96
  }
@@ -85,16 +100,18 @@ class Libri::CLI
85
100
  def menu_awards
86
101
  input = STDIN.gets.strip.downcase
87
102
 
88
- if input.to_i > 0
103
+ if input.to_i.between?(1,15)
89
104
  award = @awards_array[input.to_i - 1]
105
+ Libri::Books.all.clear
106
+ make_books(award)
90
107
  list_books(award)
91
- elsif input == "nevermore"
92
- puts LINE
93
- random_quote
94
108
  elsif input == "awards"
95
109
  list_awards
96
110
  elsif input == "exit"
97
111
  nil
112
+ elsif input == "nevermore"
113
+ puts LINE
114
+ random_quote
98
115
  else
99
116
  puts "The raven croaked, 'Please try again.'".red
100
117
  puts LINE
@@ -105,11 +122,11 @@ class Libri::CLI
105
122
  def menu_books(award)
106
123
  input = STDIN.gets.strip.downcase
107
124
 
108
- if input.to_i > 0
125
+ if input.to_i.between?(1,20)
109
126
  book = @books_array[input.to_i - 1]
110
127
  list_details(book)
111
128
  puts LINE
112
- puts " To list the books of the same award again, type books.".blue
129
+ puts " To list the books of the same award again, type books".blue
113
130
  puts " To list all the awards again, type awards".blue
114
131
  puts LINE
115
132
  menu_books(award)
data/lib/libri/quote.rb CHANGED
@@ -1,22 +1,22 @@
1
1
  class Libri::Quote
2
- attr_accessor :quote
2
+ attr_accessor :quote, :author
3
3
 
4
- def self.scrape_quote
5
- html = "https://www.goodreads.com/quotes/tag/books"
6
- quotes_page = Nokogiri::HTML(open(html))
7
- quote_section = quotes_page.css("div.quote")
4
+ @@all = []
8
5
 
9
- quotes_array = []
10
- quote_hash = {}
11
-
12
- quote_section.each { |quote|
13
- quote_hash = {
14
- :quote => quote.css("div.quoteText").first.text.scan(/(“.+”)/).join(""),
15
- :author => quote.css("div.quoteText a").first.text
16
- }
6
+ def initialize(quote_hash)
7
+ quote_hash.each { |key, val|
8
+ send "#{key}=", val
9
+ }
10
+ @@all << self
11
+ end
17
12
 
18
- quotes_array << quote_hash
13
+ def self.create_from_collection(quotes_array)
14
+ quotes_array.each { |quote_hash|
15
+ self.new(quote_hash)
19
16
  }
20
- quotes_array
17
+ end
18
+
19
+ def self.all
20
+ @@all
21
21
  end
22
22
  end
@@ -0,0 +1,90 @@
1
+ class Scraper
2
+
3
+ attr_accessor :url, :award, :book
4
+
5
+ def self.scrape_barnes_noble
6
+ html = "https://www.barnesandnoble.com/b/books/awards/_/N-29Z8q8Z1d6q"
7
+ awards_page = Nokogiri::HTML(open(html))
8
+
9
+ # awards.name = awards_page.css("ul#sidebar-section-0 li a").text
10
+ # awards.url = awards_page.css("ul#sidebar-section-0 li a").attribute("href").value
11
+
12
+ awards_array = []
13
+ awards = {}
14
+
15
+ awards_page.css("ul#sidebar-section-0 li a").take(15).each { |award|
16
+ awards = {
17
+ :name => award.text.chomp,
18
+ :url => "https://www.barnesandnoble.com" + award.attribute("href").value
19
+ }
20
+ awards_array << awards
21
+ }
22
+ awards_array
23
+ end
24
+
25
+ def self.scrape_award(award)
26
+ html = award[:url]
27
+ books_page = Nokogiri::HTML(open(html))
28
+
29
+ books_array = []
30
+ books = {}
31
+
32
+ books_page.css("div.product-shelf-info").take(20).each { |book|
33
+ books = {
34
+ :title => book.css("div.product-shelf-title").text.strip,
35
+ :author => book.css("div.product-shelf-author").text.strip,
36
+ :url => "https://www.barnesandnoble.com" + book.css("a").attribute("href").value
37
+ }
38
+
39
+ books_array << books
40
+ }
41
+ books_array
42
+ end
43
+
44
+ def self.scrape_book(book)
45
+ html = book[:url]
46
+ book_page = Nokogiri::HTML(open(html))
47
+ info_section = book_page.css("div.tabpanel")
48
+
49
+ # related_books_hash = {}
50
+
51
+ book_info_hash = {
52
+ :title_by_author => info_section.css("div#productInfoOverview div.mb-m").text,
53
+ :blurbs_and_plot => info_section.css("div#productInfoOverview p").map(&:text).join("\n").strip,
54
+ :about_author => info_section.css("div#MeetTheAuthor div.text--medium").text.strip,
55
+ :excerpt => info_section.xpath("//div[@class='read-an-excerpt']/p[not(@class) and position()<7]").map(&:text).join("\n"),
56
+ # :related_books => book_page.css("div.product-shelf-info").each { |book|
57
+ # related_books_hash = {
58
+ # :title => book.css("div.product-shelf-title").text.strip,
59
+ # :author => book.css("div.product-shelf-author").text.strip,
60
+ # :url => "https://www.barnesandnoble.com" + book.css("a").attribute("href").value
61
+ # }
62
+ # },
63
+ :availability => book_page.css("button#pdp-marketplace-btn").text.chomp,
64
+ :url => book[:url]
65
+ }
66
+
67
+ # book_info_hash.delete_if { |key, val| val.to_s.strip.empty? }
68
+
69
+ end
70
+
71
+ def self.scrape_quote
72
+ html = "https://www.goodreads.com/quotes/tag/books"
73
+ quotes_page = Nokogiri::HTML(open(html))
74
+ quote_section = quotes_page.css("div.quote")
75
+
76
+ quotes_array = []
77
+ quote_hash = {}
78
+
79
+ quote_section.each { |quote|
80
+ quote_hash = {
81
+ :quote => quote.css("div.quoteText").first.text.scan(/(“.+”)/).join(""),
82
+ :author => quote.css("div.quoteText a").first.text
83
+ }
84
+
85
+ quotes_array << quote_hash
86
+ }
87
+ quotes_array
88
+ end
89
+
90
+ end
data/lib/libri/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Libri
2
- VERSION = "0.2.1"
2
+ VERSION = "0.2.2"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: libri
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - jouissances
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-06-25 00:00:00.000000000 Z
11
+ date: 2018-06-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -125,6 +125,7 @@ files:
125
125
  - lib/libri/books.rb
126
126
  - lib/libri/cli.rb
127
127
  - lib/libri/quote.rb
128
+ - lib/libri/scraper.rb
128
129
  - lib/libri/version.rb
129
130
  - libri.gemspec
130
131
  - spec.md