nyt-bestsellers 0.0.7 → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/bin/nyt-bestsellers +1 -1
- data/lib/nyt_bestsellers.rb +10 -2
- data/lib/nytimes/book.rb +23 -23
- data/lib/nytimes/cli.rb +99 -98
- data/lib/nytimes/genre.rb +28 -29
- data/lib/nytimes/scraper.rb +36 -32
- metadata +4 -5
- data/config/environment.rb +0 -10
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6a62b5da961841947fa45b6b7802932193d0e5b3b757eecb5cb9f79778395e65
|
4
|
+
data.tar.gz: 370cb594e99fce26fe4d9fc23e3a9c3c4855539203c384dfa67e7fab2337df68
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1d3e40274785e3428548193980372760a0cb51431cddd48446185e1b67bca58faa794ca88bb707f78ea5de0bd1278aa10b8e0cd38e2497d27ccc2e7401930662
|
7
|
+
data.tar.gz: f692331ff871353e2f742a661df88ea263008c34e226807fbfb00a58cf8bf351ec33dcc4430484e8009d85a0c7d4055231364915f4b2549f38799d6a3c278bf0
|
data/bin/nyt-bestsellers
CHANGED
data/lib/nyt_bestsellers.rb
CHANGED
data/lib/nytimes/book.rb
CHANGED
@@ -1,29 +1,29 @@
|
|
1
|
-
|
1
|
+
module NYTBestsellers
|
2
|
+
class Book
|
3
|
+
attr_accessor :genre, :title, :author, :publisher, :wol, :summary
|
4
|
+
@@all = []
|
2
5
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
if !wol.empty? || !summary.empty?
|
13
|
-
@genre.books << self
|
14
|
-
@@all << self
|
6
|
+
def initialize(hash = {})
|
7
|
+
hash.each do |key, value|
|
8
|
+
self.send("#{key}=", value)
|
9
|
+
end
|
10
|
+
@genre = NYTBestsellers::Genre.find_by_name(hash[:genre])
|
11
|
+
if !wol.empty? || !summary.empty?
|
12
|
+
@genre.books << self
|
13
|
+
@@all << self
|
14
|
+
end
|
15
15
|
end
|
16
|
-
end
|
17
16
|
|
18
|
-
|
19
|
-
|
20
|
-
|
17
|
+
def self.all
|
18
|
+
@@all
|
19
|
+
end
|
21
20
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
21
|
+
def self.find_by_title(name)
|
22
|
+
self.all.find do |book|
|
23
|
+
if name == book.title
|
24
|
+
book
|
25
|
+
end
|
26
26
|
end
|
27
|
-
end
|
28
|
-
end
|
27
|
+
end
|
28
|
+
end
|
29
29
|
end
|
data/lib/nytimes/cli.rb
CHANGED
@@ -1,118 +1,119 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
1
|
+
module NYTBestsellers
|
2
|
+
class CLI
|
3
|
+
def call
|
4
|
+
NYTBestsellers::Scraper.make_genres
|
5
|
+
NYTBestsellers::Scraper.make_books
|
6
|
+
puts ""
|
7
|
+
puts "Welcome to the New York Times Bestsellers List".blue.bold
|
8
|
+
run
|
9
|
+
end
|
10
10
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
11
|
+
def run
|
12
|
+
puts "-------------------"
|
13
|
+
puts ""
|
14
|
+
puts "NYT Bestsellers - Week of #{NYTBestsellers::Scraper.get_date}".bold.blue
|
15
|
+
puts "(Lists are published early online.)".bold.blue
|
16
|
+
puts ""
|
17
|
+
puts "Here are the categories:"
|
18
|
+
puts ""
|
19
|
+
display_genres
|
20
|
+
puts ""
|
21
21
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
22
|
+
response = ""
|
23
|
+
while response != "exit"
|
24
|
+
puts "Select a category by number or type " + "'exit'".light_red + " to close the CLI."
|
25
|
+
response = gets.strip
|
26
|
+
if (1..genre_count).to_a.include?(response.to_i)
|
27
|
+
genre_books(response.to_i)
|
28
|
+
elsif response == "exit"
|
29
|
+
puts "Goodbye!~".bold.red
|
30
|
+
puts ""
|
31
|
+
exit
|
32
|
+
end
|
32
33
|
end
|
33
34
|
end
|
34
|
-
end
|
35
|
-
|
36
|
-
def genre_count
|
37
|
-
NYTBestsellers::Genre.all.count
|
38
|
-
end
|
39
35
|
|
40
|
-
|
41
|
-
|
42
|
-
puts "#{index+1} - #{genre.name}"
|
36
|
+
def genre_count
|
37
|
+
NYTBestsellers::Genre.all.count
|
43
38
|
end
|
44
|
-
end
|
45
|
-
|
46
|
-
def genre_books(response)
|
47
|
-
genre = NYTBestsellers::Genre.find_by_num(response)
|
48
|
-
puts ""
|
49
|
-
puts "****----#{genre.name.upcase}----****".blue.bold
|
50
|
-
puts ""
|
51
|
-
puts " Rank^ Title"
|
52
|
-
puts " ----- -----"
|
53
|
-
genre.books.each_with_index do |book, index|
|
54
|
-
rank = " #{index+1} "
|
55
|
-
rank << " " if (0..8).include?(index)
|
56
39
|
|
57
|
-
|
58
|
-
|
40
|
+
def display_genres
|
41
|
+
NYTBestsellers::Genre.all.each_with_index do |genre, index|
|
42
|
+
puts "#{index+1} - #{genre.name}"
|
43
|
+
end
|
59
44
|
end
|
60
45
|
|
61
|
-
|
62
|
-
|
63
|
-
|
46
|
+
def genre_books(response)
|
47
|
+
genre = NYTBestsellers::Genre.find_by_num(response)
|
48
|
+
puts ""
|
49
|
+
puts "****----#{genre.name.upcase}----****".blue.bold
|
50
|
+
puts ""
|
51
|
+
puts " Rank^ Title"
|
52
|
+
puts " ----- -----"
|
53
|
+
genre.books.each_with_index do |book, index|
|
54
|
+
rank = " #{index+1} "
|
55
|
+
rank << " " if (0..8).include?(index)
|
56
|
+
|
57
|
+
title = " #{book.title}"
|
58
|
+
puts rank + title
|
59
|
+
end
|
64
60
|
|
65
|
-
book_input = ""
|
66
|
-
while book_input != "exit"
|
67
|
-
puts "Select a book by rank number to get more info."
|
68
|
-
puts "You may type" + " 'back'".light_red + " to return to the categories or " + "'exit'".light_red + " to close the CLI."
|
69
|
-
book_input = gets.strip
|
70
61
|
puts ""
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
puts "
|
62
|
+
puts "(^refers to the current position on the bestseller's list)"
|
63
|
+
puts ""
|
64
|
+
|
65
|
+
book_input = ""
|
66
|
+
while book_input != "exit"
|
67
|
+
puts "Select a book by rank number to get more info."
|
68
|
+
puts "You may type" + " 'back'".light_red + " to return to the categories or " + "'exit'".light_red + " to close the CLI."
|
69
|
+
book_input = gets.strip
|
77
70
|
puts ""
|
78
|
-
|
71
|
+
if (1..genre.books.count).include?(book_input.to_i)
|
72
|
+
display_book_info(response, book_input)
|
73
|
+
elsif book_input == "back"
|
74
|
+
run
|
75
|
+
elsif book_input == "exit"
|
76
|
+
puts "Goodbye!~".bold.red
|
77
|
+
puts ""
|
78
|
+
exit
|
79
|
+
end
|
79
80
|
end
|
80
81
|
end
|
81
|
-
end
|
82
|
-
|
83
|
-
def display_book_info(response, book_input)
|
84
|
-
genre = NYTBestsellers::Genre.find_by_num(response)
|
85
82
|
|
86
|
-
|
87
|
-
|
88
|
-
puts "****----#{book.title}----****".blue.bold
|
89
|
-
puts ""
|
90
|
-
puts "Weeks On Bestseller:".bold + " #{book.wol}"
|
91
|
-
puts "Author:".bold + " #{book.author}"
|
92
|
-
puts "Publisher:".bold + " #{book.publisher}"
|
93
|
-
puts "Genre:".bold + " #{book.genre.name}"
|
94
|
-
puts ""
|
95
|
-
puts "---------Summary---------".bold
|
96
|
-
puts "#{book.summary}"
|
97
|
-
puts ""
|
83
|
+
def display_book_info(response, book_input)
|
84
|
+
genre = NYTBestsellers::Genre.find_by_num(response)
|
98
85
|
|
99
|
-
|
100
|
-
|
101
|
-
puts "
|
102
|
-
input = gets.strip
|
86
|
+
genre.books.each_with_index do |book, index|
|
87
|
+
if book_input.to_i == index+1
|
88
|
+
puts "****----#{book.title}----****".blue.bold
|
103
89
|
puts ""
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
90
|
+
puts "Weeks On Bestseller:".bold + " #{book.wol}"
|
91
|
+
puts "Author:".bold + " #{book.author}"
|
92
|
+
puts "Publisher:".bold + " #{book.publisher}"
|
93
|
+
puts "Genre:".bold + " #{book.genre.name}"
|
94
|
+
puts ""
|
95
|
+
puts "---------Summary---------".bold
|
96
|
+
puts "#{book.summary}"
|
97
|
+
puts ""
|
98
|
+
|
99
|
+
input = ""
|
100
|
+
while input != "exit"
|
101
|
+
puts "Type" + " 'back'".light_red + "," + " 'menu',".light_red + " or" + " 'exit'".light_red + "."
|
102
|
+
input = gets.strip
|
111
103
|
puts ""
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
104
|
+
if input == "back"
|
105
|
+
genre_books(response)
|
106
|
+
elsif input == "menu"
|
107
|
+
run
|
108
|
+
puts ""
|
109
|
+
elsif input == "exit"
|
110
|
+
puts "Goodbye!~".bold.red
|
111
|
+
puts ""
|
112
|
+
exit
|
113
|
+
end #second if
|
114
|
+
end
|
115
|
+
end #first if
|
116
|
+
end #each
|
117
|
+
end
|
117
118
|
end
|
118
119
|
end
|
data/lib/nytimes/genre.rb
CHANGED
@@ -1,31 +1,30 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
1
|
+
module NYTBestsellers
|
2
|
+
class Genre
|
3
|
+
attr_accessor :name, :url
|
4
|
+
@@all = []
|
5
|
+
|
6
|
+
def initialize(hash = {})
|
7
|
+
hash.each do |key, value|
|
8
|
+
self.send("#{key}=", value)
|
9
|
+
end
|
10
|
+
@@all << self
|
11
|
+
@books = []
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.all
|
15
|
+
@@all
|
16
|
+
end
|
17
|
+
|
18
|
+
def books
|
19
|
+
@books
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.find_by_name(genre_name)
|
23
|
+
self.all.find {|x| x.name == genre_name}
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.find_by_num(num_input)
|
27
|
+
self.all[num_input.to_i-1]
|
28
|
+
end
|
25
29
|
end
|
26
|
-
|
27
|
-
def self.find_by_num(num_input)
|
28
|
-
self.all[num_input.to_i-1]
|
29
|
-
end
|
30
|
-
|
31
30
|
end
|
data/lib/nytimes/scraper.rb
CHANGED
@@ -1,43 +1,47 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
1
|
+
module NYTBestsellers
|
2
|
+
class Scraper
|
3
|
+
def self.get_page
|
4
|
+
@@page ||= Mechanize.new.get("http://www.nytimes.com/books/best-sellers/")
|
5
|
+
end
|
6
6
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
7
|
+
def self.make_genres
|
8
|
+
get_page.css("section h2").each do |category|
|
9
|
+
NYTBestsellers::Genre.new({
|
10
|
+
name: category.css("a").text.strip,
|
11
|
+
url: "http://www.nytimes.com#{category.css("a").attr("href").text}"
|
12
|
+
})
|
13
|
+
end
|
13
14
|
end
|
14
|
-
end
|
15
15
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
16
|
+
def self.get_genre_pages
|
17
|
+
agent = Mechanize.new
|
18
|
+
NYTBestsellers::Genre.all.collect do |genre|
|
19
|
+
page = agent.get("#{genre.url}")
|
20
|
+
end
|
20
21
|
end
|
21
|
-
end
|
22
22
|
|
23
|
-
|
24
|
-
|
25
|
-
|
23
|
+
def self.make_books
|
24
|
+
get_genre_pages.each do |page|
|
25
|
+
book_genre = page.css("section h2")[0].text
|
26
|
+
books = page.css("ol")[0].css("li article a")
|
26
27
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
28
|
+
books.each do |book|
|
29
|
+
if book.css("p")[1] != nil && book.css("p")[1].attribute("itemprop").value == "author"
|
30
|
+
NYTBestsellers::Book.new({
|
31
|
+
genre: book_genre,
|
32
|
+
title: book.css("h3").text.split.collect(&:capitalize).join(" "),
|
33
|
+
author: book.css("p")[1].text.split.delete_if{|x| x == "by"}.join(" "),
|
34
|
+
publisher: book.css("p")[2].text,
|
35
|
+
wol: book.css("p")[0].text,
|
36
|
+
summary: book.css("p")[3].text
|
37
|
+
})
|
38
|
+
end
|
39
|
+
end
|
36
40
|
end
|
37
41
|
end
|
38
|
-
end
|
39
42
|
|
40
|
-
|
41
|
-
|
43
|
+
def self.get_date
|
44
|
+
get_page.css("div.date-range").text.strip
|
45
|
+
end
|
42
46
|
end
|
43
47
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: nyt-bestsellers
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Amy Back
|
@@ -106,14 +106,14 @@ dependencies:
|
|
106
106
|
requirements:
|
107
107
|
- - "~>"
|
108
108
|
- !ruby/object:Gem::Version
|
109
|
-
version:
|
109
|
+
version: 2.7.6
|
110
110
|
type: :development
|
111
111
|
prerelease: false
|
112
112
|
version_requirements: !ruby/object:Gem::Requirement
|
113
113
|
requirements:
|
114
114
|
- - "~>"
|
115
115
|
- !ruby/object:Gem::Version
|
116
|
-
version:
|
116
|
+
version: 2.7.6
|
117
117
|
description: A CLI based on the New York Times Bestsellers List. Provides list of
|
118
118
|
top-selling books by genre.
|
119
119
|
email: aback1227@gmail.com
|
@@ -123,13 +123,12 @@ extensions: []
|
|
123
123
|
extra_rdoc_files: []
|
124
124
|
files:
|
125
125
|
- bin/nyt-bestsellers
|
126
|
-
- config/environment.rb
|
127
126
|
- lib/nyt_bestsellers.rb
|
128
127
|
- lib/nytimes/book.rb
|
129
128
|
- lib/nytimes/cli.rb
|
130
129
|
- lib/nytimes/genre.rb
|
131
130
|
- lib/nytimes/scraper.rb
|
132
|
-
homepage: https://github.com/
|
131
|
+
homepage: https://github.com/aback223/nyt-bestsellers
|
133
132
|
licenses:
|
134
133
|
- MIT
|
135
134
|
metadata: {}
|