ebookdealinfo 1.0.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b2b469d9f1996f015d320bbf4948873186bea439
4
+ data.tar.gz: 3ba1dad6388407f5fff202de99e69afa8b5e0f95
5
+ SHA512:
6
+ metadata.gz: 231a456cddc2468a41eb3fbb05f28a417c669b830d38100389a1883a4ca7cfe9b027bafc578328010a2105d6c3eb40f262ce0372765e68c687de64e015e85930
7
+ data.tar.gz: db62e7b8dd07b00465778ee2d435aa9d988738e184e90f7764b6c31fa43ce25e4b0402546fd5fdc5370c9f27e46b9dd8ee6a14157b1fbc039a9f274d886d521e
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.3
5
+ before_install: gem install bundler -v 1.15.4
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source "https://rubygems.org"
2
+
3
+ #git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in EbookDealInfo.gemspec
6
+ #gemspec
7
+
8
+ gem 'ebookdealinfo'
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 kylek
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,25 @@
1
+ # ebookdealinfo
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/EbookDealInfo`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ ## Installation
6
+
7
+ $ gem install ebookdealinfo
8
+
9
+ ## Usage
10
+
11
+ Reddit /r/Ebookdeals will be scraped for a list of ebooks with current deals and then goodreads will be scraped to get more information about those books. You can then select a book from the menu to get detailed information about it.
12
+
13
+ ## Development
14
+
15
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
16
+
17
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
18
+
19
+ ## Contributing
20
+
21
+ Bug reports and pull requests are welcome on GitHub at https://github.com/<github username>/EbookDealInfo.
22
+
23
+ ## License
24
+
25
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "ebookdealinfo"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/ebookdealinfo ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require_relative "../lib/ebookdealinfo.rb"
4
+
5
+ Controller.new.call
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,37 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "ebookdealinfo/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "ebookdealinfo"
8
+ spec.version = EbookDealInfo::VERSION
9
+ spec.authors = ["kylek"]
10
+ spec.email = ["27875726+kylekinnear@users.noreply.github.com"]
11
+
12
+ spec.summary = "Grabs daily ebook deals and gets goodreads info about them."
13
+ spec.homepage = "https://github.com/kylekinnear/ebook-deal-info"
14
+ spec.license = "MIT"
15
+
16
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
17
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
18
+ # if spec.respond_to?(:metadata)
19
+ # spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
20
+ # else
21
+ # raise "RubyGems 2.0 or newer is required to protect against "
22
+ # "public gem pushes."
23
+ # end
24
+
25
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
26
+ f.match(%r{^(test|spec|features)/})
27
+ end
28
+ #spec.bindir = "exe"
29
+ spec.executables = ["ebookdealinfo"]#spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
30
+ spec.require_paths = ["lib"]
31
+
32
+ spec.add_development_dependency "bundler", "~> 1.15"
33
+ spec.add_development_dependency "rake", "~> 10.0"
34
+ spec.add_development_dependency "rspec", "~> 3.0"
35
+
36
+ spec.add_dependency "nokogiri", "~> 1.8"
37
+ end
@@ -0,0 +1,8 @@
1
+ require 'open-uri'
2
+ require 'nokogiri'
3
+
4
+ require_relative "./ebookdealinfo/version"
5
+ require_relative "./ebookdealinfo/book.rb"
6
+ require_relative "./ebookdealinfo/controller.rb"
7
+ require_relative "./ebookdealinfo/deal_scraper.rb"
8
+ require_relative "./ebookdealinfo/info_scraper.rb"
@@ -0,0 +1,47 @@
1
+ #this class holds the data for each book
2
+ #require_relative "EbookDealInfo"
3
+
4
+ class Book
5
+ attr_accessor :author, :title, :price, :genre_one, :genre_two, :series, :rating, :rates, :blurb, :completable
6
+ @@all = [] #collection to push all new books into
7
+
8
+ def initialize(author, title, price, good_scrape=1)
9
+ if good_scrape == 1
10
+ @@all << self #push new books
11
+ @author = author
12
+ @title = title
13
+ @price = price
14
+ @completable = true
15
+ InfoScraper.new.info_scrape(self)
16
+ else
17
+ @@all << self
18
+ @completable = false
19
+ end
20
+ end
21
+
22
+ def self.create(author, title, price, good_scrape=1) #deals scrape will pass in hash with title, author, and price
23
+ self.new(author, title, price, good_scrape)
24
+ end
25
+
26
+ def self.all #we'll use this to grab the collection, which the goodreads scraper will use
27
+ @@all
28
+ end
29
+
30
+ def wrap_blurb(width=78) #code from other projects to make blurbs line wrap; handle unicode replacement elsewhere
31
+ lines = []
32
+ line = ""
33
+ @blurb.split(/\s+/).each do |word|
34
+ if line.size + word.size >= width
35
+ lines << line
36
+ line = word
37
+ elsif line.empty?
38
+ line = word
39
+ else
40
+ line << " " << word
41
+ end
42
+ end
43
+ lines << line if line
44
+ return (lines.join "\n")
45
+ end
46
+
47
+ end
@@ -0,0 +1,72 @@
1
+ #this class is called by the cli and handles outputs, menus
2
+ #require_relative "EbookDealInfo"
3
+
4
+ class Controller
5
+ attr_accessor :completed_books
6
+
7
+ def call
8
+ welcome #call the intro message
9
+ end
10
+
11
+ def welcome
12
+ #intro message
13
+ puts "Welcome to the Ebook Recent Deal Info Getter"
14
+ puts "Getting the latest deals (this may take a while)"
15
+ puts "----------------------------------------------------------------------------"
16
+ DealScraper.new.scrape #call DealScraper, and through it instantiate books and call info_scraper on the books
17
+ @completed_books = [] #we'll use this to avoid interacting with bad scrapes
18
+ Book.all.each {|book| @completed_books << book if book.completable == true}
19
+ list_books #main menu
20
+ end
21
+
22
+ def list_books
23
+ #the main menu
24
+ puts "A list of the latest deals:"
25
+ puts "#{Book.all.select {|i| i.completable == false}.size} book(s) failed to load. Probably a spelling or selector error."
26
+ @completed_books.each_with_index do |book, index|
27
+ puts "#{index+1}. #{book.title} - #{book.author} - #{book.genre_one}"
28
+ end
29
+
30
+ interact
31
+ exeunt
32
+ end
33
+
34
+ def interact
35
+ input = nil
36
+ while %w[e exit q quit n no].include?(input) == false
37
+ puts "----------------------------------------------------------------------------\nEnter the number of the book you'd like more information about."
38
+ puts "You can type list to list the books again or type quit to leave."
39
+ input = gets.strip.downcase
40
+
41
+ if input.to_i > 0 && input.to_i <= completed_books.size
42
+ chosen_book = @completed_books[input.to_i-1]
43
+ puts "----------------------------------------------------------------------------\n#{chosen_book.title}"
44
+ puts "#{chosen_book.series}" if chosen_book.series.size > 0
45
+ puts "By #{chosen_book.author}"
46
+ if chosen_book.price.size > 0
47
+ puts "Price: #{chosen_book.price}"
48
+ else
49
+ puts "Couldn't find a price"
50
+ end
51
+ if chosen_book.genre_two.include?(chosen_book.genre_one)
52
+ puts "Shelved as #{chosen_book.genre_two}"
53
+ elsif chosen_book.genre_one == "No genre listed"
54
+ puts "This book doesn't have a genre listing"
55
+ else
56
+ puts "Shelved as #{chosen_book.genre_one} #{"and #{chosen_book.genre_two}" if chosen_book.genre_two.size > 0}"
57
+ end
58
+ puts "#{chosen_book.rates} people gave this book an average rating of #{chosen_book.rating}"
59
+ puts "----------------------------------------------------------------------------\n#{chosen_book.wrap_blurb}" #line wrap our blurb
60
+ elsif input == "list"
61
+ list_books
62
+ elsif %w[e exit q quit n no].include?(input) == false
63
+ puts "Sorry, I couldn't understand that. Please enter a number, list, or quit."
64
+ end
65
+ end
66
+ end
67
+
68
+ def exeunt
69
+ puts "----------------------------------------------------------------------------\nCheck back later for more ebook deals."
70
+ end
71
+
72
+ end
@@ -0,0 +1,33 @@
1
+ #this class grabs the daily deals from reddit /r/ebookdeals
2
+ #require_relative "EbookDealInfo"
3
+
4
+ class DealScraper
5
+
6
+ def scrape #this method scrapes the deals page, then for each item on that page, instantiates a new book with the relevant info; are we passing a hash or attributes?
7
+ deals = Nokogiri::HTML(open("https://www.reddit.com/r/ebookdeals/new/",'User-Agent' => 'Chrome'))
8
+
9
+ deals.search("div.link").each_with_index do |post,index|
10
+ if post.search("p.title").text.size > 0
11
+ if post.search("p.title").text.include?(";") #for posts formatted "#~Author~; ~Title; ~Price~"
12
+ author = post.search("p.title").text.split(";")[0].strip
13
+ #.search("p.title a").attribute("href").value
14
+ title = post.search("p.title").text.split(";")[1].strip
15
+ price = post.search("p.title").text.split(";")[2].strip #can't handle free books
16
+ Book.create(author, title, price)
17
+ puts "Loaded book ##{index+1} of 25"
18
+ else #for posts formatted "~Title~ by ~author~ (~Price~)"
19
+ if post.search("p.title").text.slice(/\s([Bb]y)/) != nil #ignore wacky formatted posts
20
+ author = post.search("p.title").text.split("by")[1].slice(/\A[^(,$\/]+/).split(". Kindle")[0].split("-- Kindle")[0].strip
21
+ title = post.search("p.title").text.split("by").first.gsub(/\W+\z/, "").strip
22
+ price = post.search("p.title").text.slice(/[$]\d+[.]\d+/).strip #can't handle free books
23
+ Book.create(author, title, price)
24
+ puts "Loaded book ##{index+1} of 25"
25
+ else
26
+ Book.create("","","",0)
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
32
+
33
+ end
@@ -0,0 +1,41 @@
1
+ #this is the goodreads scraper
2
+ #require_relative "EbookDealInfo"
3
+
4
+ class InfoScraper
5
+
6
+ def info_scrape(book) #for each instance of book in the class collection, go get blurb, series, gr rating/rates and add them to that instance; also author to deal with last name only from scrape?
7
+ search_string = "#{book.title} #{book.author.gsub(".", ". ").gsub(/[^\w\s]/,"")}".gsub(/(\A|\s)\S\s/," ").gsub(/[^a-zA-Z0-9']+/, "+") #turns the author + title into a usable goodreads search string
8
+ #should remove anything joining multiple authors ("&", ",") that would break the search
9
+ search_page = Nokogiri::HTML(open("https://www.goodreads.com/search?q=#{search_string}&search_type=books",'User-Agent' => 'Ruby')) #uses the search string to pull an item's goodreads page
10
+ if search_page.css("table a").size != 0
11
+ determinant = search_page.css("span.minirating").map.with_index {|i,index| [index, i.text.strip.slice(/\s(\d|,)+/).strip.gsub(",","").to_i]}.sort! {|x,y| x[1].to_i <=> y[1].to_i}.last #the search result with the most rates (and presumably most legitimate) is an array [result_index, #rates]
12
+ item_page = Nokogiri::HTML(open("https://goodreads.com/#{search_page.css("table a.bookTitle")[determinant[0]].attribute("href").value}",'User-Agent' => 'Ruby').read)
13
+ book.author = item_page.search("div#bookAuthors.stacked span :not(.greyText) :not(.smallText)").text #gets the complete author name since reddit might not provide it
14
+ book.title = item_page.search("h1#bookTitle.bookTitle").text.slice(/^(\n).+(\S\n)/).strip #goodreads provides better titles
15
+ book.series = item_page.search("h1#bookTitle.bookTitle :first-child").text.strip.gsub(/[()]/, "") #provides series
16
+ book.rating = item_page.search("span.average").text #average rating
17
+ book.rates = item_page.search("span.votes.value-title").text.strip #number of ratings
18
+ #blurb needs work
19
+ book.blurb = item_page.xpath('//span[starts-with(@id, "freeText")]')[1].text#grab the blurb
20
+ #we will scrape the top two genre entries, but when we want to check if one is a more specific form of the other
21
+ if item_page.search("div.bigBoxContent div.elementList div.left").empty?
22
+ book.genre_one = "No genre listed"
23
+ book.genre_two = ""
24
+ else
25
+ genre_one = item_page.search("div.bigBoxContent div.elementList div.left")[0].text.split("\n").map {|i| i.strip if i.strip.size > 0}.reject {|i| i==nil} # turn the first genre into a stripped array of actual content
26
+ book.genre_one = ""
27
+ genre_one.each {|i| book.genre_one << "#{i} "}
28
+ if item_page.search("div.bigBoxContent div.elementList div.left")[1] != nil
29
+ genre_two = item_page.search("div.bigBoxContent div.elementList div.left")[1].text.split("\n").map {|i| i.strip if i.strip.size > 0}.reject {|i| i==nil}
30
+ book.genre_two = ""
31
+ genre_two.each {|i| book.genre_two << "#{i} "}
32
+ else
33
+ book.genre_two = ""
34
+ end
35
+ end
36
+ else #instead of a raising a nobook error that will break the looping, let's flag the book as incomplete and not display it at the end
37
+ book.completable = false
38
+ end
39
+ end
40
+
41
+ end
@@ -0,0 +1,3 @@
1
+ module EbookDealInfo
2
+ VERSION = "1.0.0"
3
+ end
data/spec.md ADDED
@@ -0,0 +1,6 @@
1
+ # Specifications for the CLI Assessment
2
+
3
+ Specs:
4
+ - [X] Have a CLI for interfacing with the application - The program begins scraping automatically, than prompts the user with a list of books, each of which the user can use CLI to select and get more information about
5
+ - [X] Pull data from an external source - reddit is scraped for a list of books that currently have available deals, then the goodreads page for each of those books is scraped to get detailed information
6
+ - [X] Implement both list and detail views - The list is of the ~25 books (occasional weird reddit topics will produce cases the scraper will ignore) posted on reddit, and the books have individual details pages
metadata ADDED
@@ -0,0 +1,119 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ebookdealinfo
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - kylek
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-09-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.15'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.15'
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
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: nokogiri
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.8'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.8'
69
+ description:
70
+ email:
71
+ - 27875726+kylekinnear@users.noreply.github.com
72
+ executables:
73
+ - ebookdealinfo
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - ".rspec"
79
+ - ".travis.yml"
80
+ - Gemfile
81
+ - LICENSE.txt
82
+ - README.md
83
+ - Rakefile
84
+ - bin/console
85
+ - bin/ebookdealinfo
86
+ - bin/setup
87
+ - ebookdealinfo.gemspec
88
+ - lib/ebookdealinfo.rb
89
+ - lib/ebookdealinfo/book.rb
90
+ - lib/ebookdealinfo/controller.rb
91
+ - lib/ebookdealinfo/deal_scraper.rb
92
+ - lib/ebookdealinfo/info_scraper.rb
93
+ - lib/ebookdealinfo/version.rb
94
+ - spec.md
95
+ homepage: https://github.com/kylekinnear/ebook-deal-info
96
+ licenses:
97
+ - MIT
98
+ metadata: {}
99
+ post_install_message:
100
+ rdoc_options: []
101
+ require_paths:
102
+ - lib
103
+ required_ruby_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ required_rubygems_version: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ requirements: []
114
+ rubyforge_project:
115
+ rubygems_version: 2.6.7
116
+ signing_key:
117
+ specification_version: 4
118
+ summary: Grabs daily ebook deals and gets goodreads info about them.
119
+ test_files: []