Skip to content

Instantly share code, notes, and snippets.

@chaserx
Created September 5, 2014 03:45
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 chaserx/41d944da1990a66d154a to your computer and use it in GitHub Desktop.
Save chaserx/41d944da1990a66d154a to your computer and use it in GitHub Desktop.
table to array of hashes example
#!/usr/bin/env ruby -wKU
# gems required:
# - nokogiri
# - awesome_print
# - open-uri-cached
require 'nokogiri'
require 'open-uri'
require 'awesome_print'
require 'open-uri/cached'
LINKS = [
'http://sports.directv.com/Packages/NFLSundayTicket/2014_9.htm',
'http://sports.directv.com/Packages/NFLSundayTicket/2014_10.htm',
'http://sports.directv.com/Packages/NFLSundayTicket/2014_11.htm',
'http://sports.directv.com/Packages/NFLSundayTicket/2014_12.htm'
].freeze
@games = []
@teams = ['Packers']
@teams.each do |team|
LINKS.each do |link|
doc = Nokogiri::HTML(open(link))
(1..31).each do |day|
@date = doc.xpath("/html/body[@id='sportsBody']/div[@id='insideMid']/div[@id='dayTables']/h3[@id='day#{day}']")
unless @date.empty?
rows = doc.xpath("/html/body[@id='sportsBody']/div[@id='insideMid']/div[@id='dayTables']/h3[@id='day#{day}']/following-sibling::table[1]/tbody[1]/tr")
@details = rows.collect do |row|
detail = {}
[
[:game, 'td[1]/text()'],
[:time, 'td[2]/text()'],
[:sd_channel, 'td[3]/text()'],
[:hd_channel, 'td[4]/text()'],
].each do |name, xpath|
detail[name] = row.at_xpath(xpath).to_s.strip
end
detail[:date] = @date.text
detail
end
@details.keep_if{|x| x[:game].include? team }
@games << @details
end
end
end
ap @games.flatten!
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment