Networking

Unix and Linux network configuration. Multiple network interfaces. Bridged NICs. High-availability network configurations.

Applications

Reviews of latest Unix and Linux software. Helpful tips for application support admins. Automating application support.

Data

Disk partitioning, filesystems, directories, and files. Volume management, logical volumes, HA filesystems. Backups and disaster recovery.

Monitoring

Distributed server monitoring. Server performance and capacity planning. Monitoring applications, network status and user activity.

Commands & Shells

Cool Unix shell commands and options. Command-line tools and application. Things every Unix sysadmin needs to know.

Home » Commands & Shells, Featured

IMDB Lookup Script

Submitted by on March 5, 2015 – 1:02 pm 2 Comments

IMDB is one of the most complete movie and television databases on the Internet. It offers an advanced Web search UI, but Unix CLI support has been discontinued years ago and the old scripts no longer work. There are a few nice third-party scripts out there for searching IMDB. Unfortunately, most of them are not maintained and won’t work right without some tweaking.

Here’s my simplistic version that should be a breeze to maintain and it will provide you with the basics: movie description and rating. You need to feed it the movie title and release year like so: “The Godfather (1972)”.

You can also leave the year out and just enter “The Godfather”. This will use Google search instead for the imdb.com domain. Depending on the uniqueness of the title, the accuracy may drop. Additionally, running too many Google queries in a short period of time may cause Google to temporarily bad your IP or start prompting for captcha.

Originally I wrote this to go through a list of movie titles and help me figure what’s worth watching. No special dependencies apart from lynx, but that’s easy enough to install. Save the following as /usr/bin/imdb and run like so:

imdb "Movie Title (2016)"

And here’s the script itself (imdb):

#!/bin/bash

if [ $# -eq 0 ]
then
    echo 'Usage: imdb "Movie Title (Year)"'
    exit 1
else
    y=$(echo "${@}" | sed -E 's/[()]//g' | awk '{print $NF}' | grep -oE "[0-9]{4}")
    t=$(echo "${@}" | sed -E 's/[()]//g' | sed -E 's/ [0-9]{4}$//g' | sed -r 's/  */\+/g;s/\&/%26/g;s/\++$//g' | sed 's/ /\%20/g')
fi

configure() {
    tmpfile="/tmp/imdb-mf_${RANDOM}.tmp"
}

cleanup() {
    if [ -f "${tmpfile}" ]
    then
        /bin/rm -f "${tmpfile}"
    fi
}

get_imdb() {
    if [ ! -z "${y}" ]
    then
        l=$(lynx -connect_timeout=10 --source "http://www.imdb.com/search/title?release_date=${y},${y}&title=${t}&title_type=feature" | grep -m1 -oP "(?<=id=\")[a-z]{2}[0-9]{4,}(?=\|imdb)")
        if [ -z "${l}" ]
        then
            l=$(lynx -connect_timeout=10 --source "http://www.imdb.com/search/title?release_date=${y},${y}&title=${t}&title_type=tv" | grep -m1 -oP "(?<=id=\")[a-z]{2}[0-9]{4,}(?=\|imdb)")
        fi
        lynx -connect_timeout=10 --source "http://www.imdb.com/title/${l}/" > ${tmpfile} 2> /dev/null
    else
        lynx -connect_timeout=10 --source "http://www.google.com/search?q=site:imdb.com+%22${t}%22&btnI" > ${tmpfile} 2> /dev/null
    fi
}

parse_imdb() {
    year=$(grep -m 1 "\/year\/" "${tmpfile}" | grep -Eo "[0-9]{4}")
    title=$(grep -m 1 "og:title" "${tmpfile}" | grep -Eo '\".*\"' | sed -e 's/"//g')
    temp=$(grep "og:description" "${tmpfile}" | sed -e 's/content="/@/g' -e 's/" \/>/@/g' -e 's/\&quot;/\"/g' | awk -F'@' '{print $(NF-1)}')
    director=$(echo ${temp} | grep -oP "(?<=Directed by ).*?(?=\. With)")
    cast=$(echo ${temp} | grep -oP "(?<=\. With ).* ?(?=\. [A-Z0-9])" | sed -r 's/([A-Z]{1})\./@/g' | awk -F'.' '{print $1}' | sed -r 's/@/\./g')
    plot=$(echo ${temp} | sed -r "s/${cast}\. /@/g" | awk -F'@' '{print $NF}')
    rating=$(grep -m 1 -oP "[0-9]\.?[0-9]?\<span class=\"ofTen\"\>/10" "${tmpfile}" | sed -r 's/<.*>//g')
}

get_imdb2() {
    if [ -z "${year}" ]
    then
        m=$(echo "${l}" | sed 's/ [Aa]nd / \& /g')
        lynx -connect_timeout=10 --source "http://www.imdb.com/title/${m}/" > ${tmpfile} 2> /dev/null
        parse_imdb
    fi
}

get_imdb3() {
    if [ -z "${year}" ]
    then
        lynx -connect_timeout=10 --source "http://www.google.com/search?q=site:imdb.com+%22${t}%22&btnI" > ${tmpfile} 2> /dev/null
        parse_imdb
    fi
}

print_imdb() {
    if [ -z "${year}" ]
    then
        echo "Scraped the bottom of the pickle barrel but came up dry. Check the title and provide release year."
    else
        echo -e "Title:\t${title}"
        echo -e "Year:\t${year}"
        echo -e "Rating:\t${rating}"
        echo -e "Dir:\t${director}"
        echo -e "Cast:\t${cast}"
        echo -e "Plot:\t${plot}"
    fi
}

# RUNTIME

configure
cleanup
get_imdb
parse_imdb
get_imdb2
get_imdb3
print_imdb
cleanup

 

Print Friendly, PDF & Email

2 Comments »

Leave a Reply

%d bloggers like this: