Created
May 16, 2018 01:03
-
-
Save gitchs/cf03eed59084d1c2f0fc3154b6829d54 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
| export WEBDAV_URL_PREFIX="${WEBDAV_URL_PREFIX:=https://webdav.yandex.com/}" | |
| export AUTHENTICATION=${AUTHENTICATION:=${__YANDEX_AUTHENTICATION}} | |
| if [[ "${#DEBUG}" -gt "0" ]];then | |
| set -x | |
| fi | |
| function cmdList() { | |
| ROOT="$1" | |
| URL=`echo "${WEBDAV_URL_PREFIX}${ROOT}"|sed 's/ /%20/g'` | |
| export RAW=`curl -s \ | |
| --header 'Depth: 1' \ | |
| -u "${AUTHENTICATION}" \ | |
| -XPROPFIND \ | |
| "${URL}"` | |
| python3 <<EOF | |
| import os | |
| from xml.etree import ElementTree | |
| from urllib.parse import urljoin | |
| raw = """$RAW""" | |
| class FileStat(object): | |
| root = """${ROOT}""" | |
| def __init__(self, **kwargs): | |
| self.attrs = kwargs | |
| @property | |
| def size(self): | |
| retval = self.attrs.get('getcontentlength', 0) | |
| return int(retval) | |
| @property | |
| def path(self): | |
| return os.path.join(self.root, self.name) | |
| @property | |
| def name(self): | |
| return self.attrs.get('displayname', '') | |
| @property | |
| def ct(self): | |
| return self.attrs.get('creationdate', '') | |
| @property | |
| def mt(self): | |
| return self.attrs.get('getlastmodified', '') | |
| @property | |
| def href(self): | |
| href = self.attrs.get('href', '') | |
| href = urljoin('${WEBDAV_URL_PREFIX}', href) | |
| return href | |
| @property | |
| def isdir(self): | |
| return 'getcontentlength' not in self.attrs | |
| def __str__(self): | |
| row = map(str, ['Dir' if self.isdir else 'File', '%10d' % self.size, self.mt, self.path]) | |
| row = list(row) | |
| return '\t'.join(row) | |
| files = [] | |
| et = ElementTree.fromstring(raw) | |
| for response in et.getchildren(): | |
| href = response.find('{DAV:}href').text | |
| propstat = response.find('{DAV:}propstat') | |
| status = propstat.find('{DAV:}status') | |
| assert '200 OK' in status.text | |
| prop = propstat.find('{DAV:}prop') | |
| file_stat = {} | |
| for p in prop.getchildren(): | |
| key = p.tag.strip('{DAV:}') | |
| val = p.text | |
| file_stat[key] = val | |
| file_stat['href'] = href | |
| files.append(FileStat(**file_stat)) | |
| if len(files) == 0: | |
| exit(0) | |
| for file in files[1:]: | |
| print(str(file)) | |
| EOF | |
| } | |
| function cmdUpload() { | |
| FILENAME="$1" | |
| KEYNAME="$2" | |
| if [[ "${#KEYNAME}" -eq "0" ]];then | |
| KEYNAME=`basename "${FILENAME}"` | |
| fi | |
| URL=`echo -n "${WEBDAV_URL_PREFIX}${KEYNAME}"|sed 's/ /%20/g'` | |
| curl \ | |
| -u "${AUTHENTICATION}" \ | |
| -T "${FILENAME}" \ | |
| "${URL}" | |
| } | |
| function cmdMkdir() { | |
| DIRNAME="$1" | |
| if [[ "${#DIRNAME}" -eq "0" ]];then | |
| echo "dirname is empty" | |
| exit 1 | |
| fi | |
| URL=`echo -n "${WEBDAV_URL_PREFIX}${DIRNAME}"|sed 's/ /%20/g'` | |
| curl \ | |
| -u "${AUTHENTICATION}" \ | |
| -X MKCOL \ | |
| "${URL}" | |
| } | |
| function cmdRM() { | |
| FILENAME="$1" | |
| if [[ "${#FILENAME}" -eq "0" ]];then | |
| echo "cmd rm does not support empty filename" | |
| exit 1 | |
| fi | |
| URL=`echo "${WEBDAV_URL_PREFIX}${FILENAME}"|sed 's/ /%20/g'` | |
| curl \ | |
| -u "${AUTHENTICATION}" \ | |
| -X DELETE \ | |
| "${URL}" | |
| } | |
| function cmdCopy() { | |
| SRC="$1" | |
| DST="$2" | |
| URL=`echo -n "${WEBDAV_URL_PREFIX}${SRC}"|sed 's/ /%20/g'` | |
| DST=`echo -n "${WEBDAV_URL_PREFIX}${DST}"|sed 's/ /%20/g'` | |
| curl \ | |
| -u "${AUTHENTICATION}" \ | |
| -X COPY \ | |
| --header "Destination:${DST}" \ | |
| "${URL}" | |
| } | |
| function cmdMove() { | |
| OLDNAME="$1" | |
| NEWNAME="$2" | |
| if [[ "${#OLDNAME}" -eq "0" ]];then | |
| echo "cmd mv does not support empty old name" | |
| exit 1 | |
| fi | |
| if [[ "${#NEWNAME}" -eq "0" ]];then | |
| echo "cmd mv does not support empty new name" | |
| exit 1 | |
| fi | |
| URL=`echo -n "${WEBDAV_URL_PREFIX}${OLDNAME}"|sed 's/ /%20/g'` | |
| DST=`echo -n "${WEBDAV_URL_PREFIX}${NEWNAME}"|sed 's/ /%20/g'` | |
| curl \ | |
| -u "${AUTHENTICATION}" \ | |
| -X MOVE \ | |
| --header "Destination:${DST}" \ | |
| "${URL}" | |
| } | |
| function cmdDownload(){ | |
| FILENAME="$1" | |
| BASE64_AUTHENTICATION=`echo -n "${AUTHENTICATION}"|base64` | |
| aria2c \ | |
| --all-proxy=localhost:8123 \ | |
| --header 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36' \ | |
| --header "Authorization: Basic ${BASE64_AUTHENTICATION}" \ | |
| "${WEBDAV_URL_PREFIX}${FILENAME}" | |
| } | |
| CMD="$1" | |
| shift | |
| case "${CMD}" in | |
| "ls") | |
| cmdList "$@" | |
| ;; | |
| "upload") | |
| cmdUpload "$@" | |
| ;; | |
| "mkdir") | |
| cmdMkdir "$@" | |
| ;; | |
| "rm") | |
| cmdRM $@ | |
| ;; | |
| "cp") | |
| cmdCopy "$@" | |
| ;; | |
| "mv") | |
| cmdMove "$@" | |
| ;; | |
| "download"|"dl") | |
| cmdDownload "$@" | |
| ;; | |
| *) | |
| echo "not support command ${CMD}" | |
| exit 1 | |
| ;; | |
| esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment