Skip to content

Bulk asynchronous downloader for multiple file hosts

refactor: external mega (#3)

NTFSvolume17236631b9e2
diff --git a/cyberdrop_dl/crawlers/mega_nz.py b/cyberdrop_dl/crawlers/mega_nz.py
index 5e0a7c4aa..03137f765 100644
--- a/cyberdrop_dl/crawlers/mega_nz.py
+++ b/cyberdrop_dl/crawlers/mega_nz.py
@@ -7,26 +7,24 @@ It calls checks_complete_by_referer several times even if no request is going to
from __future__ import annotations
-from typing import TYPE_CHECKING, Any, ClassVar, NamedTuple, cast
+from typing import TYPE_CHECKING, ClassVar
+
+from mega.api import MegaAPI
+from mega.core import MegaCore
+from mega.crypto import b64_to_a32
+from mega.data_structures import Crypto
from cyberdrop_dl.crawlers.crawler import Crawler, DBPathBuilder, SupportedDomains, SupportedPaths, auto_task_id
from cyberdrop_dl.data_structures.url_objects import AbsoluteHttpURL
-from cyberdrop_dl.downloader import mega_nz as mega
+from cyberdrop_dl.downloader.mega_nz import MegaDownloader
from cyberdrop_dl.exceptions import LoginError, ScrapeError
from cyberdrop_dl.utils.utilities import error_handling_wrapper
if TYPE_CHECKING:
- from pathlib import Path
+ from mega.filesystem import FileSystem
from cyberdrop_dl.data_structures.url_objects import ScrapeItem
-PRIMARY_URL = AbsoluteHttpURL("https://mega.nz")
-
-
-class FileTuple(NamedTuple):
- id: str
- crypto: mega.DecryptData
-
class MegaNzCrawler(Crawler):
SUPPORTED_DOMAINS: ClassVar[SupportedDomains] = "mega.io", "mega.nz"
@@ -43,15 +41,15 @@ class MegaNzCrawler(Crawler):
"Subfolder": "/folder/<folder_id>#<share_key>/folder/<subfolder_id>",
"**NOTE**": "Downloads can not be resumed. Partial downloads will always be deleted and new downloads will start over",
}
- PRIMARY_URL: ClassVar[AbsoluteHttpURL] = PRIMARY_URL
+ PRIMARY_URL: ClassVar[AbsoluteHttpURL] = AbsoluteHttpURL("https://mega.nz")
SKIP_PRE_CHECK: ClassVar[bool] = True
DOMAIN: ClassVar[str] = "mega.nz"
FOLDER_DOMAIN: ClassVar[str] = "MegaNz"
- OLD_DOMAINS = ("mega.co.nz",)
+ OLD_DOMAINS: ClassVar[tuple[str, ...]] = ("mega.co.nz",)
create_db_path = staticmethod(DBPathBuilder.path_qs_frag)
- def __post_init__(self) -> None:
- self.downloader: mega.MegaDownloader
+ core: MegaCore
+ downloader: MegaDownloader
@property
def user(self) -> str | None:
@@ -61,139 +59,92 @@ class MegaNzCrawler(Crawler):
def password(self) -> str | None:
return self.manager.auth_config.meganz.password or None
- def _init_downloader(self) -> mega.MegaDownloader:
- self.downloader = dl = mega.MegaDownloader(self.manager, self.DOMAIN) # type: ignore[reportIncompatibleVariableOverride]
+ def _init_downloader(self) -> MegaDownloader:
+ self.core = MegaCore(MegaAPI(self.manager.client_manager._session))
+ self.downloader = dl = MegaDownloader(self.manager, self.DOMAIN) # type: ignore[reportIncompatibleVariableOverride]
dl.startup()
return dl
async def async_startup(self) -> None:
- await self.login(PRIMARY_URL)
+ await self.login(self.PRIMARY_URL)
async def fetch(self, scrape_item: ScrapeItem) -> None:
if not self.logged_in:
return
- if frag := scrape_item.url.fragment: # Mega stores access key in fragment. We can't do anything without the key
- # v1 URLs
- if frag.count("!") == 2:
- if frag.startswith("F!"):
- folder_id, _, shared_key = frag.removeprefix("F!").partition("!")
- return await self.folder(scrape_item, folder_id, shared_key)
- if frag.startswith("!"):
- # https://mega.nz/#!Ue5VRSIQ!kC2E4a4JwfWWCWYNJovGFHlbz8F
- file_id, _, shared_key = frag.removeprefix("!").partition("!")
- return await self.file(scrape_item, file_id, shared_key)
-
- # v2 URLs
- match scrape_item.url.parts[1:]:
- # https://mega.nz/folder/oZZxyBrY#oU4jASLPpJVvqGHJIMRcgQ/file/IYZABDGY
- # https://mega.nz/folder/oZZxyBrY#oU4jASLPpJVvqGHJIMRcgQ
- case ["folder", folder_id]:
- root_id = file_id = None
- shared_key, *rest = frag.split("/")
- if rest:
- match rest:
- case ["folder", id_]:
- root_id = id_
- case ["file", id_]:
- file_id = id_
- case _:
- raise ValueError
- return await self.folder(scrape_item, folder_id, shared_key, root_id or None, file_id or None)
- # https://mega.nz/file/cH51DYDR#qH7QOfRcM-7N9riZWdSjsRq
- case ["file", file_id]:
- return await self.file(scrape_item, file_id, frag)
-
- raise ValueError
+ info = self.core.parse_url(scrape_item.url)
+ if not info.is_folder:
+ return await self.file(scrape_item, info.public_handle, info.public_key)
+
+ await self.folder(scrape_item, info.public_handle, info.public_key, info.selected_folder, info.selected_file)
@error_handling_wrapper
- async def file(self, scrape_item: ScrapeItem, file_id: str, shared_key: str) -> None:
- canonical_url = (PRIMARY_URL / "file" / file_id).with_fragment(shared_key)
+ async def file(self, scrape_item: ScrapeItem, handle: str, public_key: str) -> None:
+ canonical_url = (self.PRIMARY_URL / "file" / handle).with_fragment(public_key)
if await self.check_complete_from_referer(canonical_url):
return
+
scrape_item.url = canonical_url
- full_key = mega.base64_to_a32(shared_key)
- crypto = mega.get_decrypt_data(mega.NodeType.FILE, full_key)
- file = FileTuple(file_id, crypto)
- await self._process_file(scrape_item, file)
+ full_key = b64_to_a32(public_key)
+ await self._process_file(scrape_item, handle, Crypto.decompose(full_key))
@error_handling_wrapper
- async def _process_file(self, scrape_item: ScrapeItem, file: FileTuple, *, folder_id: str | None = None) -> None:
- file_data = await self._get_file_info(file.id, folder_id)
- decrypt_data = file.crypto._replace(file_size=file_data["s"])
- self.downloader.register(scrape_item.url, decrypt_data)
- file_url = self.parse_url(file_data["g"])
- attribs_bytes = mega.base64_url_decode(file_data["at"])
- filename = mega.decrypt_attr(attribs_bytes, file.crypto.k)["n"]
- filename, ext = self.get_filename_and_ext(filename)
+ async def _process_file(
+ self,
+ scrape_item: ScrapeItem,
+ handle: str,
+ crypto: Crypto,
+ *,
+ folder_id: str | None = None,
+ ) -> None:
+ resp = await self.core.request_file_info(handle, folder_id, is_public=not folder_id)
+ if not resp.url:
+ raise ScrapeError(410, "File not accessible anymore")
+
+ name = self.core.decrypt_attrs(resp._at, crypto.key).name
+ self.downloader.register(scrape_item.url, crypto, resp.size)
+ file_url = self.parse_url(resp.url)
+ filename, ext = self.get_filename_and_ext(name)
await self.handle_file(scrape_item.url, scrape_item, filename, ext, debrid_link=file_url)
_process_file_task = auto_task_id(_process_file)
- async def _get_file_info(self, file_id: str, folder_id: str | None) -> dict[str, Any]:
- data = {"a": "g", "g": 1}
- if folder_id:
- data = data | {"n": file_id}
- query_params = {"n": folder_id}
- else:
- data = data | {"p": file_id}
- query_params = None
-
- file_data: dict[str, Any] = await self.downloader.api.request(data, query_params)
- if "g" not in file_data:
- raise ScrapeError(410, "File not accessible anymore")
- return file_data
-
@error_handling_wrapper
async def folder(
self,
scrape_item: ScrapeItem,
- folder_id: str,
- shared_key: str,
+ handle: str,
+ public_key: str,
root_id: str | None = None,
single_file_id: str | None = None,
) -> None:
if single_file_id and await self.check_complete_from_referer(scrape_item.url):
return
- nodes = await self.downloader.api.get_nodes_in_shared_folder(folder_id, shared_key)
- root_id = root_id or next(iter(nodes))
- folder_name = nodes[root_id]["attributes"]["n"]
- filesystem = await self.downloader.api.build_file_system(nodes, [root_id])
- title = self.create_title(folder_name, folder_id)
- scrape_item.setup_as_album(title, album_id=folder_id)
- canonical_url = (PRIMARY_URL / "folder" / folder_id).with_fragment(shared_key)
+
+ selected_node = root_id or single_file_id
+ fs = await self.core.get_public_filesystem(handle, public_key)
+ root = next(iter(fs))
+ title = self.create_title(root.attributes.name, handle)
+ scrape_item.setup_as_album(title, album_id=handle)
+ canonical_url = (self.PRIMARY_URL / "folder" / handle).with_fragment(public_key)
scrape_item.url = canonical_url
- await self._process_folder_filesystem(scrape_item, filesystem, single_file_id)
+ await self._process_fs(scrape_item, fs, selected_node)
- async def _process_folder_filesystem(
- self,
- scrape_item: ScrapeItem,
- filesystem: dict[Path, mega.Node],
- single_file_id: str | None,
- ) -> None:
- folder_id, shared_key = scrape_item.url.name, scrape_item.url.fragment
-
- def filter_files():
- for path, node in filesystem.items():
- if node["t"] != mega.NodeType.FILE:
- continue
- if single_file_id and node["h"] != single_file_id:
- continue
- yield path, cast("mega.File", node)
-
- for path, file in filter_files():
- file_id = file["h"]
- file_fragment = f"{shared_key}/file/{file_id}"
+ async def _process_fs(self, scrape_item: ScrapeItem, filesystem: FileSystem, selected_node_id: str | None) -> None:
+ folder_id, public_key = scrape_item.url.name, scrape_item.url.fragment
+
+ for file in filesystem.files_from(selected_node_id):
+ path = filesystem.relative_path(file.id)
+ file_fragment = f"{public_key}/file/{file.id}"
canonical_url = scrape_item.url.with_fragment(file_fragment)
if await self.check_complete_from_referer(canonical_url):
continue
- new_scrape_item = scrape_item.create_child(canonical_url, possible_datetime=file["ts"])
+ child_item = scrape_item.create_child(canonical_url, possible_datetime=file.created_at)
for part in path.parent.parts[1:]:
- new_scrape_item.add_to_parent_title(part)
+ child_item.add_to_parent_title(part)
- file = FileTuple(file_id, mega.DecryptData(file["k_decrypted"], file["iv"], file["meta_mac"]))
- self.create_task(self._process_file_task(new_scrape_item, file, folder_id=folder_id))
+ self.create_task(self._process_file_task(child_item, file.id, file._crypto, folder_id=folder_id))
scrape_item.add_children()
@error_handling_wrapper
@@ -202,7 +153,7 @@ class MegaNzCrawler(Crawler):
# TODO: Add a way to cache this login
# TODO: Show some logging message / UI about login
try:
- await self.downloader.api.login(self.user, self.password)
+ await self.core.login(self.user, self.password)
self.logged_in = True
except Exception as e:
self.disabled = True
diff --git a/cyberdrop_dl/crawlers/transfer_it.py b/cyberdrop_dl/crawlers/transfer_it.py
index ef9469f28..982e008ed 100644
--- a/cyberdrop_dl/crawlers/transfer_it.py
+++ b/cyberdrop_dl/crawlers/transfer_it.py
@@ -1,71 +1,18 @@
from __future__ import annotations
-import dataclasses
-from typing import TYPE_CHECKING, Any, ClassVar, cast
+from typing import TYPE_CHECKING, ClassVar
+
+from mega.transfer_it import TransferItClient
from cyberdrop_dl.crawlers.crawler import Crawler, DBPathBuilder, SupportedPaths
from cyberdrop_dl.data_structures.url_objects import AbsoluteHttpURL
-from cyberdrop_dl.downloader import mega_nz as mega
from cyberdrop_dl.utils.utilities import error_handling_wrapper
if TYPE_CHECKING:
- from pathlib import Path
+ from mega.data_structures import Node
+ from mega.filesystem import FileSystem
from cyberdrop_dl.data_structures.url_objects import ScrapeItem
- from cyberdrop_dl.managers.manager import Manager
-
-
[email protected](slots=True, frozen=True)
-class File:
- name: str
- id: str
- transfer_id: str
- timestamp: int | None
- password: str | None = None
-
-
-class TransferItAPI(mega.MegaApi):
- def __init__(self, manager: Manager) -> None:
- super().__init__(manager)
- # TODO: Use an URL on the base API
- self.entrypoint = "https://bt7.api.mega.co.nz/cs"
- self._url = AbsoluteHttpURL(self.entrypoint)
- self.shared_keys: mega.SharedkeysDict = {}
- self._default_params = {"v": 3, "domain": "transferit", "lang": "en", "bc": 1}
-
- async def request(
- self,
- data_input: list[dict[str, Any]] | dict[str, Any],
- add_params: dict[str, Any] | None = None,
- ) -> Any:
- add_params = self._default_params | (add_params or {})
- return await super().request(data_input, add_params)
-
- async def get_transfer_nodes(self, transfer_id: str) -> dict[str, mega.FileOrFolder]:
- folder: mega.Folder = await self.request(
- {"a": "f", "c": 1, "r": 1, "xnc": 1},
- {"x": transfer_id},
- )
- nodes = await self._process_nodes(folder["f"])
- return cast("dict[str, mega.FileOrFolder]", nodes)
-
- def _process_node(self, node: mega.Node) -> mega.Node: # pyright: ignore[reportIncompatibleMethodOverride]
- node_type = node["t"]
- if node_type == mega.NodeType.FILE or node_type == mega.NodeType.FOLDER:
- attributes_bytes = mega.base64_url_decode(node["a"])
- full_key = mega.base64_to_a32(node["k"])
- crypto = mega.get_decrypt_data(node_type, full_key)
- attributes = mega.decrypt_attr(attributes_bytes, crypto.k)
- node["attributes"] = cast("mega.Attributes", attributes) # pyright: ignore[reportInvalidCast]
- return node
-
- def get_download_link(self, file: File, *, direct: bool = True) -> AbsoluteHttpURL:
- if not direct:
- raise NotImplementedError
- url = (self._url / "g").with_query(x=file.transfer_id, n=file.id, fn=file.name)
- if file.password:
- return url.update_query(pw=file.password)
- return url
class TransferItCrawler(Crawler):
@@ -74,8 +21,10 @@ class TransferItCrawler(Crawler):
DOMAIN: ClassVar[str] = "transfer.it"
create_db_path = staticmethod(DBPathBuilder.path_qs_frag)
- def __post_init__(self) -> None:
- self.api = TransferItAPI(self.manager)
+ core: TransferItClient
+
+ async def async_startup(self) -> None:
+ self.core = TransferItClient(self.manager.client_manager._session)
async def fetch(self, scrape_item: ScrapeItem) -> None:
match scrape_item.url.parts[1:]:
@@ -87,45 +36,29 @@ class TransferItCrawler(Crawler):
@error_handling_wrapper
async def transfer(self, scrape_item: ScrapeItem, transfer_id: str) -> None:
# TODO: handle expired links and password protected links
- nodes = await self.api.get_transfer_nodes(transfer_id)
- root_id = next(iter(nodes))
- root_name = nodes[root_id]["attributes"]["n"]
- filesystem = await self.api.build_file_system(nodes, [root_id])
- title = self.create_title(root_name, transfer_id)
+ fs = await self.core.get_filesystem(transfer_id)
+ root = next(iter(fs))
+ title = self.create_title(root.attributes.name, transfer_id)
scrape_item.setup_as_album(title, album_id=transfer_id)
- self._process_filesystem(scrape_item, filesystem, transfer_id)
+ self._process_filesystem(scrape_item, fs, transfer_id)
- def _process_filesystem(self, scrape_item: ScrapeItem, filesystem: dict[Path, mega.Node], transfer_id: str) -> None:
+ def _process_filesystem(self, scrape_item: ScrapeItem, fs: FileSystem, transfer_id: str) -> None:
password = scrape_item.url.query.get("pw") or scrape_item.password
- def filter_files():
- for path, node in filesystem.items():
- if node["t"] != mega.NodeType.FILE:
- continue
-
- attrs = node["attributes"]
- timestamp = node.get("ts") or attrs.get("t")
- file = File(
- name=attrs["n"],
- id=node["h"],
- transfer_id=transfer_id,
- password=password,
- timestamp=timestamp if isinstance(timestamp, int) else None,
- )
- yield path, file
-
- for path, file in filter_files():
+ for file in fs.files:
+ path = fs.relative_path(file.id)
canonical_url = scrape_item.url.with_fragment(file.id)
new_scrape_item = scrape_item.create_child(canonical_url)
for part in path.parent.parts[1:]:
new_scrape_item.add_to_parent_title(part)
- self.create_task(self._file(new_scrape_item, file))
+ dl_link = self.core.create_download_url(transfer_id, file, password)
+ self.create_task(self._file(new_scrape_item, file, dl_link))
scrape_item.add_children()
@error_handling_wrapper
- async def _file(self, scrape_item: ScrapeItem, file: File) -> None:
- link = self.api.get_download_link(file)
- filename, ext = self.get_filename_and_ext(file.name)
- scrape_item.possible_datetime = file.timestamp
- await self.handle_file(link, scrape_item, file.name, ext, custom_filename=filename)
+ async def _file(self, scrape_item: ScrapeItem, file: Node, dl_link: str) -> None:
+ link = self.parse_url(dl_link)
+ filename, ext = self.get_filename_and_ext(file.attributes.name)
+ scrape_item.possible_datetime = file.created_at
+ await self.handle_file(link, scrape_item, file.attributes.name, ext, custom_filename=filename)
diff --git a/cyberdrop_dl/downloader/mega_nz.py b/cyberdrop_dl/downloader/mega_nz.py
index ed465599b..73f47b330 100644
--- a/cyberdrop_dl/downloader/mega_nz.py
+++ b/cyberdrop_dl/downloader/mega_nz.py
@@ -1,829 +1,29 @@
-# This file incorporates work covered by the Apache License 2.0,
-#
-# Original Apache repo: https://github.com/odwyersoftware/mega.py
-#
-# Original Apache License 2.0 Header:
-#
-# Copyright 2019 richardARPANET
-#
-# Licensed to the Apache Software Foundation (ASF) under one
-# or more contributor license agreements. See the NOTICE file
-# distributed with this work for additional information
-# regarding copyright ownership. The ASF licenses this file
-# to you under the Apache License, Version 2.0 (the
-# "License"); you may not use this file except in compliance
-# with the License. You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing,
-# software distributed under the License is distributed on an
-# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-# KIND, either express or implied. See the License for the
-# specific language governing permissions and limitations
-# under the License.
-#
-# Modifications 04-2025, licensed under the GNU General Public License version 3 (GPLv3):
-#
-# - Fixed login request parameters for the new v2 accounts
-# - Fixed RSA key computation.
-# - Fixed attribute encoding issues.
-# - Fixed AES CBC decryption without proper padding.
-# - Fixed filename encoding detection.
-# - Fixed incomplete node information when querying a folder.
-# - Fixed permission error caused by concurrent access to the same file.
-# - Simplified node attributes processing logic by deprecating deduntan tuple Node class
-# - Added xhashcash computation to handle mega challenges while logging.
-# - Replaced all code logic with asynchronous methods.
-# - Added support for downloading entire folders.
-# - Updated primary domain, mega.co.nz to mega.nz.
-# - Defined custom classes for nodes, file and folders.
-# - Added type annotations
-# - Used memory view to increase performance of MAC computation.
-# - Update syntax and dependencies to support for Python 3.9+.
-# - Separated API and client logic.
-# - Added a build_file_system method.
-# - Replaced the third-party pathlib library with the built-in pathlib
-
-
from __future__ import annotations
import asyncio
-import base64
-import binascii
-import hashlib
-import json
-import math
-import random
-import string
-import struct
-from collections.abc import Callable, Coroutine, Sequence
-from enum import IntEnum
-from http import HTTPStatus
-from pathlib import Path
-from typing import TYPE_CHECKING, Any, Final, Literal, NamedTuple, NotRequired, TypeAlias, TypedDict, TypeVar, cast
+from typing import TYPE_CHECKING, Any
import aiofiles
-import aiohttp
-from aiohttp import ClientSession, ClientTimeout
-from aiolimiter import AsyncLimiter
-from Crypto.Cipher import AES
-from Crypto.Math.Numbers import Integer
-from Crypto.PublicKey import RSA
-from Crypto.Util import Counter
+from mega.chunker import MegaChunker, get_chunks
from cyberdrop_dl.clients.download_client import DownloadClient
from cyberdrop_dl.downloader.downloader import Downloader
-from cyberdrop_dl.exceptions import CDLBaseError, DownloadError, create_error_msg
-from cyberdrop_dl.utils.logger import log
if TYPE_CHECKING:
- from collections.abc import Generator, Mapping
+ from collections.abc import Coroutine
+ import aiohttp
+ from mega.data_structures import Crypto
from yarl import URL
from cyberdrop_dl.data_structures.url_objects import MediaItem
from cyberdrop_dl.managers.manager import Manager
-T = TypeVar("T")
-Array: TypeAlias = list[T] | tuple[T, ...]
-U32Int: TypeAlias = int
-U32IntArray: TypeAlias = Array[U32Int]
-U32IntSequence: TypeAlias = Sequence[U32Int]
-U32IntTupleArray: TypeAlias = tuple[U32Int, ...]
-AnyDict: TypeAlias = dict[str, Any]
-
-
-class MegaNzError(CDLBaseError):
- def __init__(self, error: str | int, **kwargs) -> None:
- super().__init__(f"MegaNZ Error ({error})", **kwargs)
-
-
-class RequestError(MegaNzError):
- """Error in API request."""
-
- def __init__(self, msg: str | int) -> None:
- self.code = code = msg if isinstance(msg, int) else None
-
- if code is None:
- super().__init__(error=msg, message=msg)
- return
-
- name, message = ERROR_CODES[code]
- ui_failure = f"{name}[{code}]"
- super().__init__(ui_failure, message=message)
- if http_code := _HTTP_ERRORS_OVERRIDES.get(code):
- self.ui_failure = create_error_msg(http_code)
-
-
-ERROR_CODES: Final = {
- -1: ("EINTERNAL", "An internal error has occurred"),
- -2: ("EARGS", "You have passed invalid arguments to this command"),
- -3: ("EAGAIN", "Request failed. No data was altered. Please retry"),
- -4: ("ERATELIMIT", "Rate limited. Please wait a few seconds, then try again"),
- -5: ("EFAILED", "The upload failed. Please restart it from scratch"),
- -6: ("ETOOMANY", "Too many concurrent connections or transfers"),
- -7: ("ERANGE", "The upload file packet is out of range or not starting and ending on a chunk boundary"),
- -8: ("EEXPIRED", "The URL has expired"),
- -9: ("ENOENT", "Resource not found"),
- -10: ("ECIRCULAR", "Circular linkage attempted"),
- -11: ("EACCESS", "Access violation (e.g., trying to write to a read-only share)"),
- -12: ("EEXIST", "Trying to create an object that already exists"),
- -13: ("EINCOMPLETE", "Trying to access an incomplete resource"),
- -14: ("EKEY", "Cryptographic error, invalid key"), # Only used within the client. Never returned by the API
- -15: ("ESID", "Invalid or expired user session, please relogin"),
- -16: (
- "EBLOCKED",
- "File can't be downloaded as it violates our Terms of Service",
- ), # or Suspended account during login
- -17: ("EOVERQUOTA", "Request exceeds transfer quota"),
- -18: ("ETEMPUNAVAIL", "Resource temporarily not available, please try again later"),
- -19: ("ETOOMANYCONNECTIONS", "Too many connections"),
- -24: ("EGOINGOVERQUOTA", "Not enough quota"),
- -25: ("EROLLEDBACK", "Request rolled back"),
- -26: ("EMFAREQUIRED", "Multi-Factor Authentication Required"),
- -27: ("EMASTERONLY", "Access denied for sub-users"),
- -28: ("EBUSINESSPASTDUE", "Business account expired"),
- -29: ("EPAYWALL", "Over Disk Quota Paywall"),
- -400: ("ETOOERR", "Too many concurrent errors"),
- -401: ("ESHAREROVERQUOTA", "Share owner is over storage quota"),
-}
-
-_BANDWIDTH_LIMIT_EXCEEDED: Final = 509
-
-_HTTP_ERRORS_OVERRIDES: Final = {
- -9: HTTPStatus.GONE,
- -16: HTTPStatus.FORBIDDEN,
- -24: _BANDWIDTH_LIMIT_EXCEEDED,
- -401: _BANDWIDTH_LIMIT_EXCEEDED,
-}
-
-
-CHUNK_BLOCK_LEN = 16 # Hexadecimal
-EMPTY_IV = b"\0" * CHUNK_BLOCK_LEN
-
-
-class Chunk(NamedTuple):
- offset: int
- size: int
-
-
-class Attributes(TypedDict):
- n: str # name
- c: str # encoded hash # TODO: add decode logic
- t: str | int # encoded tags or timestamp
-
-
-class NodeType(IntEnum):
- DUMMY = -1
- FILE = 0
- FOLDER = 1
- ROOT_FOLDER = 2
- INBOX = 3
- TRASH = 4
-
-
-class Node(TypedDict):
- t: NodeType # type
- h: str # id (aka handle)
- p: str # parent id
- a: str # attributes (within this: 'n' Name)
- k: str # key
- u: str # user id
- ts: int # creation date (timestamp)
- g: NotRequired[str] # Access URL
-
- # Non standard properties, only used internally
- attributes: Attributes # Decrypted attributes
- k_decrypted: U32IntTupleArray
- # full key, computed from value of `k` and the master key of the owner. This is the public access key
- key_decrypted: U32IntTupleArray
-
-
-class FileOrFolder(Node):
- su: NotRequired[str] # shared user id, only present in shared files / folder. The id of the owner
- sk: NotRequired[str] # shared key. It's the base64 of `key_decrypted`
-
- # Non standard properties, only used internally
- iv: U32IntTupleArray
- meta_mac: U32IntTupleArray
- sk_decrypted: U32IntTupleArray
-
-
-class File(FileOrFolder):
- at: str # File specific attributes (encrypted)
- fa: str # File attributes
-
-
-class Folder(FileOrFolder):
- f: list[FileOrFolder] # Children (files or folders)
- ok: list[FileOrFolder]
- s: list[FileOrFolder]
-
-
-SharedKey = dict[str, U32IntTupleArray] # Mapping: (recipient) User Id ('u') -> decrypted value of shared key ('sk')
-SharedkeysDict = dict[str, SharedKey] # Mapping: (owner) Shared User Id ('su') -> SharedKey
-FilesMapping = dict[str, FileOrFolder] # key is parent_id ('p')
-
-
-class DecryptData(NamedTuple):
- k: U32IntTupleArray
- iv: U32IntTupleArray
- meta_mac: U32IntTupleArray
- file_size: int = 0
-
-
-def pad_bytes(data: bytes, length: int = CHUNK_BLOCK_LEN) -> bytes:
- """
- Pads a bytes-like object with null bytes to a multiple of the specified length.
-
- Args:
- data: The bytes-like object to pad (bytes or memoryview).
- length: The block size to pad to. Defaults to 16.
-
- Returns:
- A new bytes object that is padded with null bytes such that its length is a multiple of 'length'.
- """
-
- if len(data) % length:
- padding = b"\0" * (length - len(data) % length)
- if isinstance(data, memoryview):
- return data.tobytes() + padding
- return data + padding
- return data
-
-
-def random_u32int() -> U32Int:
- return random.randint(0, 0xFFFFFFFF)
-
-
-def _aes_cbc_encrypt(data: bytes, key: bytes) -> bytes:
- return AES.new(key, AES.MODE_CBC, EMPTY_IV).encrypt(data)
-
-
-def _aes_cbc_decrypt(data: bytes, key: bytes) -> bytes:
- return AES.new(key, AES.MODE_CBC, EMPTY_IV).decrypt(data)
-
-
-def _aes_cbc_encrypt_a32(data: U32IntSequence, key: U32IntSequence) -> U32IntTupleArray:
- return str_to_a32(_aes_cbc_encrypt(a32_to_bytes(data), a32_to_bytes(key)))
-
-
-def _aes_cbc_decrypt_a32(data: U32IntSequence, key: U32IntSequence) -> U32IntTupleArray:
- return str_to_a32(_aes_cbc_decrypt(a32_to_bytes(data), a32_to_bytes(key)))
-
-
-def make_hash(string: str, aeskey: U32IntSequence) -> str:
- s32 = str_to_a32(string)
- h32 = [0, 0, 0, 0]
- for i in range(len(s32)):
- h32[i % 4] ^= s32[i]
- for _ in range(0x4000):
- h32 = _aes_cbc_encrypt_a32(h32, aeskey)
- return a32_to_base64((h32[0], h32[2]))
-
-
-def prepare_key(arr: U32IntArray) -> U32IntArray:
- pkey: U32IntArray = [0x93C467E3, 0x7DB0C7A4, 0xD1BE3F81, 0x0152CB56]
- for _ in range(0x10000):
- for j in range(0, len(arr), 4):
- key: U32IntArray = [0, 0, 0, 0]
- for i in range(4):
- if i + j < len(arr):
- key[i] = arr[i + j]
- pkey = _aes_cbc_encrypt_a32(pkey, key)
- return pkey
-
-
-def encrypt_key(array: U32IntSequence, key: U32IntSequence) -> U32IntTupleArray:
- # this sum, which is applied to a generator of tuples, actually flattens the output list of lists of that generator
- # i.e. it's equivalent to tuple([item for t in generatorOfLists for item in t])
-
- return sum((_aes_cbc_encrypt_a32(array[index : index + 4], key) for index in range(0, len(array), 4)), ())
-
-
-def decrypt_key(array: U32IntSequence, key: U32IntSequence) -> U32IntTupleArray:
- return sum((_aes_cbc_decrypt_a32(array[index : index + 4], key) for index in range(0, len(array), 4)), ())
-
-
-def decrypt_attr(attr: bytes, key: U32IntSequence) -> AnyDict:
- attr_bytes = _aes_cbc_decrypt(attr, a32_to_bytes(key))
- try:
- attr_str = attr_bytes.decode("utf-8").rstrip("\0")
- except UnicodeDecodeError:
- attr_str = attr_bytes.decode("latin-1").rstrip("\0")
-
- if attr_str.startswith('MEGA{"'):
- start = 4
- end = attr_str.rfind("}") + 1
- error = RuntimeError(f"Unable to decode file attributes, raw content is: {attr_str}")
- if end > start:
- content = attr_str[start:end]
- try:
- return json.loads(content)
- except json.JSONDecodeError as e:
- raise error from e
-
- raise error
-
- return {}
-
-
-def a32_to_bytes(array: U32IntSequence) -> bytes:
- return struct.pack(f">{len(array):.0f}I", *array)
-
-
-def str_to_a32(bytes_or_str: str | bytes) -> U32IntTupleArray:
- if isinstance(bytes_or_str, str):
- bytes_ = bytes_or_str.encode()
- else:
- assert isinstance(bytes_or_str, bytes)
- bytes_ = bytes_or_str
-
- # pad to multiple of 4
- bytes_ = pad_bytes(bytes_, length=4)
- return struct.unpack(f">{(len(bytes_) / 4):.0f}I", bytes_)
-
-
-def mpi_to_int(data: bytes) -> int:
- """
- A Multi-precision integer (mpi) is encoded as a series of bytes in big-endian
- order. The first two bytes are a header which tell the number of bits in
- the integer. The rest of the bytes are the integer.
- """
- return int(binascii.hexlify(data[2:]), CHUNK_BLOCK_LEN)
-
-
-def base64_url_decode(data: str) -> bytes:
- data += "=="[(2 - len(data) * 3) % 4 :]
- for search, replace in (("-", "+"), ("_", "/"), (",", "")):
- data = data.replace(search, replace)
- return base64.b64decode(data)
-
-
-def base64_to_a32(string: str) -> U32IntTupleArray:
- return str_to_a32(base64_url_decode(string))
-
-
-def base64_url_encode(data: bytes) -> str:
- data_bytes = base64.b64encode(data)
- data_str = data_bytes.decode()
- for search, replace in (("+", "-"), ("/", "_"), ("=", "")):
- data_str = data_str.replace(search, replace)
- return data_str
-
-
-def a32_to_base64(array: U32IntSequence) -> str:
- return base64_url_encode(a32_to_bytes(array))
-
-
-def get_chunks(size: int) -> Generator[Chunk]:
- # generates a list of chunks (offset, chunk_size), where offset refers to the file initial position
- offset = 0
- current_size = init_size = 0x20000
- while offset + current_size < size:
- yield Chunk(offset, current_size)
- offset += current_size
- if current_size < 0x100000:
- current_size += init_size
- yield Chunk(offset, size - offset)
-
-
-def decrypt_rsa_key(private_key: bytes) -> RSA.RsaKey:
- # The private_key contains 4 MPI integers concatenated together.
- rsa_private_key = [0, 0, 0, 0]
- for i in range(4):
- # An MPI integer has a 2-byte header which describes the number
- # of bits in the integer.
- bitlength = (private_key[0] * 256) + private_key[1]
- bytelength = math.ceil(bitlength / 8)
- # Add 2 bytes to accommodate the MPI header
- bytelength += 2
- rsa_private_key[i] = mpi_to_int(private_key[:bytelength])
- private_key = private_key[bytelength:]
-
- first_factor_p = rsa_private_key[0]
- second_factor_q = rsa_private_key[1]
- private_exponent_d = rsa_private_key[2]
- crt_coeficient_u = rsa_private_key[3]
- rsa_modulus_n = first_factor_p * second_factor_q
- phi = (first_factor_p - 1) * (second_factor_q - 1)
- public_exponent_e = int(Integer(private_exponent_d).inverse(phi))
-
- rsa_components = (
- rsa_modulus_n,
- public_exponent_e,
- private_exponent_d,
- first_factor_p,
- second_factor_q,
- crt_coeficient_u,
- )
-
- rsa_key = RSA.construct(rsa_components, consistency_check=True)
- return rsa_key
-
-
-async def generate_hashcash_token(challenge: str) -> str:
- parts = challenge.split(":")
- version_str, easiness_str, _, token_str = parts
- version = int(version_str)
- if version != 1:
- raise MegaNzError("hashcash challenge is not version 1 [Mega]")
-
- easiness = int(easiness_str)
- base = ((easiness & 63) << 1) + 1
- shifts = (easiness >> 6) * 7 + 3
- threshold = base << shifts
- token = base64_url_decode(token_str)
- buffer = bytearray(4 + 262144 * 48)
- for i in range(262144):
- buffer[4 + i * 48 : 4 + (i + 1) * 48] = token
-
- def hash_buffer() -> bytes:
- return hashlib.sha256(buffer).digest()
-
- while True:
- digest = await asyncio.to_thread(hash_buffer)
- view = struct.unpack(">I", digest[:4])[0] # big-endian uint32
- if view <= threshold:
- return f"1:{token_str}:{base64_url_encode(buffer[:4])}"
-
- # Increment the first 4 bytes as a little-endian integer
- for j in range(4):
- buffer[j] = (buffer[j] + 1) & 0xFF
- if buffer[j] != 0:
- break
-
-
-def get_decrypt_data(node_type: NodeType, full_key: U32IntTupleArray) -> DecryptData:
- if node_type == NodeType.FILE:
- k = (full_key[0] ^ full_key[4], full_key[1] ^ full_key[5], full_key[2] ^ full_key[6], full_key[3] ^ full_key[7])
- else:
- k = full_key
-
- iv: U32IntTupleArray = (*full_key[4:6], 0, 0)
- meta_mac: U32IntTupleArray = full_key[6:8]
- return DecryptData(k, iv, meta_mac)
-
-
-VALID_REQUEST_ID_CHARS = string.ascii_letters + string.digits
-
-
-def _check_response_status(response: aiohttp.ClientResponse) -> None:
- if response.status > 0 and not (HTTPStatus.OK <= response.status < HTTPStatus.BAD_REQUEST):
- raise DownloadError(response.status)
-
-
-_MEGA_NZ_LIMITER = AsyncLimiter(100, 60)
-
-
-class MegaApi:
- def __init__(self, manager: Manager) -> None:
- self.manager = manager
- self.timeout = ClientTimeout(160)
- self.sid: str | None = None
- self.sequence_num: U32Int = random_u32int()
- self.request_id: str = "".join(random.choice(VALID_REQUEST_ID_CHARS) for _ in range(10))
-
- self.default_headers = {
- "Content-Type": "application/json",
- "User-Agent": manager.config_manager.global_settings_data.general.user_agent,
- }
- self.entrypoint = "https://g.api.mega.co.nz/cs" # api still uses the old mega.co.nz domain
- self.logged_in = False
- self.root_id: str = ""
- self.inbox_id: str = ""
- self.trashbin_id: str = ""
- self._limiter = _MEGA_NZ_LIMITER
- self._files = {}
- self.shared_keys: SharedkeysDict
-
- @property
- def session(self) -> ClientSession:
- return self.manager.client_manager._session
-
- async def request(self, data_input: list[AnyDict] | AnyDict, add_params: AnyDict | None = None) -> Any:
- add_params = add_params or {}
- params: AnyDict = {"id": self.sequence_num} | add_params
- self.sequence_num += 1
- if self.sid:
- params["sid"] = self.sid
-
- # ensure input data is a list
- if not isinstance(data_input, list):
- data = [data_input]
- else:
- data: list[AnyDict] = data_input
-
- async with self._limiter:
- response = await self.session.post(
- self.entrypoint, params=params, json=data, timeout=self.timeout, headers=self.default_headers
- )
-
- # Since around feb 2025, MEGA requires clients to solve a challenge during each login attempt.
- # When that happens, initial responses returns "402 Payment Required".
- # Challenge is inside the `X-Hashcash` header.
- # We need to solve the challenge and re-made the request with same params + the computed token
- # See: https://github.com/gpailler/MegaApiClient/issues/248#issuecomment-2692361193
-
- if xhashcash_challenge := response.headers.get("X-Hashcash"):
- log("[MegaNZ] Solving xhashcash login challenge, this could take a few seconds...")
- xhashcash_token = await generate_hashcash_token(xhashcash_challenge)
- headers = self.default_headers | {"X-Hashcash": xhashcash_token}
- async with self._limiter:
- response = await self.session.post(
- self.entrypoint, params=params, json=data, timeout=self.timeout, headers=headers
- )
- _check_response_status(response)
- if xhashcash_challenge := response.headers.get("X-Hashcash"):
- # Computed token failed
- msg = f"Login failed. Mega requested a proof of work with xhashcash: {xhashcash_challenge}"
- raise RequestError(msg)
-
- json_resp: list[Any] | list[int] | int = await response.json()
-
- def handle_int_resp(int_resp: int) -> Literal[0]:
- if int_resp == 0:
- return int_resp
- if int_resp == -3:
- msg = "Request failed, retrying"
- raise RuntimeError(msg)
- raise RequestError(int_resp)
-
- if isinstance(json_resp, int):
- return handle_int_resp(json_resp)
- elif not isinstance(json_resp, list):
- raise RequestError(f"Unknown response: {json_resp:r}")
- elif json_resp:
- first = json_resp[0]
- if isinstance(first, int):
- return handle_int_resp(first)
- return first
- else:
- raise RequestError(f"Unknown response: {json_resp:r}")
-
- async def login(self, email: str | None = None, password: str | None = None):
- if email and password:
- log("[MegaNZ] Logging as user [REDACTED]. This may take several seconds...")
- await self._login_user(email, password)
- else:
- log("[MegaNZ] Logging as anonymous temporary user. This may take several seconds...")
- await self.login_anonymous()
- _ = await self.get_files() # This is to set the special folders id
- self.logged_in = True
- log("[MegaNZ] Login complete")
- return self
-
- def _process_login(self, resp: AnyDict, password: U32IntArray) -> None:
- encrypted_master_key = base64_to_a32(resp["k"])
- self.master_key = decrypt_key(encrypted_master_key, password)
- if b64_tsid := resp.get("tsid"):
- tsid = base64_url_decode(b64_tsid)
- key_encrypted = a32_to_bytes(encrypt_key(str_to_a32(tsid[:16]), self.master_key))
- if key_encrypted == tsid[-16:]:
- self.sid = resp["tsid"]
-
- elif b64_csid := resp.get("csid"):
- encrypted_sid = mpi_to_int(base64_url_decode(b64_csid))
- encrypted_private_key = base64_to_a32(resp["privk"])
- private_key = a32_to_bytes(decrypt_key(encrypted_private_key, self.master_key))
- rsa_key = decrypt_rsa_key(private_key)
-
- # TODO: Investigate how to decrypt using the current pycryptodome library.
- # The _decrypt method of RSA is deprecated and no longer available.
- # The documentation suggests using Crypto.Cipher.PKCS1_OAEP,
- # but the algorithm differs and requires bytes as input instead of integers.
- decrypted_sid = int(rsa_key._decrypt(encrypted_sid)) # type: ignore
- sid_hex = f"{decrypted_sid:x}"
- sid_bytes = bytes.fromhex("0" + sid_hex if len(sid_hex) % 2 else sid_hex)
- sid = base64_url_encode(sid_bytes[:43])
- self.sid = sid
-
- async def _login_user(self, email: str, password: str) -> None:
- email = email.lower()
- get_user_salt_resp: dict = await self.request({"a": "us0", "user": email})
- if b64_salt := get_user_salt_resp.get("s"):
- # v2 user account
- user_salt = base64_to_a32(b64_salt)
- pbkdf2_key = hashlib.pbkdf2_hmac(
- hash_name="sha512",
- password=password.encode(),
- salt=a32_to_bytes(user_salt),
- iterations=100000,
- dklen=32,
- )
- password_aes = str_to_a32(pbkdf2_key[:16])
- user_hash = base64_url_encode(pbkdf2_key[-16:])
-
- else:
- # v1 user account
- password_aes = prepare_key(str_to_a32(password))
- user_hash = make_hash(email, password_aes)
-
- resp = await self.request({"a": "us", "user": email, "uh": user_hash})
- self._process_login(resp, password_aes)
-
- async def login_anonymous(self) -> None:
- master_key = [random_u32int()] * 4
- password_key = [random_u32int()] * 4
- session_self_challenge = [random_u32int()] * 4
- ts_array = a32_to_bytes(session_self_challenge) + a32_to_bytes(encrypt_key(session_self_challenge, master_key))
-
- user: str = await self.request(
- {
- "a": "up",
- "k": a32_to_base64(encrypt_key(master_key, password_key)),
- "ts": base64_url_encode(ts_array),
- }
- )
-
- resp = await self.request({"a": "us", "user": user})
- self._process_login(resp, password_key)
-
- def _process_node(self, file: Node) -> Node:
- """
- Processes a node and decrypts its metadata and attributes.
-
- Special nodes (root folder, inbox, trash bin) are identified and saved internally for reference
-
- This method is NOT thread safe. It modifies the internal state of the shared keys.
- """
- shared_keys: SharedkeysDict = self.shared_keys
- if file["t"] == NodeType.FILE or file["t"] == NodeType.FOLDER:
- file = cast("FileOrFolder", file)
- keys = dict(keypart.split(":", 1) for keypart in file["k"].split("/") if ":" in keypart)
- uid = file["u"]
- key = None
- # my objects
- if uid in keys:
- key = decrypt_key(base64_to_a32(keys[uid]), self.master_key)
- # shared folders
- elif "su" in file and "sk" in file and ":" in file["k"]:
- shared_key = decrypt_key(base64_to_a32(file["sk"]), self.master_key)
- key = decrypt_key(base64_to_a32(keys[file["h"]]), shared_key)
- shared_keys.setdefault(file["su"], {})[file["h"]] = shared_key
-
- # shared files
- elif file["u"] and file["u"] in shared_keys:
- for hkey, shared_key in shared_keys[file["u"]].items():
- if hkey in keys:
- key = decrypt_key(base64_to_a32(keys[hkey]), shared_key)
- break
-
- if file["h"] and file["h"] in shared_keys.get("EXP", {}):
- shared_key = shared_keys["EXP"][file["h"]]
- encrypted_key = str_to_a32(base64_url_decode(file["k"].split(":")[-1]))
- key = decrypt_key(encrypted_key, shared_key)
- file["sk_decrypted"] = shared_key
-
- if key is not None:
- crypto = get_decrypt_data(file["t"], key)
- file["k_decrypted"] = crypto.k
- file["iv"] = crypto.iv
- file["meta_mac"] = crypto.meta_mac
- file["key_decrypted"] = key
- attributes_bytes = base64_url_decode(file["a"])
- attributes = decrypt_attr(attributes_bytes, crypto.k)
- file["attributes"] = cast("Attributes", attributes)
-
- # other => wrong object
- elif file["k"] == "":
- file = cast("Node", file)
- file["attributes"] = {"n": "Unknown Object"}
-
- elif file["t"] == NodeType.ROOT_FOLDER:
- self.root_id: str = file["h"]
- file["attributes"] = {"n": "Cloud Drive"}
-
- elif file["t"] == NodeType.INBOX:
- self.inbox_id = file["h"]
- file["attributes"] = {"n": "Inbox"}
-
- elif file["t"] == NodeType.TRASH:
- self.trashbin_id = file["h"]
- file["attributes"] = {"n": "Trash Bin"}
-
- return file
-
- def _init_shared_keys(self, files: Folder, shared_keys: SharedkeysDict) -> None:
- """
- Init shared key not associated with a user.
- Seems to happen when a folder is shared,
- some files are exchanged and then the
- folder is un-shared.
- Keys are stored in files['s'] and files['ok']
- """
- shared_key: SharedKey = {}
- for ok_file in files["ok"]:
- decrypted_shared_key = decrypt_key(base64_to_a32(ok_file["k"]), self.master_key)
- shared_key[ok_file["h"]] = decrypted_shared_key
- for s_file in files["s"]:
- if s_file["u"] not in shared_keys:
- shared_keys[s_file["u"]] = {}
- if s_file["h"] in shared_key:
- shared_keys[s_file["u"]][s_file["h"]] = shared_key[s_file["h"]]
- self.shared_keys = shared_keys
-
- async def get_files(self) -> FilesMapping:
- if self._files:
- return self._files
-
- files = await self._get_nodes(lambda x: not bool(x.get("attributes")))
- return cast("FilesMapping", files)
-
- async def _get_nodes(self, predicate: Callable[[Node], bool] | None = None) -> dict[str, Node]:
- folder: Folder = await self.request({"a": "f", "c": 1, "r": 1})
- shared_keys: SharedkeysDict = {}
- self._init_shared_keys(folder, shared_keys)
- return await self._process_nodes(folder["f"], predicate=predicate)
-
- async def get_nodes_in_shared_folder(
- self, folder_id: str, shared_key: str | None = None
- ) -> dict[str, FileOrFolder]:
- folder: Folder = await self.request(
- {"a": "f", "c": 1, "ca": 1, "r": 1},
- {"n": folder_id},
- )
-
- return cast("dict[str, FileOrFolder]", await self._process_nodes(folder["f"], shared_key))
-
- async def _process_nodes(
- self,
- nodes: Sequence[Node],
- public_key: str | None = None,
- predicate: Callable[[Node], bool] | None = None,
- ) -> dict[str, Node]:
- """
- Processes multiple nodes at once, decrypting their metadata and attributes.
-
- If predicate is provided, only nodes for which `predicate(node)` returns `False` are included in the result.
-
- This method is NOT thread safe. It modifies the internal state of the shared keys.
- """
- # User may already have access to this folder (the key is saved in their account)
- folder_key = base64_to_a32(public_key) if public_key else None
- self.shared_keys.setdefault("EXP", {})
-
- async def process_nodes() -> dict[str, Node]:
- results = {}
- for index, node in enumerate(nodes):
- node_id = node["h"]
- if folder_key:
- self.shared_keys["EXP"][node_id] = folder_key
- processed_node = self._process_node(node)
- if predicate is None or not predicate(processed_node):
- results[node_id] = processed_node
-
- # We can compute this on another thread, so we sleep to avoid blocking the event loop for too long.
- if index % 500 == 0:
- await asyncio.sleep(0)
-
- return results
-
- return await process_nodes()
-
- def _build_file_system(self, nodes_map: Mapping[str, Node], root_ids: list[str]) -> dict[Path, Node]:
- """Builds a flattened dictionary representing a file system from a list of items.
-
- Returns:
- A 1-level dictionary where the each keys is the full path to a file/folder, and each value is the actual file/folder
- """
-
- path_mapping: dict[Path, Node] = {}
- parents_mapping: dict[str, list[Node]] = {}
-
- for item in nodes_map.values():
- parent_id = item["p"]
- if parent_id not in parents_mapping:
- parents_mapping[parent_id] = []
- parents_mapping[parent_id].append(item)
-
- def build_tree(parent_id: str, current_path: Path) -> None:
- for item in parents_mapping.get(parent_id, []):
- item_path = current_path / item["attributes"]["n"]
- path_mapping[item_path] = item
-
- if item["t"] == NodeType.FOLDER:
- build_tree(item["h"], item_path)
-
- for root_id in root_ids:
- root_item = nodes_map[root_id]
- name = root_item["attributes"]["n"]
- path = Path(name if name != "Cloud Drive" else ".")
- path_mapping[path] = root_item
- build_tree(root_id, path)
-
- sorted_mapping = dict(sorted(path_mapping.items()))
- return sorted_mapping
-
- async def build_file_system(self, nodes_map: Mapping[str, Node], root_ids: list[str]) -> dict[Path, Node]:
- return await asyncio.to_thread(self._build_file_system, nodes_map, root_ids)
-
class MegaDownloadClient(DownloadClient):
def __init__(self, manager: Manager) -> None:
super().__init__(manager, manager.client_manager)
- self.decrypt_mapping: dict[URL, DecryptData] = {}
+ self._decrypt_mapping: dict[URL, tuple[Crypto, int]] = {}
self._supports_ranges = False
async def _append_content(self, media_item: MediaItem, content: aiohttp.StreamReader) -> None:
@@ -835,14 +35,14 @@ class MegaDownloadClient(DownloadClient):
await check_free_space()
await self._pre_download_check(media_item)
- crypto_data = self.decrypt_mapping.pop(media_item.url)
- chunk_decryptor = MegaDecryptor(crypto_data)
+ crypto, file_size = self._decrypt_mapping.pop(media_item.url)
+ chunk_decryptor = MegaChunker(crypto.key, crypto.iv, crypto.meta_mac)
async with aiofiles.open(media_item.partial_file, mode="ab") as f:
- for _, chunk_size in get_chunks(crypto_data.file_size):
+ for _, chunk_size in get_chunks(file_size):
await self.manager.states.RUNNING.wait()
raw_chunk = await content.readexactly(chunk_size)
- chunk = chunk_decryptor.decrypt(raw_chunk)
+ chunk = chunk_decryptor.read(raw_chunk)
await check_free_space()
chunk_size = len(chunk)
@@ -852,7 +52,7 @@ class MegaDownloadClient(DownloadClient):
check_download_speed()
await self._post_download_check(media_item)
- chunk_decryptor.check_mac_integrity()
+ chunk_decryptor.check_integrity()
def _pre_download_check(self, media_item: MediaItem) -> Coroutine[Any, Any, None]:
def prepare() -> None:
@@ -866,10 +66,6 @@ class MegaDownloadClient(DownloadClient):
class MegaDownloader(Downloader):
client: MegaDownloadClient
- def __init__(self, manager: Manager, domain: str) -> None:
- super().__init__(manager, domain)
- self.api = MegaApi(manager)
-
@property
def max_attempts(self):
return 1
@@ -879,75 +75,5 @@ class MegaDownloader(Downloader):
self.client = MegaDownloadClient(self.manager) # type: ignore[reportIncompatibleVariableOverride]
self._semaphore = asyncio.Semaphore(self.manager.client_manager.get_download_slots(self.domain))
- def register(self, url: URL, crypto: DecryptData) -> None:
- self.client.decrypt_mapping[url] = crypto
-
-
-class MegaDecryptor:
- def __init__(self, crypto: DecryptData) -> None:
- self.chunk_decryptor = _decrypt_chunks(crypto.k, crypto.iv, crypto.meta_mac)
- _ = next(self.chunk_decryptor) # Prime chunk decryptor
-
- def decrypt(self, raw_chunk: bytes) -> bytes:
- return self.chunk_decryptor.send(raw_chunk)
-
- def check_mac_integrity(self) -> None:
- try:
- self.chunk_decryptor.send(None) # type: ignore
- except StopIteration:
- pass
-
-
-def _decrypt_chunks(
- k_decrypted: U32IntTupleArray, iv: U32IntTupleArray, meta_mac: U32IntTupleArray
-) -> Generator[bytes, bytes, None]:
- """
- Decrypts chunks of data received via `send()` and yields the decrypted chunks.
- It decrypts chunks indefinitely until a sentinel value (`None`) is sent.
-
- NOTE: You MUST send `None` once after all chunks are processed to execute the MAC check.
-
- Args:
- iv (AnyArray): Initialization vector (iv) as a list or tuple of two 32-bit unsigned integers.
- k_decrypted (TupleArray): Decryption key as a tuple of four 32-bit unsigned integers.
- meta_mac (AnyArray): The expected MAC value of the final file.
-
- Yields:
- bytes: Decrypted chunk of data. The first `yield` is a blank (`b''`) to initialize generator.
-
- """
- k_bytes = a32_to_bytes(k_decrypted)
- counter = Counter.new(128, initial_value=((iv[0] << 32) + iv[1]) << 64)
- aes = AES.new(k_bytes, AES.MODE_CTR, counter=counter)
-
- # mega.nz improperly uses CBC as a MAC mode, so after each chunk
- # the computed mac_bytes are used as IV for the next chunk MAC accumulation
- mac_bytes = b"\0" * 16
- mac_encryptor = AES.new(k_bytes, AES.MODE_CBC, mac_bytes)
- iv_bytes = a32_to_bytes([iv[0], iv[1], iv[0], iv[1]])
- raw_chunk = yield b""
- while True:
- if raw_chunk is None:
- break
- decrypted_chunk = aes.decrypt(raw_chunk)
- raw_chunk = yield decrypted_chunk
- encryptor = AES.new(k_bytes, AES.MODE_CBC, iv_bytes)
-
- # take last 16-N bytes from chunk (with N between 1 and 16, including extremes)
- mem_view = memoryview(decrypted_chunk) # avoid copying memory for the entire chunk when slicing
- modchunk = len(decrypted_chunk) % CHUNK_BLOCK_LEN
- if modchunk == 0:
- # ensure we reserve the last 16 bytes anyway, we have to feed them into mac_encryptor
- modchunk = CHUNK_BLOCK_LEN
-
- # pad last block to 16 bytes
- last_block = pad_bytes(mem_view[-modchunk:])
- rest_of_chunk = mem_view[:-modchunk]
- _ = encryptor.encrypt(rest_of_chunk)
- input_to_mac = encryptor.encrypt(last_block)
- mac_bytes = mac_encryptor.encrypt(input_to_mac)
-
- file_mac = str_to_a32(mac_bytes)
- computed_mac = file_mac[0] ^ file_mac[1], file_mac[2] ^ file_mac[3]
- if computed_mac != meta_mac:
- raise RuntimeError("Mismatched mac")
+ def register(self, url: URL, crypto: Crypto, file_size: int) -> None:
+ self.client._decrypt_mapping[url] = crypto, file_size
diff --git a/cyberdrop_dl/utils/utilities.py b/cyberdrop_dl/utils/utilities.py
index 575b9dae1..4e985db0f 100644
--- a/cyberdrop_dl/utils/utilities.py
+++ b/cyberdrop_dl/utils/utilities.py
@@ -13,6 +13,7 @@ import sys
import unicodedata
from collections.abc import Generator, Mapping
from functools import partial, wraps
+from http import HTTPStatus
from pathlib import Path
from stat import S_ISREG
from typing import (
@@ -30,6 +31,7 @@ from typing import (
)
from aiohttp import ClientConnectorError, TooManyRedirects
+from mega.errors import MegaNzError
from pydantic import ValidationError
from yarl import URL
@@ -93,6 +95,22 @@ def error_handling_context(self: Crawler | Downloader, item: ScrapeItem | MediaI
ui_failure = "Too Many Redirects"
info = json.dumps({"url": e.request_info.real_url, "history": [r.real_url for r in e.history]}, indent=4)
error_log_msg = ErrorLogMessage(ui_failure, f"{ui_failure}\n{info}")
+ except MegaNzError as e:
+ if code := getattr(e, "code", None):
+ if http_code := {
+ -9: HTTPStatus.GONE,
+ -16: HTTPStatus.FORBIDDEN,
+ -24: 509,
+ -401: 509,
+ }.get(code):
+ ui_failure = create_error_msg(http_code)
+ else:
+ ui_failure = f"MegaNZ Error [{code}]"
+ else:
+ ui_failure = "MegaNZ Error"
+
+ error_log_msg = ErrorLogMessage(ui_failure, str(e))
+
except TimeoutError as e:
error_log_msg = ErrorLogMessage("Timeout", repr(e))
except ClientConnectorError as e:
diff --git a/poetry.lock b/poetry.lock
index caf990573..286580b05 100644
--- a/poetry.lock
+++ b/poetry.lock
@@ -267,6 +267,27 @@ all-plugins = ["PGPy", "cryptography", "gntp", "paho-mqtt (!=2.0.*)", "smpplib"]
dev = ["babel", "coverage", "mock", "pytest", "pytest-cov", "pytest-mock", "ruff", "tox", "validate-pyproject"]
windows = ["pywin32", "tzdata"]
+[[package]]
+name = "async-mega-py"
+version = "2.0.2"
+description = "Python library for the Mega.nz and Transfer.it API"
+optional = false
+python-versions = ">=3.11"
+groups = ["main"]
+files = [
+ {file = "async_mega_py-2.0.2-py3-none-any.whl", hash = "sha256:9c863978e67e79569c2fc0407a793d6ef754af0de6e245b96d1f71d317d115b9"},
+ {file = "async_mega_py-2.0.2.tar.gz", hash = "sha256:4fe83c594788681782c804916d1e337c18ddfb005826a8789d34cda3467b6c8e"},
+]
+
+[package.dependencies]
+aiohttp = ">=3.11.18"
+aiolimiter = ">=1.2.1"
+pycryptodome = ">=3.20.0"
+
+[package.extras]
+cli = ["async-mega-py[default]", "typer-slim (>=0.21.1)"]
+default = ["python-dotenv (>=1.2.1)", "rich (>=14.0.0)"]
+
[[package]]
name = "attrs"
version = "25.4.0"
@@ -3056,4 +3077,4 @@ propcache = ">=0.2.1"
[metadata]
lock-version = "2.1"
python-versions = ">=3.11,<4"
-content-hash = "c5ccc6e30d6b7c1e170ee8308b41c269468660badcc4dc49630c291495a62721"
+content-hash = "0c76bf1703f8cdd2ad6050eb65b44f96bd96bb4ba8472180b05f7ccbac02e7a8"
diff --git a/pyproject.toml b/pyproject.toml
index ec718d913..d1aee8523 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -7,6 +7,7 @@ dependencies = [
"aiohttp[speedups] >=3.13.3",
"aiolimiter >=1.2.1",
"aiosqlite>=0.22.1",
+ "async-mega-py >=2.0.2",
"beautifulsoup4 >=4.14.3",
"certifi >=2026.1.4",
"curl-cffi >=0.13,<0.14; implementation_name == 'cpython' ",
diff --git a/tests/crawlers/test_cases/mega_nz.py b/tests/crawlers/test_cases/mega_nz.py
new file mode 100644
index 000000000..11b6c39cb
--- /dev/null
+++ b/tests/crawlers/test_cases/mega_nz.py
@@ -0,0 +1,108 @@
+DOMAIN = "mega.nz"
+TEST_CASES = [
+ (
+ "https://mega.nz/folder/yAkimbjQ#Ijoqfoqzesat1LDq5NKc-Q",
+ [
+ {
+ "url": "https://mega.nz/folder/yAkimbjQ#Ijoqfoqzesat1LDq5NKc-Q/file/bFtTwQ6a",
+ "filename": "dreadpirate - no blu not orange.zip",
+ "original_filename": "dreadpirate - no blu not orange.zip",
+ "referer": "https://mega.nz/folder/yAkimbjQ#Ijoqfoqzesat1LDq5NKc-Q/file/bFtTwQ6a",
+ "album_id": "yAkimbjQ",
+ "datetime": 1752175237,
+ "download_folder": "re:s4cc-lm (MegaNz)/1.116.223.1030 - enchanted by nature",
+ },
+ {
+ "url": "https://mega.nz/folder/yAkimbjQ#Ijoqfoqzesat1LDq5NKc-Q/file/bI1AFKCB",
+ "filename": "dreadpirate - no blu not orange.zip",
+ "referer": "https://mega.nz/folder/yAkimbjQ#Ijoqfoqzesat1LDq5NKc-Q/file/bI1AFKCB",
+ "datetime": 1759516939,
+ "download_folder": "re:s4cc-lm (MegaNz)/1.118.242.1030 - adventure awaits",
+ },
+ {
+ "url": "https://mega.nz/folder/yAkimbjQ#Ijoqfoqzesat1LDq5NKc-Q/file/CY0nyZaB",
+ "filename": "brntwaffles - astral lights.zip",
+ "referer": "https://mega.nz/folder/yAkimbjQ#Ijoqfoqzesat1LDq5NKc-Q/file/CY0nyZaB",
+ "datetime": 1752175007,
+ },
+ {
+ "url": "https://mega.nz/folder/yAkimbjQ#Ijoqfoqzesat1LDq5NKc-Q/file/GINRhSDb",
+ "filename": "softerhaze - milk-thistle.zip",
+ "datetime": 1653836296,
+ "download_folder": "re:s4cc-lm (MegaNz)",
+ },
+ {
+ "url": "https://mega.nz/folder/yAkimbjQ#Ijoqfoqzesat1LDq5NKc-Q/file/iAkm0KwY",
+ "filename": "brntwaffles - astral lights.zip",
+ },
+ {
+ "url": "https://mega.nz/folder/yAkimbjQ#Ijoqfoqzesat1LDq5NKc-Q/file/iY9zGKqT",
+ "filename": "softerhaze - moonglow.zip",
+ },
+ {
+ "url": "https://mega.nz/folder/yAkimbjQ#Ijoqfoqzesat1LDq5NKc-Q/file/KFlyhSxI",
+ "filename": "softerhaze - moonglow.zip",
+ },
+ {
+ "url": "https://mega.nz/folder/yAkimbjQ#Ijoqfoqzesat1LDq5NKc-Q/file/KV0WCQJb",
+ "filename": "softerhaze - twinkle toes.zip",
+ },
+ {
+ "url": "https://mega.nz/folder/yAkimbjQ#Ijoqfoqzesat1LDq5NKc-Q/file/LAF2TJ4T",
+ "filename": "brntwaffles - no blu.zip",
+ },
+ {
+ "url": "https://mega.nz/folder/yAkimbjQ#Ijoqfoqzesat1LDq5NKc-Q/file/LQ0mmarA",
+ "filename": "softerhaze - moonglow.zip",
+ },
+ {
+ "url": "https://mega.nz/folder/yAkimbjQ#Ijoqfoqzesat1LDq5NKc-Q/file/PN1CRYbZ",
+ "filename": "softerhaze - twinkle toes.zip",
+ },
+ {
+ "url": "https://mega.nz/folder/yAkimbjQ#Ijoqfoqzesat1LDq5NKc-Q/file/qVsXQLKJ",
+ "filename": "dreadpirate - no blu not orange.zip",
+ },
+ {
+ "url": "https://mega.nz/folder/yAkimbjQ#Ijoqfoqzesat1LDq5NKc-Q/file/rI1WSJCD",
+ "filename": "softerhaze - twinkle toes.zip",
+ },
+ {
+ "url": "https://mega.nz/folder/yAkimbjQ#Ijoqfoqzesat1LDq5NKc-Q/file/TU0CAJDZ",
+ "filename": "brntwaffles - no blu.zip",
+ },
+ {
+ "url": "https://mega.nz/folder/yAkimbjQ#Ijoqfoqzesat1LDq5NKc-Q/file/WUUhkRZS",
+ "filename": "brntwaffles - astral lights.zip",
+ },
+ {
+ "url": "https://mega.nz/folder/yAkimbjQ#Ijoqfoqzesat1LDq5NKc-Q/file/zZ92wS4Y",
+ "filename": "brntwaffles - no blu.zip",
+ },
+ ],
+ ),
+ ( # single file within folder
+ "https://mega.nz/folder/yAkimbjQ#Ijoqfoqzesat1LDq5NKc-Q/file/bFtTwQ6a",
+ [
+ {
+ "url": "https://mega.nz/folder/yAkimbjQ#Ijoqfoqzesat1LDq5NKc-Q/file/bFtTwQ6a",
+ "filename": "dreadpirate - no blu not orange.zip",
+ "original_filename": "dreadpirate - no blu not orange.zip",
+ "referer": "https://mega.nz/folder/yAkimbjQ#Ijoqfoqzesat1LDq5NKc-Q/file/bFtTwQ6a",
+ "album_id": "yAkimbjQ",
+ "datetime": 1752175237,
+ "download_folder": "re:s4cc-lm (MegaNz)/1.116.223.1030 - enchanted by nature",
+ },
+ ],
+ ),
+ ( # subfolder within folder
+ "https://mega.nz/folder/yAkimbjQ#Ijoqfoqzesat1LDq5NKc-Q/folder/zJkgxYia",
+ [
+ {
+ "url": "re:https://mega.nz/folder/yAkimbjQ#Ijoqfoqzesat1LDq5NKc-Q/file",
+ "download_folder": "re:s4cc-lm (MegaNz)/1.118.242.1030 - adventure awaits",
+ },
+ ],
+ 5,
+ ),
+]
diff --git a/tests/crawlers/test_cases/transfer_it.py b/tests/crawlers/test_cases/transfer_it.py
index 866d1cb1b..69bd9763c 100644
--- a/tests/crawlers/test_cases/transfer_it.py
+++ b/tests/crawlers/test_cases/transfer_it.py
@@ -1,33 +1,25 @@
-import re
-
DOMAIN = "transfer.it"
TEST_CASES = [
(
- "https://transfer.it/t/yhWbjogXxRLL",
+ "https://transfer.it/t/mgxqsZtVCZCJ",
[
{
- "url": "https://bt7.api.mega.co.nz/cs/g?x=yhWbjogXxRLL&n=qgxVBD5D&fn=start_linux.sh",
+ "url": "https://bt7.api.mega.co.nz/cs/g?x=mgxqsZtVCZCJ&n=Ac1XhBCD&fn=start_linux.sh",
"filename": "start_linux.sh",
- "referer": "https://transfer.it/t/yhWbjogXxRLL#qgxVBD5D",
- "download_folder": "re:" + re.escape("CDL test transfer (Transfer.it)/test/Cyberdrop-DL.v8.4.0"),
- "datetime": 1762696355,
- "album_id": "yhWbjogXxRLL",
+ "debrid_link": None,
+ "original_filename": "start_linux.sh",
+ "referer": "https://transfer.it/t/mgxqsZtVCZCJ#Ac1XhBCD",
+ "album_id": "mgxqsZtVCZCJ",
+ "datetime": 1771261828,
+ "download_folder": "re:Multiple files (Transfer.it)/test/Cyberdrop-DL.v8.4.0",
},
{
- "url": "https://bt7.api.mega.co.nz/cs/g?x=yhWbjogXxRLL&n=7lhHmJga&fn=start_windows.bat",
+ "url": "https://bt7.api.mega.co.nz/cs/g?x=mgxqsZtVCZCJ&n=4c9HiBpQ&fn=start_windows.bat",
"filename": "start_windows.bat",
- "referer": "https://transfer.it/t/yhWbjogXxRLL#7lhHmJga",
- "download_folder": "re:" + re.escape("CDL test transfer (Transfer.it)/test/Cyberdrop-DL.v8.4.0"),
- "datetime": 1762696355,
- "album_id": "yhWbjogXxRLL",
- },
- {
- "url": "https://bt7.api.mega.co.nz/cs/g?x=yhWbjogXxRLL&n=OxwHBbaa&fn=Cyberdrop-DL.v8.4.0.zip",
- "filename": "Cyberdrop-DL.v8.4.0.zip",
- "referer": "https://transfer.it/t/yhWbjogXxRLL#OxwHBbaa",
- "download_folder": "re:" + re.escape("CDL test transfer (Transfer.it)/test"),
- "datetime": 1762696355,
- "album_id": "yhWbjogXxRLL",
+ "original_filename": "start_windows.bat",
+ "referer": "https://transfer.it/t/mgxqsZtVCZCJ#4c9HiBpQ",
+ "album_id": "mgxqsZtVCZCJ",
+ "datetime": 1771261828,
},
],
),
diff --git a/tests/crawlers/test_mega_nz.py b/tests/crawlers/test_mega_nz.py
deleted file mode 100644
index 3f3591483..000000000
--- a/tests/crawlers/test_mega_nz.py
+++ /dev/null
@@ -1,31 +0,0 @@
-from typing import Any
-
-import pytest
-
-from cyberdrop_dl.downloader import mega_nz
-
-
- "attrs, key, expected_output",
- [
- (
- b"$7\xcc\x8e\x05G\r\x18\x9e\xa421\xb3b\x1bv\xc3\xf7\xc7\x1c\xa9N\xcd\x12\xc2\xe1D\xb3\xfe\x1c<\xd7\x07\xd2v\x8d\xac\x14\xfc\xc2\xdd\x1f+\x9c\xd4G\x86A\xef?\xd1*\xc9\xf2)\x97\x80\xcc~\x99n\xc2X\xee\xcc\xfb\xe2\x05\xc6\x01C'\xc9\xbc,\xab\xa4j\x93\xb7",
- (3008087885, 1189702641, 1874401314, 489354689),
- {
- "c": "GDO1Ezwa5QTio14u5dIHXQR0Tgtk",
- "n": "Messages — OnlyFans(4).mp4",
- },
- ),
- (
- b'\xb6E[\xe6K\xbd\xf9\t\x95\x1d\xcb\xe9\xa3N\x13 \xbb\xf3\x15\xf9H\xb7\x11\xe8\xec\\\x92"\x1d\xb45ia\xaf\x89v\xa5\xab\xd3\xb1\xa8Y\xbb\xe0\x81g\x8e\x19\x1b\xf5b\xa8\x1f`\x9d\x05b<\x13\x7fM\x07\xe0\xea',
- (1958576006, 489861153, 2106943810, 3660715586),
- {
- "c": "KGUlr2utItBMvUIKW-RuwQSUdM9j",
- "n": "SAM4}.png",
- },
- ),
- ],
-)
-def test_decrypt_attr(attrs: bytes, key: mega_nz.U32IntSequence, expected_output: dict[str, Any]):
- output = mega_nz.decrypt_attr(attrs, key)
- assert output == expected_output