1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
use base64::{Engine, engine::general_purpose};
use quick_xml::Reader;
use quick_xml::events::Event;
use crate::{
client::{
TidalClient,
models::{
mixes::TrackMixInfo,
playback::AssetPresentation,
track::{
DashManifest, ManifestType, Track, TrackManifest,
TrackPlaybackInfoPostPaywallResponse,
},
},
},
error::TidalError,
ids::TrackId,
};
impl TidalClient {
/// Retrieves track information by track ID
///
/// # Example
///
/// ```no_run
/// # use tidlers::{TidalClient, auth::init::TidalAuth};
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
/// # let auth = TidalAuth::with_oauth();
/// # let client = TidalClient::new(&auth);
/// let track = client.get_track("123456789").await?;
/// println!("Track: {} by {}", track.title, track.artist.name);
/// # Ok(())
/// # }
/// ```
pub async fn get_track(&self, track_id: impl Into<TrackId>) -> Result<Track, TidalError> {
let track_id = track_id.into();
self.request(reqwest::Method::GET, format!("/tracks/{}/", track_id))
.with_country_code()
.send()
.await
}
/// Parses a DASH XML manifest into a structured format
fn parse_dash_manifest(xml: &str) -> Result<DashManifest, TidalError> {
let mut reader = Reader::from_str(xml);
reader.config_mut().trim_text(true);
let mut urls = Vec::new();
let mut mime_type = String::new();
let mut codecs = String::new();
let mut bitrate = None;
let mut buf = Vec::new();
let mut init_url = None;
let mut media_url = None;
let mut timescale = None;
let mut duration = None;
let mut start_number = None;
loop {
match reader.read_event_into(&mut buf) {
Ok(Event::Empty(e)) | Ok(Event::Start(e)) => match e.name().as_ref() {
b"AdaptationSet" => {
for attr in e.attributes().flatten() {
if attr.key.as_ref() == b"mimeType" {
mime_type = String::from_utf8_lossy(&attr.value).to_string();
}
}
}
b"Representation" => {
for attr in e.attributes().flatten() {
match attr.key.as_ref() {
b"codecs" => {
codecs = String::from_utf8_lossy(&attr.value).to_string();
}
b"bandwidth" => {
bitrate =
String::from_utf8_lossy(&attr.value).parse::<u32>().ok();
}
_ => {}
}
}
}
b"SegmentTemplate" => {
for attr in e.attributes().flatten() {
match attr.key.as_ref() {
b"initialization" => {
init_url =
Some(String::from_utf8_lossy(&attr.value).to_string());
}
b"media" => {
media_url =
Some(String::from_utf8_lossy(&attr.value).to_string());
}
b"timescale" => {
timescale =
String::from_utf8_lossy(&attr.value).parse::<u32>().ok();
}
b"duration" => {
duration =
String::from_utf8_lossy(&attr.value).parse::<u32>().ok();
}
b"startNumber" => {
start_number =
String::from_utf8_lossy(&attr.value).parse::<u32>().ok();
}
_ => {}
}
}
}
b"BaseURL" => {
if let Ok(Event::Text(e)) = reader.read_event_into(&mut buf) {
let url = String::from_utf8_lossy(e.as_ref()).to_string();
if !url.is_empty() {
urls.push(url);
}
}
}
_ => {}
},
Ok(Event::Eof) => break,
Err(e) => return Err(TidalError::Other(format!("XML parsing error: {}", e))),
_ => {}
}
buf.clear();
}
let initialization_url = init_url.clone();
let media_url_template = media_url.clone();
if let Some(init) = init_url {
urls.push(init);
}
if let Some(media) = media_url {
urls.push(media);
}
if urls.is_empty() {
return Err(TidalError::Other(
"No URLs found in DASH manifest".to_string(),
));
}
Ok(DashManifest {
mime_type,
codecs,
urls,
bitrate,
initialization_url,
media_url_template,
timescale,
duration,
start_number,
})
}
/// Gets track playback information including streaming URLs and manifest
///
/// # Example
///
/// ```no_run
/// # use tidlers::{TidalClient, auth::init::TidalAuth};
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
/// # let auth = TidalAuth::with_oauth();
/// # let client = TidalClient::new(&auth);
/// let playback = client.get_track_postpaywall_playback_info("123456789").await?;
/// if let Some(url) = playback.get_primary_url() {
/// println!("Stream URL: {}", url);
/// }
/// # Ok(())
/// # }
/// ```
pub async fn get_track_postpaywall_playback_info(
&self,
track_id: impl Into<TrackId>,
) -> Result<TrackPlaybackInfoPostPaywallResponse, TidalError> {
let track_id = track_id.into();
let audio_quality = self.session.audio_quality.to_string();
let playback_mode = self.session.playback_mode.to_string();
let body: String = self
.request(
reqwest::Method::GET,
format!("/tracks/{}/playbackinfopostpaywall", track_id),
)
.with_country_code()
.with_param("audioquality", audio_quality)
.with_param("playbackmode", playback_mode)
.with_param("assetpresentation", AssetPresentation::Full.to_string())
.send_raw()
.await?;
let parsed = serde_json::from_str::<serde_json::Value>(&body)?;
let manifest_decoded =
general_purpose::STANDARD.decode(parsed["manifest"].as_str().unwrap())?;
let manifest_decoded_str = String::from_utf8(manifest_decoded)?;
let mut response: TrackPlaybackInfoPostPaywallResponse =
serde_json::from_str::<TrackPlaybackInfoPostPaywallResponse>(&body)?;
// Try to parse as JSON first (for LOW, HIGH, LOSSLESS)
if let Ok(json_manifest) = serde_json::from_str::<TrackManifest>(&manifest_decoded_str) {
response.manifest = Some(json_manifest.clone());
response.manifest_parsed = Some(ManifestType::Json(json_manifest));
} else {
// If JSON parsing fails, try DASH XML (for HiRes)
match Self::parse_dash_manifest(&manifest_decoded_str) {
Ok(dash_manifest) => {
response.manifest_parsed = Some(ManifestType::Dash(dash_manifest));
}
Err(e) => {
return Err(TidalError::Other(format!(
"Failed to parse manifest as JSON or DASH: {}",
e
)));
}
}
}
Ok(response)
}
pub async fn get_track_mix(
&self,
track_id: impl Into<TrackId>,
) -> Result<TrackMixInfo, TidalError> {
let track_id = track_id.into();
self.request(reqwest::Method::GET, format!("/tracks/{}/mix", track_id))
.with_country_code()
.send()
.await
}
}