Using the OMDB API to rate your movies

Published: Friday, Dec 28, 2012 Last modified: Thursday, Dec 3, 2020

This script use the Open Movie Database API to get the ratings of movies listed in the file movie-list.

Interesting things to note is the way titles of movies are URL encoded using cURL’s data-urlencode switch.

xmlstarlet pyx as mentioned in [[09020]] is a easy way of using parsed XML output using shell, without frustratingly building an Xpath.

while read -r movie
    year=$(echo $movie | grep -Eo '\b(((19|20)[0-9][0-9])|2100)' | head -n1)
    m=$(echo $movie | sed 's,\[.*,,' | tr '.' ' ' | sed 's,(.*,,' | sed 's/dvdrip.*//gi')
    test "$m" || continue
    if test "$year"
        m=$(echo $m | sed "s,${year}.*,,")
        test "$m" || continue
        imdbid=$(curl -s -G --data-urlencode "r=XML" --data-urlencode "s=$m" --data-urlencode "Y=$year" | xmlstarlet pyx | awk '$1 == "AimdbID" { print $2 }' | head -n1)
        imdbid=$(curl -s -G --data-urlencode "r=XML" --data-urlencode "s=$m" | xmlstarlet pyx | awk '$1 == "AimdbID" { print $2 }' | head -n1)
    if test "$imdbid"
        rating=$(curl -s -G --data-urlencode "r=XML" --data-urlencode "i=$imdbid" "" | xmlstarlet pyx | awk '$1 == "AimdbRating" { print $2 }')
    #echo D: $movie C: $m Y: $year I: $imdbid R: $rating
    echo $movie,$rating
done < movie-list

Finally sort by the last CSV value, the rating:

awk -F, '{print $NF,$0}' rated.txt | sort -nr | cut -f2- -d' ' > sorted.txt