python-espncricinfo 1.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1 @@
1
+ """Python client for ESPNcricinfo match, series, season, team and player data."""
espncricinfo/_core.py ADDED
@@ -0,0 +1,60 @@
1
+ """Shared HTTP and reference helpers for the ESPN core API."""
2
+
3
+ import re
4
+
5
+ import requests
6
+ from requests.adapters import HTTPAdapter
7
+ from urllib3.util import Retry
8
+
9
+ HEADERS = {"user-agent": "Mozilla/5.0"}
10
+ TIMEOUT = 30
11
+
12
+ # ESPN's core API intermittently answers "503 Backend fetch failed". A player's
13
+ # major teams take one request each, so without retries one blip fails the call.
14
+ # raise_on_status=False hands back the last response after the retries, so
15
+ # callers keep their own 404 and raise_for_status handling.
16
+ _RETRY = Retry(
17
+ total=3,
18
+ backoff_factor=0.5,
19
+ status_forcelist=(500, 502, 503, 504),
20
+ allowed_methods=("GET",),
21
+ raise_on_status=False,
22
+ )
23
+ _SESSION = requests.Session()
24
+ _SESSION.mount("http://", HTTPAdapter(max_retries=_RETRY))
25
+ _SESSION.mount("https://", HTTPAdapter(max_retries=_RETRY))
26
+
27
+
28
+ def http_get(url, headers=None):
29
+ """GET ``url`` with the shared timeout and retry policy."""
30
+ return _SESSION.get(url, headers=headers or HEADERS, timeout=TIMEOUT)
31
+
32
+
33
+ def reference_ids(url, resource):
34
+ """Return the (series_id, resource_id) pair from a core API reference URL."""
35
+ match = re.search(rf"/leagues/(\d+)/{resource}/(\d+)(?:/|$)", url)
36
+ if match is None:
37
+ raise ValueError(f"Invalid {resource} reference: {url}")
38
+ return int(match.group(1)), int(match.group(2))
39
+
40
+
41
+ def get_json(url, not_found_error):
42
+ """Return the JSON at ``url``; raise ``not_found_error`` on 404."""
43
+ response = http_get(url)
44
+ if response.status_code == 404:
45
+ raise not_found_error(f"Not found: {url}")
46
+ response.raise_for_status()
47
+ return response.json()
48
+
49
+
50
+ def collection_refs(url, not_found_error):
51
+ """Return every ``$ref`` in a paginated core API collection."""
52
+ references = []
53
+ page = 1
54
+ while True:
55
+ page_url = url if page == 1 else f"{url}?page={page}"
56
+ data = get_json(page_url, not_found_error)
57
+ references.extend(item["$ref"] for item in data.get("items", []))
58
+ if page >= data.get("pageCount", 1):
59
+ return references
60
+ page += 1
@@ -0,0 +1,25 @@
1
+ """Exceptions raised by python-espncricinfo."""
2
+
3
+
4
+ class MatchNotFoundError(TypeError):
5
+ """Raised when a match_id is not valid or does not exist."""
6
+
7
+
8
+ class PlayerNotFoundError(TypeError):
9
+ """Raised when a player_id is not valid or does not exist."""
10
+
11
+
12
+ class NoScorecardError(TypeError):
13
+ """Raised when a match has no scorecard."""
14
+
15
+
16
+ class NoSeriesError(TypeError):
17
+ """Raised when a series_id is not valid or does not exist."""
18
+
19
+
20
+ class NoSeasonError(TypeError):
21
+ """Exception raised if a season does not exist."""
22
+
23
+
24
+ class NoTeamError(TypeError):
25
+ """Exception raised if a team does not exist in a series."""