dropboxdol.shared_folder#

Read a public Dropbox shared-folder link with no token, through its folder download.

A shared-folder link (https://www.dropbox.com/scl/fo/...) with dl=1 downloads the whole folder as one zip, and its Content-Disposition names the folder. That needs no app key or access token, unlike dropboxdol.DropboxLinkReaderWithToken; the cost is a whole-folder download per refresh, so for a large folder the token-based reader stays the better choice (i2mint/dropboxdol#8).

>>> download_url("https://www.dropbox.com/scl/fo/abc/xyz?rlkey=k&dl=0")
'https://www.dropbox.com/scl/fo/abc/xyz?rlkey=k&dl=1'

DropboxSharedFolderZip(url) is a Mapping[str, bytes] of the files in the folder, keyed by their path inside it; shared_folder_name(url) is the folder’s name. Every request goes through one http(url) callable (default: urllib, following redirects only to Dropbox’s own https hosts), so tests and other clients plug in there.

Module Attributes

DROPBOX_HOSTS

Hosts a shared-folder link may name.

DROPBOX_FOLDER_PATHS

Path prefixes of shared-folder links (scl/fo today, sh for older links).

DOWNLOAD_DOMAINS

Domains a download may be redirected to (the host itself or a subdomain of it).

DFLT_MAX_BYTES

Default ceiling for the download and for the files it holds, uncompressed.

DFLT_MAX_MEMBERS

Default ceiling for the number of entries in the zip (each costs memory before any read).

Functions

download_url(url)

The shared-folder link with dl=1 (its zip download).

shared_folder_name(url, *[, http])

The shared folder's name: its download's file name without .zip.

urlopen_dropbox(url, *[, timeout])

GET url (a response: a context manager with .headers and .read(n)), following redirects only to Dropbox's https hosts.

Classes

DropboxSharedFolderZip(url, *[, include, ...])

The files of a public Dropbox shared folder, {path inside the folder: bytes}, read from the folder's zip download, with no token.

Exceptions

SharedFolderError

A shared-folder link that cannot be read (not a folder link, not shared, too big).

dropboxdol.shared_folder.DFLT_MAX_BYTES = 4294967296#

Default ceiling for the download and for the files it holds, uncompressed.

dropboxdol.shared_folder.DFLT_MAX_MEMBERS = 100000#

Default ceiling for the number of entries in the zip (each costs memory before any read).

dropboxdol.shared_folder.DOWNLOAD_DOMAINS = ('dropbox.com', 'dropboxusercontent.com')#

Domains a download may be redirected to (the host itself or a subdomain of it).

dropboxdol.shared_folder.DROPBOX_FOLDER_PATHS = ('/scl/fo/', '/sh/')#

Path prefixes of shared-folder links (scl/fo today, sh for older links).

dropboxdol.shared_folder.DROPBOX_HOSTS = ('dropbox.com', 'www.dropbox.com')#

Hosts a shared-folder link may name.

class dropboxdol.shared_folder.DropboxSharedFolderZip(url, *, include=None, cache_dir=None, max_bytes=4294967296, max_members=100000, http=<function urlopen_dropbox>)[source]#

Bases: Mapping

The files of a public Dropbox shared folder, {path inside the folder: bytes}, read from the folder’s zip download, with no token.

The zip is downloaded once, on first use: into cache_dir (then reused by later instances, until refresh()), or into an anonymous temporary file that goes away with the object. Folder entries and paths that would leave the folder are not keys. The download, and the files it holds once uncompressed, may not exceed max_bytes (SharedFolderError otherwise).

Parameters:
  • url (str) – The shared-folder link (https://www.dropbox.com/scl/fo/...).

  • include (Optional[Callable[[str], bool]]) – include(path) -> bool: which files are keys (default: all), e.g. lambda p: p.endswith(".mp3"). It may not read this store. Excluded files do not count toward max_bytes once the zip is here.

  • cache_dir – A folder to keep the zip in, named from the link’s hash.

  • max_bytes (int) – Ceiling for the download and for the included files’ total size.

  • max_members (int) – Ceiling for the number of entries in the zip.

  • http (Callable) – http(url) returning a response (a context manager with .headers and .read(n)). Default urlopen_dropbox().

Use it as a context manager (or call close()) to release the open zip. Reads are thread-safe; iterating while another thread calls refresh() is not (the keys may come from one download and the values from the next).

property cache_path: Optional[Path]#

Where the zip is kept, when cache_dir is given.

close()[source]#

Release the open zip (it is reopened, from the cache or a new download, on next use).

Return type:

None

refresh()[source]#

Download the folder again (the cached zip is replaced).

Return type:

None

exception dropboxdol.shared_folder.SharedFolderError[source]#

Bases: ValueError

A shared-folder link that cannot be read (not a folder link, not shared, too big).

dropboxdol.shared_folder.download_url(url)[source]#

The shared-folder link with dl=1 (its zip download). Refuses anything but an https Dropbox shared-folder link, so a caller cannot be made to fetch another host.

>>> download_url("https://dropbox.com/sh/abc/def?dl=0&raw=1")
'https://dropbox.com/sh/abc/def?dl=1'
>>> download_url("https://example.com/scl/fo/abc")
Traceback (most recent call last):
...
dropboxdol.shared_folder.SharedFolderError: not a Dropbox shared-folder link (https://www.dropbox.com/scl/fo/...): 'https://example.com/scl/fo/abc'
Return type:

str

dropboxdol.shared_folder.shared_folder_name(url, *, http=<function urlopen_dropbox>)[source]#

The shared folder’s name: its download’s file name without .zip. Only the response headers are read, not the body. "" when Dropbox names none. The name comes from the server: it is reduced to a plain name (no folder part), but treat it as untrusted text.

Return type:

str

dropboxdol.shared_folder.urlopen_dropbox(url, *, timeout=60)[source]#

GET url (a response: a context manager with .headers and .read(n)), following redirects only to Dropbox’s https hosts. The default http here.