Skip to content

Instantly share code, notes, and snippets.

@michalkozminski
Created November 6, 2014 21:52
Show Gist options
  • Save michalkozminski/9a8b7018bb8d29af19a9 to your computer and use it in GitHub Desktop.
Save michalkozminski/9a8b7018bb8d29af19a9 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'nokogiri'
require 'open-uri'
require 'pry'
require 'date'
require 'selenium-webdriver'
require 'terminal-table'
require 'headless'
def convert_to_dates(date)
month = date.split(' ')[0]
year = date.split(' ')[1]
case month
when 'Grudzień'
month = 12
when 'Styczeń'
month = 1
when 'Luty'
month = 2
when 'Marzec'
month = 3
when 'Kwiecień'
month = 4
when 'Maj'
month = 5
when 'Czerwiec'
month = 6
when 'Lipiec'
month = 7
when 'Sierpień'
month = 8
when 'Wrzesień'
month = 9
else
raise "Month: #{month}"
end
return {
month: month,
year: year.to_i
}
end
def parse_result(result)
flight = {
departs: [],
arives: [],
price: result['price'],
to: result['to'],
link: result['link']
}
result['departs'].each do |index, elem|
month_and_year = convert_to_dates(index)
elem.each { |x| flight[:departs].push(Date.new(month_and_year[:year], month_and_year[:month], x)) }
end
result['arives'].each do |index, elem|
month_and_year = convert_to_dates(index)
elem.each { |x| flight[:arives].push(Date.new(month_and_year[:year], month_and_year[:month], x)) }
end
return flight
end
def get_list_of_departs(link)
driver = Selenium::WebDriver.for :firefox
driver.navigate.to link
wait = Selenium::WebDriver::Wait.new(:timeout => 10)
wait.until { driver.find_element(:class, 'calendarbox') }
flights = driver.execute_script <<"EOF"
data = {
departs: {},
arives: {}
}
data['from'] = $($('.calendarbox > .fleft:not(.paddingL3)').find('.p05.ng-binding')[0]).text();
data['to'] = $($('.calendarbox > .fleft:not(.paddingL3)').find('.p05.ng-binding')[1]).text();
data['price'] = parseFloat($('.flightinfo > .price').text());
$('.marginR20').find('.calendarbox.p1').each(function(_, item){
month = $(item).find('.month').text();
data['departs'][month] = []
$(item).find('.available > a').each(function(_, a){ data['departs'][month].push(parseFloat($(a).text()))})
});
$('.grid_315:not(.marginR20)').find('.calendarbox.p1').each(function(_, item){
month = $(item).find('.month').text();
data['arives'][month] = []
$(item).find('.available > a').each(function(_, a){ data['arives'][month].push(parseFloat($(a).text()))})
});
return data
EOF
driver.quit
flights['link'] = link
return flights
end
calendars = Nokogiri::HTML(open("http://flipo.pl/kalendarze"))
flights_from = Date.parse(ARGV[0])
flights_to = Date.parse(ARGV[1])
flights = calendars.css('a.btn').map do |link|
parse_result(get_list_of_departs link['href'])
end
rows = [['Do', 'Cena', 'Link']]
flights.each do |flight|
if(flight[:departs].map {|x| x >= flights_from && x <= flights_to}.reduce{|sum, x| sum || x} &&
flight[:arives].map {|x| x <= flights_to && x >= flights_from}.reduce{|sum, x| sum || x})
rows << [ flight[:to], flight[:price], flight[:link] ]
end
end
table = Terminal::Table.new rows: rows
puts table
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment