Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/nba_api/live/http.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from nba_api.library import http

try:
from nba_api.library.debug.debug import STATS_HEADERS as _STATS_HEADERS

_COMMON_HEADERS = _STATS_HEADERS
except ImportError:
_COMMON_HEADERS = {
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
"Accept-Encoding": "gzip, deflate, br",
"Accept-Language": "en-US,en;q=0.9",
"Cache-Control": "max-age=0",
"Connection": "keep-alive",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36",
}


class BaseLiveHTTP(http.NBAHTTP):
nba_response = http.NBAResponse

def clean_contents(self, contents):
if '{"Message":"An error has occurred."}' in contents:
return "<Error><Message>An error has occurred.</Message></Error>"
return contents
8 changes: 7 additions & 1 deletion src/nba_api/live/nba/endpoints/boxscore.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
from nba_api.live.nba.endpoints._base import Endpoint
from nba_api.live.nba.library.http import NBALiveHTTP
from nba_api.live.wnba.library.http import WNBALiveHTTP

_LEAGUE_HTTP_CLIENT = {
"10": WNBALiveHTTP, # WNBA
}


class BoxScore(Endpoint):
Expand Down Expand Up @@ -321,7 +326,8 @@ def __init__(self, game_id, proxy=None, headers=None, timeout=30, get_request=Tr
self.get_request()

def get_request(self):
self.nba_response = NBALiveHTTP().send_api_request(
http_client = _LEAGUE_HTTP_CLIENT.get(self.game_id[:2], NBALiveHTTP)
self.nba_response = http_client().send_api_request(
endpoint=self.endpoint_url.format(game_id=self.game_id),
parameters={},
proxy=self.proxy,
Expand Down
25 changes: 3 additions & 22 deletions src/nba_api/live/nba/library/http.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,6 @@
from nba_api.library import http
from nba_api.live.http import _COMMON_HEADERS, BaseLiveHTTP

try:
from nba_api.library.debug.debug import STATS_HEADERS
except ImportError:
STATS_HEADERS = {
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
"Accept-Encoding": "gzip, deflate, br",
"Accept-Language": "en-US,en;q=0.9",
"Cache-Control": "max-age=0",
"Connection": "keep-alive",
"Host": "cdn.nba.com",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36",
}


class NBALiveHTTP(http.NBAHTTP):
nba_response = http.NBAResponse
class NBALiveHTTP(BaseLiveHTTP):
base_url = "https://cdn.nba.com/static/json/liveData/{endpoint}"
headers = STATS_HEADERS

def clean_contents(self, contents):
if '{"Message":"An error has occurred."}' in contents:
return "<Error><Message>An error has occurred.</Message></Error>"
return contents
headers = {**_COMMON_HEADERS, "Host": "cdn.nba.com"}
Empty file.
6 changes: 6 additions & 0 deletions src/nba_api/live/wnba/library/http.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from nba_api.live.http import _COMMON_HEADERS, BaseLiveHTTP


class WNBALiveHTTP(BaseLiveHTTP):
base_url = "https://cdn.wnba.com/static/json/liveData/{endpoint}"
headers = {**_COMMON_HEADERS, "Host": "cdn.wnba.com"}
Loading