Last active
January 19, 2021 08:44
-
-
Save georgekettle/7bcb5d26854acca38a160bbbcf8198af to your computer and use it in GitHub Desktop.
Christmas gift list
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name | bought | price | |
---|---|---|---|
macbook pro | false | 20 | |
handbag | false | 35 | |
Long Pendant Sterling Silver Necklace | false | 50 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# What are you looking for on Etsy? | |
# > Jeans | |
# Here are Etsy results for "Jeans": | |
# 1 - Levis Blue Jeans | |
# 2 - Vintage Jeans | |
# 3 - Cargo Jeans Pants | |
# 4 - White Jeans | |
# etc. | |
# Pick one to add to your list (give the number) | |
# > 2 | |
# "Vintage Jeans" added to your wishlist | |
require 'colorize' | |
require 'pry' | |
require 'open-uri' | |
require 'nokogiri' | |
require_relative 'scraper.rb' | |
# each element is like so: {name: "macbook pro", bought: false} | |
# WISH_LIST = [ | |
# {name: "macbook pro", bought: false}, | |
# {name: "masterful coding skills", bought: false} | |
# ] | |
WISH_LIST = load_wish_list | |
action = nil | |
def list_items(array = WISH_LIST) | |
puts "--------------------------" | |
puts "Items:" | |
array.each_with_index do |item, index| | |
check = item[:bought] ? "X" : "" | |
puts "#{index + 1} [#{check}] #{item[:name]} - $#{item[:price]}" | |
end | |
puts "--------------------------" | |
end | |
def add_item | |
puts "What would you like to add?" | |
new_item = gets.chomp | |
puts "What price is the item?" | |
price = gets.chomp | |
# WISH_LIST << new_item | |
WISH_LIST << {name: new_item, bought: false, price: price} | |
# Push this WISH_LIST into the csv using: | |
update_wish_list(WISH_LIST) | |
end | |
def delete_item | |
list_items | |
puts "What would you like to delete? (choose number)" | |
item_index = gets.chomp.to_i - 1 | |
WISH_LIST.delete_at(item_index) | |
# Push this WISH_LIST into the csv using: | |
update_wish_list(WISH_LIST) | |
end | |
def mark_item | |
# before that? | |
list_items | |
puts "Which item would you like to mark? (choose number)" | |
item_index = gets.chomp.to_i - 1 | |
# edit the item in the list? | |
item = WISH_LIST[item_index] | |
item[:bought] = !item[:bought] | |
# Push this WISH_LIST into the csv using: | |
update_wish_list(WISH_LIST) | |
end | |
def search_and_suggest | |
# ask user for input | |
puts "What are you looking for on Etsy?" | |
# save this input | |
search_query = gets.chomp | |
# use this search_query to search 'Etsy' | |
puts "Search results for '#{search_query}' on Etsy:" | |
search_results_array = etsy_scraper(search_query) | |
list_items(search_results_array) | |
# prompt select item | |
puts "Which item would you like to add to wishlist? (choose number)".yellow | |
# get user input and use number as index | |
item_index = gets.chomp.to_i - 1 | |
# to select item from search_results_array | |
selected_item = search_results_array[item_index] | |
# add this item to our wishlist | |
WISH_LIST << selected_item # from search results | |
# Push this WISH_LIST into the csv using: | |
update_wish_list(WISH_LIST) | |
puts "Your item has been added to your wishlist".green | |
end | |
# Pseudo-code: | |
# 1. Welcome | |
puts "Welcome to your Christmas gift list" | |
# 2. Display menu (list / add / delete / mark ) | |
until action == 'quit' | |
# 3. Get user action | |
puts "Which action [list|add|delete|mark|idea|quit]?".yellow | |
action = gets.chomp | |
# 4. Perform the right action | |
case action | |
when "list" then list_items | |
when "add" then add_item | |
when "delete" then delete_item | |
when "mark" then mark_item | |
when "idea" then search_and_suggest | |
when "quit" then break | |
else | |
puts "Invalid input, please try again" | |
end | |
end | |
puts "Goodbye" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'nokogiri' | |
require 'open-uri' | |
require 'pry' | |
require 'csv' | |
def test_scraper | |
filepath = "/Users/georgekettle/code/lewagon/fullstack-challenges-recap/01-Ruby/06-Parsing/Reboot-01-Christmas-list/results.html" | |
# 1. We get the HTML page content | |
html_content = File.open(filepath) | |
# 2. We build a Nokogiri document from this file | |
doc = Nokogiri::HTML(html_content) | |
# # 3. We search for the correct elements containing the items' title in our HTML doc | |
doc.search('.v2-listing-card .text-body').map {|element| {name: element.text.strip, bought: false} } | |
end | |
def etsy_scraper(keyword) | |
# 1. We get the HTML page content | |
html_content = open("https://www.etsy.com/search?q=#{keyword}").read | |
# 2. We build a Nokogiri document from this file | |
doc = Nokogiri::HTML(html_content) | |
# # 3. We search for the correct elements containing the items' title in our HTML doc | |
doc.search('.v2-listing-card').map do |element| | |
name = element.search('.text-body').first.text.strip | |
price = element.search('.currency-value').text.strip.to_i | |
# element.search('.text-body') | |
{name: name, price: price, bought: false} | |
end | |
end | |
def load_wish_list | |
csv_options = { col_sep: ',', quote_char: '"', headers: :first_row } | |
filepath = 'lib/database.csv' | |
wishlist = [] | |
CSV.foreach(filepath, csv_options) do |row| | |
# create an array for our wishlist | |
bought = row["bought"] == "true" | |
wishlist << { name: row["name"], bought: bought, price: row["price"] } | |
end | |
wishlist | |
end | |
def update_wish_list(wish_list) | |
csv_options = { col_sep: ',', force_quotes: true, quote_char: '"' } | |
filepath = 'lib/database.csv' | |
CSV.open(filepath, 'wb', csv_options) do |csv| | |
# We had headers to the CSV | |
csv << ['name', 'bought', 'price'] | |
wish_list.each do |item| | |
csv << [item[:name], item[:bought], item[:price].to_s] | |
end | |
end | |
end | |
# wish_list = [ | |
# {name: "macbook pro", bought: false}, | |
# {name: "masterful coding skills", bought: false} | |
# ] | |
# update_wish_list(wish_list) | |
# p etsy_scraper('shoes') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment