2017-07-21 06:16:52 +02:00
#!/usr/bin/env bash
2017-06-30 07:14:43 +02:00
# Author: Alexander Epstein https://github.com/alexanderepstein
2018-10-20 03:19:55 +02:00
currentVersion="1.22.1"
2017-06-30 07:14:43 +02:00
configuredClient=""
2017-07-05 16:35:18 +02:00
configuredPython=""
2017-07-14 19:37:35 +02:00
source ~/.bash_profile 2> /dev/null ## allows grabbing enviornment variable
2017-06-30 07:14:43 +02:00
apiKey=$TASTE_API_KEY
2017-06-30 16:39:32 +02:00
info="0" ## indicates if we want extra info
search="0" ## indivates that we want results on the item itself
2017-06-30 07:14:43 +02:00
## This function determines which http get tool the system has installed and returns an error if there isnt one
getConfiguredClient()
{
2017-07-27 14:17:15 +02:00
if command -v curl &>/dev/null; then
2017-06-30 07:14:43 +02:00
configuredClient="curl"
2017-07-27 14:17:15 +02:00
elif command -v wget &>/dev/null; then
2017-06-30 07:14:43 +02:00
configuredClient="wget"
2017-08-01 05:42:48 +02:00
elif command -v http &>/dev/null; then
configuredClient="httpie"
2017-07-27 14:17:15 +02:00
elif command -v fetch &>/dev/null; then
2017-06-30 07:14:43 +02:00
configuredClient="fetch"
else
2017-08-01 05:42:48 +02:00
echo "Error: This tool reqires either curl, wget, httpie or fetch to be installed." >&2
2017-06-30 07:14:43 +02:00
return 1
fi
2017-07-05 16:35:18 +02:00
}
2017-08-01 05:42:48 +02:00
## Allows to call the users configured client without if statements everywhere
httpGet()
{
case "$configuredClient" in
curl) curl -A curl -s "$@" ;;
wget) wget -qO- "$@" ;;
httpie) http -b GET "$@" ;;
2017-08-03 05:51:07 +02:00
fetch) fetch -q "$@" ;;
2017-08-01 05:42:48 +02:00
esac
}
2017-07-05 16:35:18 +02:00
getConfiguredPython()
{
2017-07-27 14:17:15 +02:00
if command -v python2 &>/dev/null; then
2017-07-05 16:35:18 +02:00
configuredPython="python2"
2017-07-27 14:17:15 +02:00
elif command -v python &>/dev/null; then
2017-07-05 16:35:18 +02:00
configuredPython="python"
else
echo "Error: This tool requires python 2 to be installed."
return 1
fi
}
2017-07-27 14:17:15 +02:00
if [[ $(uname) != "Darwin" ]]; then
2017-07-24 21:02:30 +02:00
python()
{
case "$configuredPython" in
2017-07-27 14:17:15 +02:00
python2) python2 "$@" ;;
python) python "$@" ;;
2017-07-24 21:02:30 +02:00
esac
}
fi
2017-06-30 07:14:43 +02:00
checkInternet()
{
2017-08-02 05:31:17 +02:00
httpGet github.com > /dev/null 2>&1 || { echo "Error: no active internet connection" >&2; return 1; } # query github with a get request
2017-06-30 07:14:43 +02:00
}
update()
{
# Author: Alexander Epstein https://github.com/alexanderepstein
2017-08-04 04:05:00 +02:00
# Update utility version 2.2.0
2017-06-30 07:14:43 +02:00
# To test the tool enter in the defualt values that are in the examples for each variable
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=$(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
2017-07-27 14:17:15 +02:00
if [[ $currentVersion == "" || $repositoryName == "" || $githubUserName == "" || $nameOfInstallFile == "" ]]; then
2017-06-30 07:14:43 +02:00
echo "Error: update utility has not been configured correctly." >&2
exit 1
2017-07-27 14:17:15 +02:00
elif [[ $latestVersion == "" ]]; then
2017-06-30 07:14:43 +02:00
echo "Error: no active internet connection" >&2
exit 1
else
if [[ "$latestVersion" != "$currentVersion" ]]; then
echo "Version $latestVersion available"
echo -n "Do you wish to update $repositoryName [Y/n]: "
read -r answer
2017-07-27 14:17:15 +02:00
if [[ "$answer" == [Yy] ]]; then
cd ~ || { echo 'Update Failed'; exit 1; }
2017-08-04 04:05:00 +02:00
if [[ -d ~/$repositoryName ]]; then rm -r -f $repositoryName || { echo "Permissions Error: try running the update as sudo"; exit 1; } ; fi
echo -n "Downloading latest version of: $repositoryName."
git clone -q "https://github.com/$githubUserName/$repositoryName" && touch .BSnippetsHiddenFile || { echo "Failure!"; exit 1; } &
while [ ! -f .BSnippetsHiddenFile ]; do { echo -n "."; sleep 2; };done
rm -f .BSnippetsHiddenFile
echo "Success!"
2017-07-27 14:17:15 +02:00
cd $repositoryName || { echo 'Update Failed'; exit 1; }
2017-06-30 07:14:43 +02:00
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" || exit 1
cd ..
2017-07-27 14:17:15 +02:00
rm -r -f $repositoryName || { echo "Permissions Error: update succesfull but cannot delete temp files located at ~/$repositoryName delete this directory with sudo"; exit 1; }
2017-06-30 07:14:43 +02:00
else
exit 1
fi
else
echo "$repositoryName is already the latest version"
fi
fi
}
2017-06-30 16:39:32 +02:00
## This function gets 3 results similar to the item of interest
2017-06-30 07:14:43 +02:00
getSimilar()
{
2017-07-05 16:35:18 +02:00
export PYTHONIOENCODING=utf8 #necessary for python in some cases
2017-07-03 22:34:32 +02:00
media=$( echo "$@" | tr " " + )
2017-06-30 07:14:43 +02:00
response=$(httpGet "https://tastedive.com/api/similar?q=$media&k=$apiKey&info=$info")
2017-06-30 16:39:32 +02:00
## Extrapolate the information by parsing the JSON
2017-07-05 16:35:18 +02:00
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)
2017-06-30 16:39:32 +02:00
if [[ $info == "1" ]];then ## if we want more detailed info we have to grab a few more fields
2017-07-05 16:35:18 +02:00
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)
2017-06-30 07:14:43 +02:00
fi
}
2017-06-30 16:39:32 +02:00
## This function grabs all the information it can on the item of interest itself
2017-06-30 07:14:43 +02:00
getInfo()
{
2017-07-05 16:35:18 +02:00
export PYTHONIOENCODING=utf8 #necessary for python in some cases
2017-07-03 22:34:32 +02:00
media=$( echo "$@" | tr " " + )
2017-06-30 07:14:43 +02:00
response=$(httpGet "https://tastedive.com/api/similar?q=$media&k=$apiKey&info=$info")
2017-07-05 16:35:18 +02:00
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']")
2017-06-30 07:14:43 +02:00
if [[ $info == "1" ]]; then
2017-07-05 16:35:18 +02:00
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']")
2017-06-30 07:14:43 +02:00
else
wiki="None"
youtube="None"
fi
}
printResults()
{
if [[ $info == "1" ]];then
echo "==================================="
echo
echo $nameOne: $typeOne
echo $wikiOne
echo
echo
echo $nameTwo: $typeTwo
echo $wikiTwo
echo
echo
echo $nameThree: $typeThree
echo $wikiThree
echo
2017-07-27 14:17:15 +02:00
if [[ $youtube != "None" ]]; then echo $youtube; fi
2017-06-30 07:14:43 +02:00
echo
echo "==================================="
else
echo "==================================="
echo $nameOne: $typeOne
echo $nameTwo: $typeTwo
echo $nameThree: $typeThree
echo "==================================="
fi
}
printInfo()
{
echo "==================================="
echo
echo $name: $type
echo $wiki
echo
2017-07-27 14:17:15 +02:00
if [[ $youtube != "None" ]]; then echo $youtube; fi
2017-06-30 07:14:43 +02:00
echo "==================================="
}
2017-06-30 07:21:27 +02:00
usage()
{
2017-07-27 14:17:15 +02:00
cat <<EOF
2017-07-27 23:54:46 +02:00
Taste
2017-07-27 14:17:15 +02:00
Description: A recommendation engine that provides 3 similar items based on some input topic.
Taste also has the ability to provide information on the item of interest.
Supports: shows, books, music, artists, movies, authors, games
Usage: taste [flag] [item]
-i Get more information on similar items
-s Get information on the item itself
-u Update Bash-Snippet Tools
-h Show the help
-v Get the tool version
Examples:
taste -i Kendrick Lamar
taste Catcher in the Rye
taste -s Red Hot Chili Peppers
EOF
2017-06-30 07:21:27 +02:00
}
2017-07-27 14:17:15 +02:00
if [[ $apiKey == "" ]]; then
cat <<EOF
Error: API key not setup properly
To get an API key visit https://tastedive.com/account/api_access
After getting the API key run the following command: export TASTE_API_KEY="yourAPIKeyGoesHere"
After following all the steps and issues still persist try adding export TASTE_API_KEY manually to your .bash_profile
EOF
2017-06-30 07:41:46 +02:00
exit 1
fi
2017-07-27 14:17:15 +02:00
if [[ $(uname) != "Darwin" ]]; then getConfiguredPython || exit 1; fi
2017-06-30 07:14:43 +02:00
getConfiguredClient || exit 1
2017-08-04 03:26:18 +02:00
2017-06-30 07:14:43 +02:00
while getopts "uvhis" opt; do
2017-07-27 14:17:15 +02:00
case "$opt" in
\?) echo "Invalid option: -$OPTARG" >&2
2017-06-30 07:14:43 +02:00
exit 1
2017-07-27 14:17:15 +02:00
;;
h) usage
exit 0
;;
v) echo "Version $currentVersion"
exit 0
;;
2017-08-04 03:26:18 +02:00
u) checkInternet || exit 1
update
2017-07-27 14:17:15 +02:00
exit 0
;;
i) if [[ $search == "0" ]]; then
info="1"
else
echo "Error: the options -i and -s are mutually exclusive (-s already uses -i)"
exit 1
fi
;;
s) if [[ $info != "1" ]]; then
search="1"
info="1"
else
echo "Error: the options -i and -s are mutually exclusive (-s already uses -i)"
exit 1
fi
;;
:) echo "Option -$OPTARG requires an argument." >&2
2017-06-30 07:14:43 +02:00
exit 1
2017-07-27 14:17:15 +02:00
;;
2017-06-30 07:14:43 +02:00
esac
done
if [[ $# == 0 ]]; then
usage
2017-07-27 14:17:15 +02:00
elif [[ $1 == "update" ]]; then
2017-08-04 03:26:18 +02:00
checkInternet || exit 1
2017-06-30 07:14:43 +02:00
update
2017-07-27 14:17:15 +02:00
elif [[ $1 == "help" ]]; then
2017-06-30 07:14:43 +02:00
usage
else
2017-08-04 03:26:18 +02:00
checkInternet || exit 1
2017-07-27 14:17:15 +02:00
if [[ $search == "0" ]]; then
if [[ $info == "0" ]]; then
2017-07-04 00:40:22 +02:00
getSimilar "$@" || exit 1 ## exit if we return 1 (chances are movie was not found)
2017-06-30 07:14:43 +02:00
printResults
else
2017-07-04 20:41:01 +02:00
getSimilar "${@:2}" || exit 1
2017-06-30 07:14:43 +02:00
printResults
fi
else
2017-07-04 00:40:22 +02:00
getInfo "${@:2}" || exit 1 ## exit if we return 1 (chances are movie was not found)
2017-06-30 07:14:43 +02:00
printInfo
fi
fi