budget-bytes-cli 0.2.5 → 0.4.2
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/config/environment.rb +1 -1
- data/lib/budget_bytes_cli/category.rb +4 -40
- data/lib/budget_bytes_cli/cli.rb +1 -2
- data/lib/budget_bytes_cli/recipe.rb +6 -47
- data/lib/budget_bytes_cli/scraper.rb +100 -0
- metadata +3 -3
- data/lib/budget_bytes_cli/category-scraper.rb +0 -18
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 928104b51be5f14c7c5e731fd245c70aefc70035
|
4
|
+
data.tar.gz: dbd15194ace0333162416f65af95ce081aa51678
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 05cc86ddb7065a5fafd3fdeefc244589522f2e85ac6aedd0d9e8a6fa5265bca2cc9c46cd7b101c972609d5293333e874e7766703fdd6c9a7dba32704299cf534
|
7
|
+
data.tar.gz: 51827b09d2c44c2c6b35c6ad6887a80f6903cd3e538d99c12373f1b54215d750bb55775af27daf1bf059ad0a2fe0ad1b8950cbdc8b0f5f8186ad80951623cbec
|
data/config/environment.rb
CHANGED
@@ -5,7 +5,7 @@ require 'launchy'
|
|
5
5
|
require 'io/console'
|
6
6
|
|
7
7
|
require_relative '../lib/budget_bytes_cli/cli'
|
8
|
-
require_relative '../lib/budget_bytes_cli/
|
8
|
+
require_relative '../lib/budget_bytes_cli/scraper'
|
9
9
|
require_relative '../lib/budget_bytes_cli/category'
|
10
10
|
require_relative '../lib/budget_bytes_cli/recipe'
|
11
11
|
require_relative '../lib/budget_bytes_cli/array-prompter'
|
@@ -14,50 +14,14 @@ class BudgetBytesCli::Category
|
|
14
14
|
end
|
15
15
|
|
16
16
|
def recipes
|
17
|
-
self.
|
18
|
-
@recipes
|
19
|
-
end
|
20
|
-
|
21
|
-
def get_recipes
|
22
|
-
@recipes = []
|
23
|
-
first_page = Nokogiri::HTML(open(self.url))
|
17
|
+
@recipes = BudgetBytesCli::Scraper.get_recipes(self.url) unless @recipes
|
24
18
|
|
25
|
-
|
26
|
-
if page_nums.empty?
|
27
|
-
pages_total = 1
|
28
|
-
else
|
29
|
-
pages_total = page_nums.map{|p| p.text.to_i}.max
|
30
|
-
end
|
31
|
-
|
32
|
-
(1..pages_total).each do |p|
|
33
|
-
get_recipes_from(create_page_url(p))
|
34
|
-
end
|
35
|
-
end
|
36
|
-
|
37
|
-
def get_recipes_from(page_url)
|
38
|
-
recipe_page = Nokogiri::HTML(open(page_url))
|
39
|
-
recipe_links = recipe_page.css(".archive-post a")
|
40
|
-
recipe_links.each do |r|
|
41
|
-
recipe_title = r.attribute("title").value
|
42
|
-
recipe_url = r.attribute("href").value
|
43
|
-
@recipes << BudgetBytesCli::Recipe.new(recipe_url, recipe_title)
|
44
|
-
end
|
45
|
-
end
|
46
|
-
|
47
|
-
def create_page_url(num)
|
48
|
-
if num == 1
|
49
|
-
self.url
|
50
|
-
else
|
51
|
-
self.url + "page/" + num.to_s + "/"
|
52
|
-
end
|
19
|
+
@recipes
|
53
20
|
end
|
54
|
-
|
21
|
+
|
55
22
|
def combine_recipes(cat_to_combine)
|
56
23
|
recipes_combined = cat_to_combine.recipes
|
57
24
|
recipe_urls = self.recipes.map {|r| r.url}
|
58
|
-
|
59
|
-
recipe_urls.include?(r.url)
|
60
|
-
end
|
61
|
-
filtered_recipes
|
25
|
+
recipes_combined.select {|r| recipe_urls.include?(r.url)}
|
62
26
|
end
|
63
27
|
end
|
data/lib/budget_bytes_cli/cli.rb
CHANGED
@@ -2,8 +2,7 @@ class BudgetBytesCli::CLI
|
|
2
2
|
|
3
3
|
def call
|
4
4
|
puts "Welcome to Budget Bytes CLI!"
|
5
|
-
|
6
|
-
scraper.create_categories
|
5
|
+
BudgetBytesCli::Scraper.create_categories
|
7
6
|
category_selector = BudgetBytesCli::ArrayPrompter.new("Selecting recipe category.")
|
8
7
|
category_selector.array_to_select = BudgetBytesCli::Category.all.map {|i| i.name}
|
9
8
|
|
@@ -6,58 +6,17 @@ class BudgetBytesCli::Recipe
|
|
6
6
|
@url = url
|
7
7
|
end
|
8
8
|
|
9
|
-
def scrape_recipe
|
10
|
-
page = Nokogiri::HTML(open(@url))
|
11
|
-
|
12
|
-
ingredient_amounts = page.css('.wprm-recipe-ingredient-amount').map {|i| i.text}
|
13
|
-
ingredient_units = page.css('.wprm-recipe-ingredient-unit').map {|i| i.text}
|
14
|
-
ingredient_names = page.css('.wprm-recipe-ingredient-name').map {|i| i.text}
|
15
|
-
|
16
|
-
ingredient_array = []
|
17
|
-
|
18
|
-
ingredient_amounts.each_with_index do |ele, idx|
|
19
|
-
ingredient_array << [ele, ingredient_units[idx], ingredient_names[idx]].join(' ').strip
|
20
|
-
end
|
21
|
-
|
22
|
-
#scraping for old site before css switched so that the code above scrapes ingredients
|
23
|
-
old_ingredients_table = page.css("tr")
|
24
|
-
|
25
|
-
#get rid of first, last rows in table (header and total cost)
|
26
|
-
old_ingredients_table.shift
|
27
|
-
old_ingredients_table.pop
|
28
|
-
|
29
|
-
old_ingredients_table.each do |old_ingredient|
|
30
|
-
old_ingredient_text = old_ingredient.text.split("\n")
|
31
|
-
|
32
|
-
#get rid of first blank item, last cost item
|
33
|
-
old_ingredient_text.shift
|
34
|
-
old_ingredient_text.pop
|
35
|
-
|
36
|
-
ingredient_array << old_ingredient_text.join(" ")
|
37
|
-
end
|
38
|
-
|
39
|
-
recipe_steps = []
|
40
|
-
|
41
|
-
#scraping for old site before css switched to have recipe instructions in own class
|
42
|
-
page.css("p").map {|i| i.text}.each do |p|
|
43
|
-
if p.split(" ")[0] == "STEP"
|
44
|
-
recipe_steps << p.split(" ").slice(2, p.length - 2).join(" ")
|
45
|
-
end
|
46
|
-
end
|
47
|
-
|
48
|
-
page.css(".wprm-recipe-instruction-text").each {|i| recipe_steps << i.text}
|
49
|
-
|
50
|
-
@ingredients = ingredient_array.join("\n")
|
51
|
-
@instructions = recipe_steps.join("\n")
|
52
|
-
end
|
53
|
-
|
54
9
|
def ingredients
|
55
|
-
|
10
|
+
unless @ingredients
|
11
|
+
@ingredients, @instructions = BudgetBytesCli::Scraper.scrape_recipe(@url)
|
12
|
+
end
|
56
13
|
@ingredients
|
57
14
|
end
|
58
15
|
|
59
16
|
def instructions
|
60
|
-
|
17
|
+
unless @instructions
|
18
|
+
@ingredients, @instructions = BudgetBytesCli::Scraper.scrape_recipe(@url)
|
19
|
+
end
|
61
20
|
@instructions
|
62
21
|
end
|
63
22
|
|
@@ -0,0 +1,100 @@
|
|
1
|
+
class BudgetBytesCli::Scraper
|
2
|
+
|
3
|
+
#functions for getting categories
|
4
|
+
def self.open_page
|
5
|
+
Nokogiri::HTML(open("https://www.budgetbytes.com/recipes/"))
|
6
|
+
end
|
7
|
+
|
8
|
+
def self.locate_categories
|
9
|
+
open_page.css(".cat-item")
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.create_categories
|
13
|
+
locate_categories.each do |item|
|
14
|
+
url = item.css("a").attribute("href").value
|
15
|
+
title = item.css("a").children[0].text
|
16
|
+
BudgetBytesCli::Category.new(url, title)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
#functions for scraping recipes within a category
|
21
|
+
def self.get_recipes(url)
|
22
|
+
first_page = Nokogiri::HTML(open(url))
|
23
|
+
|
24
|
+
page_nums = first_page.css(".page-numbers")
|
25
|
+
if page_nums.empty?
|
26
|
+
pages_total = 1
|
27
|
+
else
|
28
|
+
pages_total = page_nums.map{|p| p.text.to_i}.max
|
29
|
+
end
|
30
|
+
|
31
|
+
(1..pages_total).map {|p|get_recipes_from(create_page_url(p, url))}.flatten
|
32
|
+
end
|
33
|
+
|
34
|
+
def self.get_recipes_from(page_url)
|
35
|
+
recipe_page = Nokogiri::HTML(open(page_url))
|
36
|
+
recipe_links = recipe_page.css(".archive-post a")
|
37
|
+
|
38
|
+
recipe_links.map do |r|
|
39
|
+
recipe_title = r.attribute("title").value
|
40
|
+
recipe_url = r.attribute("href").value
|
41
|
+
BudgetBytesCli::Recipe.new(recipe_url, recipe_title)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def self.create_page_url(num, url)
|
46
|
+
if num == 1
|
47
|
+
url
|
48
|
+
else
|
49
|
+
url + "page/" + num.to_s + "/"
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
#scrape instructions, ingredients from recipe page
|
54
|
+
def self.scrape_recipe(url)
|
55
|
+
page = Nokogiri::HTML(open(url))
|
56
|
+
|
57
|
+
ingredient_amounts = page.css('.wprm-recipe-ingredient-amount').map {|i| i.text}
|
58
|
+
ingredient_units = page.css('.wprm-recipe-ingredient-unit').map {|i| i.text}
|
59
|
+
ingredient_names = page.css('.wprm-recipe-ingredient-name').map {|i| i.text}
|
60
|
+
|
61
|
+
ingredient_array = []
|
62
|
+
|
63
|
+
#scraping ingredients for new site
|
64
|
+
ingredient_amounts.each_with_index do |ele, idx|
|
65
|
+
ingredient_array << [ele, ingredient_units[idx], ingredient_names[idx]].join(' ').strip
|
66
|
+
end
|
67
|
+
|
68
|
+
#scraping for old site before css switched so that the code above scrapes ingredients
|
69
|
+
old_ingredients_table = page.css("tr")
|
70
|
+
|
71
|
+
#get rid of first, last rows in table (header and total cost)
|
72
|
+
old_ingredients_table.shift
|
73
|
+
old_ingredients_table.pop
|
74
|
+
|
75
|
+
old_ingredients_table.each do |old_ingredient|
|
76
|
+
old_ingredient_text = old_ingredient.text.split("\n")
|
77
|
+
|
78
|
+
#get rid of first blank item, last cost item
|
79
|
+
old_ingredient_text.shift
|
80
|
+
old_ingredient_text.pop
|
81
|
+
|
82
|
+
ingredient_array << old_ingredient_text.join(" ")
|
83
|
+
end
|
84
|
+
|
85
|
+
recipe_steps = []
|
86
|
+
|
87
|
+
#scraping instructions for old site before css switched to have recipe instructions in own class
|
88
|
+
page.css("p").map {|i| i.text}.each do |p|
|
89
|
+
if p.split(" ")[0] == "STEP"
|
90
|
+
recipe_steps << p.split(" ").slice(2, p.length - 2).join(" ")
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
#scraping instructions for new site
|
95
|
+
page.css(".wprm-recipe-instruction-text").each {|i| recipe_steps << i.text}
|
96
|
+
|
97
|
+
[ingredient_array.join("\n"), recipe_steps.join("\n")]
|
98
|
+
end
|
99
|
+
|
100
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: budget-bytes-cli
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2
|
4
|
+
version: 0.4.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Larry Weinstein
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-12-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -106,10 +106,10 @@ files:
|
|
106
106
|
- config/environment.rb
|
107
107
|
- lib/budget_bytes_cli.rb
|
108
108
|
- lib/budget_bytes_cli/array-prompter.rb
|
109
|
-
- lib/budget_bytes_cli/category-scraper.rb
|
110
109
|
- lib/budget_bytes_cli/category.rb
|
111
110
|
- lib/budget_bytes_cli/cli.rb
|
112
111
|
- lib/budget_bytes_cli/recipe.rb
|
112
|
+
- lib/budget_bytes_cli/scraper.rb
|
113
113
|
homepage: https://github.com/Larry-42/budget-bytes-cli
|
114
114
|
licenses:
|
115
115
|
- MIT
|
@@ -1,18 +0,0 @@
|
|
1
|
-
class BudgetBytesCli::CategoryScraper
|
2
|
-
|
3
|
-
def open_page
|
4
|
-
Nokogiri::HTML(open("https://www.budgetbytes.com/recipes/"))
|
5
|
-
end
|
6
|
-
|
7
|
-
def locate_categories
|
8
|
-
open_page.css(".cat-item")
|
9
|
-
end
|
10
|
-
|
11
|
-
def create_categories
|
12
|
-
locate_categories.each do |item|
|
13
|
-
url = item.css("a").attribute("href").value
|
14
|
-
title = item.css("a").children[0].text
|
15
|
-
BudgetBytesCli::Category.new(url, title)
|
16
|
-
end
|
17
|
-
end
|
18
|
-
end
|