Skip to content

Commit 5e3a2ee

Browse files
committed
add support for get track by isrc and get album by barcode id
1 parent 288fc1e commit 5e3a2ee

File tree

2 files changed

+48
-2
lines changed

2 files changed

+48
-2
lines changed

tidalapi/request.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ def basic_request(
6767
params: Optional[Params] = None,
6868
data: Optional[JsonObj] = None,
6969
headers: Optional[MutableMapping[str, str]] = None,
70+
base_url: Optional[str] = None,
7071
) -> requests.Response:
7172
request_params = {
7273
"sessionId": self.session.session_id,
@@ -90,7 +91,10 @@ def basic_request(
9091
headers["authorization"] = (
9192
self.session.token_type + " " + self.session.access_token
9293
)
93-
url = urljoin(self.session.config.api_v1_location, path)
94+
if base_url is None:
95+
base_url = self.session.config.api_v1_location
96+
97+
url = urljoin(base_url, path)
9498
request = self.session.request_session.request(
9599
method, url, params=request_params, data=data, headers=headers
96100
)
@@ -123,6 +127,7 @@ def request(
123127
params: Optional[Params] = None,
124128
data: Optional[JsonObj] = None,
125129
headers: Optional[MutableMapping[str, str]] = None,
130+
base_url: Optional[str] = None,
126131
) -> requests.Response:
127132
"""Method for tidal requests.
128133
@@ -133,10 +138,11 @@ def request(
133138
:param params: The parameters you want to supply with the request.
134139
:param data: The data you want to supply with the request.
135140
:param headers: The headers you want to include with the request
141+
:param base_url: The base url to use for the request
136142
:return: The json data at specified api endpoint.
137143
"""
138144

139-
request = self.basic_request(method, path, params, data, headers)
145+
request = self.basic_request(method, path, params, data, headers, base_url)
140146
log.debug("request: %s", request.request.url)
141147
try:
142148
request.raise_for_status()

tidalapi/session.py

+40
Original file line numberDiff line numberDiff line change
@@ -857,6 +857,46 @@ def track(
857857
log.warning("Track '%s' is unavailable", track_id)
858858
raise
859859

860+
def get_tracks_by_isrc(self, isrc: str) -> list[media.Track]:
861+
"""Function to search all tracks with a specific ISRC code.
862+
863+
:param isrc: The ISRC of the Track.
864+
:return: Returns a list of :class:`.Track` objects that have access to the session instance used.
865+
"""
866+
try:
867+
res = self.request.request(
868+
"GET",
869+
"tracks",
870+
params={
871+
"filter[isrc]": isrc,
872+
},
873+
base_url="https://openapi.tidal.com/v2/").json()
874+
875+
return [self.track(track['id']) for track in res['data']]
876+
except requests.HTTPError:
877+
log.warning("Wrong ISRC code '%s'", isrc)
878+
raise
879+
880+
def get_albums_by_barcode(self, barcode: str) -> list[album.Album]:
881+
"""Function to search all albums with a specific UPC code.
882+
883+
:param barcode: The UPC of the Album.
884+
:return: Returns a list of :class:`.Album` objects that have access to the session instance used.
885+
"""
886+
try:
887+
res = self.request.request(
888+
"GET",
889+
"albums",
890+
params={
891+
"filter[barcodeId]": barcode,
892+
},
893+
base_url="https://openapi.tidal.com/v2/").json()
894+
895+
return [self.album(album['id']) for album in res['data']]
896+
except HTTPError:
897+
log.warning("Wrong barcode '%s'", barcode)
898+
raise
899+
860900
def video(self, video_id: Optional[str] = None) -> media.Video:
861901
"""Function to create a Video object with access to the session instance in a
862902
smoother way. Calls :class:`tidalapi.Video(session=session, video_id=video_id)

0 commit comments

Comments
 (0)