Added optional save destination path to download functions
This commit is contained in:
parent
2493a10b88
commit
8dcd2d37eb
3 changed files with 17 additions and 10 deletions
|
@ -33,10 +33,11 @@ This is a work in progress, further functionality coming shortly.
|
|||
file = m.upload('myfile.doc')
|
||||
m.get_upload_link(file)
|
||||
|
||||
### Download a file from URL or file obj
|
||||
### Download a file from URL or file obj, optionally specify destination fold
|
||||
file = m.find('myfile.doc')
|
||||
m.download(file)
|
||||
m.download_url('https://mega.co.nz/#!utYjgSTQ!OM4U3V5v_W4N5edSo0wolg1D5H0fwSrLD3oLnLuS9pc')
|
||||
m.download(file, '/home/john-smith/Desktop')
|
||||
|
||||
### Search account for a file, and get its public link
|
||||
file = m.find('myfile.doc')
|
||||
|
|
14
mega.py
14
mega.py
|
@ -131,21 +131,21 @@ class Mega(object):
|
|||
else:
|
||||
raise errors.ValidationError('File id and key must be present')
|
||||
|
||||
def download_url(self, url):
|
||||
def download_url(self, url, dest_path=None):
|
||||
'''
|
||||
Download a file by it's public url
|
||||
'''
|
||||
path = self.parse_url(url).split('!')
|
||||
file_id = path[0]
|
||||
file_key = path[1]
|
||||
self.download_file(file_id, file_key, is_public=True)
|
||||
self.download_file(file_id, file_key, dest_path, is_public=True)
|
||||
|
||||
def download(self, file):
|
||||
def download(self, file, dest_path=None):
|
||||
'''
|
||||
Download a file by it's file object
|
||||
'''
|
||||
url = self.get_link(file)
|
||||
self.download_url(url)
|
||||
self.download_url(url, dest_path)
|
||||
|
||||
def parse_url(self, url):
|
||||
#parse file id and key from url
|
||||
|
@ -222,7 +222,7 @@ class Mega(object):
|
|||
return node
|
||||
|
||||
|
||||
def download_file(self, file_handle, file_key, is_public=False):
|
||||
def download_file(self, file_handle, file_key, dest_path=None, is_public=False):
|
||||
if is_public:
|
||||
file_key = base64_to_a32(file_key)
|
||||
file_data = self.api_request({'a': 'g', 'g': 1, 'p': file_handle})
|
||||
|
@ -245,6 +245,10 @@ class Mega(object):
|
|||
file_url)
|
||||
|
||||
input_file = requests.get(file_url, stream=True).raw
|
||||
|
||||
if dest_path:
|
||||
output_file = open(dest_path + '/' + file_name, 'wb')
|
||||
else:
|
||||
output_file = open(file_name, 'wb')
|
||||
|
||||
counter = Counter.new(
|
||||
|
|
|
@ -41,9 +41,11 @@ def test():
|
|||
#trash a file by it's id
|
||||
print(m.delete(file[1]['k']))
|
||||
|
||||
##download file, by id+key or url
|
||||
#file = m.find('myfile.doc')
|
||||
##download file
|
||||
#file = m.find('test.py')
|
||||
#m.download(file)
|
||||
##specify destination folder
|
||||
#m.download(file, '/home/user_name/Desktop')
|
||||
#m.download_url('https://mega.co.nz/#!6hBW0R4a!By7-Vjj5xal8K5w_IXH3PlGNyZ1VvIrjZkOmHGq1X00')
|
||||
|
||||
|
||||
|
|
Loading…
Reference in a new issue