Added file search function and get public link function

This commit is contained in:
richard@richard.do 2013-02-07 18:06:03 +00:00
parent 321aa6524b
commit 1eab67d628
3 changed files with 42 additions and 7 deletions

View file

@ -30,6 +30,12 @@ This is a work in progress, further functionality coming shortly.
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 a file
m.get_link('utYjgSTQ','OM4U3V5v_W4N5edSo0wolg1D5H0fwSrLD3oLnLuS9pc')
### Search for a file
m.find('myfile.doc')
### Trash a file from URL or it's ID ### Trash a file from URL or it's ID
m.delete('utYjgSTQ') m.delete('utYjgSTQ')

17
mega.py
View file

@ -91,6 +91,12 @@ 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_link(self, file_id, file_key):
if file_id and file_key:
return '{0}://{1}#!{2}!{3}'.format(self.schema, self.domain, file_id, file_key)
else:
raise errors.RequestError('Url key missing')
def download_url(self, url): def download_url(self, url):
path = self.parse_url(url).split('!') path = self.parse_url(url).split('!')
file_id = path[0] file_id = path[0]
@ -123,6 +129,17 @@ class Mega(object):
#straight delete by id #straight delete by id
return self.move(file_id, 4) return self.move(file_id, 4)
def find(self, filename):
'''
Return file object from given filename
'''
files = self.get_files()
for file in files.items():
if file[1]['a'] and file[1]['a']['n'] == filename:
return file
def move(self, file_id, target): def move(self, file_id, target):
#TODO node_id improvements #TODO node_id improvements
''' '''

View file

@ -7,30 +7,42 @@ def test():
mega = Mega() mega = Mega()
#login ##login
m = mega.login(email, password) m = mega.login(email, password)
#get user details ##get user details
details = m.get_user() details = m.get_user()
print(details) print(details)
#get account files ##get account files
files = m.get_files() files = m.get_files()
#example iterate over files #example iterate over files
for file in files: for file in files:
if files[file]['a'] != False: if files[file]['a'] != False:
print files[file] print files[file]
#upload file ##upload file
print(m.upload('test.py')) print(m.upload('test.py'))
#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'))
#download file, by id+key or url ##search for a file on mega
files = m.find('test.py')
if files:
#trash a file by it's id
#iterate to trash multiple results
print(m.delete(files[1]['k']))
##download file, by id+key or url
#m.download('6hBW0R4a','By7-Vjj5xal8K5w_IXH3PlGNyZ1VvIrjZkOmHGq1X00') #m.download('6hBW0R4a','By7-Vjj5xal8K5w_IXH3PlGNyZ1VvIrjZkOmHGq1X00')
m.download_url('https://mega.co.nz/#!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__': if __name__ == '__main__':
test() test()