#!/bin/sh -e
#
# dependencies: json2tsv

if test "$1" = ""; then
  echo "Usage: $(basename "$0") <imdb_URL>" >&2
  exit 1
fi

curl \
  -f -s -m 10 \
  -L --max-redirs 0 \
  -H 'Accept-Language: en' \
  -H 'User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:103.0) Gecko/20100101 Firefox/103.0' \
  "$1" \
  | extractjson \
  | sed 1q \
  | json2tsv \
  | LC_ALL=C awk '
BEGIN {
  FS = OFS = "\t";
}
$1 == ".@type" { type = $3; }
$1 == ".description" { description = $3; }
$1 == ".name" { name = $3; }
$1 == ".aggregateRating.ratingValue" { rating = $3; }
$1 == ".datePublished" { published = substr($3, 1, 4); }
END {
  if (length(name))
    printf("%s (%s) from %s, rating: %s: %s\n",
      name, type, published, rating, description);
}'
