Skip to content

Bulk asynchronous downloader for multiple file hosts

refactor: update URL parsing (WeTransfer) (#1379)

NTFSvolume41ed4534a530
diff --git a/cyberdrop_dl/crawlers/wetransfer.py b/cyberdrop_dl/crawlers/wetransfer.py
index ed4af904e..98766eede 100644
--- a/cyberdrop_dl/crawlers/wetransfer.py
+++ b/cyberdrop_dl/crawlers/wetransfer.py
@@ -1,7 +1,5 @@
from __future__ import annotations
-import json
-from dataclasses import dataclass
from typing import TYPE_CHECKING, Any, ClassVar
from cyberdrop_dl.crawlers.crawler import Crawler, SupportedDomains, SupportedPaths
@@ -12,82 +10,61 @@ from cyberdrop_dl.utils.utilities import error_handling_wrapper
if TYPE_CHECKING:
from cyberdrop_dl.data_structures.url_objects import ScrapeItem
-PRIMARY_URL = AbsoluteHttpURL("https://wetransfer.com/")
-API_ENTRYPOINT = AbsoluteHttpURL("https://wetransfer.com/api/v4/transfers")
+_PRIMARY_URL = AbsoluteHttpURL("https://wetransfer.com/")
+_API_ENTRYPOINT = _PRIMARY_URL / "api/v4/transfers"
class WeTransferCrawler(Crawler):
SUPPORTED_PATHS: ClassVar[SupportedPaths] = {
- "Public link": "wetransfer.com/<file_id>/<security_hash>",
- "Share Link": "wetransfer.com/<file_id>/<recipient_id>/<security_hash>",
- "Short Link": "we.tl/<id>",
+ "Public link": "wetransfer.com/downloads/<file_id>/<security_hash>",
+ "Share Link": "wetransfer.com/downloads/<file_id>/<recipient_id>/<security_hash>",
+ "Short Link": "we.tl/<short_file_id>",
"Direct links": "download.wetransfer.com/...",
}
SUPPORTED_DOMAINS: ClassVar[SupportedDomains] = "wetransfer.com", "we.tl"
- PRIMARY_URL: ClassVar[AbsoluteHttpURL] = PRIMARY_URL
+ PRIMARY_URL: ClassVar[AbsoluteHttpURL] = _PRIMARY_URL
DOMAIN: ClassVar[str] = "wetransfer"
FOLDER_DOMAIN: ClassVar[str] = "WeTransfer"
async def fetch(self, scrape_item: ScrapeItem) -> None:
- if "download." in scrape_item.url.host:
- # We can download but db entry will not have a canonical URL
- return await self.direct_file(scrape_item)
- await self.file(scrape_item)
+ match scrape_item.url.parts[1:]:
+ case [_] if scrape_item.url.host == "we.tl":
+ return await self.follow_redirect(scrape_item)
+ case ["downloads", file_id, security_hash]:
+ return await self.file(scrape_item, file_id, security_hash)
+ case ["downloads", file_id, recipient_id, security_hash]:
+ return await self.file(scrape_item, file_id, security_hash, recipient_id)
+ case [*_] if "download." in scrape_item.url.host:
+ return await self.direct_file(scrape_item)
+ case _:
+ raise ValueError
+
+ @classmethod
+ def _json_response_check(cls, json_resp: dict[str, Any]) -> None:
+ if json_resp.get("direct_link"):
+ return
- @error_handling_wrapper
- async def file(self, scrape_item: ScrapeItem) -> None:
- if scrape_item.url.host == "we.tl":
- scrape_item.url = await self._get_redirect_url(scrape_item.url)
+ msg: str = json_resp.get("message") or ""
+ if "No download access to this Transfer" in msg:
+ code = 401
+ elif "Couldn't find Transfer" in msg:
+ code = 410
+ else:
+ code = 422
+ raise ScrapeError(code, msg)
- file_info = get_file_info(scrape_item.url)
- if await self.check_complete_from_referer(file_info.download_url):
+ @error_handling_wrapper
+ async def file(
+ self, scrape_item: ScrapeItem, file_id: str, security_hash: str, recipient_id: str | None = None
+ ) -> None:
+ if await self.check_complete_from_referer(scrape_item):
return
- json_resp: dict[str, Any] = await self.request_json(file_info.download_url, method="POST", json=file_info.json)
- link_str: str | None = json_resp.get("direct_link")
- if not link_str:
- code, msg = parse_error(json_resp)
- raise ScrapeError(code, message=msg)
+ payload = {"intent": "entire_transfer", "security_hash": security_hash}
+ if recipient_id:
+ payload["recipient_id"] = recipient_id
- link = self.parse_url(link_str)
+ api_url = _API_ENTRYPOINT / file_id / "download"
+ resp: dict[str, str] = await self.request_json(api_url, method="POST", json=payload)
+ link = self.parse_url(resp["direct_link"])
await self.direct_file(scrape_item, link)
-
-
-@dataclass(frozen=True, order=True)
-class FileInfo:
- id: str
- security_hash: str
- recipient_id: str | None = None
-
- @property
- def json(self) -> str:
- details = {"intent": "entire_transfer", "security_hash": self.security_hash}
- if self.recipient_id:
- details["recipient_id"] = self.recipient_id
- return json.dumps(details)
-
- @property
- def download_url(self) -> AbsoluteHttpURL:
- return API_ENTRYPOINT / self.id / "download"
-
-
-def get_file_info(url: AbsoluteHttpURL) -> FileInfo:
- parts = [p for p in url.parts if p not in ("/", "downloads")]
- assert len(parts) >= 2
- if len(parts) >= 3:
- return FileInfo(id=parts[0], recipient_id=parts[0], security_hash=parts[2])
- return FileInfo(id=parts[0], security_hash=parts[1])
-
-
-def parse_error(json_resp: dict) -> tuple[int, str | None]:
- msg = json_resp.get("message") or ""
- code = get_error_code(msg)
- return code, msg or None
-
-
-def get_error_code(msg: str) -> int:
- if msg == "No download access to this Transfer":
- return 401
- if "Couldn't find Transfer" in msg:
- return 410
- return 422