Using the OMDB API to rate your movies

Published: Friday, Dec 28, 2012 Last modified: Monday, Apr 8, 2024

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
do
	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"
	then
		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" http://www.omdbapi.com/ | xmlstarlet pyx | awk '$1 == "AimdbID" { print $2 }' | head -n1)
	else
		imdbid=$(curl -s -G --data-urlencode "r=XML" --data-urlencode "s=$m" http://www.omdbapi.com/ | xmlstarlet pyx | awk '$1 == "AimdbID" { print $2 }' | head -n1)
	fi
	if test "$imdbid"
	then
		rating=$(curl -s -G --data-urlencode "r=XML" --data-urlencode "i=$imdbid" "http://www.omdbapi.com/" | xmlstarlet pyx | awk '$1 == "AimdbRating" { print $2 }')
	fi
	#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