diff --git a/movies/movies b/movies/movies index 4c02cf7..32fd575 100755 --- a/movies/movies +++ b/movies/movies @@ -3,6 +3,7 @@ currentVersion="1.9.0" configuredClient="" +configuredPython="" ## This function determines which http get tool the system has installed and returns an error if there isnt one getConfiguredClient() @@ -20,6 +21,27 @@ getConfiguredClient() } +getConfiguredPython() +{ + if command -v python2 &>/dev/null ; then + configuredPython="python2" + elif command -v python &>/dev/null ; then + configuredPython="python" + else + echo "Error: This tool requires python 2 to be installed." + return 1 + fi +} + + +python() +{ + case "$configuredPython" in + python2) python2 "$@";; + python) python "$@";; + esac +} + ## Allows to call the users configured client without if statements everywhere httpGet() { @@ -42,25 +64,25 @@ checkInternet() fi } -## This function grabs information about a movie and using python2 parses the +## This function grabs information about a movie and using python parses the ## JSON response to extrapolate the information for storage getMovieInfo() { apiKey=946f500a # try not to abuse this it is a key that came from the ruby-scripts repo I link to. movie=$(echo "$@" | tr " " + ) ## format the inputs to use for the api - export PYTHONIOENCODING=utf8 #necessary for python2 in some cases + export PYTHONIOENCODING=utf8 #necessary for python in some cases movieInfo=$(httpGet "http://www.omdbapi.com/?t=$movie&apikey=$apiKey") > /dev/null # query the server and get the JSON response - checkResponse=$(echo $movieInfo | python2 -c "import sys, json; print json.load(sys.stdin)['Response']" 2> /dev/null) + checkResponse=$(echo $movieInfo | python -c "import sys, json; print json.load(sys.stdin)['Response']" 2> /dev/null) 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 python2 from the JSON response - title=$(echo $movieInfo | python2 -c "import sys, json; print json.load(sys.stdin)['Title']" 2> /dev/null) - year=$(echo $movieInfo | python2 -c "import sys, json; print json.load(sys.stdin)['Year']" 2> /dev/null) - score=$(echo $movieInfo | python2 -c "import sys, json; print json.load(sys.stdin)['Ratings'][1]['Value']" 2> /dev/null) - rated=$(echo $movieInfo | python2 -c "import sys, json; print json.load(sys.stdin)['Rated']" 2> /dev/null) - genre=$(echo $movieInfo | python2 -c "import sys, json; print json.load(sys.stdin)['Genre']" 2> /dev/null) - director=$(echo $movieInfo | python2 -c "import sys, json; print json.load(sys.stdin)['Director']" 2> /dev/null) - actors=$(echo $movieInfo | python2 -c "import sys, json; print json.load(sys.stdin)['Actors']" 2> /dev/null) - plot=$(echo $movieInfo | python2 -c "import sys, json; print json.load(sys.stdin)['Plot']" 2> /dev/null) + # 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']" 2> /dev/null) + year=$(echo $movieInfo | python -c "import sys, json; print json.load(sys.stdin)['Year']" 2> /dev/null) + score=$(echo $movieInfo | python -c "import sys, json; print json.load(sys.stdin)['Ratings'][1]['Value']" 2> /dev/null) + rated=$(echo $movieInfo | python -c "import sys, json; print json.load(sys.stdin)['Rated']" 2> /dev/null) + genre=$(echo $movieInfo | python -c "import sys, json; print json.load(sys.stdin)['Genre']" 2> /dev/null) + director=$(echo $movieInfo | python -c "import sys, json; print json.load(sys.stdin)['Director']" 2> /dev/null) + actors=$(echo $movieInfo | python -c "import sys, json; print json.load(sys.stdin)['Actors']" 2> /dev/null) + plot=$(echo $movieInfo | python -c "import sys, json; print json.load(sys.stdin)['Plot']" 2> /dev/null) unset movieInfo # don't need this anymore unset checkResponse # don't need this anymore } @@ -132,6 +154,7 @@ usage() echo " -v Get the tool version" } +getConfiguredPython || exit 1 getConfiguredClient || exit 1 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 diff --git a/short/short b/short/short index 667ded9..f9d7e25 100755 --- a/short/short +++ b/short/short @@ -3,6 +3,7 @@ currentVersion="1.9.0" configuredClient="" +configuredPython="" ## This function determines which http get tool the system has installed and returns an error if there isnt one getConfiguredClient() @@ -20,6 +21,28 @@ getConfiguredClient() } + +getConfiguredPython() +{ + if command -v python2 &>/dev/null ; then + configuredPython="python2" + elif command -v python &>/dev/null ; then + configuredPython="python" + else + echo "Error: This tool requires python 2 to be installed." + return 1 + fi +} + + +python() +{ + case "$configuredPython" in + python2) python2 "$@";; + python) python "$@";; + esac +} + ## Allows to call the users configured client without if statements everywhere httpGet() { @@ -104,12 +127,12 @@ expandURL() url=$1 fi response=$(httpGet http://x.datasig.io/short?url=$url) - errorCheck=$(echo $response | python2 -c "import sys, json; print json.load(sys.stdin)['/short']['message']" 2> /dev/null ) + errorCheck=$(echo $response | python -c "import sys, json; print json.load(sys.stdin)['/short']['message']" 2> /dev/null ) if [[ $errorCheck == "Error in /short" ]];then echo "Error: 404 could not find the website" return 1 fi - expandedURL=$(echo $response | python2 -c "import sys, json; print json.load(sys.stdin)['/short']['destination']" 2> /dev/null ) + expandedURL=$(echo $response | python -c "import sys, json; print json.load(sys.stdin)['/short']['destination']" 2> /dev/null ) } @@ -121,6 +144,7 @@ printResults() echo "=====================================================================" } +getConfiguredPython || exit 1 getConfiguredClient || exit 1 checkInternet || exit 1 diff --git a/stocks/stocks b/stocks/stocks index 7c9efd7..69ec12b 100755 --- a/stocks/stocks +++ b/stocks/stocks @@ -3,6 +3,7 @@ currentVersion="1.9.0" configuredClient="" +configuredPython="" ## This function determines which http get tool the system has installed and returns an error if there isnt one getConfiguredClient() @@ -20,6 +21,27 @@ getConfiguredClient() } +getConfiguredPython() +{ + if command -v python2 &>/dev/null ; then + configuredPython="python2" + elif command -v python &>/dev/null ; then + configuredPython="python" + else + echo "Error: This tool requires python 2 to be installed." + return 1 + fi +} + + +python() +{ + case "$configuredPython" in + python2) python2 "$@";; + python) python "$@";; + esac +} + ## Allows to call the users configured client without if statements everywhere httpGet() { @@ -41,24 +63,24 @@ checkInternet() fi } -## This function grabs information about a stock and using python2 parses the +## This function grabs information about a stock and using python parses the ## JSON response to extrapolate the information for storage getStockInformation() { stockInfo=$(httpGet "https://www.alphavantage.co/query?function=GLOBAL_QUOTE&symbol=$1&apikey=KPCCCRJVMOGN9L6T") > /dev/null #grab the JSON response - export PYTHONIOENCODING=utf8 #necessary for python2 in some cases - echo $stockInfo | python2 -c "import sys, json; print json.load(sys.stdin)['Realtime Global Securities Quote']['02. Exchange Name']" > /dev/null 2>&1 || { echo "Not a valid stock symbol" ; exit 1; } #checking if we get any information back from the server if not chances are it isnt a valid stock symbol - # The rest of the code is just extrapolating the data with python2 from the JSON response - exchangeName=$(echo $stockInfo | python2 -c "import sys, json; print json.load(sys.stdin)['Realtime Global Securities Quote']['02. Exchange Name']") - latestPrice=$(echo $stockInfo | python2 -c "import sys, json; print json.load(sys.stdin)['Realtime Global Securities Quote']['03. Latest Price']") - open=$(echo $stockInfo | python2 -c "import sys, json; print json.load(sys.stdin)['Realtime Global Securities Quote']['04. Open (Current Trading Day)']") - high=$(echo $stockInfo | python2 -c "import sys, json; print json.load(sys.stdin)['Realtime Global Securities Quote']['05. High (Current Trading Day)']") - low=$(echo $stockInfo | python2 -c "import sys, json; print json.load(sys.stdin)['Realtime Global Securities Quote']['06. Low (Current Trading Day)']") - close=$(echo $stockInfo | python2 -c "import sys, json; print json.load(sys.stdin)['Realtime Global Securities Quote']['07. Close (Previous Trading Day)']") - priceChange=$(echo $stockInfo | python2 -c "import sys, json; print json.load(sys.stdin)['Realtime Global Securities Quote']['08. Price Change']") - priceChangePercentage=$(echo $stockInfo | python2 -c "import sys, json; print json.load(sys.stdin)['Realtime Global Securities Quote']['09. Price Change Percentage']") - volume=$(echo $stockInfo | python2 -c "import sys, json; print json.load(sys.stdin)['Realtime Global Securities Quote']['10. Volume (Current Trading Day)']") - lastUpdated=$(echo $stockInfo | python2 -c "import sys, json; print json.load(sys.stdin)['Realtime Global Securities Quote']['11. Last Updated']") + export PYTHONIOENCODING=utf8 #necessary for python in some cases + echo $stockInfo | python -c "import sys, json; print json.load(sys.stdin)['Realtime Global Securities Quote']['02. Exchange Name']" > /dev/null 2>&1 || { echo "Not a valid stock symbol" ; exit 1; } #checking if we get any information back from the server if not chances are it isnt a valid stock symbol + # The rest of the code is just extrapolating the data with python from the JSON response + exchangeName=$(echo $stockInfo | python -c "import sys, json; print json.load(sys.stdin)['Realtime Global Securities Quote']['02. Exchange Name']") + latestPrice=$(echo $stockInfo | python -c "import sys, json; print json.load(sys.stdin)['Realtime Global Securities Quote']['03. Latest Price']") + open=$(echo $stockInfo | python -c "import sys, json; print json.load(sys.stdin)['Realtime Global Securities Quote']['04. Open (Current Trading Day)']") + high=$(echo $stockInfo | python -c "import sys, json; print json.load(sys.stdin)['Realtime Global Securities Quote']['05. High (Current Trading Day)']") + low=$(echo $stockInfo | python -c "import sys, json; print json.load(sys.stdin)['Realtime Global Securities Quote']['06. Low (Current Trading Day)']") + close=$(echo $stockInfo | python -c "import sys, json; print json.load(sys.stdin)['Realtime Global Securities Quote']['07. Close (Previous Trading Day)']") + priceChange=$(echo $stockInfo | python -c "import sys, json; print json.load(sys.stdin)['Realtime Global Securities Quote']['08. Price Change']") + priceChangePercentage=$(echo $stockInfo | python -c "import sys, json; print json.load(sys.stdin)['Realtime Global Securities Quote']['09. Price Change Percentage']") + volume=$(echo $stockInfo | python -c "import sys, json; print json.load(sys.stdin)['Realtime Global Securities Quote']['10. Volume (Current Trading Day)']") + lastUpdated=$(echo $stockInfo | python -c "import sys, json; print json.load(sys.stdin)['Realtime Global Securities Quote']['11. Last Updated']") unset stockInfo # done with the JSON response not needed anymore } @@ -89,7 +111,7 @@ printStockInformation() getTicker() { response=$(httpGet "http://d.yimg.com/autoc.finance.yahoo.com/autoc?query=$1+$2+$3+$4+$5+$6+$7+$8+$9®ion=1&lang=en%22") > /dev/null - symbol=$(echo $response | python2 -c "import sys, json; print json.load(sys.stdin)['ResultSet']['Result'][0]['symbol']") # using python2 to extrapolate the stock symbol + symbol=$(echo $response | python -c "import sys, json; print json.load(sys.stdin)['ResultSet']['Result'][0]['symbol']") # using python to extrapolate the stock symbol unset response #just unsets the entire response after using it since all I need is the stock ticker } @@ -143,6 +165,7 @@ usage() echo " -v Get the tool version" } +getConfiguredPython || exit 1 getConfiguredClient || exit 1 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 diff --git a/taste/taste b/taste/taste index 3695df0..40438f1 100755 --- a/taste/taste +++ b/taste/taste @@ -3,6 +3,7 @@ currentVersion="1.9.0" configuredClient="" +configuredPython="" source ~/.bash_profile ## allows grabbing enviornment variable apiKey=$TASTE_API_KEY info="0" ## indicates if we want extra info @@ -21,7 +22,27 @@ getConfiguredClient() echo "Error: This tool reqires either curl, wget, or fetch to be installed." return 1 fi +} +getConfiguredPython() +{ + if command -v python2 &>/dev/null ; then + configuredPython="python2" + elif command -v python &>/dev/null ; then + configuredPython="python" + else + echo "Error: This tool requires python 2 to be installed." + return 1 + fi +} + + +python() +{ + case "$configuredPython" in + python2) python2 "$@";; + python) python "$@";; + esac } ## Allows to call the users configured client without if statements everywhere @@ -90,35 +111,35 @@ update() ## This function gets 3 results similar to the item of interest getSimilar() { - export PYTHONIOENCODING=utf8 #necessary for python2 in some cases + export PYTHONIOENCODING=utf8 #necessary for python in some cases media=$( echo "$@" | tr " " + ) response=$(httpGet "https://tastedive.com/api/similar?q=$media&k=$apiKey&info=$info") ## Extrapolate the information by parsing the JSON - nameOne=$(echo $response | python2 -c "import sys, json; print json.load(sys.stdin)['Similar']['Results'][0]['Name']" 2> /dev/null || { echo "Error: Did you search a valid item?"; return 1; } ) - typeOne=$(echo $response | python2 -c "import sys, json; print json.load(sys.stdin)['Similar']['Results'][0]['Type']" 2> /dev/null) - nameTwo=$(echo $response | python2 -c "import sys, json; print json.load(sys.stdin)['Similar']['Results'][1]['Name']" 2> /dev/null) - typeTwo=$(echo $response | python2 -c "import sys, json; print json.load(sys.stdin)['Similar']['Results'][1]['Type']" 2> /dev/null) - nameThree=$(echo $response | python2 -c "import sys, json; print json.load(sys.stdin)['Similar']['Results'][2]['Name']" 2> /dev/null) - typeThree=$(echo $response | python2 -c "import sys, json; print json.load(sys.stdin)['Similar']['Results'][2]['Type']" 2> /dev/null) + nameOne=$(echo $response | python -c "import sys, json; print json.load(sys.stdin)['Similar']['Results'][0]['Name']" 2> /dev/null || { echo "Error: Did you search a valid item?"; return 1; } ) + typeOne=$(echo $response | python -c "import sys, json; print json.load(sys.stdin)['Similar']['Results'][0]['Type']" 2> /dev/null) + nameTwo=$(echo $response | python -c "import sys, json; print json.load(sys.stdin)['Similar']['Results'][1]['Name']" 2> /dev/null) + typeTwo=$(echo $response | python -c "import sys, json; print json.load(sys.stdin)['Similar']['Results'][1]['Type']" 2> /dev/null) + nameThree=$(echo $response | python -c "import sys, json; print json.load(sys.stdin)['Similar']['Results'][2]['Name']" 2> /dev/null) + typeThree=$(echo $response | python -c "import sys, json; print json.load(sys.stdin)['Similar']['Results'][2]['Type']" 2> /dev/null) if [[ $info == "1" ]];then ## if we want more detailed info we have to grab a few more fields - wikiOne=$(echo $response | python2 -c "import sys, json; print json.load(sys.stdin)['Similar']['Results'][0]['wTeaser']" 2> /dev/null) - wikiTwo=$(echo $response | python2 -c "import sys, json; print json.load(sys.stdin)['Similar']['Results'][1]['wTeaser']" 2> /dev/null) - wikiThree=$(echo $response | python2 -c "import sys, json; print json.load(sys.stdin)['Similar']['Results'][2]['wTeaser']" 2> /dev/null) - youtube=$(echo $response | python2 -c "import sys, json; print json.load(sys.stdin)['Similar']['Results'][0]['yUrl']" 2> /dev/null) + wikiOne=$(echo $response | python -c "import sys, json; print json.load(sys.stdin)['Similar']['Results'][0]['wTeaser']" 2> /dev/null) + wikiTwo=$(echo $response | python -c "import sys, json; print json.load(sys.stdin)['Similar']['Results'][1]['wTeaser']" 2> /dev/null) + wikiThree=$(echo $response | python -c "import sys, json; print json.load(sys.stdin)['Similar']['Results'][2]['wTeaser']" 2> /dev/null) + youtube=$(echo $response | python -c "import sys, json; print json.load(sys.stdin)['Similar']['Results'][0]['yUrl']" 2> /dev/null) fi } ## This function grabs all the information it can on the item of interest itself getInfo() { - export PYTHONIOENCODING=utf8 #necessary for python2 in some cases + export PYTHONIOENCODING=utf8 #necessary for python in some cases media=$( echo "$@" | tr " " + ) response=$(httpGet "https://tastedive.com/api/similar?q=$media&k=$apiKey&info=$info") - name=$(echo $response | python2 -c "import sys, json; print json.load(sys.stdin)['Similar']['Info'][0]['Name']") - type=$(echo $response | python2 -c "import sys, json; print json.load(sys.stdin)['Similar']['Info'][0]['Type']") + name=$(echo $response | python -c "import sys, json; print json.load(sys.stdin)['Similar']['Info'][0]['Name']") + type=$(echo $response | python -c "import sys, json; print json.load(sys.stdin)['Similar']['Info'][0]['Type']") if [[ $info == "1" ]]; then - wiki=$(echo $response | python2 -c "import sys, json; print json.load(sys.stdin)['Similar']['Info'][0]['wTeaser']") - youtube=$(echo $response | python2 -c "import sys, json; print json.load(sys.stdin)['Similar']['Info'][0]['yUrl']") + wiki=$(echo $response | python -c "import sys, json; print json.load(sys.stdin)['Similar']['Info'][0]['wTeaser']") + youtube=$(echo $response | python -c "import sys, json; print json.load(sys.stdin)['Similar']['Info'][0]['yUrl']") else wiki="None" youtube="None" @@ -183,6 +204,7 @@ if [[ $apiKey == "" ]];then echo "After following all the steps and issues still persist try adding TASTE_API_KEY manually to your .bashrc" exit 1 fi +getConfiguredPython || exit 1 getConfiguredClient || exit 1 checkInternet || exit 1