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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
//! Media streaming types and formats for Deezer.
//!
//! This module handles media access requests and responses, including:
//! * Track/episode download URLs
//! * Audio formats and quality levels
//! * Content encryption
//! * Access tokens and expiry
//! * External streaming URLs (podcasts)
//! * Multiple quality streams (radio)
//!
//! # Authentication
//!
//! Media access requires two types of tokens:
//! * License token - For general media access rights
//! * Track tokens - For specific content access
//!
//! Both tokens have expiration times and must be refreshed periodically.
//!
//! # Content Types
//!
//! Three main content categories:
//! * Regular tracks - Encrypted, quality selection, token auth
//! * External content - No encryption, direct URLs
//! * Previews - Short samples, usually no encryption
//!
//! Each type may have different authentication and delivery requirements.
//!
//! # Error Handling
//!
//! Media access can fail in several ways:
//! * Authentication errors (invalid/expired tokens)
//! * Availability errors (geo-restrictions, takedowns)
//! * Technical errors (network issues, invalid formats)
//!
//! Errors include both a code and human-readable message.
//! Common error codes:
//! * 404 - Content not found
//! * 403 - Access denied
//! * 429 - Too many requests
//!
//! # Wire Format
//!
//! Request:
//! ```json
//! {
//! "license_token": "secret",
//! "media": [{
//! "type": "FULL",
//! "formats": [{
//! "cipher": "BF_CBC_STRIPE",
//! "format": "MP3_320"
//! }]
//! }],
//! "track_tokens": ["token1", "token2"]
//! }
//! ```
//!
//! Response:
//! ```json
//! {
//! "data": [{
//! "media": [{
//! "media_type": "FULL",
//! "cipher": {"type": "BF_CBC_STRIPE"},
//! "format": "MP3_320",
//! "sources": [{
//! "url": "https://...",
//! "provider": "cdn"
//! }],
//! "nbf": 1234567890,
//! "exp": 1234599999
//! }]
//! }]
//! }
//! ```
use ;
use ;
use ;
use Url;
use Redact;
use AudioQuality;
/// Media access request.
///
/// Used to request streaming URLs for tracks with specific
/// format and encryption requirements.
/// Media format request.
///
/// Specifies the desired media type (full/preview) and formats
/// with their encryption methods. Multiple format/cipher combinations
/// can be requested to handle fallback scenarios.
/// Media content type.
///
/// Determines whether to return the full track or just
/// a preview clip.
/// Formats the media type for display.
///
/// Shows either "FULL" or "PREVIEW" matching the protocol's
/// string representation.
///
/// # Examples
///
/// ```rust
/// use deezer::protocol::media::Type;
///
/// assert_eq!(Type::FULL.to_string(), "FULL");
/// assert_eq!(Type::PREVIEW.to_string(), "PREVIEW");
/// ```
/// Format and encryption combination.
///
/// Specifies both the audio format (quality level) and
/// encryption method for the content. Used to request
/// specific quality/security combinations.
///
/// For livestreams, only `EXTERNAL` format with `NONE` cipher is used
/// since radio streams use direct unencrypted URLs.
///
/// # Examples
///
/// ```rust
/// use deezer::protocol::media::{CipherFormat, Cipher, Format};
///
/// // For regular tracks
/// let format = CipherFormat {
/// cipher: Cipher::BF_CBC_STRIPE,
/// format: Format::MP3_320,
/// };
///
/// // For livestreams
/// let format = CipherFormat {
/// cipher: Cipher::NONE,
/// format: Format::EXTERNAL,
/// };
/// ```
/// Content encryption method.
/// Formats the cipher type for display.
///
/// Shows either "`BF_CBC_STRIPE`" or "NONE" matching the protocol's
/// string representation.
///
/// # Examples
///
/// ```rust
/// use deezer::protocol::media::Cipher;
///
/// assert_eq!(Cipher::BF_CBC_STRIPE.to_string(), "BF_CBC_STRIPE");
/// assert_eq!(Cipher::NONE.to_string(), "NONE");
/// ```
/// Audio format and quality level.
///
/// Represents different audio formats and their quality levels,
/// mapped to specific numeric IDs in the protocol.
/// Formats the audio format for display.
///
/// Shows the format name (e.g., "`MP3_320`", "FLAC") matching
/// the protocol's string representation.
///
/// # Examples
///
/// ```rust
/// use deezer::protocol::media::Format;
///
/// assert_eq!(Format::MP3_320.to_string(), "MP3_320");
/// assert_eq!(Format::FLAC.to_string(), "FLAC");
/// ```
/// Converts a media format to its corresponding audio quality level.
///
/// Maps format types to quality levels:
/// * `MP3_64` -> Basic (64 kbps)
/// * `MP3_128` -> Standard (128 kbps)
/// * `MP3_320` -> High (320 kbps)
/// * FLAC -> Lossless
/// * Others -> Unknown
///
/// # Examples
///
/// ```rust
/// use deezer::protocol::{media::Format, connect::AudioQuality};
///
/// assert_eq!(AudioQuality::from(Format::MP3_320), AudioQuality::High);
/// assert_eq!(AudioQuality::from(Format::FLAC), AudioQuality::Lossless);
/// assert_eq!(AudioQuality::from(Format::EXTERNAL), AudioQuality::Unknown);
/// ```
/// Media access response.
///
/// Contains either:
/// * Successful media access information with URLs and formats
/// * Error details when access fails
///
/// Multiple media entries may be returned when requesting
/// multiple tracks or formats.
/// Response data variant.
///
/// Can contain either media information or error details.
/// Media access error.
///
/// Represents an error response from the media server, containing
/// both an error code and descriptive message.
///
/// # Wire Format
///
/// ```json
/// {
/// "errors": [{
/// "code": 404,
/// "message": "Track not found"
/// }]
/// }
/// ```
///
/// # Examples
///
/// ```rust
/// use deezer::protocol::media::Error;
///
/// let error = Error {
/// code: 404,
/// message: "Track not found".to_string(),
/// };
///
/// // Displays as: "Track not found (404)"
/// println!("{}", error);
/// ```
/// Formats an error for display.
///
/// Shows both the error message and code in the format:
/// `"{message} ({code})"`
///
/// # Examples
///
/// ```rust
/// use deezer::protocol::media::Error;
///
/// let error = Error {
/// code: 404,
/// message: "Not found".to_string(),
/// };
/// assert_eq!(error.to_string(), "Not found (404)");
/// ```
/// Media access details.
///
/// Contains all information needed to access a media file,
/// including URLs, format, and validity period.
/// Encryption method wrapper for media content.
///
/// Used in responses to specify how the media content is encrypted.
/// The wrapper structure matches the protocol's JSON format.
///
/// # Wire Format
///
/// ```json
/// {
/// "type": "BF_CBC_STRIPE" // or "NONE"
/// }
/// ```
///
/// # Examples
///
/// ```rust
/// use deezer::protocol::media::{Cipher, CipherType};
///
/// let cipher = CipherType {
/// typ: Cipher::BF_CBC_STRIPE,
/// };
///
/// // Default is BF_CBC_STRIPE
/// assert_eq!(CipherType::default().typ, Cipher::BF_CBC_STRIPE);
/// ```
/// Media source information.
///
/// Contains URL and provider information for content delivery:
/// * URLs are redacted in debug output for security
/// * Provider indicates delivery network (e.g., "cdn")
///
/// Multiple sources may be available for redundancy.