From b91f84cabd79af001853a3e396c52016c3e38ad5 Mon Sep 17 00:00:00 2001 From: "richard@richard.do" Date: Thu, 7 Feb 2013 19:44:57 +0000 Subject: [PATCH] get_link() working, added get_file() for single file --- README.md | 13 ++++++++++--- mega.py | 39 +++++++++++++++++++++++---------------- tests/test.py | 18 ++++++++++-------- 3 files changed, 43 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index 16a6d1d..d38123b 100644 --- a/README.md +++ b/README.md @@ -22,6 +22,10 @@ This is a work in progress, further functionality coming shortly. files = m.get_files() +### Get single account file + + file = m.get_file('utYjgSTQ') + ### Upload a file m.upload('myfile.doc') @@ -30,10 +34,10 @@ This is a work in progress, further functionality coming shortly. m.download('utYjgSTQ','OM4U3V5v_W4N5edSo0wolg1D5H0fwSrLD3oLnLuS9pc') m.download_url('https://mega.co.nz/#!utYjgSTQ!OM4U3V5v_W4N5edSo0wolg1D5H0fwSrLD3oLnLuS9pc') -### Get a public link for a file - m.get_link('utYjgSTQ','OM4U3V5v_W4N5edSo0wolg1D5H0fwSrLD3oLnLuS9pc') +### Get a public link for account file + m.get_link('utYjgSTQ') -### Search for a file +### Search account for a file m.find('myfile.doc') ### Trash a file from URL, it's ID, or from search @@ -63,3 +67,6 @@ This is a work in progress, further functionality coming shortly. - https://mega.co.nz/#developers + Thanks to http://juIien-marchand.fr/blog/contact for examples + + diff --git a/mega.py b/mega.py index b07474f..43d9ef6 100644 --- a/mega.py +++ b/mega.py @@ -91,11 +91,30 @@ class Mega(object): files_dict[file['h']] = self.process_file(file) return files_dict - def get_link(self, file_id, file_key): - if file_id and file_key: - return '{0}://{1}#!{2}!{3}'.format(self.schema, self.domain, file_id, file_key) + def get_file(self, file_id): + ''' + Return file object from given filename + ''' + files = self.get_files() + for file in files.items(): + if file[1]['h'] == file_id: + return file + + def get_link(self, file_id): + ''' + Get a files public link including decrypted key + ''' + file = self.get_file(file_id) + if file: + file_key = file[1]['k'] + if file_id and file_key: + public_handle = self.api_request({'a': 'l', 'n': file_id}) + decrypted_key = a32_to_base64(file_key) + return '{0}://{1}/#!%s!%s'.format(self.schema, self.domain) % (public_handle, decrypted_key) + else: + raise errors.ValidationError('File id and key must be set') else: - raise errors.RequestError('Url key missing') + raise errors.ValidationError('File not found') def download_url(self, url): path = self.parse_url(url).split('!') @@ -238,18 +257,6 @@ class Mega(object): if (file_mac[0] ^ file_mac[1], file_mac[2] ^ file_mac[3]) != meta_mac: raise ValueError('Mismatched mac') - def get_public_url(self, file_id, file_key): - ''' - Get a files public link including decrypted key - ''' - if file_id and file_key: - public_handle = self.api_request({'a': 'l', 'n': file_id}) - decrypted_key = a32_to_base64(file_key) - return '{0}://{1}/#!%s!%s'.format(self.schema, self.domain) % (public_handle, decrypted_key) - else: - raise errors.ValidationError('File id and key must be set') - - def upload(self, filename, dest=None): #determine storage node if dest is None: diff --git a/tests/test.py b/tests/test.py index e549985..ec55448 100644 --- a/tests/test.py +++ b/tests/test.py @@ -21,6 +21,12 @@ def test(): if files[file]['a'] != False: print files[file] + ##get single file + #print(m.get_file('f14U0JhD')) + + ##get file's public link + #print(m.get_link('ChZCXTzA')) + ##upload file print(m.upload('test.py')) @@ -28,19 +34,15 @@ def test(): #print(m.delete('f14U0JhD')) #print(m.delete_url('https://mega.co.nz/#!f14U0JhD!S_2k-EvB5U1N3s0vm3I5C0JN2toHSGkVf0UxQsiKZ8A')) - ##search for a file on mega - files = m.find('test.py') - if files: + ##search for a file in account + file = m.find('test.py') + if file: #trash a file by it's id - #iterate to trash multiple results - print(m.delete(files[1]['k'])) + print(m.delete(file[1]['k'])) ##download file, by id+key or url #m.download('6hBW0R4a','By7-Vjj5xal8K5w_IXH3PlGNyZ1VvIrjZkOmHGq1X00') #m.download_url('https://mega.co.nz/#!6hBW0R4a!By7-Vjj5xal8K5w_IXH3PlGNyZ1VvIrjZkOmHGq1X00') - ##get a public link for a file - print(m.get_link('6hBW0R4a','By7-Vjj5xal8K5w_IXH3PlGNyZ1VvIrjZkOmHGq1X00')) - if __name__ == '__main__': test() \ No newline at end of file