3 Reasons to invest in real estate in the Region of Waterloo

1. Kitchener-Waterloo ranks as a highly livable city! The Waterloo region has been ranked as one of the best places in Ontario to live, work and study. — 2. With so many tech companies, it is a future forward city. — 3. It's a great choice for investing in real estate!

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




The Euro Trip Planner

When my friend came to visit me this holidays, I was secretly relieved. You know how much it sucks to be away from your family during the holidays?! During our conversations, we realized that we could plan for a nice, short EuroTrip in January. We looked for different countries, airports, flights but in the end, the prices were as usual — terribly expensive. Congratulations! we just wasted our 1 hour for nothing. Then it struck me, why not write a program to do all the dirty work for us and we find the ideal Eurotrip (and some other trips as well) quickly? After all, the steps are the same most of the times. It will be the perfect opportunity to learn something new and make plans for future trips a lot easier! And so I did!

First, let me explain what I had in mind. Consider that we are planning for a nice, 7 day, multi-city vacation in Spain. There are a few airports near me from which it is feasible to take off and return back to, for example, Berlin Tegel and Berlin Schoenefeld. Let’s call them “source” airports. We have a bunch of places (or “destinations”) in Spain on our bucket list: Seville, Madrid, Barcelona, and Valencia. Now, I prefer to use buses and trains when travelling to close cities like these. In this way, you can enjoy the country and it is often the cheaper, environment-friendly and convenient way. But this means that we can start our trip in one destination and end it in another destination. And as cheap airlines like Ryanair and Easyjet have no discount for return flights, we can go for multi-city flights! For example, I can go from Berlin → Barcelona and come back from Madrid → Berlin. Now let’s talk about the dates of the travel. Generally, I have a range of days ( say 1st to 15th Jan), of which I can only spare 7 days for the trip. Which means I can fly in 1 to 7 Jan and come back on 7 to 15 Jan, depending on the takeoff and arrival days. The goal here is to find the cheapest flight combination which satisfies our trip conditions.

To begin, I created a simple search for flights going from Berlin ( BERL-sky) to London ( LON-sky) on 22 Jan using requests library in Python. (Note that header contains custom API key which you get when signing up with RapidAPI)

Now I am making a “GET” HTTP request to the specific URL endpoint mentioned in myurl variable. We will get a JSON response.

Now I am making a GET HTTP request to the specific URL endpoint mentioned in myurl variable. We will get a JSON response.

{"Quotes":[{"QuoteId":1,"MinPrice":18.0,"Direct":true,"OutboundLeg":{"CarrierIds":[1090],"OriginId":82398,"DestinationId":82582,"DepartureDate":"2020-01-22T00:00:00"},"QuoteDateTime":"2019-12-27T11:03:00"}],"Places":[{"PlaceId":66270,"IataCode":"LTN","Name":"London Luton","Type":"Station","SkyscannerCode":"LTN","CityName":"London","CityId":"LOND","CountryName":"United Kingdom"},{"PlaceId":81678,"IataCode":"SEN","Name":"London Southend","Type":"Station","SkyscannerCode":"SEN","CityName":"London","CityId":"LOND","CountryName":"United Kingdom"},{"PlaceId":82398,"IataCode":"STN","Name":"London Stansted","Type":"Station","SkyscannerCode":"STN","CityName":"London","CityId":"LOND","CountryName":"United Kingdom"},{"PlaceId":82582,"IataCode":"SXF","Name":"Berlin Schoenefeld","Type":"Station","SkyscannerCode":"SXF","CityName":"Berlin","CityId":"BERL","CountryName":"Germany"}],"Carriers":[{"CarrierId":50441,"Name":"easyJet"},{"CarrierId":1090,"Name":"Ryanair"}],"Currencies":[{"Code":"EUR","Symbol":"€","ThousandsSeparator":".","DecimalSeparator":",","SymbolOnLeft":false,"SpaceBetweenAmountAndSymbol":true,"RoundingCoefficient":0,"DecimalDigits":2}]}

Cool! We got our first result. But there is a lot to unpack about what just happened! First, let’s have a look at what we requested. You see the last field in the myurl? That is the date we intend to fly. Then we have origin and destination, which is specified as the Skyscanner's location format. Now Berlin has 2 airports: Tegel and Schoenefeld, but in the API, we can just use BERL-sky which will give results from both! (Similarly, London has 4!) This location is available through another endpoint where you can simply query for a location string and it will return a JSON of possible Skyscanner locations.

originCountry is the place where we are doing the search from. A little side note: changing this can drastically change the prices!! I found the German prices to be the cheapest. However, changing the currency didn’t affect the prices.

Let’s parse and pretty-print the response JSON so we can clearly understand is the information that we are getting back.

That is so much information!! First, we have "Quotes". This gives us the cheapest quote for that day and combination. We have exact airlines, origin and destination mentioned in form of IDs, which have to be resolved by looking at the subsequent fields in Carriers and Places. For example "Places": is the json list from where the flights are possible on that day.

But did you notice that we do not receive any kind of time? Look at the following:

Yes! This is what they call Browsing for the flights. Once we are more sure about the search, we have to get more information by asking for a different request. That will give us more details and also the exact URL of where to book this selection. Then the URL directs to booking website and our job is over.

For now, let’s can focus on collecting the data. The simplest way I could imagine is:

Let’s continue and create 2 arrays for origin and destination. The best thing about the API is that you can have whole countries as a place! So we can use IT-sky for all the airports in Italy and ES-sky as all the airports in Spain and so on!!! But if we do that, we include all the possible airports in the country. Which means for Spain, it selects Palma island, which we don't want to go (at least for now!). Let's just use all the airports that we mentioned earlier.

Now we loop through all possible options in the array and request the results for each pair. We are still looking for one way result for 22 of January.

But we can already start making our algorithm smarter. Every time, we get a list of airports which we then have to cross-reference with the Places field. Let's create a simple python dictionary which will save all the airports and their IDs. As python dictionaries are hashmaps, we will get the result in O(n) complexity. To top it off, let's add print statements which print the exact airports and price instead of the whole response JSON.

We get following result back:

Interesting! We have 5 flight options and we already see that flying to Madrid will be the cheapest! Now, let’s say we want to fly on some date in 18th Jan — 24 th Jan. We will have to add another for loop which loops through all possible dates. And to ignore expensive flights, let us add a maxbudget variable which sets our one way budget to 40 €.

I want to create a class so we can create a neat system. I know this would be overkill! But bear with me, it might be useful later!

To analyze the performance of the code, I want to see which parts the program is spending it’s most time. This is known as benchmarking.

And we get back the following:

Wow, this means our program is pretty quick. But the API is a bit slow. Hmm.. What if somehow we can make multiple requests at the same time?…

Add a comment

Related posts:

Human Resources Recruiter Job Overview

In order to gain a competitive advantage and fuel a company’s growth, there is a need for qualified and highly skilled professionals. To recruit such talent pool, HR recruiter plays the most…

Benefits of Mobile Access to Medical Practice Management Software

Use of mobile devices and mobile apps has been growing at an exponential rate in almost any industry. Healthcare professionals and their assistants are also making extensive use of the mobile version…

MEME Marketing

We have all grown up seeing the old classy advertisements, a problem is shown in the ad, the person cries about it and a someone miraculously suggests a product that is heavenly. Then we saw some ads…