spotify-cli 0.5.0

A command-line interface for Spotify
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
//! Playback state types from Spotify API.

use serde::{Deserialize, Serialize};

use super::common::ExternalUrls;
use super::track::Track;

/// Current playback state.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PlaybackState {
    /// The device currently playing.
    pub device: Option<Device>,
    /// Repeat mode: off, track, context.
    pub repeat_state: Option<String>,
    /// Whether shuffle is on.
    pub shuffle_state: Option<bool>,
    /// Playback context (album, playlist, etc.).
    pub context: Option<PlaybackContext>,
    /// Unix timestamp of when data was fetched.
    pub timestamp: Option<u64>,
    /// Progress into the currently playing track (ms).
    pub progress_ms: Option<u64>,
    /// Whether something is currently playing.
    pub is_playing: bool,
    /// The currently playing track/episode.
    pub item: Option<Track>,
    /// Currently playing type: track, episode, ad, unknown.
    pub currently_playing_type: Option<String>,
    /// Actions available/restricted.
    pub actions: Option<PlaybackActions>,
}

/// Playback context (what's being played from).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PlaybackContext {
    /// Context type: album, artist, playlist.
    #[serde(rename = "type")]
    pub context_type: Option<String>,
    /// Spotify URL.
    pub href: Option<String>,
    /// External URLs.
    pub external_urls: Option<ExternalUrls>,
    /// Spotify URI.
    pub uri: Option<String>,
}

/// Available playback actions.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PlaybackActions {
    /// Whether interrupting playback is allowed.
    pub interrupting_playback: Option<bool>,
    /// Whether pausing is allowed.
    pub pausing: Option<bool>,
    /// Whether resuming is allowed.
    pub resuming: Option<bool>,
    /// Whether seeking is allowed.
    pub seeking: Option<bool>,
    /// Whether skipping next is allowed.
    pub skipping_next: Option<bool>,
    /// Whether skipping previous is allowed.
    pub skipping_prev: Option<bool>,
    /// Whether toggling repeat context is allowed.
    pub toggling_repeat_context: Option<bool>,
    /// Whether toggling shuffle is allowed.
    pub toggling_shuffle: Option<bool>,
    /// Whether toggling repeat track is allowed.
    pub toggling_repeat_track: Option<bool>,
    /// Whether transferring playback is allowed.
    pub transferring_playback: Option<bool>,
}

/// Device information.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Device {
    /// Device ID.
    pub id: Option<String>,
    /// Whether this is the currently active device.
    pub is_active: bool,
    /// Whether the device is in a private session.
    pub is_private_session: Option<bool>,
    /// Whether controlling this device is restricted.
    pub is_restricted: Option<bool>,
    /// Device name.
    pub name: String,
    /// Device type: computer, smartphone, speaker, etc.
    #[serde(rename = "type")]
    pub device_type: String,
    /// Current volume percentage.
    pub volume_percent: Option<u32>,
    /// Whether the device supports volume control.
    pub supports_volume: Option<bool>,
}

/// Devices response.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DevicesResponse {
    /// List of devices.
    pub devices: Vec<Device>,
}

/// Queue response.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct QueueResponse {
    /// Currently playing track.
    pub currently_playing: Option<Track>,
    /// Upcoming tracks in the queue.
    pub queue: Vec<Track>,
}

/// Recently played item.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PlayHistory {
    /// The track that was played.
    pub track: Track,
    /// When the track was played.
    pub played_at: String,
    /// Playback context.
    pub context: Option<PlaybackContext>,
}

/// Recently played response (cursor-paginated).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RecentlyPlayedResponse {
    /// URL to the API endpoint.
    pub href: Option<String>,
    /// Maximum number of items.
    pub limit: Option<u32>,
    /// URL to the next page.
    pub next: Option<String>,
    /// Cursors for pagination.
    pub cursors: Option<RecentlyPlayedCursors>,
    /// Total count (may be null).
    pub total: Option<u32>,
    /// The recently played items.
    pub items: Vec<PlayHistory>,
}

/// Cursors for recently played pagination.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RecentlyPlayedCursors {
    /// Cursor to the next page.
    pub after: Option<String>,
    /// Cursor to the previous page.
    pub before: Option<String>,
}

impl PlaybackState {
    /// Get the currently playing track name.
    pub fn track_name(&self) -> Option<&str> {
        self.item.as_ref().map(|t| t.name.as_str())
    }

    /// Get the currently playing artist name.
    pub fn artist_name(&self) -> Option<&str> {
        self.item.as_ref().and_then(|t| t.artist_name())
    }

    /// Get progress as MM:SS string.
    pub fn progress_str(&self) -> String {
        let ms = self.progress_ms.unwrap_or(0);
        let total_secs = ms / 1000;
        let mins = total_secs / 60;
        let secs = total_secs % 60;
        format!("{}:{:02}", mins, secs)
    }

    /// Get duration as MM:SS string.
    pub fn duration_str(&self) -> String {
        self.item
            .as_ref()
            .map(|t| t.duration_str())
            .unwrap_or_else(|| "0:00".to_string())
    }

    /// Get the device name.
    pub fn device_name(&self) -> Option<&str> {
        self.device.as_ref().map(|d| d.name.as_str())
    }
}

impl Device {
    /// Get a display-friendly device type.
    pub fn device_type_display(&self) -> &str {
        match self.device_type.as_str() {
            "Computer" => "Computer",
            "Smartphone" => "Phone",
            "Speaker" => "Speaker",
            "TV" => "TV",
            "AVR" => "Receiver",
            "STB" => "Set-top Box",
            "AudioDongle" => "Audio Dongle",
            "GameConsole" => "Game Console",
            "CastVideo" => "Cast",
            "CastAudio" => "Cast",
            "Automobile" => "Car",
            "Tablet" => "Tablet",
            _ => &self.device_type,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    #[test]
    fn device_deserializes() {
        let json = json!({
            "id": "device123",
            "is_active": true,
            "name": "Living Room Speaker",
            "type": "Speaker",
            "volume_percent": 75
        });
        let device: Device = serde_json::from_value(json).unwrap();
        assert_eq!(device.id, Some("device123".to_string()));
        assert!(device.is_active);
        assert_eq!(device.name, "Living Room Speaker");
        assert_eq!(device.device_type, "Speaker");
    }

    #[test]
    fn device_type_display_known_types() {
        let make_device = |t: &str| Device {
            id: None,
            is_active: false,
            is_private_session: None,
            is_restricted: None,
            name: "Test".to_string(),
            device_type: t.to_string(),
            volume_percent: None,
            supports_volume: None,
        };

        assert_eq!(make_device("Computer").device_type_display(), "Computer");
        assert_eq!(make_device("Smartphone").device_type_display(), "Phone");
        assert_eq!(make_device("Speaker").device_type_display(), "Speaker");
        assert_eq!(make_device("TV").device_type_display(), "TV");
        assert_eq!(make_device("AVR").device_type_display(), "Receiver");
        assert_eq!(make_device("Automobile").device_type_display(), "Car");
        assert_eq!(make_device("Tablet").device_type_display(), "Tablet");
    }

    #[test]
    fn device_type_display_unknown() {
        let device = Device {
            id: None,
            is_active: false,
            is_private_session: None,
            is_restricted: None,
            name: "Test".to_string(),
            device_type: "NewDeviceType".to_string(),
            volume_percent: None,
            supports_volume: None,
        };
        assert_eq!(device.device_type_display(), "NewDeviceType");
    }

    #[test]
    fn devices_response_deserializes() {
        let json = json!({
            "devices": [
                {"id": "dev1", "is_active": true, "name": "Device 1", "type": "Computer"},
                {"id": "dev2", "is_active": false, "name": "Device 2", "type": "Speaker"}
            ]
        });
        let resp: DevicesResponse = serde_json::from_value(json).unwrap();
        assert_eq!(resp.devices.len(), 2);
    }

    #[test]
    fn playback_context_deserializes() {
        let json = json!({
            "type": "playlist",
            "uri": "spotify:playlist:abc123",
            "href": "https://api.spotify.com/v1/playlists/abc123"
        });
        let ctx: PlaybackContext = serde_json::from_value(json).unwrap();
        assert_eq!(ctx.context_type, Some("playlist".to_string()));
    }

    #[test]
    fn playback_actions_deserializes() {
        let json = json!({
            "pausing": true,
            "resuming": true,
            "seeking": true,
            "skipping_next": true,
            "skipping_prev": false
        });
        let actions: PlaybackActions = serde_json::from_value(json).unwrap();
        assert_eq!(actions.pausing, Some(true));
        assert_eq!(actions.skipping_prev, Some(false));
    }

    #[test]
    fn playback_state_deserializes() {
        let json = json!({
            "is_playing": true,
            "progress_ms": 60000,
            "repeat_state": "off",
            "shuffle_state": false
        });
        let state: PlaybackState = serde_json::from_value(json).unwrap();
        assert!(state.is_playing);
        assert_eq!(state.progress_ms, Some(60000));
        assert_eq!(state.repeat_state, Some("off".to_string()));
    }

    #[test]
    fn playback_state_track_name() {
        let json = json!({
            "is_playing": true,
            "item": {
                "id": "track1",
                "name": "Test Track",
                "type": "track",
                "uri": "spotify:track:track1",
                "duration_ms": 200000
            }
        });
        let state: PlaybackState = serde_json::from_value(json).unwrap();
        assert_eq!(state.track_name(), Some("Test Track"));
    }

    #[test]
    fn playback_state_track_name_none() {
        let json = json!({
            "is_playing": false
        });
        let state: PlaybackState = serde_json::from_value(json).unwrap();
        assert!(state.track_name().is_none());
    }

    #[test]
    fn playback_state_progress_str() {
        let json = json!({
            "is_playing": true,
            "progress_ms": 125000  // 2:05
        });
        let state: PlaybackState = serde_json::from_value(json).unwrap();
        assert_eq!(state.progress_str(), "2:05");
    }

    #[test]
    fn playback_state_progress_str_zero() {
        let json = json!({
            "is_playing": false
        });
        let state: PlaybackState = serde_json::from_value(json).unwrap();
        assert_eq!(state.progress_str(), "0:00");
    }

    #[test]
    fn playback_state_duration_str() {
        let json = json!({
            "is_playing": true,
            "item": {
                "id": "track1",
                "name": "Test",
                "type": "track",
                "uri": "spotify:track:track1",
                "duration_ms": 210000  // 3:30
            }
        });
        let state: PlaybackState = serde_json::from_value(json).unwrap();
        assert_eq!(state.duration_str(), "3:30");
    }

    #[test]
    fn playback_state_duration_str_no_item() {
        let json = json!({
            "is_playing": false
        });
        let state: PlaybackState = serde_json::from_value(json).unwrap();
        assert_eq!(state.duration_str(), "0:00");
    }

    #[test]
    fn playback_state_device_name() {
        let json = json!({
            "is_playing": true,
            "device": {
                "id": "dev1",
                "is_active": true,
                "name": "My Computer",
                "type": "Computer"
            }
        });
        let state: PlaybackState = serde_json::from_value(json).unwrap();
        assert_eq!(state.device_name(), Some("My Computer"));
    }

    #[test]
    fn queue_response_deserializes() {
        let json = json!({
            "currently_playing": null,
            "queue": []
        });
        let queue: QueueResponse = serde_json::from_value(json).unwrap();
        assert!(queue.currently_playing.is_none());
        assert!(queue.queue.is_empty());
    }

    #[test]
    fn play_history_deserializes() {
        let json = json!({
            "track": {
                "id": "track1",
                "name": "Recent Track",
                "type": "track",
                "uri": "spotify:track:track1",
                "duration_ms": 180000
            },
            "played_at": "2024-01-15T10:30:00Z"
        });
        let history: PlayHistory = serde_json::from_value(json).unwrap();
        assert_eq!(history.track.name, "Recent Track");
        assert_eq!(history.played_at, "2024-01-15T10:30:00Z");
    }

    #[test]
    fn recently_played_response_deserializes() {
        let json = json!({
            "items": [],
            "limit": 20
        });
        let resp: RecentlyPlayedResponse = serde_json::from_value(json).unwrap();
        assert!(resp.items.is_empty());
        assert_eq!(resp.limit, Some(20));
    }

    #[test]
    fn recently_played_cursors_deserializes() {
        let json = json!({
            "after": "1234567890",
            "before": "0987654321"
        });
        let cursors: RecentlyPlayedCursors = serde_json::from_value(json).unwrap();
        assert_eq!(cursors.after, Some("1234567890".to_string()));
        assert_eq!(cursors.before, Some("0987654321".to_string()));
    }
}