Flight Finder!

Imagine you plan to travel to a different country but want to find a flight that fits into your budget! Isn’t it kind of annoying to keep visiting flight site’s daily to know if you can find a fare that is light on your pocket? Well, this is an attempt to automate this process via Splinter (a wrapper around Selenium), Python and Slack – for sending you updates.

The bot sends you an update every half an hour (can be adjusted) so that you never miss an opportunity to book a budget airline! Here’s the code.

To install:

  • Clone this repo.
  • Run pip3 install -r requirements.txt
  • Run python3 main.py

Sample Slack Message:

Slack_Message_Example

Apartment-Bot

It’s a painful task to find an apartment that fits into your budget especially if you are living in San Francisco Bay Area! Wouldn’t it be nice to automate this process and post you the details via Slack whenever a new post/ad is posted by owner?

Well, inspired by this article, I have written a simpler version that gets the job done and keeps one updated with the newest post and never miss them! The code uses the libraries Python-Craigslist and Python-SlackClient.

The Slack-Message is as shown:

Slack-Message

Hope you enjoy the code! Go through the article to have a better understanding of the workings of the code.

XKCD!

Automate the boring stuff!

Isn’t it a manual task to visit a website every day to view your favorite comic strip? Why not let a script do it for you? And maybe send it as an email attachment at a specified time every day of the week! Well, with the help of Python book, ‘Automate the Boring Stuff’ let’s create our small script to download and send that as an email attachment.

Details

I have created download_xkcd.py which scrapes https://xkcd.com for a comic and downloads it in a local folder. The email_xkcd.py contains code to send the downloaded comic as an email attachment. But you don’t want to receive a duplicate comic if there is no new comic’s, right!? So, for this, I keep a record of the previous comic in a text file called comic_records.txt which I will read before sending out an email.

Cron Job

I followed Ole Michelsen’s article to create a Cron Job, which is straight forward in MacOS.

To run your python script every day of the week at 6 PM, you can do something like this:

  0 18 * * * cd /folder/where/your/script/resides/ && python 

Note: If you are using Gmail to send an email then you will need to allow less secure apps to access Gmail! The first time you try to access Gmail using Python, your access will not be granted and you will receive an email from Google saying, Review blocked sign-in attempt, click allowing access to less secure apps link and select Turn on and there you go, you can access your gmail using Python!

Youtube bot using Python!

Wouldn’t it be nice to get a video list along with the number of views and link to that video so that you need not visit each and every video of the conference you missed? Well, I have attempted to create such a script inspired by Miguel’s article.

I have used Python’s `requests`, `BeautifulSoup` and `multiprocessing` libraries to make this work.

The idea is simple, select a Python conference from `pyvideo.org` using `requests` module. Parse the request using `BeautifulSoup` and get all the video links of that conference. Visit each and every youtube link and collect stats like, ‘Title’, ‘URL’, and ‘Number of views’ and present it to the user.

The code can be found here. Hope you enjoy it!

API Testing

What is an API?

Well, an API stands for Application Programming Interface. It is basically the functions exposed by the software developers which makes it easy for other developers/business entities to use them in their products. Got it?

Ok, lets break it down. I believe many of you have used the travelling sites like Expedia, MakeMyTrip etc. Have you ever wondered how these sites know about the flight details, its schedules and seat availabilities? Well, the companies that operates flights expose their apis and travelling sites makes use to integrate those into their sites, thus making availablility of the data. Ok I think this gives you a hint of what an API is? Lets leave it as a further reading for you and jump straight into using scripts.

Python is an elegant language with many supporting libraries and here we will be using ‘requests’ module, which basically handles most of the heavy underlying work and gives the users a simple syntax like requests.get, requests.post.

Ok lets dive into the code, we will use Google’s map api here:

First lets try to use the below link directly into your browser:

http://maps.googleapis.com/maps/api/geocode/json?sensor=false&address=San%20Jose

and you will get a json reply as below:

{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "San Jose",
               "short_name" : "San Jose",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Santa Clara County",
               "short_name" : "Santa Clara County",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "California",
               "short_name" : "CA",
               "types" : [ "administrative_area_level_1", "political" ]
            },
truncated....

Hope you got an idea of what we will be doing, lets start the code. But first install the requests module using:

pip install requests

Now lets code:

import requests, pprint

google_api = 'https://maps.googleapis.com/maps/api/geocode/json?'

address = input('Enter the location to search for: ')

extra = {'sensor':'false', 'address':address}

r = requests.get(google_api, params=extra)

pprint.pprint(r.json())

In the above code, pprint is for pretty printing, it will print the json response in a indent format. The below will be the response:

google_requests

Yay!  you have sent your first API request from Python. Awesome! Now lets try to test whether we got the results which we are looking for. In the above example we searched for ‘India’, and the first thing we have to make sure is the status code, whether we have successfully received the query, if it is then 200 should be returned.

In below code we will search for ‘India’ first and check for few fields of json and redo the search with a different name, say ‘San Jose’:

import requests, pprint

google_api = 'https://maps.googleapis.com/maps/api/geocode/json?'

address = input('Enter the location to search for: ')

extra = {'sensor':'false', 'address':address}

r = requests.get(google_api, params=extra)

pprint.pprint(r.json())

#print(r.json()['results'][0]['types'][0])
#print(r.json()['results'][0]['address_components'][0]['long_name'])

if r.status_code == 200:
 print("Status Code == 200")
else:
 print("Failed")
 
if r.json()['results'][0]['types'][0] == 'country':
 print("Search for 'India' results in type == country")
else:
 print("Search is not a Country")
 
if r.json()['results'][0]['address_components'][0]['long_name'] == 'India':
 print("Long name field has name of India")
else:
 print("Long name field does not have name == India")

India_google

Now search for 'San Jose' will result in:

san_jose_google

Hope you enjoy!