Skip to content

Instantly share code, notes, and snippets.

@prwteas
Last active December 14, 2015 21:39
Show Gist options
  • Save prwteas/5152804 to your computer and use it in GitHub Desktop.
Save prwteas/5152804 to your computer and use it in GitHub Desktop.
Script to download images from www dot buttonlovers dot com
class Button
require 'mechanize'
def get_all_categories
a= Mechanize.new
linkdet=[]
page = a.get("https://www.buttonlovers.com/storefrontCommerce/categorybrowse.do?category_name=Buttons&path=Imported%2f%2f%2f%2fSlimline%E2%84%A2&currentPage=1&numResults=48")
cats = page.links_with(:href=>/.*subcategory.*numResults/)
cats.each do |cat|
quantity = cat.text.gsub(",","").scan(/\d+/).first
name = cat.text.scan(/\w+.?\w+/).first
href = cat.href
linkdet << Hash[:name=>name,:quantity=>quantity,:href=>href]
end
linkdet
end
def get_them_all
categories = self.get_all_categories
categories.map do |cate|
puts "\rGetting #{cate[:name]}"
link = cate[:href].gsub("numResults=48","numResults=#{cate[:quantity]}")
self.get_all_buttons("https://www.buttonlovers.com/storefrontCommerce/"+link,cate[:name])
end
end
def get_all_buttons(url,dirname)
$stdout.sync=true
a=Mechanize.new
page = a.get(url)
(1...10).each do |k|
print "\rWe are waiting for #{k} secs before the page is prepared"
sleep(1)
end
products = a.get(url).links_with(:text=>/Product#/)
puts "\rThere are #{products.count} pics in total"
FileUtils.mkdir_p(dirname)
pr_count = products.count
products.each do |i|
at = i.attributes['onclick'].scan(/\d+/)
item_id = at.first
item_custom_id = at.last
img_text = i.attributes.text.partition(/\n\n/)
pr_name = img_text[0]
pr_id = img_text[2].scan(/\d+/).first
a=Mechanize.new
page = a.get("https://www.buttonlovers.com/storefrontCommerce/itemDetail.do?itm_id=#{item_id}&cust_item=#{item_custom_id}")
imageurl = page.search("div#browse-details-middle").css("img").first.attributes['src'].value.partition("&")[0]
unless File.exists?("#{dirname}/#{pr_id}.jpg")
download = a.get("http://www.buttonlovers.com#{imageurl}").save("#{dirname}/#{pr_id}.jpg")
end
unless File.exists?("#{dirname}/#{pr_id}.html")
download_text = File.open("#{dirname}/#{pr_id}.html","w"){|file| file.write(page.search("div#browse-details-left").to_html+page.search("div#product-aside").to_html);file.close}
end
print "\rThere are just #{pr_count = pr_count - 1} to go"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment