Skip to content

Instantly share code, notes, and snippets.

@cadwallion
Created May 15, 2012 18:50
Show Gist options
  • Save cadwallion/2704135 to your computer and use it in GitHub Desktop.
Save cadwallion/2704135 to your computer and use it in GitHub Desktop.
Ruck beer list scraper
#!/usr/bin/env ruby
# A Thor-based commandline tool for all BeerMenu.
# Based on @jsteiner's Ruby equivalent of @jlet's Python Ruck
# beer list scraper
# @jsteiner's Original: https://gist.github.com/2703889
# @jlet's Original: ttps://gist.github.com/2659721
require 'open-uri'
require 'nokogiri'
require 'thor'
class BeerMenu < Thor
ATTRIBUTES = %w(name ounces serving abv price efficiency)
desc 'list [PLACE]', 'list of beer info for PLACE'
def list place
doc = Nokogiri::HTML open "http://www.beermenus.com/places/#{place}"
beers = []
order = ATTRIBUTES.include?(ARGV[1]) ? ARGV[1] : 'name'
doc.css('table.beermenu tbody tr').each do |tr|
beer = {}
serving_info = /(\d+)\s*oz\.?\s+(\w+)/.match(tr.css(".serving").text)
beer[:name] = tr.css("td:first-child a").text
beer[:ounces], beer[:serving] = serving_info[1].to_i, serving_info[2]
beer[:abv] = (tr.css('.abv').text + '0').to_f
beer[:price] = (tr.css('.price').text.sub('$', '')).to_f
beer[:efficiency] = (beer[:ounces] * beer[:abv]) / beer[:price]
beers << beer
end
beers.sort_by { |beer| beer[order.to_sym] }.each do |beer|
puts "%-45s %2d-oz %-6s %4.1f%% ABV, $%-5.2f (Eff'cy: %5.2f)" %
[
beer[:name], beer[:ounces], beer[:serving],
beer[:abv], beer[:price], beer[:efficiency]
]
end
end
desc 'search LOCATION', 'search for location on beermenus.com and return ID'
def search place
doc = Nokogiri::HTML open "http://www.beermenus.com/search?q=#{URI.escape(place)}"
locations = []
doc.css('#search a').each do |a|
id = a.attr('href').match /^\/places\/(.+)/
next unless id
locations << {
name: a.inner_text,
id: id[1]
}
end
puts 'Search Results:'
locations.each do |place|
puts "#{place[:id]} - #{place[:name]}"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment