nyt-bestsellers 0.0.7 → 0.1.4
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 +4 -4
- data/lib/nyt_bestsellers.rb +12 -4
- data/lib/nytimes/book.rb +28 -28
- data/lib/nytimes/cli.rb +118 -117
- data/lib/nytimes/genre.rb +29 -30
- data/lib/nytimes/scraper.rb +47 -43
- metadata +6 -22
- 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: 5fe7fe081125f8c59f398fd15e60f686df204d97180ee0e627731baae27444df
|
4
|
+
data.tar.gz: 9c675e9c24485fdc720d5d0337552ff47a1f48ce9bafbebfab59ee940453e622
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b791e8d0b56af005a790196beee89326ce59dd70133d3afa5285cae83bb3446366f86b1acf5627e9e5aac4dd5d1c098d046aa394af851190f2ab31306b4a2ad8
|
7
|
+
data.tar.gz: fc880be8603a47393860b2377f91ccfdf65408a94b7c8e2d17ae6ebe0142f8946b94ad468f7b1edf83c0a1fd94ef21d4133313990572b1259300e5a463e0c709
|
data/bin/nyt-bestsellers
CHANGED
@@ -1,5 +1,5 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
|
3
|
-
require 'nyt_bestsellers'
|
4
|
-
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require 'bundler/setup'
|
3
|
+
require 'nyt_bestsellers'
|
4
|
+
|
5
5
|
NYTBestsellers::CLI.new.call
|
data/lib/nyt_bestsellers.rb
CHANGED
@@ -1,4 +1,12 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
require '
|
1
|
+
require 'pry'
|
2
|
+
require 'nokogiri'
|
3
|
+
require 'open-uri'
|
4
|
+
require 'colorize'
|
5
|
+
require 'mechanize'
|
6
|
+
require 'scraper'
|
7
|
+
require 'book'
|
8
|
+
require 'cli'
|
9
|
+
require 'genre'
|
10
|
+
|
11
|
+
module NYTBestsellers
|
12
|
+
end
|
data/lib/nytimes/book.rb
CHANGED
@@ -1,29 +1,29 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
end
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
end
|
27
|
-
end
|
28
|
-
end
|
1
|
+
module NYTBestsellers
|
2
|
+
class Book
|
3
|
+
attr_accessor :genre, :title, :author, :publisher, :wol, :summary
|
4
|
+
@@all = []
|
5
|
+
|
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
|
+
end
|
16
|
+
|
17
|
+
def self.all
|
18
|
+
@@all
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.find_by_title(name)
|
22
|
+
self.all.find do |book|
|
23
|
+
if name == book.title
|
24
|
+
book
|
25
|
+
end
|
26
|
+
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
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
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
|
+
|
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
|
+
|
22
|
+
response = ""
|
23
|
+
while response != "exit"
|
24
|
+
puts "Select a category by number or type " + "'exit'".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
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def genre_count
|
37
|
+
NYTBestsellers::Genre.all.count
|
38
|
+
end
|
39
|
+
|
40
|
+
def display_genres
|
41
|
+
NYTBestsellers::Genre.all.each_with_index do |genre, index|
|
42
|
+
puts "#{index+1} - #{genre.name}"
|
43
|
+
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
|
+
|
57
|
+
title = " #{book.title}"
|
58
|
+
puts rank + title
|
59
|
+
end
|
60
|
+
|
61
|
+
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'".red + " to return to the categories or " + "'exit'".red + " to close the CLI."
|
69
|
+
book_input = gets.strip
|
70
|
+
puts ""
|
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
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
def display_book_info(response, book_input)
|
84
|
+
genre = NYTBestsellers::Genre.find_by_num(response)
|
85
|
+
|
86
|
+
genre.books.each_with_index do |book, index|
|
87
|
+
if book_input.to_i == index+1
|
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 ""
|
98
|
+
|
99
|
+
input = ""
|
100
|
+
while input != "exit"
|
101
|
+
puts "Type" + " 'back'".red + "," + " 'menu',".red + " or" + " 'exit'".red + "."
|
102
|
+
input = gets.strip
|
103
|
+
puts ""
|
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
|
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
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
end
|
30
|
-
|
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
|
29
|
+
end
|
31
30
|
end
|
data/lib/nytimes/scraper.rb
CHANGED
@@ -1,43 +1,47 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
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
|
+
|
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
|
14
|
+
end
|
15
|
+
|
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
|
21
|
+
end
|
22
|
+
|
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")
|
27
|
+
|
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
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def self.get_date
|
44
|
+
get_page.css("time").text.strip
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
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.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Amy Back
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2022-10-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -24,20 +24,6 @@ dependencies:
|
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '1.11'
|
27
|
-
- !ruby/object:Gem::Dependency
|
28
|
-
name: rake
|
29
|
-
requirement: !ruby/object:Gem::Requirement
|
30
|
-
requirements:
|
31
|
-
- - "~>"
|
32
|
-
- !ruby/object:Gem::Version
|
33
|
-
version: '10.0'
|
34
|
-
type: :development
|
35
|
-
prerelease: false
|
36
|
-
version_requirements: !ruby/object:Gem::Requirement
|
37
|
-
requirements:
|
38
|
-
- - "~>"
|
39
|
-
- !ruby/object:Gem::Version
|
40
|
-
version: '10.0'
|
41
27
|
- !ruby/object:Gem::Dependency
|
42
28
|
name: rspec
|
43
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -106,14 +92,14 @@ dependencies:
|
|
106
92
|
requirements:
|
107
93
|
- - "~>"
|
108
94
|
- !ruby/object:Gem::Version
|
109
|
-
version:
|
95
|
+
version: 2.7.6
|
110
96
|
type: :development
|
111
97
|
prerelease: false
|
112
98
|
version_requirements: !ruby/object:Gem::Requirement
|
113
99
|
requirements:
|
114
100
|
- - "~>"
|
115
101
|
- !ruby/object:Gem::Version
|
116
|
-
version:
|
102
|
+
version: 2.7.6
|
117
103
|
description: A CLI based on the New York Times Bestsellers List. Provides list of
|
118
104
|
top-selling books by genre.
|
119
105
|
email: aback1227@gmail.com
|
@@ -123,13 +109,12 @@ extensions: []
|
|
123
109
|
extra_rdoc_files: []
|
124
110
|
files:
|
125
111
|
- bin/nyt-bestsellers
|
126
|
-
- config/environment.rb
|
127
112
|
- lib/nyt_bestsellers.rb
|
128
113
|
- lib/nytimes/book.rb
|
129
114
|
- lib/nytimes/cli.rb
|
130
115
|
- lib/nytimes/genre.rb
|
131
116
|
- lib/nytimes/scraper.rb
|
132
|
-
homepage: https://github.com/
|
117
|
+
homepage: https://github.com/aback223/nyt-bestsellers
|
133
118
|
licenses:
|
134
119
|
- MIT
|
135
120
|
metadata: {}
|
@@ -149,8 +134,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
149
134
|
- !ruby/object:Gem::Version
|
150
135
|
version: '0'
|
151
136
|
requirements: []
|
152
|
-
|
153
|
-
rubygems_version: 2.7.6
|
137
|
+
rubygems_version: 3.1.2
|
154
138
|
signing_key:
|
155
139
|
specification_version: 4
|
156
140
|
summary: Lists books by genre from the NYT Bestsellers List
|