Skip to content

Instantly share code, notes, and snippets.

@charles-eric
Created January 13, 2015 16:59
Show Gist options
  • Save charles-eric/644e5fb3b0f342416426 to your computer and use it in GitHub Desktop.
Save charles-eric/644e5fb3b0f342416426 to your computer and use it in GitHub Desktop.
etsy wishlist program
require_relative "scraper"
def print_list(list)
list.each_with_index do |article, index|
if article[:bought]
puts "#{index + 1} - #{article[:name]} - (#{article[:price]}€)[X]"
else
puts "#{index + 1} - #{article[:name]} - (#{article[:price]}€)[-]"
end
end
end
wishlist = [
{name: "bracelet", price: 19, bought: false},
{name: "collier", price: 29, bought: true},
{name: "bague", price: 39, bought: false}
]
while true
print_list(wishlist)
puts "1- ajouter un article"
puts "2- marquer comme acheté"
puts "3- s'inspirer sur etsy"
puts "other- exit"
action = gets.chomp.to_i
if action == 1
puts "nom de l'article ?"
name = gets.chomp
puts "prix de l'article ?"
price = gets.chomp.to_i
wishlist << {name: name, price: price, bought: false}
elsif action == 2
puts "numéro de l'article à marquer comme acheté"
num = gets.chomp.to_i
wishlist[num - 1][:bought] = true
elsif action == 3
puts "What are u looking for?"
category = gets.chomp
etsy_list = scraper(category)
print_list(etsy_list)
puts "Which one? (give the nb)"
num = gets.chomp.to_i
wishlist << etsy_list[num - 1]
else
break
end
end
puts "Bye Bye"
require "open-uri"
require "nokogiri"
def scraper(category)
result = []
doc = Nokogiri::HTML(open("https://www.etsy.com/search?q=#{category}"))
doc.search('.listing-detail').each do |element|
name = element.search(".title").text
price = element.search(".currency-value").text.to_f
result << {name: "#{name[0..10]}...", price: price, bought: false}
end
return result
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment