Skip to content

get

Get a RaBe CRID.

get(timestamp=None, fragment='')

Get a RaBe CRID.

Examples:


You can get a CRID for a specific time.

```python
>>> from unittest.mock import MagicMock, patch
>>> from datetime import datetime
>>> from pytz import timezone
>>> mock_resp = MagicMock()
>>> mock_show_data = {"data": [{"attributes": {"label": "Klangbecken"}}]}
>>> mock_resp.json.return_value = mock_show_data
>>> # 2020-03-01 is in the past; patch the archive strategy to stay offline
>>> with patch("cridlib.strategy.past.get_session") as mock_gs:
...     mock_gs.return_value.get.return_value = mock_resp
...     crid = get(datetime(2020, 3, 1, 0, 0, tzinfo=timezone('Europe/Zurich')))
>>> print(f"version: {crid.version}, start: {crid.start}")
version: v1, start: ...

```

timestamp: Exact time you want a CRID for.
    If left empty, a CRID for the current time is generated.
fragment: Optional fragment to add to the end of the CRID.

CRID: The generated CRID.
Source code in cridlib/get.py
def get(timestamp: datetime | None = None, fragment: str = "") -> CRID:
    """Get a RaBe CRID.

    Examples:
    --------
        You can get a CRID for a specific time.

        ```python
        >>> from unittest.mock import MagicMock, patch
        >>> from datetime import datetime
        >>> from pytz import timezone
        >>> mock_resp = MagicMock()
        >>> mock_show_data = {"data": [{"attributes": {"label": "Klangbecken"}}]}
        >>> mock_resp.json.return_value = mock_show_data
        >>> # 2020-03-01 is in the past; patch the archive strategy to stay offline
        >>> with patch("cridlib.strategy.past.get_session") as mock_gs:
        ...     mock_gs.return_value.get.return_value = mock_resp
        ...     crid = get(datetime(2020, 3, 1, 0, 0, tzinfo=timezone('Europe/Zurich')))
        >>> print(f"version: {crid.version}, start: {crid.start}")
        version: v1, start: ...

        ```

    Args:
    ----
        timestamp: Exact time you want a CRID for.
            If left empty, a CRID for the current time is generated.
        fragment: Optional fragment to add to the end of the CRID.

    Returns:
    -------
        CRID: The generated CRID.

    """
    _now = datetime.now(timezone.utc)
    _ts = timestamp or _now
    _show = ""
    if _ts == _now:
        _show = now.get_show()
    elif _ts < _now:
        _show = past.get_show(past=_ts)
    elif _ts > _now:  # pragma: no cover
        _show = future.get_show(future=_ts)

    if _show:
        _show = canonicalize_show(_show)

    _tscode = f"t=clock={_ts.strftime('%Y%m%dT%H%M%S.%f')[:-4]}Z"
    _fragment = f"{_tscode}{'&' + fragment if fragment else ''}"

    return CRID(f"crid://rabe.ch/v1{'/' + _show if _show else ''}#{_fragment}")