[Python] How to get the total size of a public MEGA folder URL using python

Discussão em 'Python' iniciado por Stack, Junho 20, 2025.

  1. Stack

    Stack Membro Participativo

    I want to get the total size of all files in a public MEGA folder, given its URL (with decryption key), using Python only. Example: https://mega.nz/folder/...

    I tried using mega.py, but it doesn't seem to support public folders, only public file links.

    python
    Python 3.11.0 (main, Oct 24 2022, 18:26:48) [MSC v.1933 64 bit (AMD64)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> from mega import Mega
    >>> mega = Mega()
    >>> m = mega.login('username', 'passwrod')
    >>> link = "https://mega.nz/folder/[link]#[key]"
    >>> link_info = m.get_public_url_info(link)
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    File "...\AppData\Local\Programs\Python\Python311\Lib\site-packages\mega\mega.py", line 990, in get_public_url_info
    file_handle, file_key = self._parse_url(url).split('!')
    ^^^^^^^^^^^^^^^^^^^^
    File "...\AppData\Local\Programs\Python\Python311\Lib\site-packages\mega\mega.py", line 206, in _parse_url
    raise RequestError('Url key missing')
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "...\AppData\Local\Programs\Python\Python311\Lib\site-packages\mega\errors.py", line 58, in __init__
    code_desc, long_desc = _CODE_TO_DESCRIPTIONS
    Código:
                               ~~~~~~~~~~~~~~~~~~~~~^^^^^^
    KeyError: 'Url key missing'
    
    >>> link = "https://mega.nz/file/[link]#[key]"
    >>> link_info = m.get_public_url_info(link)
    >>> print(link_info)
    {'size': 6732143421, 'name': 'Name.mp4'}
    
    
    The documentation for mega.py is here: [URL]https://github.com/odwyersoftware/mega.py[/URL]
    
    From the code, the _parse_url method only supports parsing file URLs, not folder URLs. Here’s the relevant snippet:
    
    def _parse_url(self, url):
        """Parse file id and key from url."""
        if '/file/' in url:
            # V2 URL structure
            url = url.replace(' ', '')
            file_id = re.findall(r'\W\w\w\w\w\w\w\w\w\W', url)[0][1:-1]
            id_index = re.search(file_id, url).end()
            key = url[id_index + 1:]
            return f'{file_id}!{key}'
        elif '!' in url:
            # V1 URL structure
            match = re.findall(r'/#!(.*)', url)
            path = match[0]
            return path
        else:
            raise RequestError('Url key missing')
    
    
    I tried other tools like: mega-cmd, rclone, megatool,etc... but none seem to support getting the size of a public folder URL.
    
    Also, The official mega-cmd documentation is here: [URL]https://github.com/meganz/MEGAcmd[/URL] I haven’t found support for public folder size queries either.
    
    Is there any Python based way to get the file sizes from a public folder?
    
    Thanks in advance.
    
    [url="https://stackoverflow.com/questions/79673468/how-to-get-the-total-size-of-a-public-mega-folder-url-using-python"]Continue reading...[/url]

Compartilhe esta Página