Spaces:
Sleeping
Sleeping
Download src/auth.py from sammmmmmm25/marine-tracker: direct link, hf CLI and curl.
- Browser
- Download file 1.47 kB
-
https://huggingface.co/spaces/sammmmmmm25/marine-tracker/resolve/main/src/auth.py
- Command line
-
hf download hf://spaces/sammmmmmm25/marine-tracker/src/auth.py
-
curl -L -o auth.py https://huggingface.co/spaces/sammmmmmm25/marine-tracker/resolve/main/src/auth.py
1.47 kB
| """Optional Movebank credentials, loaded from environment variables. | |
| Movebank's live tracking API (www.movebank.org/movebank/service/direct-read) | |
| requires HTTP Basic Auth on every request, including study discovery -- there | |
| is no anonymous tier. If MOVEBANK_USERNAME and MOVEBANK_PASSWORD are not set, | |
| callers should fall back to the Movebank Data Repository (a separate, fully | |
| public, DOI-archival system used by ingest.py for historical studies). | |
| Credentials are read from a local .env file (via python-dotenv) or from the | |
| shell environment. Never hardcode credentials in source. | |
| """ | |
| import os | |
| from dotenv import load_dotenv | |
| load_dotenv() | |
| def has_live_credentials(): | |
| """Return True if both MOVEBANK_USERNAME and MOVEBANK_PASSWORD are set and non-empty.""" | |
| return bool(os.environ.get("MOVEBANK_USERNAME")) and bool(os.environ.get("MOVEBANK_PASSWORD")) | |
| def get_credentials(): | |
| """Return (username, password) from the environment, or (None, None) if unset.""" | |
| return os.environ.get("MOVEBANK_USERNAME") or None, os.environ.get("MOVEBANK_PASSWORD") or None | |
| def get_auth(): | |
| """Return a (username, password) tuple suitable for requests' `auth=` kwarg, or None. | |
| Returns None when no credentials are configured, signaling callers to use | |
| the public Data Repository fallback instead of the live API. | |
| """ | |
| if not has_live_credentials(): | |
| return None | |
| username, password = get_credentials() | |
| return (username, password) | |