mega.py
mega.py copied to clipboard
Errors with the import_public_url() Function
Hello, I'm trying to build an automation to a project of mine - using Mega links of shared files to import the files (and folders) to my account, and to each uploaded foleder/file to create a shared link of my own.
The login function is ok, but when I've tried to use the "import_public_url()" I've encountered a few problems: The URL parsing function is using the wrong split sign ("!" instead of "#") between the Decryption Key and the Link. Also "#!" is needed after the "/folder/" or "/file/" part in the link.
I've tried to edit the module but I didn't succeed, so I changed my links format to fit the module and it worked.
Until - I got this error :
"
Raise RequestError(int_resp)
mega.errors.RequestError: EACCESS, Access violation (e.g., trying to write to a read-only share)
"
I saw in other forums that this error is because "The original owner of this file needs to log in again to re-create keys" or something like that, but when I do this manually through the web I get no errors and the file/folder is imported easily.
Can someone help me in this one?
Thanks!
Hello!
I have the same problem here. It could potentially be due to whether or not you logged in with the mega.login()
function. I think you need to enter your username and password. The problem with my code is if I enter my password, it will be public due to the fact my code is open source, and there is sadly no way of disabling it unless I pay, due to the fact I'm using Repl.it.
Hope that helps!
You can put private variables on replit
Hello! I have the same problem here. It could potentially be due to whether or not you logged in with the
mega.login()
function. I think you need to enter your username and password. The problem with my code is if I enter my password, it will be public due to the fact my code is open source, and there is sadly no way of disabling it unless I pay, due to the fact I'm using Repl.it. Hope that helps!
I have fixed this issue, i know it's too late lol, but i'm still posting the solution in case anyone needs it.
first u need to create ur own custom mega class by inheriting from the Mega.py then u need to modify _parse_url function so that it supports new gen urls of mega.nz, here's the solution:
class CMega(Mega):
def _parse_url(self, url):
"""Parse file id and key from url."""
parsed_url = urlparse(url)
if parsed_url.netloc not in ['mega.nz', 'mega.co.nz']:
raise RequestError('Invalid URL: wrong hostname')
if '/file/' in parsed_url.path:
# New URL structure
file_handle = parsed_url.path.split('/')[-1]
file_key = parsed_url.fragment
return f'{file_handle}!{file_key}'
elif '!' in parsed_url.fragment:
# Old URL structure
split = parsed_url.fragment.split('!')
if len(split) == 3:
file_handle = split[1]
file_key = split[2]
return f'{file_handle}!{file_key}'
else:
raise RequestError('Invalid URL: format not recognized')
else:
raise RequestError('Url key missing')
then import from custom mega class and it should work fine
from .utils import CMega
mega = CMega()
m = mega.login(email, password)
m.import_public_url(shareable_link)
the solution may not be perfect but it works for now!