def get_cloud_space(session, csrf='', login=LOGIN):
""" returns available free space in bytes """
assert csrf is not None, 'no CSRF'
timestamp = str(int(time.mktime(datetime.datetime.now().timetuple())* 1000))
quoted_login = quote_plus(login)
command = ('user/space?api=' + str(API_VER) + '&email=' + quoted_login +
'&x-email=' + quoted_login + '&token=' + csrf + '&_=' + timestamp)
url = urljoin(CLOUD_URL, command)
try:
r = session.get(url, verify = VERIFY_SSL)
except Exception as e:
if LOGGER:
LOGGER.error('Get cloud space HTTP request error: {}'.format(e))
return 0
if r.status_code == requests.codes.ok:
r_json = r.json()
total_bytes = r_json['body']['total'] * 1024 * 1024
used_bytes = r_json['body']['used'] * 1024 * 1024
return total_bytes - used_bytes
elif LOGGER:
LOGGER.error('Cloud free space request error. Check your connection. \
HTTP code: {}, msg: {}'.format(r.status_code, r.text))
return 0
评论列表
文章目录