From 1eab67d62884e286d3ded05836608e2e7ac45ab6 Mon Sep 17 00:00:00 2001 From: "richard@richard.do" Date: Thu, 7 Feb 2013 18:06:03 +0000 Subject: [PATCH] Added file search function and get public link function --- README.md | 6 ++++++ mega.py | 17 +++++++++++++++++ tests/test.py | 26 +++++++++++++++++++------- 3 files changed, 42 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 39d9e7b..5070b17 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,12 @@ 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') + +### Search for a file + m.find('myfile.doc') + ### Trash a file from URL or it's ID m.delete('utYjgSTQ') diff --git a/mega.py b/mega.py index 59b29db..8b48dd4 100644 --- a/mega.py +++ b/mega.py @@ -91,6 +91,12 @@ 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) + else: + raise errors.RequestError('Url key missing') + def download_url(self, url): path = self.parse_url(url).split('!') file_id = path[0] @@ -123,6 +129,17 @@ class Mega(object): #straight delete by id return self.move(file_id, 4) + 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 move(self, file_id, target): #TODO node_id improvements ''' diff --git a/tests/test.py b/tests/test.py index 1e123bb..cd6ba17 100644 --- a/tests/test.py +++ b/tests/test.py @@ -7,30 +7,42 @@ def test(): mega = Mega() - #login + ##login m = mega.login(email, password) - #get user details + ##get user details details = m.get_user() print(details) - #get account files + ##get account files files = m.get_files() #example iterate over files for file in files: if files[file]['a'] != False: print files[file] - #upload file + ##upload file print(m.upload('test.py')) - #trash a file, by id or url + ##trash a file, by id or url #print(m.delete('f14U0JhD')) #print(m.delete_url('https://mega.co.nz/#!f14U0JhD!S_2k-EvB5U1N3s0vm3I5C0JN2toHSGkVf0UxQsiKZ8A')) - #download file, by id+key or url + ##search for a file on mega + files = m.find('test.py') + + if files: + #trash a file by it's id + #iterate to trash multiple results + print(m.delete(files[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') + #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