#!/usr/bin/rexx
/* ---------------------------------------------------------------- */
/* A simple excercise to show the ease with which OMDB can be       */
/* accessed.                                                        */
/*                                                                  */
/* Retrieves information for a movie/tv series/etc  from            */
/*   https://www.omdbapi.com  and uses the result to find           */
/* the poster associated with the movie etc. and display the        */
/* picture on the screen.                                           */
/*                                                                  */
/* Note: Written for linux Mint, where CURL is standard and pix is  */
/* the standard picture viewer. CURL can be downloaded from         */
/*   https://curl.haxx.se                                           */
/*                                                                  */
/* Also: OMDB requires the use of an apikey (free of charge is      */
/* possible), which can be obtained within minutes.                 */
/*                                                                  */
/* ---------------------------------------------------------------- */
/*                                                                  */
/* Originally by Ruurd J. Idenburg                                  */
/*                                                                  */
/* No copyright, no licence, no guarantees or warrantees, be it     */
/* explicit, implicit or whatever. Usage is totally and completely  */
/* at the users own risk, the author shall not be liable for any    */
/* damages whatsoever, for any reason whatsoever.                   */
/*                                                                  */
/* Please keep this comment block intact when modifying this code   */
/* and add a note with date and a description.                      */
/*                                                                  */
/* ---------------------------------------------------------------- */
/* 2020/03/06 - Initial version approximately                       */
/* ---------------------------------------------------------------- */
-- Get the title we are looking for
   parse arg title
   title = title~translate('+',' ')
-- I keep the ApiKey as an environment variable for my userid
  apiKey = Value("OMDB_APIKEY", ,"ENVIRONMENT")
-- Catch json output into an array
  jsonArray = .array~new
  address system with output using (jsonArray)
-- Set the query for OMDB
  "curl -s 'https://www.omdbapi.com/?t="title"&apikey="apiKey"'"
-- Stringify the response
  json = jsonArray~toString('c')
-- Rexxify the json string
  rexx = .json~new()~fromJson(json)
-- Quit when title is not found or some other error
  if rexx["Response"]=="False" then do
    say rexx["Error"]
    exit
  end
-- Get the path to the poster
  poster = rexx["Poster"]
-- Replace spaces in movie title with underscore
  title = rexx["Title"]~translate('_',' ')
-- Set the filename to receive output
  file = title".jpg"
-- Let's get the image if poster is not .nil 
  if (poster=="N/A"|.nil==poster) then do
    say "No poster available"
    exit
  end
  address system with output stream (file)
  "curl -s" "'"poster"'"
-- Possibly do something more useful here
-- pix is the standard picture viewer in Linux Mint 19.3
  address path "pix" file
 exit

::requires 'json.cls'
