Skip to content

Instantly share code, notes, and snippets.

@Radiotechniman
Forked from Neal/putiodl
Created June 15, 2018 19:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Radiotechniman/a04e3ae82115d32e1cd905ab38582f15 to your computer and use it in GitHub Desktop.
Save Radiotechniman/a04e3ae82115d32e1cd905ab38582f15 to your computer and use it in GitHub Desktop.
put.io cli downloader
#!/bin/bash
#
# putiodl - put.io cli downloader
#
# Copyright (C) 2013 Neal <neal@ineal.me>
#
# 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment