From 3cf4584be0c2af00c52a067dfa6ae7d4208e7a97 Mon Sep 17 00:00:00 2001 From: "richard@richard.do" Date: Thu, 7 Feb 2013 23:30:37 +0000 Subject: [PATCH] get_link() partially working --- README.md | 14 ++++---------- mega.py | 30 ++++++++++-------------------- tests/test.py | 11 +++++------ 3 files changed, 19 insertions(+), 36 deletions(-) diff --git a/README.md b/README.md index 80e48a6..a0aa41a 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ Python library for the Mega.co.nz API, currently supporting: - downloading - deleting - searching - + This is a work in progress, further functionality coming shortly. @@ -28,21 +28,15 @@ This is a work in progress, further functionality coming shortly. files = m.get_files() -### Get single account file +### Upload a file, and get its public link - file = m.get_file('utYjgSTQ') - -### Upload a file - - m.upload('myfile.doc') + file = m.upload('myfile.doc') + m.get_link(file) ### Download a file from URL or it's ID,key combo m.download('utYjgSTQ','OM4U3V5v_W4N5edSo0wolg1D5H0fwSrLD3oLnLuS9pc') m.download_url('https://mega.co.nz/#!utYjgSTQ!OM4U3V5v_W4N5edSo0wolg1D5H0fwSrLD3oLnLuS9pc') -### Get a public link for account file - m.get_link('utYjgSTQ') - ### Search account for a file m.find('myfile.doc') diff --git a/mega.py b/mega.py index 43d9ef6..779fddd 100644 --- a/mega.py +++ b/mega.py @@ -9,6 +9,7 @@ import binascii import requests import errors from crypto import * +import ast class Mega(object): @@ -91,30 +92,19 @@ class Mega(object): files_dict[file['h']] = self.process_file(file) return files_dict - 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): + def get_link(self, file): ''' Get a files public link including decrypted key + Currently only works if passed upload() output ''' - 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') + if 'f' in file: + file = file['f'][0] + public_handle = self.api_request({'a': 'l', 'n': file['h']}) + file_key = file['k'][file['k'].index(':') + 1:] + decrypted_key = a32_to_base64(decrypt_key(base64_to_a32(file_key), self.master_key)) + return '{0}://{1}/#!{2}!{3}'.format(self.schema, self.domain, public_handle, decrypted_key) else: - raise errors.ValidationError('File not found') + raise ValueError('This function requires upload() file object') def download_url(self, url): path = self.parse_url(url).split('!') diff --git a/tests/test.py b/tests/test.py index ec55448..d230e97 100644 --- a/tests/test.py +++ b/tests/test.py @@ -21,15 +21,14 @@ 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')) + ##get file's public link + #NOTE: currently this only works with upload() file obj, as below + file = m.upload('test.py') + print(m.get_link(file)) + ##trash a file, by id or url #print(m.delete('f14U0JhD')) #print(m.delete_url('https://mega.co.nz/#!f14U0JhD!S_2k-EvB5U1N3s0vm3I5C0JN2toHSGkVf0UxQsiKZ8A'))