Created
April 9, 2011 21:28
-
-
Save 7even/911790 to your computer and use it in GitHub Desktop.
football match scores parser
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby -wKU | |
# encoding: utf-8 | |
class FootballParser | |
require 'rubygems' | |
require 'nokogiri' | |
require 'open-uri' | |
URL = 'http://www.liveresult.ru/' | |
ITEM_SELECTOR = '#pane_g_172_football_actual .a .item, #pane_g_172_football_actual .b .item' | |
ICON_SELECTOR = 'img.stico' | |
MONTHES = { | |
1 => 'января', | |
2 => 'февраля', | |
3 => 'марта', | |
4 => 'апреля', | |
5 => 'мая', | |
6 => 'июня', | |
7 => 'июля', | |
8 => 'августа', | |
9 => 'сентября', | |
10 => 'октября', | |
11 => 'ноября', | |
12 => 'декабря' | |
} | |
STATE_ICONS = { | |
'st_ns.gif' => '-', # match didn't start yet | |
'st_min.gif' => '=', # match is in progress | |
'st_ft.gif' => '+' # match is over | |
} | |
Match = Struct.new(:state, :time, :title) | |
def initialize | |
doc = Nokogiri::HTML(open(URL)) | |
@dates, current_date = {}, Time.now | |
doc.css(ITEM_SELECTOR).each do |item| | |
if item['class'] =~ /date/ | |
date_ru = item.content | |
match = /(\d{1,2}) ([а-я]+) (\d{4})/.match(date_ru) | |
current_date = Time.gm(match[3], MONTHES.invert[match[2]], match[1]) | |
else | |
state = item.at_css(ICON_SELECTOR)[:src].split('/').last | |
(@dates[current_date] ||= []) << Match.new( | |
STATE_ICONS[state], | |
item.at_css('.date').content, | |
item.at_css('.title a').content | |
) | |
end | |
end | |
yield self if block_given? | |
end | |
def each_date | |
@dates.keys.sort.each do |date| | |
date.instance_variable_set('@matches', @dates[date]) | |
def date.to_s | |
strftime('%d.%m.%Y') | |
end | |
def date.each_match | |
@matches.each do |match| | |
yield match | |
end | |
end | |
yield date | |
end | |
end | |
end | |
FootballParser.new do |parser| | |
parser.each_date do |date| | |
puts | |
puts date | |
puts '*' * 50 | |
date.each_match do |match| | |
puts "#{match.state} #{match.time} #{match.title}" | |
end | |
end | |
puts | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment