Created
July 20, 2021 15:40
-
-
Save Haumer/4121ace214cd6485705e15ce339e581e to your computer and use it in GitHub Desktop.
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 'open-uri' | |
require 'nokogiri' | |
puts "what would you like to look for?" | |
keyword = gets.chomp | |
url = "https://www.etsy.com/uk/search?q=#{keyword}" | |
html_file = URI.open(url).read | |
html_doc = Nokogiri::HTML(html_file) | |
html_doc.search('.v2-listing-card').each do |element| | |
p element.search("h3").text.strip | |
end |
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 'csv' | |
# Pseudo-code: | |
# 1. Welcome => with acsii waves!!! | |
puts "{¯`·._.• .·´« {¯`·._.•..•._.·´¯} »`·.•._.·´¯}" | |
puts "{¯`·._.• .·´« Christmas Wishlist »`·.•._.·´¯}" | |
puts "{¯`·._.• .·´« {¯`·._.•..•._.·´¯} »`·.•._.·´¯}" | |
def print_wish_list(gifts) | |
gifts.each_with_index do |gift, index| | |
marked = gift["marked"] ? "X" : " " | |
puts "#{index + 1}. - [#{marked}] #{gift["name"].capitalize}: £#{gift["price"]}" | |
end | |
end | |
def load_csv | |
csv_options = { col_sep: ',', quote_char: '"', headers: :first_row } | |
filepath = 'gifts.csv' | |
array = [] | |
CSV.foreach(filepath, csv_options) do |row| | |
new_gift = {} | |
new_gift["price"] = row["Price"].to_i | |
new_gift["marked"] = row["Marked"] == "true" | |
new_gift["name"] = row["Name"] | |
array << new_gift | |
end | |
array | |
end | |
def save_csv(gifts) | |
csv_options = { col_sep: ',', force_quotes: true, quote_char: '"' } | |
filepath = 'gifts.csv' | |
CSV.open(filepath, 'wb', csv_options) do |csv| | |
csv << ['Name', 'Price', 'Marked'] | |
gifts.each do |gift| | |
csv << [gift["name"], gift["price"], gift["marked"]] | |
# ["socks", 10, false] | |
end | |
end | |
end | |
user_action = "" | |
possible_actions = ["list", "add", "delete", "mark", "quit"] | |
gifts = load_csv | |
# [{}, {}, {}] | |
# 0 1 2 | |
until user_action == "quit" | |
# 2. Display menu (list / add / delete / quit) | |
puts "Which action [#{possible_actions.join(" | ")}]?" | |
# 3. Get user action | |
user_action = gets.chomp | |
# 4. Perform the right action | |
if user_action == "list" | |
# 5. iterate over the gifts array and puts out the name and price of the gift | |
puts "Currently you have the following on your Christmas Wishlist:" | |
print_wish_list(gifts) | |
elsif user_action == "add" | |
# puts "TODO: add feature" | |
# 6. ask the user for gift name (store that in a variable) | |
puts "Whats the name of the gift?" | |
user_choice = gets.chomp | |
# 7. ask the user for the price (store that in a variable) | |
puts "How much does it cost?" | |
user_price = gets.chomp.to_i | |
# 8. build the correct hash name => user_gift_name, price => user_gift_price | |
new_gift = {} | |
new_gift["name"] = user_choice | |
new_gift["price"] = user_price | |
new_gift["marked"] = false | |
# 9. push the hash into the gifts array | |
gifts << new_gift | |
save_csv(gifts) | |
# gifts << { "name" => user_choice, "price" => user_price} | |
elsif user_action == "delete" | |
# 10. ask the user which item to delete | |
puts "Which gift would you like to delete?" | |
# 11. print all gifts from the wishlist | |
print_wish_list(gifts) | |
# 12. store the index in variable | |
deleted_gift_index = gets.chomp.to_i | |
# 13. delete the gift at that index | |
gifts.delete_at(deleted_gift_index - 1) | |
save_csv(gifts) | |
elsif user_action == "mark" | |
# TODO: | |
# add a mark action to possible_actions array | |
# add a new branch to my if statement for the mark action | |
# ask the user which item to mark as bought | |
# store response as a variable (convert to integer and subtract 1) | |
# update the relevant gift => set marked to true | |
puts "Which index?" | |
print_wish_list(gifts) | |
marked_item = gets.chomp.to_i - 1 | |
gift = gifts[marked_item] | |
gift["marked"] = true | |
save_csv(gifts) | |
elsif !possible_actions.include?(user_action) | |
puts "Thats not a valid option!" | |
end | |
end | |
puts "Goodbye" | |
# TODO: | |
# add a etsy action to possible_actions array | |
# add a new branch to my if statement for the etsy action | |
# ask the user what he/she wants to look for | |
# scrape etsy for user_keyword | |
# display results to the user and ask which one should be kept (index) | |
# save to gifts array and save to csv | |
# hash = {} | |
# user_gift_name = "t-shirt" | |
# user_gift_price = 10 | |
# hash["name"] = user_gift_name | |
# hash["price"] = user_gift_price |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment