sync_dis_boi 0.6.2

a music streaming platform synchronization tool
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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
mod model;
mod response;

use std::io::Read;
use std::path::PathBuf;

use async_trait::async_trait;
use color_eyre::Result;
use color_eyre::eyre::eyre;
use model::{TidalMediaResponse, TidalMediaResponseSingle, TidalOAuthDeviceRes};
use reqwest::Response;
use reqwest::header::HeaderMap;
use serde::de::DeserializeOwned;
use serde_json::json;
use tracing::info;

use self::model::{TidalPageResponse, TidalPlaylistResponse, TidalSongItemResponse};
use crate::ConfigArgs;
use crate::music_api::{
    MusicApi, MusicApiType, OAuthRefreshToken, OAuthReqToken, OAuthToken, PLAYLIST_DESC, Playlist,
    Playlists, Song, Songs,
};
use crate::tidal::model::{TidalPlaylistCreateResponse, TidalSearchResponse};

pub struct TidalApi {
    client: reqwest::Client,
    config: ConfigArgs,
    user_id: String,
    country_code: String,
}

#[derive(Debug)]
enum HttpMethod<'a> {
    Get(&'a serde_json::Value),
    Post(&'a serde_json::Value),
    Put(&'a serde_json::Value),
}

impl TidalApi {
    const API_URL: &'static str = "https://api.tidal.com";
    const API_V2_URL: &'static str = "https://openapi.tidal.com/v2";

    const AUTH_URL: &'static str = "https://auth.tidal.com/v1/oauth2/device_authorization";
    const TOKEN_URL: &'static str = "https://auth.tidal.com/v1/oauth2/token";
    const SCOPE: &'static str = "r_usr w_usr w_sub";

    pub async fn new(
        client_id: &str,
        client_secret: &str,
        oauth_token_path: PathBuf,
        clear_cache: bool,
        config: ConfigArgs,
    ) -> Result<Self> {
        let token = if !oauth_token_path.exists() || clear_cache {
            info!("requesting new token");
            Self::request_token(client_id, client_secret, config.debug).await?
        } else {
            info!("refreshing token");
            Self::refresh_token(client_id, client_secret, &oauth_token_path, config.debug).await?
        };
        // Write new token
        let mut file = std::fs::File::create(&oauth_token_path)?;
        serde_json::to_writer(&mut file, &token)?;

        let mut headers = HeaderMap::new();
        headers.insert(
            "Authorization",
            format!("Bearer {}", token.access_token).parse()?,
        );
        headers.insert("Content-Type", "application/vnd.tidal.v1+json".parse()?);

        let mut client = reqwest::Client::builder()
            .cookie_store(true)
            .default_headers(headers);
        if let Some(proxy) = &config.proxy {
            client = client
                .proxy(reqwest::Proxy::all(proxy)?)
                .danger_accept_invalid_certs(true)
        }
        let client = client.build()?;

        let url = format!("{}/users/me", Self::API_V2_URL);
        let me_res: TidalMediaResponseSingle = Self::make_request_json_internal(
            &client,
            &url,
            &HttpMethod::Get(&json!({})),
            None,
            config.debug,
        )
        .await?;
        let country_code = me_res.data.attributes.country.unwrap_or("US".into());

        Ok(Self {
            client,
            config,
            user_id: me_res.data.id,
            country_code,
        })
    }

    async fn request_token(
        client_id: &str,
        client_secret: &str,
        debug: bool,
    ) -> Result<OAuthToken> {
        let client = reqwest::Client::new();
        let params = json!({
            "client_id": client_id,
            "scope": Self::SCOPE,
        });

        let device_res: TidalOAuthDeviceRes = Self::make_request_json_internal(
            &client,
            Self::AUTH_URL,
            &HttpMethod::Post(&params),
            None,
            debug,
        )
        .await?;

        let url = format!("https://{}", device_res.verification_uri_complete);

        webbrowser::open(&url)?;
        info!("please authorize the app in your browser and press enter");
        std::io::stdin().read_exact(&mut [0])?;

        let auth_token = OAuthReqToken {
            client_id: client_id.to_string(),
            device_code: device_res.device_code.clone(),
            grant_type: "urn:ietf:params:oauth:grant-type:device_code".to_string(),
            scope: Self::SCOPE.to_string(),
        };
        let res = client
            .post(Self::TOKEN_URL)
            .basic_auth(client_id, Some(client_secret))
            .form(&auth_token)
            .send()
            .await?;
        // TODO: move to Self::make_request_json_internal, the basic_auth is the problem
        let token: OAuthToken = res.json().await?;

        Ok(token)
    }

    async fn refresh_token(
        client_id: &str,
        client_secret: &str,
        oauth_token_path: &PathBuf,
        debug: bool,
    ) -> Result<OAuthToken> {
        let client = reqwest::Client::new();
        let reader = std::fs::File::open(oauth_token_path)?;
        let mut oauth_token: OAuthToken = serde_json::from_reader(reader)?;

        let params = json!({
            "client_id": client_id,
            "client_secret": client_secret,
            "grant_type": "refresh_token",
            "refresh_token": &oauth_token.refresh_token,
        });
        let refresh_token: OAuthRefreshToken = Self::make_request_json_internal(
            &client,
            Self::TOKEN_URL,
            &HttpMethod::Post(&params),
            None,
            debug,
        )
        .await?;

        oauth_token.access_token = refresh_token.access_token;
        oauth_token.expires_in = refresh_token.expires_in;
        oauth_token.scope = refresh_token.scope;
        Ok(oauth_token)
    }

    async fn paginated_request<T>(
        &self,
        url: &str,
        method: &HttpMethod<'_>,
        limit: usize,
    ) -> Result<TidalPageResponse<T>>
    where
        T: DeserializeOwned + std::fmt::Debug,
    {
        let mut res: TidalPageResponse<T> = self.make_request_json(url, method, limit, 0).await?;
        if res.items.is_empty() {
            return Ok(res);
        }

        let mut offset = limit;
        while offset < res.total_number_of_items {
            let res2: TidalPageResponse<T> =
                self.make_request_json(url, method, limit, offset).await?;
            if res2.items.is_empty() {
                break;
            }
            res.items.extend(res2.items);
            offset += limit;
        }
        Ok(res)
    }

    async fn make_request(
        &self,
        url: &str,
        method: &HttpMethod<'_>,
        lim_off: Option<(usize, usize)>,
    ) -> Result<Response> {
        Self::make_request_internal(&self.client, url, method, lim_off).await
    }

    async fn make_request_json<T>(
        &self,
        url: &str,
        method: &HttpMethod<'_>,
        limit: usize,
        offset: usize,
    ) -> Result<T>
    where
        T: DeserializeOwned,
    {
        Self::make_request_json_internal(
            &self.client,
            url,
            method,
            Some((limit, offset)),
            self.config.debug,
        )
        .await
    }

    async fn make_request_internal(
        client: &reqwest::Client,
        url: &str,
        method: &HttpMethod<'_>,
        lim_off: Option<(usize, usize)>,
    ) -> Result<Response> {
        let mut request = match method {
            HttpMethod::Get(p) => client.get(url).query(p),
            HttpMethod::Post(b) => client.post(url).form(b),
            HttpMethod::Put(b) => client.put(url).form(b),
        };
        if let Some((limit, offset)) = lim_off {
            request = request.query(&[("limit", limit), ("offset", offset)]);
        }
        let res = request.send().await?;
        let res = res.error_for_status()?;
        Ok(res)
    }

    async fn make_request_json_internal<T>(
        client: &reqwest::Client,
        url: &str,
        method: &HttpMethod<'_>,
        lim_off: Option<(usize, usize)>,
        debug: bool,
    ) -> Result<T>
    where
        T: DeserializeOwned,
    {
        let res = Self::make_request_internal(client, url, method, lim_off).await?;
        let obj = if debug {
            let text = res.text().await?;
            std::fs::write("debug/tidal_last_res.json", &text)?;
            serde_json::from_str(&text)?
        } else {
            res.json().await?
        };
        Ok(obj)
    }
}

#[async_trait]
impl MusicApi for TidalApi {
    fn api_type(&self) -> MusicApiType {
        MusicApiType::Tidal
    }

    fn country_code(&self) -> &str {
        &self.country_code
    }

    async fn create_playlist(&self, name: &str, public: bool) -> Result<Playlist> {
        let url = format!(
            "{}/v2/my-collection/playlists/folders/create-playlist",
            Self::API_URL
        );
        let params = json!({
            "name": name,
            "description": PLAYLIST_DESC,
            "public": public,
            "folderId": "root"
        });
        let res: TidalPlaylistCreateResponse = self
            .make_request_json(&url, &HttpMethod::Put(&params), 0, 5)
            .await?;

        Ok(Playlist {
            id: res.data.uuid,
            name: name.to_string(),
            songs: vec![],
        })
    }

    async fn get_playlists_info(&self) -> Result<Vec<Playlist>> {
        let url = format!("{}/v1/users/{}/playlists", Self::API_URL, self.user_id);
        let params = json!({
            "countryCode": self.country_code,
        });
        let res: TidalPageResponse<TidalPlaylistResponse> = self
            .paginated_request(&url, &HttpMethod::Get(&params), 100)
            .await?;
        let playlists: Playlists = res.try_into()?;
        Ok(playlists.0)
    }

    async fn get_playlist_songs(&self, id: &str) -> Result<Vec<Song>> {
        let url = format!("{}/v1/playlists/{}/items", Self::API_URL, id);
        let params = json!({
            "countryCode": self.country_code,
        });
        // NOTE: a limit > 100 triggers a 400 error
        let res: TidalPageResponse<TidalSongItemResponse> = self
            .paginated_request(&url, &HttpMethod::Get(&params), 100)
            .await?;
        let songs: Songs = res.try_into()?;
        Ok(songs.0)
    }

    async fn add_songs_to_playlist(&self, playlist: &mut Playlist, songs: &[Song]) -> Result<()> {
        if songs.is_empty() {
            return Ok(());
        }

        let url = format!("{}/v1/playlists/{}", Self::API_URL, playlist.id);
        let params = json!({
            "countryCode": self.country_code,
        });
        let res = self
            .make_request(&url, &HttpMethod::Get(&params), None)
            .await?;
        let etag = res
            .headers()
            .get("ETag")
            .ok_or(eyre!("No ETag in Tidal Response"))?;

        // TODO: accomodate make_request to access request headers + body

        let url = format!("{}/v1/playlists/{}/items", Self::API_URL, playlist.id);
        let params = json!({
            "trackIds": songs.iter().map(|s| s.id.as_str()).collect::<Vec<_>>().join(","),
            "onDuplicate": "FAIL",
            "onArtifactNotFound": "FAIL",
        });
        let res = self
            .client
            .post(url)
            .header("If-None-Match", etag)
            .form(&params)
            .send()
            .await?;
        res.error_for_status()?;

        Ok(())
    }

    async fn remove_songs_from_playlist(
        &self,
        _playlist: &mut Playlist,
        _songs_ids: &[Song],
    ) -> Result<()> {
        todo!()
    }

    async fn delete_playlist(&self, playlist: Playlist) -> Result<()> {
        let url = format!(
            "{}/v2/my-collection/playlists/folders/remove",
            Self::API_URL
        );
        let params = json!({
            "trns": format!("trn:playlist:{}", playlist.id),
        });
        let _res = self
            .make_request(&url, &HttpMethod::Put(&params), None)
            .await?;
        Ok(())
    }

    async fn search_song(&self, song: &Song) -> Result<Option<Song>> {
        if let Some(isrc) = &song.isrc {
            let url = format!("{}/tracks", Self::API_V2_URL);
            let params = json!({
                "countryCode": self.country_code,
                "include": "albums,artists",
                "filter[isrc]": isrc.to_uppercase(),
            });
            let res: TidalMediaResponse = self
                .make_request_json(&url, &HttpMethod::Get(&params), 0, 1)
                .await?;
            if res.data.is_empty() {
                return Ok(None);
            }
            let mut res_songs: Songs = res.try_into()?;
            if res_songs.0.is_empty() {
                return Ok(None);
            }
            return Ok(Some(res_songs.0.remove(0)));
        }

        let url = format!("{}/v1/search", Self::API_URL);
        let mut queries = song.build_queries();

        while let Some(query) = queries.pop() {
            let params = json!({
                "countryCode": self.country_code,
                "query": query,
                "type": "TRACKS",
            });
            let res: TidalSearchResponse = self
                .make_request_json(&url, &HttpMethod::Get(&params), 0, 3)
                .await?;
            let res_songs: Songs = res.try_into()?;
            // iterate over top 3 results
            for res_song in res_songs.0.into_iter().take(3) {
                if song.compare(&res_song) {
                    return Ok(Some(res_song));
                }
            }
        }
        Ok(None)
    }

    async fn add_likes(&self, songs: &[Song]) -> Result<()> {
        if songs.is_empty() {
            return Ok(());
        }

        let url = format!(
            "{}/v1/users/{}/favorites/tracks",
            Self::API_URL,
            self.user_id
        );
        let tracks = songs.iter().map(|s| s.id.as_str()).collect::<Vec<_>>();

        // NOTE: we get error 500 if we like too much songs at once
        for tracks_chunk in tracks.chunks(100) {
            let params = json!({
                "countryCode": self.country_code,
                "trackIds": tracks_chunk.join(","),
                "onArtifactNotFound": "FAIL",
            });
            self.make_request(&url, &HttpMethod::Post(&params), None)
                .await?;
        }
        Ok(())
    }

    async fn get_likes(&self) -> Result<Vec<Song>> {
        let url = format!(
            "{}/v1/users/{}/favorites/tracks",
            Self::API_URL,
            self.user_id
        );
        let params = json!({
            "countryCode": self.country_code,
        });
        let res: TidalPageResponse<TidalSongItemResponse> = self
            .paginated_request(&url, &HttpMethod::Get(&params), 1000)
            .await?;
        let songs: Songs = res.try_into()?;
        Ok(songs.0)
    }
}