Skip to content

Instantly share code, notes, and snippets.

@kturney
Last active December 21, 2015 14:59
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 kturney/6323837 to your computer and use it in GitHub Desktop.
Save kturney/6323837 to your computer and use it in GitHub Desktop.
A Ruby script to help make a list of all the beers at Untapped Fest Dallas 2013
require 'nokogiri'
require 'open-uri'
doc = Nokogiri::HTML(open('http://dallasbrewscene.com/brewlist.html'))
#File.open("/home/kyle/Downloads/untapped.html", "w") { |io| io.write(doc) }
#breweries = doc.css('div#accordion h4').map { |e| e.text }
#beers = doc.css('div#accordion p').map { |e| e.text.split(', ') }
#tapList = Hash[breweries.zip(beers)]
breweries = []
beers = []
cideries = []
ciders = []
atCiders = false
# Iterate through so that ciders can be separated
doc.at('//div[@id=\'accordion\']').search('//h4 | //p | //h2[text() = \'Ciders\']').each { |node|
case node.name
when 'h4'
if atCiders
cideries.push node.text
else
breweries.push node.text
end
when 'p'
if atCiders
ciders.push node.text.split(', ')
else
beers.push node.text.split(', ')
end
when 'h2'
atCiders = true
else
puts 'should never get here'
end
}
tapList = Hash[breweries.zip(beers)]
ciderList = Hash[cideries.zip(ciders)]
builder = Nokogiri::HTML::Builder.new { |doc|
doc.html {
doc.head {
doc.meta charset: 'UTF-8'
}
doc.body {
doc.div "Brewery Count: #{breweries.size}"
doc.div "Beer Count: #{beers.map{ |e| e.size }.reduce(:+)}"
doc.div "Cidery Count: #{cideries.size}"
doc.div "Cider Count: #{ciders.map{ |e| e.size }.reduce(:+)}"
# Use spaces instead of a list because it works better when pasting into Google Doc
doc.h2 'Beers'
tapList.sort.each { |brewery, beer|
doc.b brewery
beer.each { |e| doc.div "    #{e}" }
}
doc.h2 'Ciders'
ciderList.sort.each { |cidery, cider|
doc.b cidery
cider.each { |e| doc.div "    #{e}" }
}
}
}
}
# Use gsub! to fix Nokogiri/libxml2's auto escaping of '&'
File.open('taplist.html', 'w') { |file| file.write(builder.to_html.gsub!(' ', ' ')) }
puts 'DONE!'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment