def test_req_wrap_get_exception_exhaust(mocker, mock_requests, mock_sleep):
    mock_requests.get.side_effect = [
        ValueError,
        ValueError,
        ValueError,
        ValueError,
        ValueError,
    ]
    with pytest.raises(ValueError):
        get()
Beispiel #2
0
def fetch_games(url: str) -> dict:
    """
    Gets all games from the NHL API
    """
    tprint(f"Looking up games..")
    tprint(f"@ {url}", debug_only=True)
    return requests.get(url).json()
Beispiel #3
0
def _get_download_from_stream(stream: Stream) -> Download:
    authorization: str = get_auth_cookie_value_login_if_needed()

    extra_headers: Dict[str, str] = {
        "Authorization": authorization,
        "User-Agent": UA_NHL,
    }

    session_key = _get_session_key(stream)

    stream_rsp = requests.get(
        get_stream_url(stream.content_id, session_key),
        headers={
            **HEADERS,
            **extra_headers
        },
    )

    stream_json = stream_rsp.json()

    dump_json_if_debug_enabled(stream_json)

    _verify_nhltv_request_status_succeeded(stream_json)
    _verify_game_is_not_blacked_out(stream_json)

    stream_url: str = _extract_stream_url(stream_json)
    media_auth: str = _extract_media_auth(stream_json)
    pretty_game_str: str = _extract_pretty_game_str(stream_json)

    media_auth_cookie = create_nhl_cookie("mediaAuth",
                                          media_auth.replace("mediaAuth=", ""))

    auth_cookie = create_nhl_cookie("Authorization", authorization)

    save_cookies_to_txt([media_auth_cookie, auth_cookie],
                        f"{stream.game_id}.txt")

    return Download(stream.game_id, pretty_game_str, stream_url, session_key)
Beispiel #4
0
def _get_session_key(stream: Stream) -> str:
    """
    Gets the session key for a stream
    """
    extra_headers: Dict[str, Optional[str]] = {
        "Authorization": get_auth_cookie_value_login_if_needed(),
        "Referer": get_referer(stream),
    }

    session_json: dict = requests.get(
        get_session_key_url(stream.event_id),
        headers={
            **HEADERS,
            **extra_headers
        },
    ).json()

    dump_json_if_debug_enabled(session_json)

    _verify_nhltv_request_status_succeeded(session_json)
    _verify_game_is_not_blacked_out(session_json)

    return _extract_session_key(session_json)
def test_req_wrap_get_exception(mocker, mock_requests, mock_sleep):
    mock_requests.get.side_effect = [ValueError, 1]
    assert get() == 1
    mock_sleep.assert_called_once_with(15)
def test_req_wrap_get_both(mocker, mock_requests):
    get(1, test=2)
    mock_requests.get.assert_called_once_with(1, test=2)
def test_req_wrap_get_kw(mocker, mock_requests):
    get(test=1)
    mock_requests.get.assert_called_once_with(test=1)
Beispiel #8
0
def fetch_teams() -> dict:
    return requests.get(TEAMS_URL).json()