Filter your video collection with Python and IMDbPY

Posted August 23rd, 2010 in Python by Florentin

If you have a large collection of movies (documentaries, dvd rips, etc) and you would like to decide on what worth watching next, this script may come in handy.
It’s a small Python script which scans your movie collection, collects data about each one from imdb.com and allows you to filter the movies by genres, actors or publishing studio.

The movies are collected from subdirectories (of indefinite depth) of a single parent directory which is either the default path (defined in the DEFAULT_MOVIES_PATH variable inside the script) or specified by the “-p” (or –path) parameter.
movies.py is looking for directories which contain at least one movie file (a file having the extension .avi, .mkv or .mpeg) The name of the movie is extracted from the directory name, not the file name.

Examples:
├── temp
│   ├── Capitalism.A.Love.Story
│   │   └── cd1
│   │   └── movie.avi
│   ├── Home.tt1014762
│   │   ├── imdb.html
│   │   ├── imdb.info
│   │   └── movie.dvdrip.avi
│   ├── Religuous.tt0815241
│   │   ├── imdb.html
│   │   ├── imdb.info
│   │   └── moviefile.avi
│   ├── stuff
│   │   └── nothinghere.txt
│   └── Tapped.tt1344784
│   ├── file.avi
│   ├── imdb.html
│   └── imdb.info

movies.py -p temp will identify the following directories as movies: “Home.tt1014762″, “Religuous.tt0815241″, Tapped.tt1344784 and “Cd1″
“Cd1″ is only a subdirectory but is seen as a movie because there is one movie file inside it.
You will have to move the movie.avi file from inside “Cd1″ to it’s parent, so that “Capitalism.A.Love.Story” will be recognized as a movie name.
If your movie is not properly identified on imdb, you may rename the directory and add the imdb movie id to it, e.g., “Capitalism.A.Love.Story.tt1232207″ or “Capitalism.A.Love.Story.1232207″

In order to use movies.py, you will need a couple of things:
1. Python 2.6 on a Linux machine (I have Ubuntu 10.04)
2. imdbpy library available
sudo pip install imdbpy
OR
sudo easy_install imdbpy
3. prettytable library
sudo pip install prettytable
OR
sudo easy_install prettytable

Download the script on your machine and execute:
# A) this will make your script executable
chmod +x yourusername
# B1) use an alias to run the ‘movies’ script from anywhere
alias movies=”/your/path/to/movies.py”
# B2) alternatively, you can make use of symbolic links
sudo ln -s /your/path/to/movies.py /usr/local/bin/movies.py

Examples of how to use the script, in case you have put movies.py somewhere in your PATH (method B2)

scan and refresh movies from inside the path “/home/elmo/Movies”. In case you don’t use “-p”, the default path is stored in the script variable DEFAULT_MOVIES_PATH (defined inside the script code)
movies.py -p “/home/elmo/Movies” -s -r

show movies with “Andy Serkis” but not with “Brad Pitt”
movies.py -l -a “+andy serkis,-brad pitt”

movies with at least one of these actors playing
movies.py -l -a “?andy serkis,?angelina jolie”

movies with at least one favorite actor playing
movies.py -l -o actor

comedy movies but no dramas
movies.py -l -g “+comedy,-drama”

movies made by “lionsgate” production company
movies.py -c “lionsgate”

movies made by any of the favorite production companies
movies.py -o company

There are a couple of ‘predefined’ filters.
For example, in the script’s code you will see a large list of (good) actors. If you want to find movies where at least one favorite actor is playing, run:
movies.py -o actor
The same is true for production companies
movies.py -o company

Example of output:
movies.py -l -g “+documentary,-drama”
+———+———–+——————–+——+———————+
| Imdb ID | Movie | Genres | Rate | Basename |
+———+———–+——————–+——+———————+
| 1014762 | Home | documentary | 8.4 | Home.tt1014762 |
| 0815241 | Religuous | documentary,comedy | 7.8 | Religuous.tt0815241 |
| 1344784 | Tapped | documentary | 6.8 | Tapped.tt1344784 |
+———+———–+——————–+——+———————+

View the whole list of options using:
movies.py -h
OR
movies.py –help

I hope you will enjoy the script and stay tuned for updates :)

Download here