Skip to content

Instantly share code, notes, and snippets.

@Neal
Created July 17, 2013 02:53
Show Gist options
  • Save Neal/6017311 to your computer and use it in GitHub Desktop.
Save Neal/6017311 to your computer and use it in GitHub Desktop.
put.io cli downloader
#!/bin/bash
#
# putiodl - put.io cli downloader
#
# Copyright (C) 2013 Neal <[email protected]>
#
# Dependencies: jq, curl, wget
#
# Make sure ~/.putiodl exists and contains your put.io OAuth token.
#
OAUTH_TOKEN=$(cat ~/.putiodl)
if [[ ${OAUTH_TOKEN} == "" ]]; then
echo "Make sure ~/.putiodl exists and contains your put.io OAuth token."
fi
usage() {
cat << EOF
usage: putiodl [options]...
-l List all files
-i File ID
-o Output file location
-n Get file info
-z Download zip
EOF
exit 1
}
while getopts "hli:o:nz" OPTION; do
case ${OPTION} in
l)
FILEID="list"
INFO_ONLY=true
;;
i)
FILEID="${OPTARG}"
;;
o)
OUTFILELOC="${OPTARG}"
;;
n)
INFO_ONLY=true
;;
z)
DLZIP=true
;;
?)
usage
;;
esac
done
if [[ $(which curl) == "" ]]; then
echo "curl not found"
exit 1
fi
if [[ $(which wget) == "" ]]; then
echo "wget not found"
exit 1
fi
if [[ $(which jq) == "" ]]; then
echo "jq not found"
exit 1
fi
if [[ ${FILEID} == "" ]]; then
usage
fi
FILEINFO=$(curl -sH 'Accept: application/json' "https://api.put.io/v2/files/${FILEID}?oauth_token=${OAUTH_TOKEN}")
if [[ ${INFO_ONLY} == true ]]; then
echo "${FILEINFO}"
exit 0
fi
if [[ $(echo ${FILEINFO} | jq ".status") != "\"OK\"" ]]; then
echo "Invalid File ID."
exit 1
fi
[[ ${DLZIP} == true ]] && DL="zip" || DL="download"
if [[ ${OUTFILELOC} == "" ]]; then
OUTFILELOC=$(echo ${FILEINFO} | jq -r '.file.name')
[[ ${DLZIP} == true ]] && OUTFILELOC="${OUTFILELOC}.zip"
fi
wget -c "https://api.put.io/v2/files/${FILEID}/${DL}?oauth_token=${OAUTH_TOKEN}" -O "${OUTFILELOC}"
exit 0
@bdmorin
Copy link

bdmorin commented May 12, 2017

What is jq?

jq - Command-line JSON processor
https://stedolan.github.io/jq/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment