get_link() working, added get_file() for single file

master
richard@richard.do 2013-02-07 19:44:57 +00:00
parent 7ed5bcee43
commit b91f84cabd
3 changed files with 43 additions and 27 deletions

View File

@ -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

37
mega.py
View File

@ -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):
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:
return '{0}://{1}#!{2}!{3}'.format(self.schema, self.domain, file_id, 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.RequestError('Url key missing')
raise errors.ValidationError('File id and key must be set')
else:
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:

View File

@ -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()