ytmapi-rs 0.2.1

An asynchronous (tokio) pure Rust API for Youtube Music using Google's internal API
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
use super::{
    ParseFrom, RUN_TEXT, SECONDARY_SECTION_LIST_ITEM, STRAPLINE_RUNS, TAB_CONTENT, THUMBNAILS,
    THUMBNAIL_RENDERER, TITLE_TEXT, VISUAL_HEADER,
};
use crate::common::{
    EpisodeID, LibraryStatus, PlaylistID, PodcastChannelID, PodcastChannelParams, PodcastID,
    Thumbnail,
};
use crate::nav_consts::{
    CAROUSEL, CAROUSEL_TITLE, DESCRIPTION, DESCRIPTION_SHELF, GRID_ITEMS, MMRLIR, MTRIR,
    MUSIC_SHELF, NAVIGATION_BROWSE, NAVIGATION_BROWSE_ID, PLAYBACK_DURATION_TEXT,
    PLAYBACK_PROGRESS_TEXT, RESPONSIVE_HEADER, SECTION_LIST, SECTION_LIST_ITEM, SINGLE_COLUMN_TAB,
    SUBTITLE, SUBTITLE3, SUBTITLE_RUNS, TITLE, TWO_COLUMN,
};
use crate::query::{
    GetChannelEpisodesQuery, GetChannelQuery, GetEpisodeQuery, GetNewEpisodesQuery, GetPodcastQuery,
};
use crate::Result;
use const_format::concatcp;
use itertools::Itertools;
use json_crawler::{JsonCrawler, JsonCrawlerOwned};
use serde::{Deserialize, Serialize};

#[derive(PartialEq, Debug, Clone, Deserialize, Serialize)]
#[non_exhaustive]
pub struct GetPodcastChannel {
    pub title: String,
    pub thumbnails: Vec<Thumbnail>,
    pub episode_params: Option<PodcastChannelParams<'static>>,
    pub episodes: Vec<Episode>,
    pub podcasts: Vec<GetPodcastChannelPodcast>,
    pub playlists: Vec<GetPodcastChannelPlaylist>,
}
#[derive(PartialEq, Debug, Clone, Deserialize, Serialize)]
#[non_exhaustive]
pub struct Episode {
    pub title: String,
    pub description: String,
    pub total_duration: String,
    pub remaining_duration: String,
    pub date: String,
    pub episode_id: EpisodeID<'static>,
    pub thumbnails: Vec<Thumbnail>,
}
#[derive(PartialEq, Debug, Clone, Deserialize, Serialize)]
#[non_exhaustive]
pub struct GetPodcastChannelPodcast {
    pub title: String,
    pub channels: Vec<ParsedPodcastChannel>,
    pub podcast_id: PodcastID<'static>,
    pub thumbnails: Vec<Thumbnail>,
}
#[derive(PartialEq, Debug, Clone, Deserialize, Serialize)]
#[non_exhaustive]
pub struct GetPodcastChannelPlaylist {
    pub title: String,
    pub channel: ParsedPodcastChannel,
    pub playlist_id: PlaylistID<'static>,
    pub views: String,
    pub thumbnails: Vec<Thumbnail>,
}
#[derive(PartialEq, Debug, Clone, Deserialize, Serialize)]
// Intentionally not marked non_exhaustive - not expected to change.
pub struct ParsedPodcastChannel {
    pub name: String,
    pub id: Option<PodcastChannelID<'static>>,
}
#[derive(PartialEq, Debug, Clone, Deserialize, Serialize)]
// Intentionally not marked non_exhaustive - not expected to change.
pub enum IsSaved {
    Saved,
    NotSaved,
}
#[derive(Eq, PartialEq, Debug, Clone, Deserialize, Serialize, Hash)]
// Intentionally not marked non_exhaustive - not expected to change.
pub enum PodcastChannelTopResult {
    #[serde(rename = "Latest episodes")]
    Episodes,
    Podcasts,
    Playlists,
}
#[derive(PartialEq, Debug, Clone, Deserialize, Serialize)]
#[non_exhaustive]
pub struct GetPodcast {
    pub channels: Vec<ParsedPodcastChannel>,
    pub title: String,
    pub description: String,
    // TODO: How to add a podcast to library?
    pub library_status: LibraryStatus,
    pub episodes: Vec<Episode>,
}
#[derive(PartialEq, Debug, Clone, Deserialize, Serialize)]
#[non_exhaustive]
pub struct GetEpisode {
    pub podcast_name: String,
    pub podcast_id: PodcastID<'static>,
    pub title: String,
    pub date: String,
    pub total_duration: String,
    pub remaining_duration: String,
    pub saved: IsSaved,
    pub description: String,
}

// NOTE: This is technically the same page as the GetArtist page. It's possible
// this could be generalised.
impl ParseFrom<GetChannelQuery<'_>> for GetPodcastChannel {
    fn parse_from(p: crate::ProcessedResult<GetChannelQuery>) -> Result<Self> {
        fn parse_podcast(crawler: impl JsonCrawler) -> Result<GetPodcastChannelPodcast> {
            let mut podcast = crawler.navigate_pointer(MTRIR)?;
            let title = podcast.take_value_pointer(TITLE_TEXT)?;
            let podcast_id = podcast.take_value_pointer(NAVIGATION_BROWSE_ID)?;
            let thumbnails = podcast.take_value_pointer(THUMBNAIL_RENDERER)?;
            let channels = podcast
                .navigate_pointer(SUBTITLE_RUNS)?
                .try_into_iter()?
                .map(parse_podcast_channel)
                .collect::<Result<Vec<_>>>()?;
            Ok(GetPodcastChannelPodcast {
                title,
                channels,
                podcast_id,
                thumbnails,
            })
        }
        fn parse_playlist(crawler: impl JsonCrawler) -> Result<GetPodcastChannelPlaylist> {
            let mut podcast = crawler.navigate_pointer(MTRIR)?;
            let title = podcast.take_value_pointer(TITLE_TEXT)?;
            let playlist_id = podcast.take_value_pointer(NAVIGATION_BROWSE_ID)?;
            let thumbnails = podcast.take_value_pointer(THUMBNAIL_RENDERER)?;
            let views = podcast.take_value_pointer(SUBTITLE3)?;
            let channel =
                parse_podcast_channel(podcast.navigate_pointer(SUBTITLE_RUNS)?.navigate_index(2)?)?;
            Ok(GetPodcastChannelPlaylist {
                title,
                channel,
                thumbnails,
                playlist_id,
                views,
            })
        }
        let mut json_crawler = JsonCrawlerOwned::from(p);
        let mut header = json_crawler.borrow_pointer(VISUAL_HEADER)?;
        let title = header.take_value_pointer(TITLE_TEXT)?;
        let thumbnails = header.take_value_pointer(THUMBNAILS)?;
        let mut podcasts = Vec::new();
        let mut episodes = Vec::new();
        let mut playlists = Vec::new();
        let mut episode_params = None;
        // I spent a good few hours trying to make this declarative. It this stage this
        // seems to be more readable and more efficient. The best declarative approach I
        // could find used a collect into a HashMap and the process_results()
        // function...
        for carousel in json_crawler
            .borrow_pointer(concatcp!(SINGLE_COLUMN_TAB, SECTION_LIST))?
            .try_into_iter()?
            .map(|item| item.navigate_pointer(CAROUSEL))
        {
            let mut carousel = carousel?;
            match carousel
                .take_value_pointer::<PodcastChannelTopResult>(concatcp!(CAROUSEL_TITLE, "/text"))?
            {
                PodcastChannelTopResult::Episodes => {
                    episode_params = carousel.take_value_pointer(concatcp!(
                        CAROUSEL_TITLE,
                        NAVIGATION_BROWSE,
                        "/params"
                    ))?;
                    episodes = carousel
                        .navigate_pointer("/contents")?
                        .try_into_iter()?
                        .map(parse_episode)
                        .collect::<Result<_>>()?;
                }
                PodcastChannelTopResult::Podcasts => {
                    podcasts = carousel
                        .navigate_pointer("/contents")?
                        .try_into_iter()?
                        .map(parse_podcast)
                        .collect::<Result<_>>()?;
                }
                PodcastChannelTopResult::Playlists => {
                    playlists = carousel
                        .navigate_pointer("/contents")?
                        .try_into_iter()?
                        .map(parse_playlist)
                        .collect::<Result<_>>()?;
                }
            }
        }
        Ok(GetPodcastChannel {
            title,
            thumbnails,
            episode_params,
            episodes,
            podcasts,
            playlists,
        })
    }
}
impl ParseFrom<GetChannelEpisodesQuery<'_>> for Vec<Episode> {
    fn parse_from(p: crate::ProcessedResult<GetChannelEpisodesQuery>) -> Result<Self> {
        let json_crawler = JsonCrawlerOwned::from(p);
        json_crawler
            .navigate_pointer(concatcp!(SINGLE_COLUMN_TAB, SECTION_LIST_ITEM, GRID_ITEMS))?
            .try_into_iter()?
            .map(parse_episode)
            .collect()
    }
}
impl ParseFrom<GetPodcastQuery<'_>> for GetPodcast {
    fn parse_from(p: crate::ProcessedResult<GetPodcastQuery>) -> Result<Self> {
        let json_crawler = JsonCrawlerOwned::from(p);
        let mut two_column = json_crawler.navigate_pointer(TWO_COLUMN)?;
        let episodes = two_column
            .borrow_pointer(concatcp!(
                "/secondaryContents",
                SECTION_LIST_ITEM,
                MUSIC_SHELF,
                "/contents"
            ))?
            .try_into_iter()?
            .map(parse_episode)
            .collect::<Result<_>>()?;
        let mut responsive_header = two_column.navigate_pointer(concatcp!(
            TAB_CONTENT,
            SECTION_LIST_ITEM,
            RESPONSIVE_HEADER,
        ))?;
        let library_status = match responsive_header
            .take_value_pointer::<bool>("/buttons/1/toggleButtonRenderer/isToggled")?
        {
            true => LibraryStatus::InLibrary,
            false => LibraryStatus::NotInLibrary,
        };
        let channels = responsive_header
            .borrow_pointer(STRAPLINE_RUNS)?
            .try_into_iter()?
            .map(parse_podcast_channel)
            .collect::<Result<_>>()?;
        let mut description_shelf =
            responsive_header.navigate_pointer(concatcp!("/description", DESCRIPTION_SHELF))?;
        let description = description_shelf.take_value_pointer(DESCRIPTION)?;
        let title = description_shelf.take_value_pointer(concatcp!("/header", RUN_TEXT))?;
        Ok(GetPodcast {
            channels,
            title,
            description,
            library_status,
            episodes,
        })
    }
}
impl ParseFrom<GetEpisodeQuery<'_>> for GetEpisode {
    fn parse_from(p: crate::ProcessedResult<GetEpisodeQuery>) -> Result<Self> {
        let json_crawler = JsonCrawlerOwned::from(p);
        let mut two_column = json_crawler.navigate_pointer(TWO_COLUMN)?;
        let mut responsive_header = two_column.borrow_pointer(concatcp!(
            TAB_CONTENT,
            SECTION_LIST_ITEM,
            RESPONSIVE_HEADER,
        ))?;
        let title = responsive_header.take_value_pointer(TITLE_TEXT)?;
        let date = responsive_header.take_value_pointer(SUBTITLE)?;
        let total_duration = responsive_header.take_value_pointer(
            "/progress/musicPlaybackProgressRenderer/playbackProgressText/runs/1/text",
        )?;
        let remaining_duration = responsive_header.take_value_pointer(
            "/progress/musicPlaybackProgressRenderer/durationText/runs/1/text",
        )?;
        let saved = match responsive_header
            .take_value_pointer::<bool>("/buttons/0/toggleButtonRenderer/isToggled")?
        {
            true => IsSaved::Saved,
            false => IsSaved::NotSaved,
        };
        let mut strapline = responsive_header.navigate_pointer(concatcp!(STRAPLINE_RUNS, "/0"))?;
        let podcast_name = strapline.take_value_pointer("/text")?;
        let podcast_id = strapline.take_value_pointer(NAVIGATION_BROWSE_ID)?;
        let description = two_column
            .navigate_pointer(concatcp!(
                SECONDARY_SECTION_LIST_ITEM,
                DESCRIPTION_SHELF,
                "/description/runs"
            ))?
            .try_into_iter()?
            .map(|mut item| item.take_value_pointer::<String>("/text"))
            .process_results(|iter| iter.collect())?;
        Ok(GetEpisode {
            title,
            date,
            total_duration,
            remaining_duration,
            saved,
            description,
            podcast_name,
            podcast_id,
        })
    }
}
impl ParseFrom<GetNewEpisodesQuery> for Vec<Episode> {
    fn parse_from(p: crate::ProcessedResult<GetNewEpisodesQuery>) -> Result<Self> {
        let json_crawler = JsonCrawlerOwned::from(p);
        json_crawler
            .navigate_pointer(concatcp!(
                TWO_COLUMN,
                "/secondaryContents",
                SECTION_LIST_ITEM,
                MUSIC_SHELF,
                "/contents"
            ))?
            .try_into_iter()?
            .map(parse_episode)
            .collect()
    }
}

pub(crate) fn parse_podcast_channel(mut data: impl JsonCrawler) -> Result<ParsedPodcastChannel> {
    Ok(ParsedPodcastChannel {
        name: data.take_value_pointer("/text")?,
        id: data.take_value_pointer(NAVIGATION_BROWSE_ID).ok(),
    })
}

fn parse_episode(crawler: impl JsonCrawler) -> Result<Episode> {
    let mut episode = crawler.navigate_pointer(MMRLIR)?;
    let description = episode.take_value_pointer(DESCRIPTION)?;
    let total_duration = episode.take_value_pointer(PLAYBACK_DURATION_TEXT)?;
    let remaining_duration = episode.take_value_pointer(PLAYBACK_PROGRESS_TEXT)?;
    let date = episode.take_value_pointer(SUBTITLE)?;
    let thumbnails = episode.take_value_pointer(THUMBNAILS)?;
    let mut title_run = episode.navigate_pointer(TITLE)?;
    let title = title_run.take_value_pointer("/text")?;
    let episode_id = title_run.take_value_pointer(NAVIGATION_BROWSE_ID)?;
    Ok(Episode {
        title,
        description,
        total_duration,
        remaining_duration,
        date,
        episode_id,
        thumbnails,
    })
}

#[cfg(test)]
mod tests {
    use crate::auth::BrowserToken;
    use crate::common::{EpisodeID, PodcastChannelID, PodcastChannelParams, PodcastID, YoutubeID};
    use crate::query::{
        GetChannelEpisodesQuery, GetChannelQuery, GetEpisodeQuery, GetNewEpisodesQuery,
        GetPodcastQuery,
    };

    #[tokio::test]
    async fn test_get_channel() {
        parse_test!(
            "./test_json/get_channel_20240830.json",
            "./test_json/get_channel_20240830_output.txt",
            GetChannelQuery::new(PodcastChannelID::from_raw("")),
            BrowserToken
        );
    }
    #[tokio::test]
    async fn test_get_channel_episodes() {
        parse_test!(
            "./test_json/get_channel_episodes_20240830.json",
            "./test_json/get_channel_episodes_20240830_output.txt",
            GetChannelEpisodesQuery::new(
                PodcastChannelID::from_raw(""),
                PodcastChannelParams::from_raw("")
            ),
            BrowserToken
        );
    }
    #[tokio::test]
    async fn test_get_podcast() {
        parse_test!(
            "./test_json/get_podcast_20240830.json",
            "./test_json/get_podcast_20240830_output.txt",
            GetPodcastQuery::new(PodcastID::from_raw("")),
            BrowserToken
        );
    }
    #[tokio::test]
    async fn test_get_episode() {
        parse_test!(
            "./test_json/get_episode_20240830.json",
            "./test_json/get_episode_20240830_output.txt",
            GetEpisodeQuery::new(EpisodeID::from_raw("")),
            BrowserToken
        );
    }
    #[tokio::test]
    async fn test_get_new_episodes() {
        parse_test!(
            "./test_json/get_new_episodes_20240830.json",
            "./test_json/get_new_episodes_20240830_output.txt",
            GetNewEpisodesQuery,
            BrowserToken
        );
    }
}