nyt-bestsellers 0.0.4 → 0.0.6
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 +2 -2
- data/lib/nytimes/book.rb +11 -12
- data/lib/nytimes/cli.rb +8 -8
- data/lib/nytimes/genre.rb +5 -4
- data/lib/nytimes/scraper.rb +16 -21
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ce03add5d949656a5a1f634cf4405f043502abc3
|
4
|
+
data.tar.gz: 80a8b71c17efef3eb02188d38f3427f8229ec254
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 131a105fbd6c5c88aec6ecb88a34000f7744735fe9380488b4e2f360d79558288887dcc5e7d4d8bd29cdd0da5e8fd44017e884de95b14e3ea2831b2b14c0c846
|
7
|
+
data.tar.gz: c825661ef3a52c018994974b0dba48facb6418d77f72b6b286f83d9d0f026d3c9d66e37a262905002ff9505e9851bf6b4f2e5bfbc54dbd22972351f6086241cf
|
data/bin/nyt-bestsellers
CHANGED
data/lib/nytimes/book.rb
CHANGED
@@ -1,18 +1,18 @@
|
|
1
|
-
class Book
|
1
|
+
class NYTBestsellers::Book
|
2
2
|
|
3
3
|
attr_accessor :genre, :title, :author, :publisher, :wol, :summary
|
4
4
|
|
5
5
|
@@all = []
|
6
6
|
|
7
|
-
def initialize(
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
@
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
7
|
+
def initialize(hash = {})
|
8
|
+
hash.each do |key, value|
|
9
|
+
self.send("#{key}=", value)
|
10
|
+
end
|
11
|
+
@genre = NYTBestsellers::Genre.find_by_name(hash[:genre])
|
12
|
+
if !wol.empty? || !summary.empty?
|
13
|
+
@genre.books << self
|
14
|
+
@@all << self
|
15
|
+
end
|
16
16
|
end
|
17
17
|
|
18
18
|
def self.all
|
@@ -25,6 +25,5 @@ class Book
|
|
25
25
|
book
|
26
26
|
end
|
27
27
|
end
|
28
|
-
end
|
29
|
-
|
28
|
+
end
|
30
29
|
end
|
data/lib/nytimes/cli.rb
CHANGED
@@ -1,8 +1,8 @@
|
|
1
|
-
class CLI
|
1
|
+
class NYTBestsellers::CLI
|
2
2
|
|
3
3
|
def call
|
4
|
-
Scraper.make_genres
|
5
|
-
Scraper.make_books
|
4
|
+
NYTBestsellers::Scraper.make_genres
|
5
|
+
NYTBestsellers::Scraper.make_books
|
6
6
|
puts ""
|
7
7
|
puts "Welcome to the New York Times Bestsellers List".blue.bold
|
8
8
|
run
|
@@ -11,7 +11,7 @@ class CLI
|
|
11
11
|
def run
|
12
12
|
puts "-------------------"
|
13
13
|
puts ""
|
14
|
-
puts "NYT Bestsellers - Week of #{Scraper.get_date}".bold.blue
|
14
|
+
puts "NYT Bestsellers - Week of #{NYTBestsellers::Scraper.get_date}".bold.blue
|
15
15
|
puts "(Lists are published early online.)".bold.blue
|
16
16
|
puts ""
|
17
17
|
puts "Here are the categories:"
|
@@ -34,17 +34,17 @@ class CLI
|
|
34
34
|
end
|
35
35
|
|
36
36
|
def genre_count
|
37
|
-
Genre.all.count
|
37
|
+
NYTBestsellers::Genre.all.count
|
38
38
|
end
|
39
39
|
|
40
40
|
def display_genres
|
41
|
-
Genre.all.each_with_index do |genre, index|
|
41
|
+
NYTBestsellers::Genre.all.each_with_index do |genre, index|
|
42
42
|
puts "#{index+1} - #{genre.name}"
|
43
43
|
end
|
44
44
|
end
|
45
45
|
|
46
46
|
def genre_books(response)
|
47
|
-
genre = Genre.find_by_num(response)
|
47
|
+
genre = NYTBestsellers::Genre.find_by_num(response)
|
48
48
|
puts ""
|
49
49
|
puts "****----#{genre.name.upcase}----****".blue.bold
|
50
50
|
puts ""
|
@@ -81,7 +81,7 @@ class CLI
|
|
81
81
|
end
|
82
82
|
|
83
83
|
def display_book_info(response, book_input)
|
84
|
-
genre = Genre.find_by_num(response)
|
84
|
+
genre = NYTBestsellers::Genre.find_by_num(response)
|
85
85
|
|
86
86
|
genre.books.each_with_index do |book, index|
|
87
87
|
if book_input.to_i == index+1
|
data/lib/nytimes/genre.rb
CHANGED
@@ -1,12 +1,13 @@
|
|
1
|
-
class Genre
|
1
|
+
class NYTBestsellers::Genre
|
2
2
|
|
3
3
|
attr_accessor :name, :url
|
4
4
|
|
5
5
|
@@all = []
|
6
6
|
|
7
|
-
def initialize(
|
8
|
-
|
9
|
-
|
7
|
+
def initialize(hash = {})
|
8
|
+
hash.each do |key, value|
|
9
|
+
self.send("#{key}=", value)
|
10
|
+
end
|
10
11
|
@@all << self
|
11
12
|
@books = []
|
12
13
|
end
|
data/lib/nytimes/scraper.rb
CHANGED
@@ -1,26 +1,21 @@
|
|
1
|
-
class Scraper
|
1
|
+
class NYTBestsellers::Scraper
|
2
2
|
|
3
3
|
def self.get_page
|
4
|
-
|
5
|
-
page = agent.get("http://www.nytimes.com/books/best-sellers/")
|
6
|
-
end
|
7
|
-
|
8
|
-
def self.scrape_genres
|
9
|
-
get_page.css("section.subcategory")
|
4
|
+
@@page ||= Mechanize.new.get("http://www.nytimes.com/books/best-sellers/")
|
10
5
|
end
|
11
6
|
|
12
7
|
def self.make_genres
|
13
|
-
|
14
|
-
Genre.new(
|
15
|
-
category.css("a.subcategory-heading-link").text.strip,
|
16
|
-
"http://www.nytimes.com#{category.css("a").attr("href").text}"
|
17
|
-
|
8
|
+
get_page.css("section.subcategory").each do |category|
|
9
|
+
NYTBestsellers::Genre.new({
|
10
|
+
name: category.css("a.subcategory-heading-link").text.strip,
|
11
|
+
url: "http://www.nytimes.com#{category.css("a").attr("href").text}"
|
12
|
+
})
|
18
13
|
end
|
19
14
|
end
|
20
15
|
|
21
16
|
def self.get_genre_pages
|
22
17
|
agent = Mechanize.new
|
23
|
-
Genre.all.collect do |genre|
|
18
|
+
NYTBestsellers::Genre.all.collect do |genre|
|
24
19
|
page = agent.get("#{genre.url}")
|
25
20
|
end
|
26
21
|
end
|
@@ -30,14 +25,14 @@ class Scraper
|
|
30
25
|
books = page.css("div.book-body")
|
31
26
|
|
32
27
|
books.each do |book|
|
33
|
-
Book.new(
|
34
|
-
page.css("h1").text.split(/\s-\s/)[0].strip, #genre
|
35
|
-
book.css("h2.title").text.split.collect(&:capitalize).join(" "),
|
36
|
-
book.css("p.author").text.split.delete_if{|x| x == "by"}.join(" "),
|
37
|
-
book.css("p.publisher").text,
|
38
|
-
book.css("p.freshness").text,
|
39
|
-
book.css("p.description").text
|
40
|
-
|
28
|
+
NYTBestsellers::Book.new({
|
29
|
+
genre: page.css("h1").text.split(/\s-\s/)[0].strip, #genre
|
30
|
+
title: book.css("h2.title").text.split.collect(&:capitalize).join(" "),
|
31
|
+
author: book.css("p.author").text.split.delete_if{|x| x == "by"}.join(" "),
|
32
|
+
publisher: book.css("p.publisher").text,
|
33
|
+
wol: book.css("p.freshness").text,
|
34
|
+
summary: book.css("p.description").text
|
35
|
+
})
|
41
36
|
end
|
42
37
|
end
|
43
38
|
end
|