Created
January 18, 2017 14:35
-
-
Save spot62/1ceea5c8a7ce96aa9cedd94b18461b8a to your computer and use it in GitHub Desktop.
Yandex Disk file uploader
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# article: http://neblog.info/skript-bekapa-na-yandeks-disk | |
# | |
# REST API: https://tech.yandex.ru/disk/api/reference/upload-docpage/ | |
TOKEN="" | |
# NB: slash ending | |
UPLOADING_PATH="/" | |
function parse_json() | |
{ | |
local output | |
regex="(\"$1\":[\"]?)([^\",\}]+)([\"]?)" | |
[[ $2 =~ $regex ]] && output=${BASH_REMATCH[2]} | |
echo $output | |
} | |
function check_error() | |
{ | |
echo $(parse_json 'error' "${1}") | |
} | |
function file_upload() | |
{ | |
local path="path=${UPLOADING_PATH}${1}" | |
local param="overwrite=true" | |
local url="https://cloud-api.yandex.net:443/v1/disk/resources/upload" | |
local json_out=`curl -s -H "Authorization: OAuth ${TOKEN}" -G ${url} --data-urlencode "${path}" --data "${param}"` | |
local json_error=$(check_error "${json_out}") | |
if [[ $json_error == '' ]]; then | |
url=$(parse_json 'href' ${json_out}) | |
json_out=`curl -s -T ${1} -H "Authorization: OAuth ${TOKEN}" ${url}` | |
json_error=$(check_error "$json_out") | |
if [[ $json_error == '' ]]; then | |
return 0; | |
else | |
echo $json_error | |
return 1; | |
fi | |
else | |
echo $json_error | |
return 1 | |
fi | |
} | |
function file_publish() | |
{ | |
local path="path=${UPLOADING_PATH}${1}" | |
local url="https://cloud-api.yandex.net/v1/disk/resources/publish" | |
local json_out=`curl -s -X PUT -H "Authorization: OAuth ${TOKEN}" -G ${url} --data-urlencode "${path}"` | |
local json_error=$(check_error "${json_out}") | |
if [[ $json_error == '' ]]; then | |
url=$(parse_json 'href' $json_out) | |
json_out=`curl -s -H "Authorization: OAuth ${TOKEN}" ${url}` | |
json_error=$(check_error "$json_out") | |
if [[ $json_error != '' ]]; | |
then | |
echo $json_error | |
else | |
echo $(parse_json 'public_url' $json_out) | |
return 0 | |
fi | |
else | |
echo $json_error | |
return 1 | |
fi | |
} | |
function file_delete() | |
{ | |
local path="path=${UPLOADING_PATH}${1}" | |
local param="permanently=true" | |
local url="https://cloud-api.yandex.net/v1/disk/resources" | |
local json_out=`curl -s -X DELETE -H "Authorization: OAuth ${TOKEN}" -G ${url} --data-urlencode "${path}" --data "${param}"` | |
json_error=$(check_error "$json_out") | |
if [[ $json_error != '' ]]; | |
then | |
echo "json_error: $json_error" | |
return 1 | |
else | |
return 0 | |
fi | |
} | |
function files_list() | |
{ | |
local path="path=${UPLOADING_PATH}" | |
local param="sort=created&limit=100" | |
local url="https://cloud-api.yandex.net/v1/disk/resources" | |
curl -s -H "Authorization: OAuth $TOKEN" -G ${url} --data-urlencode "${path}" --data "${param}" | tr "{},[]" "\n" | grep "name[[:graph:]]${1}" | cut -d: -f 2 | tr -d '"' | |
} | |
function usage | |
{ | |
cat <<EOF | |
USAGE: $0 OPTIONS | |
OPTIONS: | |
-h Show this message | |
-l List of files | |
-m Mask of list files | |
-f <filename> Specify filename | |
-u upload this file | |
-p publish this file | |
-d delete this file | |
EOF | |
} | |
# --------------- OPTIONS ------------- | |
while getopts ":hlm:f:udp" opt; do | |
case $opt in | |
\?) | |
echo "Invalid option: -$OPTARG. $0 -h for help" >&2 | |
exit 1 | |
;; | |
:) | |
echo "Option -$OPTARG requires an argument." >&2 | |
exit 1 | |
;; | |
h) | |
usage | |
exit 1 | |
;; | |
l) | |
list=true; | |
;; | |
m) | |
mask="$(echo ${OPTARG} | tr -d '\\')" | |
#echo "${mask}" | |
#exit 1 | |
;; | |
f) | |
filename=${OPTARG} | |
if [ ! -f ${filename} ]; | |
then | |
echo "File not found: ${filename}" >&2 | |
exit 1 | |
fi | |
;; | |
u) | |
upload=true | |
;; | |
p) | |
publish=true | |
;; | |
d) | |
delete=true | |
;; | |
esac | |
done | |
# --------------- MAIN ---------------- | |
if [[ "true" == ${list} ]]; then | |
files_list ${mask} | |
fi | |
if [[ "true" == ${upload} ]]; then | |
if [[ -z ${filename} ]]; then | |
usage | |
exit 1 | |
fi | |
rc=$(file_upload ${filename}) || { | |
echo "json_error: $rc" >&2 | |
exit 1 | |
} | |
fi | |
if [[ "true" == ${publish} ]]; then | |
if [[ -z ${filename} ]]; then | |
usage | |
exit 1 | |
fi | |
rc=$(file_publish ${filename}) && { | |
echo "$rc" >&1 | |
} || { | |
echo "json_error: $rc" >&2 | |
exit 1 | |
} | |
fi | |
if [[ "true" == ${delete} ]]; then | |
if [[ -z ${filename} ]]; then | |
usage | |
exit 1 | |
fi | |
rc=$(file_delete ${filename}) || { | |
echo "json_error: $rc" >&2 | |
exit 1 | |
} | |
fi | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment