Story behind this project
I am an out and out movie buff, not only movies, shows too. Much of my days, leisure time and vacations are spersed by long and continuous hours of #NetflixAndChill or on my PC’s VLC player with downloaded media.
But with great power, comes great responsibility. With high-speed Internet connection and plenty of free time, comes the most inevitable choice of all times, what to watch and what not to. A movie or an entire season of a show gets downloaded within minutes; but you have to spend an entire two-hour period or much more in case of shows for that. And you find at the end of the running time that the entire show was a loss and this was one of the worst movies you have ever watched. Completely unforgivable offence, right?
But this is not a problem anymore. Enthusiasts made a site called Internet Movie Database, which houses the largest database of ratings for movies and shows yet in this whole universe. Voila! Now you can sort your list and just see those movies, which have been branded good by people. BUT.
Suppose, you just downloaded a film, say One Flew Over The Cuckoo’s Nest. You know nothing about it and want to check what people have said about it, the plot synopsis and the overall rating which it has received. I will just describe the entire procedure for it, for no reason.
- Open up Chrome — 1 click
- Go to Google (considering Google is already one of your frequently visited sites. Typing a ‘g’ or at most ‘goo’ will give you Google) — 3 strokes.
- Type ‘imdb’ in search bar. Press Enter — 5 strokes.
- The IMDb page comes up. To find ‘One Flew Over The Cuckoo’s Nest’, typing in ‘one fl’ does the job — 7 strokes, including one mouse click.
Wow. An 8.7 rating. Definitely a worthy piece. Bye bye, world!
I counted the number of keystrokes or clicks by the way. Wondering, why? So, I sum up the total number of strokes or clicks for finding out about the movie I set the example on and 16 strokes and clicks at least.
But I hover in and around IMDb almost all the time of the day, searching about the stuff and obviously, I spend thousands of keystrokes on the website everyday. So, what if I told you, you can do all this just by ONE RIGHT MOUSE CLICK AND ANOTHER LEFT MOUSE CLICK? 16 strokes reduced to 2. I take an average of 15 strokes per movie and say, you search about 10 movies on one of your very busy movie days. So, 150 strokes reduced to 20.
Project Details
You can find the code for this project at GitHub here and can fork or star it from the buttons above.
Packages and utilities used
- urllib2 - For automatically opening URLs.
- Webbrowser - A high-level interface to allow displaying Web-based documents to users.
- OMDb API - A RESTful web service to obtain content information on movies or TV shows.
Working of the script
The concept underlying is that you cannot run a Python script upon a file. To do that you have to have a .cmd or a .bat file, which can be set up in one of the options for Send To when you right click a folder.
The .cmd file creation is mentioned for Windows systems only.
- Go to any folder and type shell:sendto in the address bar of the folder. The ways with which you can do a Send To > is listed here, like Desktop and Mail Recepient to name a few.
- Create a new text file.
Type this in it and save it as runner.cmd.
@echo off
cls
py <Address of Python File> %*
pause
The lines mentioned above does the following:
@echo off - turns off the command-line echoing.
cls - clears screen of terminal.
py file_path_name %* - The path of the Python file (more about it below) should be inserted here to be called when this cmd file is being run.
pause - Pauses the command line screen from disappearing within moments.
Now create a Python file anywhere in your computer, the path for which you have to insert in the .cmd file above.
The code opens with the import statements to import all the packages which I have used for this.
import sys
import urllib2
import webbrowser as w
import re
import os
When the folder is right clicked, sys.argv takes up the complete path name of the folder and the basename class of the os package outputs out only the current folder name.
name=sys.argv[1]
name=os.path.basename(name)
OMDbapi is a RESTful API which stores info of the entire database of IMDb films along with the details in separate JSON files. And its search criteria is replace the spaces in the name of the film by ‘%20’.
name=name.replace(" ","%20")
The URL string (notice the pattern in the code below) is stored in a variable. And that variable is used to collect the info from the OMDb API which is then stored in a variable web. web is a variable of bytes type and encoded to a string type.
url="http://www.omdbapi.com/?t="+name
r=urllib.urlopen(url)
web=r.read()
web=web.decode('UTF-8')
IMDb is searched via an imdbID, which is found in the JSON file given by OMDb. All the IDs of IMDb start with ‘tt’, like ‘tt0877890’ is an arbitrary one. This statement finds every occurence of ‘tt’ along with at least one occurence of a digit from web. This gives me an array with the film ID, for which I am searching.
The URL is adjusted as per condition and along with the rules of the IMDb search URL.
m=re.findall(r'tt[0-9]+',web)
url='http://www.imdb.com/title/'+m[0]+'/'
The URL is pushed to the default browser to be opened automatically.
w.open_new_tab(url)
Below is an embedded video for this above program, which shows the working of IMDb Automation.