diff --git a/stocks/stocks b/stocks/stocks index 7e13031..f6fa799 100755 --- a/stocks/stocks +++ b/stocks/stocks @@ -2,13 +2,32 @@ # Author: Alexander Epstein https://github.com/alexanderepstein currentVersion="1.2.1" +configuredClient="" -checkCurl() +## This function determines which http get tool the system has installed and returns an error if there isnt one +getConfiguredClient() { - if ! command -v curl &>/dev/null ; then - echo "Error: this tool requires 'curl', please install it." - return 1 - fi + if command -v curl &>/dev/null ; then + configuredClient="curl" + elif command -v wget &>/dev/null ; then + configuredClient="wget" + elif command -v fetch &>/dev/null ; then + configuredClient="fetch" + else + echo "Error: This tool reqires either curl, wget, or fetch to be installed." + return 1 + fi + +} + +## Allows to call the users configured client without if statements everywhere +httpGet() +{ + case "$configuredClient" in + curl) curl -A curl -s "$@";; + wget) wget -qO- "$@";; + fetch) fetch -o "...";; + esac } checkInternet() @@ -26,7 +45,7 @@ checkInternet() ## JSON response to extrapolate the information for storage getStockInformation() { - stockInfo=$(curl -Acurl -s "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 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 @@ -69,7 +88,7 @@ printStockInformation() ## and it will determine the stock symbol for apple is AAPL and move on from there getTicker() { - response=$(curl -Acurl -s "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 + 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 | 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 } @@ -82,7 +101,7 @@ update() repositoryName="Bash-Snippets" #Name of repostiory to be updated ex. Sandman-Lite githubUserName="alexanderepstein" #username that hosts the repostiory ex. alexanderepstein nameOfInstallFile="install.sh" # change this if the installer file has a different name be sure to include file extension if there is one - latestVersion=$(curl -Acurl -s 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 + 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 if [[ $currentVersion == "" || $repositoryName == "" || $githubUserName == "" || $nameOfInstallFile == "" ]];then echo "Error: update utility has not been configured correctly." >&2 @@ -102,7 +121,7 @@ update() cd $repositoryName || { echo 'Update Failed' ; exit 1 ;} 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." chmod a+x install.sh #this might be necessary in your case but wasnt in mine. - ./$nameOfInstallFile "update" || { echo "Permissions Error: try running the update as sudo"; exit 1; } + ./$nameOfInstallFile "update" || exit 1 cd .. rm -r -f $repositoryName || { echo "Permissions Error: update succesfull but cannot delete temp files located at ~/$repositoryName delete this directory with sudo"; exit 1; } else @@ -124,7 +143,7 @@ usage() echo " -v Get the tool version" } -checkCurl || 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