diff --git a/movies/movies b/movies/movies index 7e732e6..138dbb4 100755 --- a/movies/movies +++ b/movies/movies @@ -3,22 +3,25 @@ checkInternet() { - echo -e "GET http://google.com HTTP/1.0\n\n" | nc google.com 80 > /dev/null 2>&1 - if [ $? -eq 0 ]; then + echo -e "GET http://google.com HTTP/1.0\n\n" | nc google.com 80 > /dev/null 2>&1 # query google with a get request + if [ $? -eq 0 ]; then #check if the output is 0, if so no errors have occured and we have connected to google successfully return 0 else - echo "Error: no active internet connection" >&2 + echo "Error: no active internet connection" >&2 #sent to stderr return 1 fi } +## This function grabs information about a movie and using python parses the +## JSON response to extrapolate the information for storage getMovieInfo() { - apiKey=946f500a - movie="$1+$2+$3+$4+$5+$6+$7+$8+$9" - movieInfo=$(curl -s "http://www.omdbapi.com/?t=$movie&apikey=$apiKey") > /dev/null + apiKey=946f500a # try not to abuse this it is a key that came from the ruby-scripts repo I link to. + movie="$1+$2+$3+$4+$5+$6+$7+$8+$9" ## format the inputs to use for the api + movieInfo=$(curl -s "http://www.omdbapi.com/?t=$movie&apikey=$apiKey") > /dev/null # query the server and get the JSON response checkResponse=$(echo $movieInfo | python -c "import sys, json; print json.load(sys.stdin)['Response']") - if [[ $checkResponse == "False" ]];then { echo "No movie found" ; return 1 ;} fi + if [[ $checkResponse == "False" ]];then { echo "No movie found" ; return 1 ;} fi ## check to see if the movie was found + # The rest of the code is just extrapolating the data with python from the JSON response title=$(echo $movieInfo | python -c "import sys, json; print json.load(sys.stdin)['Title']") year=$(echo $movieInfo | python -c "import sys, json; print json.load(sys.stdin)['Year']") score=$(echo $movieInfo | python -c "import sys, json; print json.load(sys.stdin)['Ratings'][1]['Value']") @@ -27,8 +30,11 @@ getMovieInfo() director=$(echo $movieInfo | python -c "import sys, json; print json.load(sys.stdin)['Director']") actors=$(echo $movieInfo | python -c "import sys, json; print json.load(sys.stdin)['Actors']") plot=$(echo $movieInfo | python -c "import sys, json; print json.load(sys.stdin)['Plot']") + unset movieInfo # don't need this anymore + unset checkResponse # don't need this anymore } +# Prints the movie information out in a human readable format printMovieInfo() { echo @@ -45,6 +51,6 @@ printMovieInfo() echo } -checkInternet || exit 1 -getMovieInfo $1 $2 $3 $4 $5 $6 $7 $8 $9 || exit 1 -printMovieInfo +checkInternet || exit 1 # check if we have a valid internet connection if this isnt true the rest of the script will not work so stop here +getMovieInfo $1 $2 $3 $4 $5 $6 $7 $8 $9 || exit 1 ## exit if we return 1 (chances are movie was not found) +printMovieInfo ## print out the data