get_link() partially working

master
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
- 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')

30
mega.py
View File

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

View File

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