tidalrs 0.4.1

Tidal (v1) API client
Documentation
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
use crate::Error;
use crate::List;
use crate::TIDAL_API_BASE_URL;
use crate::TidalClient;
use crate::artist::ArtistSummary;
use crate::track::Track;
use reqwest::Method;
use serde::{Deserialize, Serialize};
use serde_json::Value;

/// Represents a playlist from the Tidal catalog.
///
/// This structure contains all available information about a playlist,
/// including metadata, statistics, and modification capabilities.
#[derive(Default, Debug, Serialize, Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct Playlist {
    /// Unique playlist identifier (UUID format)
    pub uuid: String,
    /// Playlist title
    pub title: String,
    /// Tidal URL for the playlist
    pub url: String,
    /// Information about the playlist creator
    pub creator: PlaylistCreator,
    /// Playlist description
    #[serde(default)]
    pub description: String,

    /// Total number of tracks in the playlist
    pub number_of_tracks: u32,
    /// Total number of videos in the playlist
    pub number_of_videos: u32,
    /// Total duration of the playlist in seconds
    pub duration: u32,
    /// Popularity score for the playlist
    pub popularity: u32,

    /// ISO timestamp when the playlist was last updated
    pub last_updated: String,
    /// ISO timestamp when the playlist was created
    pub created: String,
    /// ISO timestamp when the last item was added to the playlist
    pub last_item_added_at: Option<String>,

    /// Type of playlist (e.g., "USER", "EDITORIAL")
    #[serde(rename = "type")]
    pub playlist_type: Option<String>,
    /// Whether the playlist is publicly visible
    pub public_playlist: bool,
    /// Playlist cover image identifier
    ///
    /// Use image_url() to get the full URL of the image
    pub image: Option<String>,
    /// Square version of the playlist cover image
    ///
    /// Use square_image_url() to get the full URL of the square image
    pub square_image: Option<String>,
    /// Custom image URL for the playlist
    pub custom_image_url: Option<String>,
    /// Artists promoted in this playlist
    pub promoted_artists: Option<Vec<ArtistSummary>>,

    /// ETag for concurrency control when modifying the playlist
    ///
    /// This is needed for adding or removing tracks from the playlist
    pub etag: Option<String>,
}

impl Playlist {
    /// Generate a URL for the playlist cover image at the specified dimensions.
    ///
    /// # Arguments
    ///
    /// * `height` - Height of the image in pixels
    /// * `width` - Width of the image in pixels
    ///
    /// # Returns
    ///
    /// Returns `Some(String)` with the full URL if an image is available,
    /// or `None` if no image is set.
    pub fn image_url(&self, height: u16, width: u16) -> Option<String> {
        self.image.as_ref().map(|image| {
            let image_path = image.replace('-', "/");
            format!("https://resources.tidal.com/images/{image_path}/{height}x{width}.jpg")
        })
    }

    /// Generate a URL for the square playlist cover image at the specified size.
    ///
    /// # Arguments
    ///
    /// * `size` - Size of the square image in pixels
    ///
    /// # Returns
    ///
    /// Returns `Some(String)` with the full URL if a square image is available,
    /// or `None` if no square image is set.
    pub fn square_image_url(&self, size: u16) -> Option<String> {
        self.square_image.as_ref().map(|square_image| {
            let square_image_path = square_image.replace('-', "/");
            format!("https://resources.tidal.com/images/{square_image_path}/{size}x{size}.jpg")
        })
    }
}

/// Information about the creator of a playlist.
///
/// This structure contains details about who created the playlist,
/// which can be a user or system-generated content.
#[derive(Default, Debug, Serialize, Deserialize, Clone)]
pub struct PlaylistCreator {
    /// The user ID of the playlist creator.
    /// Will be None or zero if the playlist creator is not a known user.
    #[serde(default)]
    pub id: Option<u64>,
}

impl TidalClient {
    /// Get playlist information by ID.
    ///
    /// # Arguments
    ///
    /// * `playlist_id` - The unique identifier (UUID) of the playlist
    ///
    /// # Returns
    ///
    /// Returns a `Playlist` structure with all available metadata.
    ///
    /// # Example
    ///
    /// ```no_run
    /// let playlist = client.playlist("12345678-1234-1234-1234-123456789abc").await?;
    /// println!("Playlist: {}", playlist.title);
    /// ```
    pub async fn playlist(&self, playlist_id: &str) -> Result<Playlist, Error> {
        let url = format!("{TIDAL_API_BASE_URL}/playlists/{playlist_id}");
        let params = serde_json::json!({
            "countryCode": self.get_country_code(),
            "locale": self.get_locale(),
            "deviceType": self.get_device_type().as_ref(),
        });
        let resp: Playlist = self
            .do_request(Method::GET, &url, Some(params), None)
            .await?;

        Ok(resp)
    }

    /// Get all tracks in a specific playlist with pagination support.
    ///
    /// # Arguments
    ///
    /// * `playlist_id` - The unique identifier (UUID) of the playlist
    /// * `offset` - Number of tracks to skip (default: 0)
    /// * `limit` - Maximum number of tracks to return (default: 100)
    ///
    /// # Returns
    ///
    /// Returns a paginated list of tracks in the playlist.
    ///
    /// # Example
    ///
    /// ```no_run
    /// let tracks = client.playlist_tracks("12345678-1234-1234-1234-123456789abc", Some(0), Some(20)).await?;
    /// for track in tracks.items {
    ///     println!("Track: {}", track.title);
    /// }
    /// ```
    pub async fn playlist_tracks(
        &self,
        playlist_id: &str,
        offset: Option<u32>,
        limit: Option<u32>,
    ) -> Result<List<Track>, Error> {
        let offset = offset.unwrap_or(0);
        let limit = limit.unwrap_or(100);
        let url = format!("{TIDAL_API_BASE_URL}/playlists/{playlist_id}/tracks");
        let params = serde_json::json!({
            "offset": offset,
            "limit": limit,
            "countryCode": self.get_country_code(),
            "locale": self.get_locale(),
            "deviceType": self.get_device_type().as_ref(),
        });

        let resp: List<Track> = self
            .do_request(Method::GET, &url, Some(params), None)
            .await?;

        Ok(resp)
    }

    /// Create a new playlist for the authenticated user.
    ///
    /// # Arguments
    ///
    /// * `title` - The title of the new playlist
    /// * `description` - A description of the playlist
    ///
    /// # Returns
    ///
    /// Returns the newly created `Playlist` with all metadata.
    ///
    /// # Example
    ///
    /// ```no_run
    /// let playlist = client.create_playlist("My Favorites", "A collection of my favorite songs").await?;
    /// println!("Created playlist: {}", playlist.title);
    /// ```
    pub async fn create_playlist(&self, title: &str, description: &str) -> Result<Playlist, Error> {
        let user_id = self
            .get_user_id()
            .ok_or(Error::UserAuthenticationRequired)?;
        let url = format!("{TIDAL_API_BASE_URL}/users/{user_id}/playlists");
        let params = serde_json::json!({
            "title": title,
            "description": description,
            "countryCode": self.get_country_code(),
            "locale": self.get_locale(),
            "deviceType": self.get_device_type().as_ref(),
        });
        let resp: Playlist = self
            .do_request(Method::POST, &url, Some(params), None)
            .await?;
        Ok(resp)
    }

    /// Add multiple tracks to a playlist.
    ///
    /// # Arguments
    ///
    /// * `playlist_id` - The unique identifier (UUID) of the playlist
    /// * `playlist_etag` - The ETag from the playlist (required for concurrency control)
    /// * `track_ids` - Vector of track IDs to add to the playlist
    /// * `add_dupes` - Whether to add duplicate tracks (true) or fail if duplicates exist (false)
    ///
    /// # Example
    ///
    /// ```no_run
    /// let playlist = client.playlist("12345678-1234-1234-1234-123456789abc").await?;
    /// let track_ids = vec![123456789, 987654321];
    /// client.add_tracks_to_playlist(&playlist.uuid, &playlist.etag.unwrap(), track_ids, false).await?;
    /// println!("Tracks added to playlist!");
    /// ```
    pub async fn add_tracks_to_playlist(
        &self,
        playlist_id: &str,
        playlist_etag: &str,
        track_ids: Vec<u64>,
        add_dupes: bool,
    ) -> Result<(), Error> {
        let url = format!("{TIDAL_API_BASE_URL}/playlists/{playlist_id}/items");

        // Convert track IDs to comma-separated string
        let track_ids_str = track_ids
            .iter()
            .map(|id| id.to_string())
            .collect::<Vec<_>>()
            .join(",");

        let on_dupes = if add_dupes { "ADD" } else { "FAIL" };

        let params = serde_json::json!({
            "trackIds": track_ids_str,
            "onDupes": on_dupes,
            "countryCode": self.get_country_code(),
            "locale": self.get_locale(),
            "deviceType": self.get_device_type().as_ref(),
        });

        let _: Value = self
            .do_request(Method::POST, &url, Some(params), Some(playlist_etag))
            .await?;

        Ok(())
    }

    /// Remove a track from a playlist by its index position.
    ///
    /// # Arguments
    ///
    /// * `playlist_id` - The unique identifier (UUID) of the playlist
    /// * `playlist_etag` - The ETag from the playlist (required for concurrency control)
    /// * `index` - The zero-based index of the track to remove
    ///
    /// # Example
    ///
    /// ```no_run
    /// let playlist = client.playlist("12345678-1234-1234-1234-123456789abc").await?;
    /// client.remove_track_from_playlist_by_index(&playlist.uuid, &playlist.etag.unwrap(), 0).await?;
    /// println!("Track removed from playlist!");
    /// ```
    pub async fn remove_track_from_playlist_by_index(
        &self,
        playlist_id: &str,
        playlist_etag: &str,
        index: usize,
    ) -> Result<(), Error> {
        let url = format!("{TIDAL_API_BASE_URL}/playlists/{playlist_id}/items/{index}");

        let _: Value = self
            .do_request(Method::DELETE, &url, None, Some(playlist_etag))
            .await?;

        Ok(())
    }

    /// Remove a specific track from a playlist by track ID.
    ///
    /// This method will search through the playlist to find the track
    /// and remove it. If the track appears multiple times, only the
    /// first occurrence will be removed.
    ///
    /// # Arguments
    ///
    /// * `playlist_id` - The unique identifier (UUID) of the playlist
    /// * `playlist_etag` - The ETag from the playlist (required for concurrency control)
    /// * `track_id` - The unique identifier of the track to remove
    ///
    /// # Returns
    ///
    /// Returns an error if the track is not found in the playlist.
    ///
    /// # Example
    ///
    /// ```no_run
    /// let playlist = client.playlist("12345678-1234-1234-1234-123456789abc").await?;
    /// client.remove_track_from_playlist(&playlist.uuid, &playlist.etag.unwrap(), 123456789).await?;
    /// println!("Track removed from playlist!");
    /// ```
    pub async fn remove_track_from_playlist(
        &self,
        playlist_id: &str,
        playlist_etag: &str,
        track_id: u64,
    ) -> Result<(), Error> {
        // Find the index of the track in the playlist

        let track_index: Option<u32>;
        let mut offset: u32 = 0;

        'outer: loop {
            let playlist_tracks = self
                .playlist_tracks(playlist_id, Some(offset), None)
                .await?;

            for (index, track) in playlist_tracks.items.iter().enumerate() {
                if track.id == track_id {
                    track_index = Some(index as u32);
                    break 'outer;
                }
            }

            if playlist_tracks.num_left() == 0 {
                return Err(Error::PlaylistTrackNotFound(
                    playlist_id.to_string(),
                    track_id,
                ));
            }

            offset += playlist_tracks.items.len() as u32;
        }

        let track_index = track_index.ok_or(Error::PlaylistTrackNotFound(
            playlist_id.to_string(),
            track_id,
        ))?;

        self.remove_track_from_playlist_by_index(playlist_id, playlist_etag, track_index as usize)
            .await?;

        Ok(())
    }

    /// Get all playlists created by the authenticated user.
    ///
    /// # Arguments
    ///
    /// * `offset` - Number of playlists to skip (default: 0)
    /// * `limit` - Maximum number of playlists to return (default: 100)
    ///
    /// # Returns
    ///
    /// Returns a paginated list of playlists created by the user.
    ///
    /// # Example
    ///
    /// ```no_run
    /// let playlists = client.user_playlists(None, Some(10)).await?;
    /// for playlist in playlists.items {
    ///     println!("Playlist: {}", playlist.title);
    /// }
    /// ```
    pub async fn user_playlists(
        &self,
        offset: Option<u32>,
        limit: Option<u32>,
    ) -> Result<List<Playlist>, Error> {
        let user_id = self
            .get_user_id()
            .ok_or(Error::UserAuthenticationRequired)?;
        let offset = offset.unwrap_or(0);
        let limit = limit.unwrap_or(100);
        let url = format!("{TIDAL_API_BASE_URL}/users/{user_id}/playlists");
        let params = serde_json::json!({
            "offset": offset,
            "limit": limit,
            "countryCode": self.get_country_code(),
            "locale": self.get_locale(),
            "deviceType": self.get_device_type().as_ref(),
        });

        let resp: List<Playlist> = self
            .do_request(Method::GET, &url, Some(params), None)
            .await?;

        Ok(resp)
    }
}