Rewrite function _parse_url to return tuple.

Every usage of this function was calling split on the output anyway.
This commit is contained in:
Ethan Dalool 2020-03-23 19:49:16 -07:00
parent e7a8aeda01
commit 412cd21189

View file

@ -188,13 +188,17 @@ class Mega:
self.sid = base64_url_encode(sid[:43]) self.sid = base64_url_encode(sid[:43])
def _parse_url(self, url): def _parse_url(self, url):
# parse file id and key from url """
if '!' in url: Given a url like 'https://mega.nz/#!fileid!filekey', return a tuple
match = re.findall(r'/#!(.*)', url) (fileid, filekey).
path = match[0] """
return path # File urls are '#!', Folder urls are '#F!'
else: match = re.findall(r'/#F?!(.*)!(.*)', url)
raise RequestError('Url key missing') if not match:
raise ValidationError('Invalid public url. Should have /#!id!key')
(public_handle, decryption_key) = match[0]
return (public_handle, decryption_key)
def _process_file(self, file, shared_keys): def _process_file(self, file, shared_keys):
if file['t'] in [NODE_TYPE_FILE, NODE_TYPE_DIR]: if file['t'] in [NODE_TYPE_FILE, NODE_TYPE_DIR]:
@ -545,8 +549,7 @@ class Mega:
""" """
Delete a file by its url Delete a file by its url
""" """
path = self._parse_url(url).split('!') (public_handle, decryption_key) = self._parse_url(url)
public_handle = path[0]
file_id = self.get_id_from_public_handle(public_handle) file_id = self.get_id_from_public_handle(public_handle)
return self.move(file_id, NODE_TYPE_TRASH) return self.move(file_id, NODE_TYPE_TRASH)
@ -565,8 +568,7 @@ class Mega:
""" """
Destroy a file by its url Destroy a file by its url
""" """
path = self._parse_url(url).split('!') (public_handle, decryption_key) = self._parse_url(url)
public_handle = path[0]
file_id = self.get_id_from_public_handle(public_handle) file_id = self.get_id_from_public_handle(public_handle)
return self.destroy(file_id) return self.destroy(file_id)
@ -660,12 +662,10 @@ class Mega:
""" """
Download a file by it's public url Download a file by it's public url
""" """
path = self._parse_url(url).split('!') (public_handle, decryption_key) = self._parse_url(url)
file_id = path[0]
file_key = path[1]
return self._download_file( return self._download_file(
file_handle=file_id, file_handle=public_handle,
file_key=file_key, file_key=decryption_key,
dest_path=dest_path, dest_path=dest_path,
dest_filename=dest_filename, dest_filename=dest_filename,
is_public=True, is_public=True,
@ -1017,16 +1017,16 @@ class Mega:
""" """
Get size and name from a public url, dict returned Get size and name from a public url, dict returned
""" """
file_handle, file_key = self._parse_url(url).split('!') (public_handle, decryption_key) = self._parse_url(url)
return self.get_public_file_info(file_handle, file_key) return self.get_public_file_info(public_handle, decryption_key)
def import_public_url(self, url, dest_node=None, dest_name=None): def import_public_url(self, url, dest_node=None, dest_name=None):
""" """
Import the public url into user account Import the public url into user account
""" """
file_handle, file_key = self._parse_url(url).split('!') (public_handle, decryption_key) = self._parse_url(url)
return self.import_public_file( return self.import_public_file(
file_handle, file_key, dest_node=dest_node, dest_name=dest_name public_handle, decryption_key, dest_node=dest_node, dest_name=dest_name
) )
def get_public_file_info(self, file_handle, file_key): def get_public_file_info(self, file_handle, file_key):