issue #44 manually added as would not merge. small spelling changes
This commit is contained in:
parent
8466a04efe
commit
e490bf74d3
2 changed files with 84 additions and 1 deletions
|
@ -69,6 +69,12 @@ m.download(file, '/home/john-smith/Desktop')
|
|||
# specify optional download filename (download_url() supports this also)
|
||||
m.download(file, '/home/john-smith/Desktop', 'myfile.zip')
|
||||
```
|
||||
### Import a file from URL, optionally specify destination folder
|
||||
```python
|
||||
m.import_public_url('https://mega.co.nz/#!utYjgSTQ!OM4U3V5v_W4N5edSo0wolg1D5H0fwSrLD3oLnLuS9pc')
|
||||
folder_node = m.find('Documents')[1]
|
||||
m.import_public_url('https://mega.co.nz/#!utYjgSTQ!OM4U3V5v_W4N5edSo0wolg1D5H0fwSrLD3oLnLuS9pc', dest_node=folder_node)
|
||||
```
|
||||
### Create a folder
|
||||
```python
|
||||
m.create_folder('new_folder')
|
||||
|
|
79
mega/mega.py
79
mega/mega.py
|
@ -664,4 +664,81 @@ class Mega(object):
|
|||
# data=json.dumps(None),
|
||||
# timeout=self.timeout)
|
||||
#json_resp = json.loads(req.text)
|
||||
#print json_resp
|
||||
#print json_resp
|
||||
|
||||
def get_public_url_info(self, url):
|
||||
"""
|
||||
Get size and name from a public url, dict returned
|
||||
"""
|
||||
file_handle, file_key = self.parse_url(url).split('!')
|
||||
return self.get_public_file_info(file_handle, file_key)
|
||||
|
||||
def import_public_url(self, url, dest_node=None, dest_name=None):
|
||||
"""
|
||||
Import the public url into user account
|
||||
"""
|
||||
file_handle, file_key = self.parse_url(url).split('!')
|
||||
return self.import_public_file(file_handle, file_key, dest_node=dest_node, dest_name=dest_name)
|
||||
|
||||
def get_public_file_info(self, file_handle, file_key):
|
||||
"""
|
||||
Get size and name of a public file
|
||||
"""
|
||||
data = self.api_request({
|
||||
'a': 'g',
|
||||
'p': file_handle,
|
||||
'ssm': 1})
|
||||
|
||||
#if numeric error code response
|
||||
if isinstance(data, int):
|
||||
raise RequestError(data)
|
||||
|
||||
if 'at' not in data or 's' not in data:
|
||||
raise ValueError("Unexpected result", data)
|
||||
|
||||
key = base64_to_a32(file_key)
|
||||
k = (key[0] ^ key[4], key[1] ^ key[5], key[2] ^ key[6], key[3] ^ key[7])
|
||||
|
||||
size = data['s']
|
||||
unencrypted_attrs = decrypt_attr(base64_url_decode(data['at']), k)
|
||||
if not(unencrypted_attrs):
|
||||
return None
|
||||
|
||||
result = {
|
||||
'size': size,
|
||||
'name': unencrypted_attrs['n']}
|
||||
|
||||
return result
|
||||
|
||||
def import_public_file(self, file_handle, file_key, dest_node=None, dest_name=None):
|
||||
"""
|
||||
Import the public file into user account
|
||||
"""
|
||||
|
||||
# Providing dest_node spare an API call to retrieve it.
|
||||
if dest_node is None:
|
||||
# Get '/Cloud Drive' folder no dest node specified
|
||||
dest_node = self.get_node_by_type(2)[1]
|
||||
|
||||
# Providing dest_name spares an API call to retrieve it.
|
||||
if dest_name is None:
|
||||
pl_info = self.get_public_file_info(file_handle, file_key)
|
||||
dest_name = pl_info['name']
|
||||
|
||||
key = base64_to_a32(file_key)
|
||||
k = (key[0] ^ key[4], key[1] ^ key[5], key[2] ^ key[6], key[3] ^ key[7])
|
||||
|
||||
encrypted_key = a32_to_base64(encrypt_key(key, self.master_key))
|
||||
encrypted_name = base64_url_encode(encrypt_attr({'n': dest_name}, k))
|
||||
|
||||
data = self.api_request({
|
||||
'a': 'p',
|
||||
't': dest_node['h'],
|
||||
'n': [{
|
||||
'ph': file_handle,
|
||||
't': 0,
|
||||
'a': encrypted_name,
|
||||
'k': encrypted_key}]})
|
||||
|
||||
#return API msg
|
||||
return data
|
||||
|
|
Loading…
Reference in a new issue