mega.py icon indicating copy to clipboard operation
mega.py copied to clipboard

Errors with the import_public_url() Function

Open knowledgeorange opened this issue 4 years ago • 3 comments

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 : image " 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!

knowledgeorange avatar Jul 07 '20 09:07 knowledgeorange

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!

uberBuilder24 avatar Mar 08 '21 00:03 uberBuilder24

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!

flolep2607 avatar May 17 '21 18:05 flolep2607

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!

snoofox avatar Jan 21 '24 13:01 snoofox