Last active
August 5, 2022 02:58
-
-
Save vhsalazar/deed927b66957c41c204729906d3aa83 to your computer and use it in GitHub Desktop.
flights.rb
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
require 'date' | |
require 'active_support/core_ext/object/to_query' | |
require "active_support/core_ext/numeric/time" | |
require 'launchy' | |
flights = [ | |
{from: 'SYD', to: 'LAX', date: Date.new(2022,12,9), delta: (0..2)}, | |
{from: 'LAX', to: 'NAN', date: Date.new(2023,02,2), delta: (0..2)}, | |
{from: 'NAN', to: 'SYD', date: Date.new(2023,02,7), delta: (0..2)} | |
] | |
def skyscanner_url(flights) | |
base_url = "https://www.skyscanner.net/transport/d/%{multi}?adults=1&adultsv2=1&cabinclass=economy&children=0&childrenv2=&infants=0&ref=home" | |
#syd/2022-12-08/mdea/mdea/2023-02-02/lax/lax/2023-02-10/syd/ | |
multi = flights.map do |flight| | |
"#{flight[:from]}/#{flight[:date].strftime("%Y-%m-%d")}/#{flight[:to]}" | |
end.join('/') | |
url = base_url % { multi: } | |
end | |
def build_flights(flights) | |
flights.map do |flight| | |
flight[:delta].map do |delta| | |
flight.clone.tap do |f| | |
f[:date] += delta.day | |
end | |
end | |
end | |
end | |
flights_routes = build_flights(flights) | |
result = flights_routes.shift | |
flights_routes.each do |flights| | |
result = result.product(flights) | |
end | |
puts "Flights: #{result.count}" | |
result.each_with_index do |route, index| | |
puts index | |
url = skyscanner_url(route.flatten) | |
Launchy.open url | |
sleep(15) | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://www.youtube.com/watch?v=M2MHMxvydbA