Code to pep8 standards, shared folders working
This commit is contained in:
parent
10b4a883a8
commit
8b1052a214
1 changed files with 62 additions and 62 deletions
64
mega/mega.py
64
mega/mega.py
|
@ -21,8 +21,8 @@ class Mega(object):
|
|||
self.request_id = make_id(10)
|
||||
|
||||
@classmethod
|
||||
def login(class_, email, password):
|
||||
instance = class_()
|
||||
def login(cls, email, password):
|
||||
instance = cls()
|
||||
instance.login_user(email, password)
|
||||
return instance
|
||||
|
||||
|
@ -153,13 +153,13 @@ class Mega(object):
|
|||
return file
|
||||
|
||||
def init_shared_keys(self, files, shared_keys):
|
||||
'''
|
||||
"""
|
||||
Init shared key not associated with a user.
|
||||
It seems to happen when a folder is shared,
|
||||
some files are exachanged and then the
|
||||
folder is not shared anymore.
|
||||
Seems to happen when a folder is shared,
|
||||
some files are exchanged and then the
|
||||
folder is un-shared.
|
||||
Keys are stored in files['s'] and files['ok']
|
||||
'''
|
||||
"""
|
||||
ok_dict = {}
|
||||
for ok_item in files['ok']:
|
||||
shared_key = decrypt_key(base64_to_a32(ok_item['k']), self.master_key)
|
||||
|
@ -173,18 +173,18 @@ class Mega(object):
|
|||
##########################################################################
|
||||
# GET
|
||||
def find(self, filename):
|
||||
'''
|
||||
"""
|
||||
Return file object from given filename
|
||||
'''
|
||||
"""
|
||||
files = self.get_files()
|
||||
for file in files.items():
|
||||
if file[1]['a'] and file[1]['a']['n'] == filename:
|
||||
return file
|
||||
|
||||
def get_files(self):
|
||||
'''
|
||||
"""
|
||||
Get all files in account
|
||||
'''
|
||||
"""
|
||||
files = self.api_request({'a': 'f', 'c': 1})
|
||||
files_dict = {}
|
||||
shared_keys = {}
|
||||
|
@ -197,10 +197,10 @@ class Mega(object):
|
|||
return files_dict
|
||||
|
||||
def get_upload_link(self, file):
|
||||
'''
|
||||
"""
|
||||
Get a files public link inc. decrypted key
|
||||
Requires upload() response as input
|
||||
'''
|
||||
"""
|
||||
if 'f' in file:
|
||||
file = file['f'][0]
|
||||
public_handle = self.api_request({'a': 'l', 'n': file['h']})
|
||||
|
@ -216,9 +216,9 @@ class Mega(object):
|
|||
use get_link() for regular file input''')
|
||||
|
||||
def get_link(self, file):
|
||||
'''
|
||||
"""
|
||||
Get a file public link from given file object
|
||||
'''
|
||||
"""
|
||||
file = file[1]
|
||||
if 'h' in file and 'k' in file:
|
||||
public_handle = self.api_request({'a': 'l', 'n': file['h']})
|
||||
|
@ -237,23 +237,23 @@ class Mega(object):
|
|||
return user_data
|
||||
|
||||
def get_node_by_type(self, type):
|
||||
'''
|
||||
"""
|
||||
Get a node by it's numeric type id, e.g:
|
||||
0: file
|
||||
1: dir
|
||||
2: special: root cloud drive
|
||||
3: special: inbox
|
||||
4: special trash bin
|
||||
'''
|
||||
"""
|
||||
nodes = self.get_files()
|
||||
for node in nodes.items():
|
||||
if (node[1]['t'] == type):
|
||||
return node
|
||||
|
||||
def get_files_in_node(self, target):
|
||||
'''
|
||||
"""
|
||||
Get all files in a given target, e.g. 4=trash
|
||||
'''
|
||||
"""
|
||||
node_id = self.get_node_by_type(target)
|
||||
files = self.api_request({'a': 'f', 'c': 1})
|
||||
files_dict = {}
|
||||
|
@ -278,17 +278,17 @@ class Mega(object):
|
|||
return node_id
|
||||
|
||||
def get_quota(self):
|
||||
'''
|
||||
"""
|
||||
Get current remaining disk quota in MegaBytes
|
||||
'''
|
||||
"""
|
||||
json_resp = self.api_request({'a': 'uq', 'xfer': 1})
|
||||
#convert bytes to megabyes
|
||||
return json_resp['mstrg'] / 1048576
|
||||
|
||||
def get_balance(self):
|
||||
'''
|
||||
"""
|
||||
Get account monetary balance, Pro accounts only
|
||||
'''
|
||||
"""
|
||||
user_data = self.api_request({"a": "uq", "pro": 1})
|
||||
if 'balance' in user_data:
|
||||
return user_data['balance']
|
||||
|
@ -311,6 +311,7 @@ class Mega(object):
|
|||
return self.api_request({'a': 'd',
|
||||
'n': file_id,
|
||||
'i': self.request_id})
|
||||
|
||||
def destroy_url(self, url):
|
||||
#delete a file via it's url
|
||||
path = self.parse_url(url).split('!')
|
||||
|
@ -319,7 +320,7 @@ class Mega(object):
|
|||
return self.destroy(file_id)
|
||||
|
||||
def move(self, file_id, target):
|
||||
'''
|
||||
"""
|
||||
Move a file to another parent node
|
||||
params:
|
||||
a : command
|
||||
|
@ -331,7 +332,7 @@ class Mega(object):
|
|||
2 : root
|
||||
3 : inbox
|
||||
4 : trash
|
||||
'''
|
||||
"""
|
||||
|
||||
#determine target_node_id
|
||||
target_node_id = str(self.get_node_by_type(target)[0])
|
||||
|
@ -340,7 +341,6 @@ class Mega(object):
|
|||
't': target_node_id,
|
||||
'i': self.request_id})
|
||||
|
||||
|
||||
def empty_trash(self):
|
||||
# get list of files in rubbish out
|
||||
files = self.get_files_in_node(4)
|
||||
|
@ -357,16 +357,16 @@ class Mega(object):
|
|||
##########################################################################
|
||||
# DOWNLOAD
|
||||
def download(self, file, dest_path=None):
|
||||
'''
|
||||
"""
|
||||
Download a file by it's file object
|
||||
'''
|
||||
"""
|
||||
url = self.get_link(file)
|
||||
self.download_url(url, dest_path)
|
||||
|
||||
def download_url(self, url, dest_path=None):
|
||||
'''
|
||||
"""
|
||||
Download a file by it's public url
|
||||
'''
|
||||
"""
|
||||
path = self.parse_url(url).split('!')
|
||||
file_id = path[0]
|
||||
file_key = path[1]
|
||||
|
@ -452,7 +452,7 @@ class Mega(object):
|
|||
ul_url = self.api_request({'a': 'u', 's': size})['p']
|
||||
|
||||
#generate random aes key (128) for file
|
||||
ul_key = [random.randint(0, 0xFFFFFFFF) for r in range(6)]
|
||||
ul_key = [random.randint(0, 0xFFFFFFFF) for _ in range(6)]
|
||||
count = Counter.new(128, initial_value=((ul_key[4] << 32) + ul_key[5]) << 64)
|
||||
aes = AES.new(a32_to_str(ul_key[:4]), AES.MODE_CTR, counter=count)
|
||||
|
||||
|
@ -512,7 +512,7 @@ class Mega(object):
|
|||
dest = self.root_id
|
||||
|
||||
#generate random aes key (128) for folder
|
||||
ul_key = [random.randint(0, 0xFFFFFFFF) for r in range(6)]
|
||||
ul_key = [random.randint(0, 0xFFFFFFFF) for _ in range(6)]
|
||||
|
||||
#encrypt attribs
|
||||
attribs = {'n': name}
|
||||
|
|
Loading…
Reference in a new issue