# 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).

```pycon
>>> 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`](#dropboxdol.shared_folder.DROPBOX_HOSTS)        | Hosts a shared-folder link may name.                                                      |
|-----------------------------------------------------------------------|-------------------------------------------------------------------------------------------|
| [`DROPBOX_FOLDER_PATHS`](#dropboxdol.shared_folder.DROPBOX_FOLDER_PATHS) | Path prefixes of shared-folder links (`scl/fo` today, `sh` for older links).              |
| [`DOWNLOAD_DOMAINS`](#dropboxdol.shared_folder.DOWNLOAD_DOMAINS)     | Domains a download may be redirected to (the host itself or a subdomain of it).           |
| [`DFLT_MAX_BYTES`](#dropboxdol.shared_folder.DFLT_MAX_BYTES)       | Default ceiling for the download and for the files it holds, uncompressed.                |
| [`DFLT_MAX_MEMBERS`](#dropboxdol.shared_folder.DFLT_MAX_MEMBERS)     | Default ceiling for the number of entries in the zip (each costs memory before any read). |

### Functions

| [`download_url`](#dropboxdol.shared_folder.download_url)(url)                   | The shared-folder link with `dl=1` (its zip download).                                                                       |
|--------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------|
| [`shared_folder_name`](#dropboxdol.shared_folder.shared_folder_name)(url, \*[, http]) | The shared folder's name: its download's file name without `.zip`.                                                           |
| [`urlopen_dropbox`](#dropboxdol.shared_folder.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`](#dropboxdol.shared_folder.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`](#dropboxdol.shared_folder.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>)

Bases: [`Mapping`](https://docs.python.org/3/library/collections.abc.html#collections.abc.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()`](#dropboxdol.shared_folder.DropboxSharedFolderZip.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`](#dropboxdol.shared_folder.SharedFolderError) otherwise).

* **Parameters:**
  * **url** ([`str`](https://docs.python.org/3/builtins/stdtypes.html#str)) – The shared-folder link (`https://www.dropbox.com/scl/fo/...`).
  * **include** ([`Optional`](https://docs.python.org/3/library/typing.html#typing.Optional)[[`Callable`](https://docs.python.org/3/library/collections.abc.html#collections.abc.Callable)[[[`str`](https://docs.python.org/3/builtins/stdtypes.html#str)], [`bool`](https://docs.python.org/3/builtins/functions.html#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`](https://docs.python.org/3/builtins/functions.html#int)) – Ceiling for the download and for the included files’ total size.
  * **max_members** ([`int`](https://docs.python.org/3/builtins/functions.html#int)) – Ceiling for the number of entries in the zip.
  * **http** ([`Callable`](https://docs.python.org/3/library/collections.abc.html#collections.abc.Callable)) – `http(url)` returning a response (a context manager with
    `.headers` and `.read(n)`). Default [`urlopen_dropbox()`](#dropboxdol.shared_folder.urlopen_dropbox).

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

#### *property* cache_path *: [Optional](https://docs.python.org/3/library/typing.html#typing.Optional)[[Path](https://docs.python.org/3/library/pathlib.html#pathlib.Path)]*

Where the zip is kept, when `cache_dir` is given.

#### close()

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

* **Return type:**
  [`None`](https://docs.python.org/3/builtins/constants.html#None)

#### refresh()

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

* **Return type:**
  [`None`](https://docs.python.org/3/builtins/constants.html#None)

### *exception* dropboxdol.shared_folder.SharedFolderError

Bases: [`ValueError`](https://docs.python.org/3/builtins/exceptions.html#ValueError)

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

### dropboxdol.shared_folder.download_url(url)

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.

```pycon
>>> 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`](https://docs.python.org/3/builtins/stdtypes.html#str)

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

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`](https://docs.python.org/3/builtins/stdtypes.html#str)

### dropboxdol.shared_folder.urlopen_dropbox(url, , timeout=60)

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