from pathlib import Path
import secrets


def _get_download_token() -> str:
    """Return a download token from ~/.deeppavlov/token file.

    If token file does not exists, creates the file and writes to it a random URL-safe text string
    containing 32 random bytes.

    Returns:
        32 byte URL-safe text string from ~/.deeppavlov/token.

    """
    token_file = Path.home() / '.deeppavlov' / 'token'
    if not token_file.exists():
        if token_file.parent.is_file():
            token_file.parent.unlink()
        token_file.parent.mkdir(parents=True, exist_ok=True)
        token_file.write_text(secrets.token_urlsafe(32), encoding='utf8')

    return token_file.read_text(encoding='utf8').strip()