Skip to content

Instantly share code, notes, and snippets.

@jrmehle
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jrmehle/b306bb6c743edc58cc4e to your computer and use it in GitHub Desktop.
Save jrmehle/b306bb6c743edc58cc4e to your computer and use it in GitHub Desktop.
A script to quickly see if any bikes I'm interested in are in stock on my favorite site to buy bikes from.
require 'nokogiri'
require 'open-uri'
require 'colorize'
class BikeScraper
BIKES_DIRECT_URL = 'http://www.bikesdirect.com/products/cross_bikes.htm'
TARGET_BIKE_SIZE_REGEX = /56cm/
TARGET_BIKE_NAME = 'Fantom'
def execute
bikes_index_page = open_page(BIKES_DIRECT_URL)
target_bikes = bikes_index_page.css("a:contains(\"#{TARGET_BIKE_NAME}\")")
bike_urls = target_bikes.collect { |node| { title: sanitize_title(node.css('strong').text), url: node.attributes['href'].value } }.uniq
bike_urls.each do |link|
bike_page = open_page(link[:url])
puts
puts "### #{link[:title]} ###"
puts link[:url]
puts bike_sizes(bike_page)
end
end
private
def bike_sizes(bike_page)
bike_page.css('select').collect do |bike_size_selector|
bike_sizes = bike_size_selector.children.select { |node| node['value'] if node['value'] =~ /\d+cm/ }
next if bike_sizes.empty?
bike_sizes.collect { |s| s.attributes['value'].value =~ TARGET_BIKE_SIZE_REGEX ? s.attributes['value'].value.red : s.attributes['value'].value }
end.compact.join("\t\n")
end
def sanitize_title(title)
title.gsub(/NEW|CLEARANCE|SALE|SaleSave Thousands|,* Save Up To 60\%|Save Over \d+\%|Sale2014,/, '')
.gsub('PROFINAL', 'PRO')
.gsub('UNOOnly', 'UNO')
.strip
end
def open_page(url)
Nokogiri::HTML(open(url))
end
end
BikeScraper.new.execute
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment