get_link() partially working

This commit is contained in:
richard@richard.do 2013-02-07 23:30:37 +00:00
parent a28031ca47
commit 3cf4584be0
3 changed files with 19 additions and 36 deletions

View file

@ -6,7 +6,7 @@ Python library for the Mega.co.nz API, currently supporting:
- downloading - downloading
- deleting - deleting
- searching - searching
This is a work in progress, further functionality coming shortly. 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() files = m.get_files()
### Get single account file ### Upload a file, and get its public link
file = m.get_file('utYjgSTQ') file = m.upload('myfile.doc')
m.get_link(file)
### Upload a file
m.upload('myfile.doc')
### Download a file from URL or it's ID,key combo ### Download a file from URL or it's ID,key combo
m.download('utYjgSTQ','OM4U3V5v_W4N5edSo0wolg1D5H0fwSrLD3oLnLuS9pc') m.download('utYjgSTQ','OM4U3V5v_W4N5edSo0wolg1D5H0fwSrLD3oLnLuS9pc')
m.download_url('https://mega.co.nz/#!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 ### Search account for a file
m.find('myfile.doc') m.find('myfile.doc')

30
mega.py
View file

@ -9,6 +9,7 @@ import binascii
import requests import requests
import errors import errors
from crypto import * from crypto import *
import ast
class Mega(object): class Mega(object):
@ -91,30 +92,19 @@ class Mega(object):
files_dict[file['h']] = self.process_file(file) files_dict[file['h']] = self.process_file(file)
return files_dict return files_dict
def get_file(self, file_id): def get_link(self, file):
'''
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 Get a files public link including decrypted key
Currently only works if passed upload() output
''' '''
file = self.get_file(file_id) if 'f' in file:
if file: file = file['f'][0]
file_key = file[1]['k'] public_handle = self.api_request({'a': 'l', 'n': file['h']})
if file_id and file_key: file_key = file['k'][file['k'].index(':') + 1:]
public_handle = self.api_request({'a': 'l', 'n': file_id}) decrypted_key = a32_to_base64(decrypt_key(base64_to_a32(file_key), self.master_key))
decrypted_key = a32_to_base64(file_key) return '{0}://{1}/#!{2}!{3}'.format(self.schema, self.domain, public_handle, decrypted_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: else:
raise errors.ValidationError('File not found') raise ValueError('This function requires upload() file object')
def download_url(self, url): def download_url(self, url):
path = self.parse_url(url).split('!') path = self.parse_url(url).split('!')

View file

@ -21,15 +21,14 @@ def test():
if files[file]['a'] != False: if files[file]['a'] != False:
print files[file] print files[file]
##get single file
#print(m.get_file('f14U0JhD'))
##get file's public link
#print(m.get_link('ChZCXTzA'))
##upload file ##upload file
print(m.upload('test.py')) 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 ##trash a file, by id or url
#print(m.delete('f14U0JhD')) #print(m.delete('f14U0JhD'))
#print(m.delete_url('https://mega.co.nz/#!f14U0JhD!S_2k-EvB5U1N3s0vm3I5C0JN2toHSGkVf0UxQsiKZ8A')) #print(m.delete_url('https://mega.co.nz/#!f14U0JhD!S_2k-EvB5U1N3s0vm3I5C0JN2toHSGkVf0UxQsiKZ8A'))