Skip to content

Commit

Permalink
add support for get track by isrc and get album by barcode id
Browse files Browse the repository at this point in the history
  • Loading branch information
M4TH1EU committed Sep 4, 2024
1 parent 288fc1e commit 5e3a2ee
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
10 changes: 8 additions & 2 deletions tidalapi/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ def basic_request(
params: Optional[Params] = None,
data: Optional[JsonObj] = None,
headers: Optional[MutableMapping[str, str]] = None,
base_url: Optional[str] = None,
) -> requests.Response:
request_params = {
"sessionId": self.session.session_id,
Expand All @@ -90,7 +91,10 @@ def basic_request(
headers["authorization"] = (
self.session.token_type + " " + self.session.access_token
)
url = urljoin(self.session.config.api_v1_location, path)
if base_url is None:
base_url = self.session.config.api_v1_location

url = urljoin(base_url, path)
request = self.session.request_session.request(
method, url, params=request_params, data=data, headers=headers
)
Expand Down Expand Up @@ -123,6 +127,7 @@ def request(
params: Optional[Params] = None,
data: Optional[JsonObj] = None,
headers: Optional[MutableMapping[str, str]] = None,
base_url: Optional[str] = None,
) -> requests.Response:
"""Method for tidal requests.
Expand All @@ -133,10 +138,11 @@ def request(
:param params: The parameters you want to supply with the request.
:param data: The data you want to supply with the request.
:param headers: The headers you want to include with the request
:param base_url: The base url to use for the request
:return: The json data at specified api endpoint.
"""

request = self.basic_request(method, path, params, data, headers)
request = self.basic_request(method, path, params, data, headers, base_url)
log.debug("request: %s", request.request.url)
try:
request.raise_for_status()
Expand Down
40 changes: 40 additions & 0 deletions tidalapi/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -857,6 +857,46 @@ def track(
log.warning("Track '%s' is unavailable", track_id)
raise

def get_tracks_by_isrc(self, isrc: str) -> list[media.Track]:
"""Function to search all tracks with a specific ISRC code.
:param isrc: The ISRC of the Track.
:return: Returns a list of :class:`.Track` objects that have access to the session instance used.
"""
try:
res = self.request.request(
"GET",
"tracks",
params={
"filter[isrc]": isrc,
},
base_url="https://openapi.tidal.com/v2/").json()

return [self.track(track['id']) for track in res['data']]
except requests.HTTPError:
log.warning("Wrong ISRC code '%s'", isrc)
raise

def get_albums_by_barcode(self, barcode: str) -> list[album.Album]:
"""Function to search all albums with a specific UPC code.
:param barcode: The UPC of the Album.
:return: Returns a list of :class:`.Album` objects that have access to the session instance used.
"""
try:
res = self.request.request(
"GET",
"albums",
params={
"filter[barcodeId]": barcode,
},
base_url="https://openapi.tidal.com/v2/").json()

return [self.album(album['id']) for album in res['data']]
except HTTPError:
log.warning("Wrong barcode '%s'", barcode)
raise

def video(self, video_id: Optional[str] = None) -> media.Video:
"""Function to create a Video object with access to the session instance in a
smoother way. Calls :class:`tidalapi.Video(session=session, video_id=video_id)
Expand Down

0 comments on commit 5e3a2ee

Please sign in to comment.