v1ral_ITS

movies

Jun 27th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 8.73 KB | None | 0 0
  1. #!/usr/bin/env bash
  2. # Author: Alexander Epstein https://github.com/alexanderepstein
  3.  
  4. currentVersion="1.22.0"
  5. configuredClient=""
  6. configuredPython=""
  7. detail=false
  8.  
  9. ## This function determines which http get tool the system has installed and returns an error if there isnt one
  10. getConfiguredClient()
  11. {
  12.   if  command -v curl &>/dev/null; then
  13.     configuredClient="curl"
  14.   elif command -v wget &>/dev/null; then
  15.     configuredClient="wget"
  16.   elif command -v http &>/dev/null; then
  17.     configuredClient="httpie"
  18.   elif command -v fetch &>/dev/null; then
  19.     configuredClient="fetch"
  20.   else
  21.     echo "Error: This tool reqires either curl, wget, httpie or fetch to be installed." >&2
  22.     return 1
  23.   fi
  24. }
  25.  
  26. ## Allows to call the users configured client without if statements everywhere
  27. httpGet()
  28. {
  29.   case "$configuredClient" in
  30.     curl)  curl -A curl -s "$@" ;;
  31.     wget)  wget -qO- "$@" ;;
  32.     httpie) http -b GET "$@" ;;
  33.     fetch) fetch -q "$@" ;;
  34.   esac
  35. }
  36.  
  37. getConfiguredPython()
  38. {
  39.   if  command -v python2 &>/dev/null ; then
  40.     configuredPython="python2"
  41.   elif command -v python &>/dev/null ; then
  42.     configuredPython="python"
  43.   else
  44.     echo "Error: This tool requires python 2 to be installed."
  45.     return 1
  46.   fi
  47. }
  48.  
  49. if [[ $(uname) != "Darwin" ]]; then
  50.   python()
  51.   {
  52.     case "$configuredPython" in
  53.       python2) python2 "$@";;
  54.       python) python "$@";;
  55.     esac
  56.   }
  57. fi
  58.  
  59. checkInternet()
  60. {
  61.   httpGet github.com > /dev/null 2>&1 || { echo "Error: no active internet connection" >&2; return 1; } # query github with a get request
  62. }
  63.  
  64. ## This function grabs information about a movie and using python parses the
  65. ## JSON response to extrapolate the information for storage
  66. getMovieInfo()
  67. {
  68.   apiKey=946f500a # try not to abuse this it is a key that came from the ruby-scripts repo I link to.
  69.   movie=$( (echo "$@" | tr " " + ) | sed 's/-d+//g' ) ## format the inputs to use for the api. Added sed command to filter -d flag.
  70.   export PYTHONIOENCODING=utf8 #necessary for python in some cases
  71.   movieInfo=$(httpGet "http://www.omdbapi.com/?t=$movie&apikey=$apiKey") > /dev/null # query the server and get the JSON response
  72.   checkResponse=$(echo $movieInfo | python -c "import sys, json; print json.load(sys.stdin)['Response']" 2> /dev/null)
  73.   if [[ $checkResponse == "False" ]]; then { echo "No movie found" ; return 1 ;} fi ## check to see if the movie was found
  74.   # The rest of the code is just extrapolating the data with python from the JSON response
  75.   title=$(echo $movieInfo | python -c "import sys, json; print json.load(sys.stdin)['Title']" 2> /dev/null)
  76.   year=$(echo $movieInfo | python -c "import sys, json; print json.load(sys.stdin)['Year']" 2> /dev/null)
  77.   runtime=$(echo $movieInfo | python -c "import sys, json; print json.load(sys.stdin)['Runtime']" 2> /dev/null)
  78.   imdbScore=$(echo $movieInfo | python -c "import sys, json; print json.load(sys.stdin)['Ratings'][0]['Value']" 2> /dev/null)
  79.   tomatoScore=$(echo $movieInfo | python -c "import sys, json; print json.load(sys.stdin)['Ratings'][1]['Value']" 2> /dev/null)
  80.   rated=$(echo $movieInfo | python -c "import sys, json; print json.load(sys.stdin)['Rated']" 2> /dev/null)
  81.   genre=$(echo $movieInfo | python -c "import sys, json; print json.load(sys.stdin)['Genre']" 2> /dev/null)
  82.   director=$(echo $movieInfo | python -c "import sys, json; print json.load(sys.stdin)['Director']" 2> /dev/null)
  83.   actors=$(echo $movieInfo | python -c "import sys, json; print json.load(sys.stdin)['Actors']" 2> /dev/null)
  84.   plot=$(echo $movieInfo | python -c "import sys, json; print json.load(sys.stdin)['Plot']" 2> /dev/null)
  85.  
  86.   if $detail; then
  87.     awards=$(echo $movieInfo | python -c "import sys, json; print json.load(sys.stdin)['Awards']" 2> /dev/null)
  88.     boxOffice=$(echo $movieInfo | python -c "import sys, json; print json.load(sys.stdin)['BoxOffice']" 2> /dev/null)
  89.     metacriticScore=$(echo $movieInfo | python -c "import sys, json; print json.load(sys.stdin)['Ratings'][2]['Value']" 2> /dev/null)
  90.     production=$(echo $movieInfo | python -c "import sys, json; print json.load(sys.stdin)['Production']" 2> /dev/null)
  91.   fi
  92. }
  93.  
  94. # Prints the movie information out in a human readable format
  95. printMovieInfo()
  96. {
  97.   echo
  98.   echo '=================================================='
  99.   echo "| Title: $title"
  100.   echo "| Year: $year"
  101.   echo "| Runtime: $runtime"
  102.   if [[ $imdbScore != "" ]]; then echo "| IMDB: $imdbScore"; fi
  103.   if [[ $tomatoScore != "" ]]; then echo "| Tomato: $tomatoScore"; fi
  104.   if $detail; then
  105.     if [[ $metacriticScore != "" ]]; then echo "| Metascore: $metacriticScore"; fi
  106.   fi
  107.   if [[ $rated != "N/A" && $rated != "" ]]; then echo "| Rated: $rated"; fi
  108.   echo "| Genre: $genre"
  109.   echo "| Director: $director"
  110.   echo "| Actors: $actors"
  111.   if [[ $plot != "N/A" && $plot != "" ]]; then echo "| Plot: $plot"; fi
  112.   if $detail; then
  113.     if [[ $boxOffice != "" ]]; then echo "| Box Office: $boxOffice"; fi
  114.     if [[ $production != "" ]]; then echo "| Production: $production"; fi
  115.     if [[ $awards != "" ]]; then echo "| Awards: $awards"; fi
  116.   fi
  117.   echo '=================================================='
  118.   echo
  119. }
  120.  
  121. update()
  122. {
  123.   # Author: Alexander Epstein https://github.com/alexanderepstein
  124.   # Update utility version 2.2.0
  125.   # To test the tool enter in the defualt values that are in the examples for each variable
  126.   repositoryName="Bash-Snippets" #Name of repostiory to be updated ex. Sandman-Lite
  127.   githubUserName="alexanderepstein" #username that hosts the repostiory ex. alexanderepstein
  128.   nameOfInstallFile="install.sh" # change this if the installer file has a different name be sure to include file extension if there is one
  129.   latestVersion=$(httpGet https://api.github.com/repos/$githubUserName/$repositoryName/tags | grep -Eo '"name":.*?[^\\]",'| head -1 | grep -Eo "[0-9.]+" ) #always grabs the tag without the v option
  130.  
  131.   if [[ $currentVersion == "" || $repositoryName == "" || $githubUserName == "" || $nameOfInstallFile == "" ]]; then
  132.     echo "Error: update utility has not been configured correctly." >&2
  133.     exit 1
  134.   elif [[ $latestVersion == "" ]]; then
  135.     echo "Error: no active internet connection" >&2
  136.     exit 1
  137.   else
  138.     if [[ "$latestVersion" != "$currentVersion" ]]; then
  139.       echo "Version $latestVersion available"
  140.       echo -n "Do you wish to update $repositoryName [Y/n]: "
  141.       read -r answer
  142.       if [[ "$answer" == [Yy] ]]; then
  143.         cd ~ || { echo 'Update Failed'; exit 1; }
  144.         if [[ -d  ~/$repositoryName ]]; then rm -r -f $repositoryName || { echo "Permissions Error: try running the update as sudo"; exit 1; } ; fi
  145.         echo -n "Downloading latest version of: $repositoryName."
  146.         git clone -q "https://github.com/$githubUserName/$repositoryName" && touch .BSnippetsHiddenFile || { echo "Failure!"; exit 1; } &
  147.         while [ ! -f .BSnippetsHiddenFile ]; do { echo -n "."; sleep 2; };done
  148.         rm -f .BSnippetsHiddenFile
  149.         echo "Success!"
  150.         cd $repositoryName || { echo 'Update Failed'; exit 1; }
  151.         git checkout "v$latestVersion" 2> /dev/null || git checkout "$latestVersion" 2> /dev/null || echo "Couldn't git checkout to stable release, updating to latest commit."
  152.         chmod a+x install.sh #this might be necessary in your case but wasnt in mine.
  153.         ./$nameOfInstallFile "update" || exit 1
  154.         cd ..
  155.         rm -r -f $repositoryName || { echo "Permissions Error: update succesfull but cannot delete temp files located at ~/$repositoryName delete this directory with sudo"; exit 1; }
  156.       else
  157.         exit 1
  158.       fi
  159.     else
  160.       echo "$repositoryName is already the latest version"
  161.     fi
  162.   fi
  163. }
  164.  
  165. usage()
  166. {
  167.   cat <<EOF
  168. Movies
  169. Description: Provides relevant information about a certain movie.
  170. Usage: movies [flag] or movies [movieToSearch]
  171.   -u  Update Bash-Snippet Tools
  172.   -h  Show the help
  173.   -v  Get the tool version
  174.   -d  Show detailed information
  175. Examples:
  176.   movies Argo
  177.   movies Inception
  178. EOF
  179. }
  180.  
  181. if [[ $(uname) != "Darwin" ]]; then getConfiguredPython || exit 1; fi
  182. getConfiguredClient || exit 1
  183.  
  184.  
  185. while getopts 'ud:hv' flag; do
  186.   case "${flag}" in
  187.     u) 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
  188.       update
  189.        exit 0 ;;
  190.     d) detail=true ;;
  191.     h) usage
  192.        exit 0 ;;
  193.     v) echo "Version $currentVersion"
  194.        exit 0 ;;
  195.     :) echo "Option -$OPTARG requires an argument." >&2
  196.        exit 1 ;;
  197.     *) exit 1 ;;
  198.   esac
  199. done
  200.  
  201. if [[ $# == 0 ]]; then
  202.   usage
  203. elif [[ $1 == "update" ]]; then
  204.   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
  205.   update
  206. elif [[ $1 == "help" ]]; then
  207.   usage
  208. else
  209.   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
  210.   getMovieInfo "$@" || exit 1 ## exit if we return 1 (chances are movie was not found)
  211.   printMovieInfo ## print out the data
  212. fi
Add Comment
Please, Sign In to add comment