spotify_rs/model/
audiobook.rs

1use serde::Deserialize;
2use spotify_rs_macros::docs;
3
4use super::*;
5
6/// An audiobook.
7#[derive(Clone, Debug, Deserialize, PartialEq)]
8#[docs]
9pub struct Audiobook {
10    /// The author(s) of the audiobook.
11    pub authors: Vec<Author>,
12    #[serde(default)]
13    pub available_markets: Vec<String>,
14    pub copyrights: Vec<Copyright>,
15    /// A text description of the audiobook.
16    pub description: String,
17    /// An description of the audiobook that may contain HTML tags.
18    pub html_description: String,
19    /// The edition of the audiobook.
20    pub edition: String,
21    pub explicit: bool,
22    pub external_urls: ExternalUrls,
23    pub href: String,
24    pub id: String,
25    pub images: Vec<Image>,
26    pub languages: Vec<String>,
27    pub media_type: String,
28    pub name: String,
29    /// The narrator(s) of the audiobook.
30    pub narrators: Vec<Narrator>,
31    pub publisher: String,
32    pub r#type: String,
33    pub uri: String,
34    /// The amount of chapters the audiobook contains.
35    pub total_chapters: u32,
36    /// The chapters of the audiobook.
37    pub chapters: Page<SimplifiedChapter>,
38}
39
40/// A simplified audiobook, missing some details, that is usually obtained
41/// through endpoints not specific to audiobooks. The `href` field may be
42/// used to get a full audiobook.
43#[derive(Clone, Debug, Deserialize, PartialEq)]
44#[docs(name = "audiobook")]
45pub struct SimplifiedAudiobook {
46    /// The author(s) of the audiobook.
47    pub authors: Vec<Author>,
48    #[serde(default)]
49    pub available_markets: Vec<String>,
50    pub copyrights: Vec<Copyright>,
51    /// A text description of the audiobook.
52    pub description: String,
53    /// An description of the audiobook that may contain HTML tags.
54    pub html_description: String,
55    /// The edition of the audiobook.
56    pub edition: String,
57    /// Whether or not the audiobook has explicit content.
58    /// `false` can also mean *unknown*.
59    pub explicit: bool,
60    pub external_urls: ExternalUrls,
61    pub href: String,
62    pub id: String,
63    pub images: Vec<Image>,
64    /// A list of [ISO 639](https://en.wikipedia.org/wiki/ISO_639) codes for the
65    /// languages spoken in the audiobook.
66    pub languages: Vec<String>,
67    /// The type of the media of the audiobook.
68    pub media_type: String,
69    pub name: String,
70    /// The narrator(s) of the audiobook.
71    pub narrators: Vec<Narrator>,
72    /// The publisher of the audiobook.
73    pub publisher: String,
74    pub r#type: String,
75    pub uri: String,
76    /// The amount of chapters the audiobook contains.
77    pub total_chapters: Option<u32>,
78}
79
80// Used only to deserialize JSON responses with arrays that are named objects.
81#[derive(Clone, Debug, Deserialize, PartialEq)]
82pub(crate) struct Audiobooks {
83    pub(crate) audiobooks: Vec<Option<Audiobook>>,
84}
85
86/// An audiobook chapter.
87#[derive(Clone, Debug, Deserialize, PartialEq)]
88#[docs]
89pub struct Chapter {
90    /// The URL for a 30 second MP3 preview of the chapter.
91    ///
92    /// **Note:** This attribute has been deprecated by Spotify. It continues to work for
93    /// applications already using the extended mode in the API.
94    ///
95    /// You can read more about this [here](https://developer.spotify.com/blog/2024-11-27-changes-to-the-web-api).
96    pub audio_preview_url: Option<String>,
97    #[serde(default)]
98    pub available_markets: Vec<String>,
99    /// The number of the chapter in the audiobook it belongs to.
100    pub chapter_number: u32,
101    /// A text description of the audiobook.
102    pub description: String,
103    /// An description of the audiobook that may contain HTML tags.
104    pub html_description: String,
105    pub duration_ms: u32,
106    /// Whether or not the audiobook has explicit content.
107    /// `false` can also mean *unknown*.
108    pub explicit: bool,
109    pub external_urls: ExternalUrls,
110    pub href: String,
111    pub id: String,
112    pub images: Vec<Image>,
113    pub is_playable: Option<bool>,
114    /// A list of [ISO 639](https://en.wikipedia.org/wiki/ISO_639) codes for the
115    /// languages spoken in the audiobook.
116    pub languages: Vec<String>,
117    pub name: String,
118    pub release_date: String,
119    pub release_date_precision: DatePrecision,
120    pub resume_point: Option<ResumePoint>,
121    pub r#type: String,
122    pub uri: String,
123    /// Included in the response when a content restriction is applied.
124    pub restrictions: Option<Restriction>,
125    /// The audiobook to which the chapter belongs.
126    pub audiobook: SimplifiedAudiobook,
127}
128
129/// A simplified chapter, missing some details, that is usually obtained
130/// through endpoints not specific to chapters. The `href` field may be
131/// used to get a full chapter.
132#[derive(Clone, Debug, Deserialize, PartialEq)]
133#[docs(name = "chapter")]
134pub struct SimplifiedChapter {
135    /// The URL for a 30 second MP3 preview of the chapter.
136    ///
137    /// **Note:** This attribute has been deprecated by Spotify. It continues to work for
138    /// applications already using the extended mode in the API.
139    ///
140    /// You can read more about this [here](https://developer.spotify.com/blog/2024-11-27-changes-to-the-web-api).
141    pub audio_preview_url: Option<String>,
142    #[serde(default)]
143    pub available_markets: Vec<String>,
144    /// The number of the chapter in the audiobook it belongs to.
145    pub chapter_number: u32,
146    pub description: String,
147    pub html_description: String,
148    pub duration_ms: u32,
149    pub explicit: bool,
150    pub external_urls: ExternalUrls,
151    pub href: String,
152    pub id: String,
153    pub images: Vec<Image>,
154    pub is_playable: Option<bool>,
155    pub languages: Vec<String>,
156    pub name: String,
157    pub release_date: String,
158    pub release_date_precision: DatePrecision,
159    pub resume_point: Option<ResumePoint>,
160    pub r#type: String,
161    pub uri: String,
162    /// Included in the response when a content restriction is applied.
163    pub restrictions: Option<Restriction>,
164}
165
166// Used only to deserialize JSON responses with arrays that are named objects.
167#[derive(Clone, Debug, Deserialize, PartialEq)]
168pub(crate) struct Chapters {
169    pub(crate) chapters: Vec<Option<Chapter>>,
170}
171
172// Even though there's no point to these containers (and other types in the
173// Spotify API), this library tries to adhere as closely as possible to the API.
174/// An author of an audiobook.
175#[derive(Clone, Debug, Deserialize, PartialEq)]
176pub struct Author {
177    /// The name of the author.
178    pub name: String,
179}
180
181/// A narrator of an audiobook
182#[derive(Clone, Debug, Deserialize, PartialEq)]
183pub struct Narrator {
184    /// The name of the narrator.
185    pub name: String,
186}
187
188impl Audiobook {
189    /// Get the names of the author(s) of the audiobook.
190    pub fn author_names(&self) -> Vec<String> {
191        self.authors.iter().map(|a| a.name.clone()).collect()
192    }
193
194    /// Get the names of the narrator(s) of the audiobook.
195    pub fn narrator_names(&self) -> Vec<String> {
196        self.narrators.iter().map(|n| n.name.clone()).collect()
197    }
198}
199
200impl SimplifiedAudiobook {
201    /// Get the names of the author(s) of the audiobook.
202    pub fn author_names(&self) -> Vec<String> {
203        self.authors.iter().map(|a| a.name.clone()).collect()
204    }
205
206    /// Get the names of the narrator(s) of the audiobook.
207    pub fn narrator_names(&self) -> Vec<String> {
208        self.narrators.iter().map(|n| n.name.clone()).collect()
209    }
210}