Fixing python bug in OSX

This commit is contained in:
Alex Epstein 2017-07-05 10:35:18 -04:00
parent 114a734c92
commit 8703967165
4 changed files with 137 additions and 45 deletions

View File

@ -3,6 +3,7 @@
currentVersion="1.9.0" currentVersion="1.9.0"
configuredClient="" configuredClient=""
configuredPython=""
## This function determines which http get tool the system has installed and returns an error if there isnt one ## This function determines which http get tool the system has installed and returns an error if there isnt one
getConfiguredClient() 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 ## Allows to call the users configured client without if statements everywhere
httpGet() httpGet()
{ {
@ -42,25 +64,25 @@ checkInternet()
fi 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 ## JSON response to extrapolate the information for storage
getMovieInfo() getMovieInfo()
{ {
apiKey=946f500a # try not to abuse this it is a key that came from the ruby-scripts repo I link to. 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 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 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 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 # The rest of the code is just extrapolating the data with python from the JSON response
title=$(echo $movieInfo | python2 -c "import sys, json; print json.load(sys.stdin)['Title']" 2> /dev/null) title=$(echo $movieInfo | python -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) year=$(echo $movieInfo | python -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) score=$(echo $movieInfo | python -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) rated=$(echo $movieInfo | python -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) genre=$(echo $movieInfo | python -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) director=$(echo $movieInfo | python -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) actors=$(echo $movieInfo | python -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) 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 movieInfo # don't need this anymore
unset checkResponse # don't need this anymore unset checkResponse # don't need this anymore
} }
@ -132,6 +154,7 @@ usage()
echo " -v Get the tool version" echo " -v Get the tool version"
} }
getConfiguredPython || exit 1
getConfiguredClient || 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 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

View File

@ -3,6 +3,7 @@
currentVersion="1.9.0" currentVersion="1.9.0"
configuredClient="" configuredClient=""
configuredPython=""
## This function determines which http get tool the system has installed and returns an error if there isnt one ## This function determines which http get tool the system has installed and returns an error if there isnt one
getConfiguredClient() 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 ## Allows to call the users configured client without if statements everywhere
httpGet() httpGet()
{ {
@ -104,12 +127,12 @@ expandURL()
url=$1 url=$1
fi fi
response=$(httpGet http://x.datasig.io/short?url=$url) 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 if [[ $errorCheck == "Error in /short" ]];then
echo "Error: 404 could not find the website" echo "Error: 404 could not find the website"
return 1 return 1
fi 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 "=====================================================================" echo "====================================================================="
} }
getConfiguredPython || exit 1
getConfiguredClient || exit 1 getConfiguredClient || exit 1
checkInternet || exit 1 checkInternet || exit 1

View File

@ -3,6 +3,7 @@
currentVersion="1.9.0" currentVersion="1.9.0"
configuredClient="" configuredClient=""
configuredPython=""
## This function determines which http get tool the system has installed and returns an error if there isnt one ## This function determines which http get tool the system has installed and returns an error if there isnt one
getConfiguredClient() 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 ## Allows to call the users configured client without if statements everywhere
httpGet() httpGet()
{ {
@ -41,24 +63,24 @@ checkInternet()
fi 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 ## JSON response to extrapolate the information for storage
getStockInformation() getStockInformation()
{ {
stockInfo=$(httpGet "https://www.alphavantage.co/query?function=GLOBAL_QUOTE&symbol=$1&apikey=KPCCCRJVMOGN9L6T") > /dev/null #grab the JSON response 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 export PYTHONIOENCODING=utf8 #necessary for python 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 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 python2 from the JSON response # The rest of the code is just extrapolating the data with python from the JSON response
exchangeName=$(echo $stockInfo | python2 -c "import sys, json; print json.load(sys.stdin)['Realtime Global Securities Quote']['02. Exchange Name']") exchangeName=$(echo $stockInfo | python -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']") latestPrice=$(echo $stockInfo | python -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)']") 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 | python2 -c "import sys, json; print json.load(sys.stdin)['Realtime Global Securities Quote']['05. High (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 | python2 -c "import sys, json; print json.load(sys.stdin)['Realtime Global Securities Quote']['06. Low (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 | python2 -c "import sys, json; print json.load(sys.stdin)['Realtime Global Securities Quote']['07. Close (Previous 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 | python2 -c "import sys, json; print json.load(sys.stdin)['Realtime Global Securities Quote']['08. Price Change']") priceChange=$(echo $stockInfo | python -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']") priceChangePercentage=$(echo $stockInfo | python -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)']") 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 | python2 -c "import sys, json; print json.load(sys.stdin)['Realtime Global Securities Quote']['11. Last Updated']") 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 unset stockInfo # done with the JSON response not needed anymore
} }
@ -89,7 +111,7 @@ printStockInformation()
getTicker() getTicker()
{ {
response=$(httpGet "http://d.yimg.com/autoc.finance.yahoo.com/autoc?query=$1+$2+$3+$4+$5+$6+$7+$8+$9&region=1&lang=en%22") > /dev/null response=$(httpGet "http://d.yimg.com/autoc.finance.yahoo.com/autoc?query=$1+$2+$3+$4+$5+$6+$7+$8+$9&region=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 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" echo " -v Get the tool version"
} }
getConfiguredPython || exit 1
getConfiguredClient || 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 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

View File

@ -3,6 +3,7 @@
currentVersion="1.9.0" currentVersion="1.9.0"
configuredClient="" configuredClient=""
configuredPython=""
source ~/.bash_profile ## allows grabbing enviornment variable source ~/.bash_profile ## allows grabbing enviornment variable
apiKey=$TASTE_API_KEY apiKey=$TASTE_API_KEY
info="0" ## indicates if we want extra info 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." echo "Error: This tool reqires either curl, wget, or fetch to be installed."
return 1 return 1
fi 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 ## 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 ## This function gets 3 results similar to the item of interest
getSimilar() getSimilar()
{ {
export PYTHONIOENCODING=utf8 #necessary for python2 in some cases export PYTHONIOENCODING=utf8 #necessary for python in some cases
media=$( echo "$@" | tr " " + ) media=$( echo "$@" | tr " " + )
response=$(httpGet "https://tastedive.com/api/similar?q=$media&k=$apiKey&info=$info") response=$(httpGet "https://tastedive.com/api/similar?q=$media&k=$apiKey&info=$info")
## Extrapolate the information by parsing the JSON ## 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; } ) 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 | python2 -c "import sys, json; print json.load(sys.stdin)['Similar']['Results'][0]['Type']" 2> /dev/null) typeOne=$(echo $response | python -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) nameTwo=$(echo $response | python -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) typeTwo=$(echo $response | python -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) nameThree=$(echo $response | python -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) 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 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) wikiOne=$(echo $response | python -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) wikiTwo=$(echo $response | python -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) wikiThree=$(echo $response | python -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) youtube=$(echo $response | python -c "import sys, json; print json.load(sys.stdin)['Similar']['Results'][0]['yUrl']" 2> /dev/null)
fi fi
} }
## This function grabs all the information it can on the item of interest itself ## This function grabs all the information it can on the item of interest itself
getInfo() getInfo()
{ {
export PYTHONIOENCODING=utf8 #necessary for python2 in some cases export PYTHONIOENCODING=utf8 #necessary for python in some cases
media=$( echo "$@" | tr " " + ) media=$( echo "$@" | tr " " + )
response=$(httpGet "https://tastedive.com/api/similar?q=$media&k=$apiKey&info=$info") 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']") name=$(echo $response | python -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']") type=$(echo $response | python -c "import sys, json; print json.load(sys.stdin)['Similar']['Info'][0]['Type']")
if [[ $info == "1" ]]; then if [[ $info == "1" ]]; then
wiki=$(echo $response | python2 -c "import sys, json; print json.load(sys.stdin)['Similar']['Info'][0]['wTeaser']") wiki=$(echo $response | python -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']") youtube=$(echo $response | python -c "import sys, json; print json.load(sys.stdin)['Similar']['Info'][0]['yUrl']")
else else
wiki="None" wiki="None"
youtube="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" echo "After following all the steps and issues still persist try adding TASTE_API_KEY manually to your .bashrc"
exit 1 exit 1
fi fi
getConfiguredPython || exit 1
getConfiguredClient || exit 1 getConfiguredClient || exit 1
checkInternet || exit 1 checkInternet || exit 1