rustypipe 0.11.4

Client for the public YouTube / YouTube Music API (Innertube), inspired by NewPipe
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
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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
use std::{borrow::Cow, fmt::Debug};

use crate::{
    client::response::url_endpoint::NavigationEndpoint,
    error::{Error, ExtractionError},
    model::{
        paginator::{ContinuationEndpoint, Paginator},
        richtext::RichText,
        AlbumId, ChannelId, MusicAlbum, MusicPlaylist, TrackItem, TrackType,
    },
    serializer::{text::TextComponents, MapResult},
    util::{self, dictionary, TryRemove, DOT_SEPARATOR},
};

use self::response::url_endpoint::MusicPageType;

use super::{
    response::{
        self,
        music_item::{map_album_type, map_artist_id, map_artists, MusicListMapper},
    },
    ClientType, MapRespCtx, MapResponse, QBrowse, RustyPipeQuery,
};

impl RustyPipeQuery {
    /// Get a playlist from YouTube Music
    #[tracing::instrument(skip(self), level = "error")]
    pub async fn music_playlist<S: AsRef<str> + Debug>(
        &self,
        playlist_id: S,
    ) -> Result<MusicPlaylist, Error> {
        let playlist_id = playlist_id.as_ref();
        let request_body = QBrowse {
            browse_id: &format!("VL{playlist_id}"),
        };

        self.execute_request::<response::MusicPlaylist, _, _>(
            ClientType::DesktopMusic,
            "music_playlist",
            playlist_id,
            "browse",
            &request_body,
        )
        .await
    }

    /// Get an album from YouTube Music
    #[tracing::instrument(skip(self), level = "error")]
    pub async fn music_album<S: AsRef<str> + Debug>(
        &self,
        album_id: S,
    ) -> Result<MusicAlbum, Error> {
        let album_id = album_id.as_ref();
        let request_body = QBrowse {
            browse_id: album_id,
        };

        let mut album = self
            .execute_request::<response::MusicPlaylist, MusicAlbum, _>(
                ClientType::DesktopMusic,
                "music_album",
                album_id,
                "browse",
                &request_body,
            )
            .await?;

        // In rare cases, albums may have track numbers =0 (example: MPREb_RM0QfZ0eSKL)
        // They should be replaced with the track number derived from the previous track.
        let mut n_prev = 0;
        for track in &mut album.tracks {
            let tn = track.track_nr.unwrap_or_default();
            if tn == 0 {
                n_prev += 1;
                track.track_nr = Some(n_prev);
            } else {
                n_prev = tn;
            }
        }

        // YouTube Music is replacing album tracks with their respective music videos. To get the original
        // tracks, we have to fetch the album as a playlist and replace the offending track ids.
        if let Some(playlist_id) = &album.playlist_id {
            // Get a list of music videos in the album
            let to_replace = album
                .tracks
                .iter()
                .enumerate()
                .filter_map(|(i, track)| {
                    if track.track_type.is_video() {
                        Some((i, track.name.clone()))
                    } else {
                        None
                    }
                })
                .collect::<Vec<_>>();

            let last_tn = album
                .tracks
                .last()
                .and_then(|t| t.track_nr)
                .unwrap_or_default();
            if !to_replace.is_empty() || last_tn < album.track_count {
                tracing::debug!(
                    "fetching album playlist ({} tracks, {} to replace)",
                    album.track_count,
                    to_replace.len()
                );
                let mut playlist = self.music_playlist(playlist_id).await?;
                playlist
                    .tracks
                    .extend_limit(&self, album.track_count.into())
                    .await?;

                for (i, title) in to_replace {
                    let found_track = playlist.tracks.items.iter().find_map(|track| {
                        if track.name == title && track.track_type.is_track() {
                            Some((track.id.clone(), track.duration))
                        } else {
                            None
                        }
                    });
                    if let Some((track_id, duration)) = found_track {
                        album.tracks[i].id = track_id;
                        if let Some(duration) = duration {
                            album.tracks[i].duration = Some(duration);
                        }
                        album.tracks[i].track_type = TrackType::Track;
                    }
                }

                // Extend the list of album tracks with the ones from the playlist if the playlist returned more tracks
                // This is the case for albums with more than 200 tracks (e.g. audiobooks)
                if album.tracks.len() < playlist.tracks.items.len() {
                    let mut tn = last_tn;
                    for mut t in playlist.tracks.items.into_iter().skip(album.tracks.len()) {
                        tn += 1;
                        t.album = album.tracks.first().and_then(|t| t.album.clone());
                        t.track_nr = Some(tn);
                        album.tracks.push(t);
                    }
                }
            }
        }
        Ok(album)
    }
}

impl MapResponse<MusicPlaylist> for response::MusicPlaylist {
    fn map_response(
        self,
        ctx: &MapRespCtx<'_>,
    ) -> Result<MapResult<MusicPlaylist>, ExtractionError> {
        let contents = match self.contents {
            Some(c) => c,
            None => {
                if self.microformat.microformat_data_renderer.noindex {
                    return Err(ExtractionError::NotFound {
                        id: ctx.id.to_owned(),
                        msg: "no contents".into(),
                    });
                } else {
                    return Err(ExtractionError::InvalidData("no contents".into()));
                }
            }
        };

        let (header, music_contents) = match contents {
            response::music_playlist::Contents::SingleColumnBrowseResultsRenderer(c) => (
                self.header,
                c.contents
                    .into_iter()
                    .next()
                    .ok_or(ExtractionError::InvalidData(Cow::Borrowed("no content")))?
                    .tab_renderer
                    .content
                    .section_list_renderer,
            ),
            response::music_playlist::Contents::TwoColumnBrowseResultsRenderer {
                secondary_contents,
                tabs,
            } => (
                tabs.into_iter()
                    .next()
                    .and_then(|t| {
                        t.tab_renderer
                            .content
                            .section_list_renderer
                            .contents
                            .into_iter()
                            .next()
                    })
                    .or(self.header),
                secondary_contents.section_list_renderer,
            ),
        };
        let shelf = music_contents
            .contents
            .into_iter()
            .find_map(|section| match section {
                response::music_item::ItemSection::MusicShelfRenderer(shelf) => Some(shelf),
                _ => None,
            })
            .ok_or(ExtractionError::InvalidData(Cow::Borrowed(
                "no sectionListRenderer content",
            )))?;

        if let Some(playlist_id) = shelf.playlist_id {
            if playlist_id != ctx.id {
                return Err(ExtractionError::WrongResult(format!(
                    "got wrong playlist id {}, expected {}",
                    playlist_id, ctx.id
                )));
            }
        }

        let mut mapper = MusicListMapper::new(ctx.lang);
        mapper.map_response(shelf.contents);

        let ctoken = mapper.ctoken.clone().or_else(|| {
            shelf
                .continuations
                .into_iter()
                .next()
                .map(|cont| cont.next_continuation_data.continuation)
        });
        let map_res = mapper.conv_items();

        let track_count = if ctoken.is_some() {
            header.as_ref().and_then(|h| {
                let parts = h
                    .music_detail_header_renderer
                    .second_subtitle
                    .split(|p| p == DOT_SEPARATOR)
                    .collect::<Vec<_>>();
                parts
                    .get(usize::from(parts.len() > 2))
                    .and_then(|txt| util::parse_numeric::<u64>(&txt[0]).ok())
            })
        } else {
            Some(map_res.c.len() as u64)
        };

        let related_ctoken = music_contents
            .continuations
            .into_iter()
            .next()
            .map(|c| c.next_continuation_data.continuation);

        let (from_ytm, channel, name, thumbnail, description) = match header {
            Some(header) => {
                let h = header.music_detail_header_renderer;

                let (from_ytm, channel) = match h.facepile {
                    Some(facepile) => {
                        let from_ytm = facepile.avatar_stack_view_model.text.starts_with("YouTube");
                        let channel = facepile
                            .avatar_stack_view_model
                            .renderer_context
                            .command_context
                            .and_then(|c| {
                                c.on_tap
                                    .innertube_command
                                    .music_page()
                                    .filter(|p| p.typ == MusicPageType::User)
                                    .map(|p| p.id)
                            })
                            .map(|id| ChannelId {
                                id,
                                name: facepile.avatar_stack_view_model.text,
                            });

                        (from_ytm && channel.is_none(), channel)
                    }
                    None => {
                        let st = match h.strapline_text_one {
                            Some(s) => s,
                            None => h.subtitle,
                        };

                        let from_ytm = st.0.iter().any(util::is_ytm);
                        let channel = st.0.into_iter().find_map(|c| ChannelId::try_from(c).ok());
                        (from_ytm, channel)
                    }
                };

                (
                    from_ytm,
                    channel,
                    h.title,
                    h.thumbnail.into(),
                    h.description.map(TextComponents::from),
                )
            }
            None => {
                // Album playlists fetched via the playlist method dont include a header
                let (album, cover) = map_res
                    .c
                    .first()
                    .and_then(|t: &TrackItem| {
                        t.album.as_ref().map(|a| (a.clone(), t.cover.clone()))
                    })
                    .ok_or(ExtractionError::InvalidData(Cow::Borrowed(
                        "playlist without header or album items",
                    )))?;

                if !map_res.c.iter().all(|t| {
                    t.album
                        .as_ref()
                        .map(|a| a.id == album.id)
                        .unwrap_or_default()
                }) {
                    return Err(ExtractionError::InvalidData(Cow::Borrowed(
                        "album playlist containing items from different albums",
                    )));
                }

                (true, None, album.name, cover, None)
            }
        };

        Ok(MapResult {
            c: MusicPlaylist {
                id: ctx.id.to_owned(),
                name,
                thumbnail,
                channel,
                description: description.map(RichText::from),
                track_count,
                from_ytm,
                tracks: Paginator::new_ext(
                    track_count,
                    map_res.c,
                    ctoken,
                    ctx.visitor_data.map(str::to_owned),
                    ContinuationEndpoint::MusicBrowse,
                    ctx.authenticated,
                ),
                related_playlists: Paginator::new_ext(
                    None,
                    Vec::new(),
                    related_ctoken,
                    ctx.visitor_data.map(str::to_owned),
                    ContinuationEndpoint::MusicBrowse,
                    ctx.authenticated,
                ),
            },
            warnings: map_res.warnings,
        })
    }
}

impl MapResponse<MusicAlbum> for response::MusicPlaylist {
    fn map_response(self, ctx: &MapRespCtx<'_>) -> Result<MapResult<MusicAlbum>, ExtractionError> {
        let contents = match self.contents {
            Some(c) => c,
            None => {
                if self.microformat.microformat_data_renderer.noindex {
                    return Err(ExtractionError::NotFound {
                        id: ctx.id.to_owned(),
                        msg: "no contents".into(),
                    });
                } else {
                    return Err(ExtractionError::InvalidData("no contents".into()));
                }
            }
        };

        let (header, sections) = match contents {
            response::music_playlist::Contents::SingleColumnBrowseResultsRenderer(c) => (
                self.header,
                c.contents
                    .into_iter()
                    .next()
                    .ok_or(ExtractionError::InvalidData(Cow::Borrowed("no content")))?
                    .tab_renderer
                    .content
                    .section_list_renderer
                    .contents,
            ),
            response::music_playlist::Contents::TwoColumnBrowseResultsRenderer {
                secondary_contents,
                tabs,
            } => (
                tabs.into_iter()
                    .next()
                    .and_then(|t| {
                        t.tab_renderer
                            .content
                            .section_list_renderer
                            .contents
                            .into_iter()
                            .next()
                    })
                    .or(self.header),
                secondary_contents.section_list_renderer.contents,
            ),
        };
        let header = header
            .ok_or(ExtractionError::InvalidData(Cow::Borrowed("no header")))?
            .music_detail_header_renderer;

        let mut shelf = None;
        let mut album_variants = None;
        for section in sections {
            match section {
                response::music_item::ItemSection::MusicShelfRenderer(sh) => shelf = Some(sh),
                response::music_item::ItemSection::MusicCarouselShelfRenderer(sh) => {
                    if sh
                        .header
                        .map(|h| {
                            h.music_carousel_shelf_basic_header_renderer
                                .title
                                .first_str()
                                == dictionary::entry(ctx.lang).album_versions_title
                        })
                        .unwrap_or_default()
                    {
                        album_variants = Some(sh.contents);
                    }
                }
                _ => (),
            }
        }
        let shelf = shelf.ok_or(ExtractionError::InvalidData(Cow::Borrowed(
            "no sectionListRenderer content",
        )))?;

        let mut subtitle_split = header.subtitle.split(util::DOT_SEPARATOR);

        let (year_txt, artists_p) = match header.strapline_text_one {
            // New (2column) album layout
            Some(sl) => {
                let year_txt = subtitle_split
                    .try_swap_remove(1)
                    .and_then(|t| t.0.first().map(|c| c.as_str().to_owned()));
                (year_txt, Some(sl))
            }
            // Old album layout
            None => match subtitle_split.len() {
                3.. => {
                    let year_txt = subtitle_split
                        .swap_remove(2)
                        .0
                        .first()
                        .map(|c| c.as_str().to_owned());
                    (year_txt, subtitle_split.try_swap_remove(1))
                }
                2 => {
                    // The second part may either be the year or the artist
                    let p2 = subtitle_split.swap_remove(1);
                    let is_year =
                        p2.0.len() == 1 && p2.0[0].as_str().chars().all(|c| c.is_ascii_digit());
                    if is_year {
                        (Some(p2.0[0].as_str().to_owned()), None)
                    } else {
                        (None, Some(p2))
                    }
                }
                _ => (None, None),
            },
        };

        let (artists, by_va) = map_artists(artists_p);
        let album_type_txt = subtitle_split
            .into_iter()
            .next()
            .map(|part| part.to_string())
            .unwrap_or_default();

        let album_type = map_album_type(album_type_txt.as_str(), ctx.lang);
        let year = year_txt.and_then(|txt| util::parse_numeric(&txt).ok());

        fn map_playlist_id(ep: &NavigationEndpoint) -> Option<String> {
            if let NavigationEndpoint::WatchPlaylist {
                watch_playlist_endpoint,
            } = ep
            {
                Some(watch_playlist_endpoint.playlist_id.to_owned())
            } else {
                None
            }
        }

        let playlist_id = self
            .microformat
            .microformat_data_renderer
            .url_canonical
            .and_then(|x| {
                x.strip_prefix("https://music.youtube.com/playlist?list=")
                    .map(str::to_owned)
            });
        let (playlist_id, artist_id) = header
            .menu
            .or_else(|| header.buttons.into_iter().next())
            .map(|menu| {
                (
                    playlist_id.or_else(|| {
                        menu.menu_renderer
                            .top_level_buttons
                            .iter()
                            .find_map(|btn| {
                                map_playlist_id(&btn.button_renderer.navigation_endpoint)
                            })
                            .or_else(|| {
                                menu.menu_renderer.items.iter().find_map(|itm| {
                                    map_playlist_id(
                                        &itm.menu_navigation_item_renderer.navigation_endpoint,
                                    )
                                })
                            })
                    }),
                    map_artist_id(menu.menu_renderer.items),
                )
            })
            .unwrap_or_default();
        let artist_id = artist_id.or_else(|| artists.first().and_then(|a| a.id.clone()));

        let second_subtitle_parts = header
            .second_subtitle
            .split(|p| p == DOT_SEPARATOR)
            .collect::<Vec<_>>();
        let track_count = second_subtitle_parts
            .get(usize::from(second_subtitle_parts.len() > 2))
            .and_then(|txt| util::parse_numeric::<u16>(&txt[0]).ok());

        let mut mapper = MusicListMapper::with_album(
            ctx.lang,
            artists.clone(),
            by_va,
            AlbumId {
                id: ctx.id.to_owned(),
                name: header.title.clone(),
            },
        );
        mapper.map_response(shelf.contents);
        let tracks_res = mapper.conv_items();
        let mut warnings = tracks_res.warnings;

        let mut variants_mapper = MusicListMapper::new(ctx.lang);
        if let Some(res) = album_variants {
            variants_mapper.map_response(res);
        }
        let mut variants_res = variants_mapper.conv_items();
        warnings.append(&mut variants_res.warnings);

        Ok(MapResult {
            c: MusicAlbum {
                id: ctx.id.to_owned(),
                playlist_id,
                name: header.title,
                cover: header.thumbnail.into(),
                artists,
                artist_id,
                description: header
                    .description
                    .map(|t| RichText::from(TextComponents::from(t))),
                album_type,
                year,
                by_va,
                track_count: track_count.unwrap_or(tracks_res.c.len() as u16),
                tracks: tracks_res.c,
                variants: variants_res.c,
            },
            warnings,
        })
    }
}

#[cfg(test)]
mod tests {
    use std::{fs::File, io::BufReader};

    use path_macro::path;
    use rstest::rstest;

    use super::*;
    use crate::{model, util::tests::TESTFILES};

    #[rstest]
    #[case::short("short", "RDCLAK5uy_kFQXdnqMaQCVx2wpUM4ZfbsGCDibZtkJk")]
    #[case::long("long", "PL5dDx681T4bR7ZF1IuWzOv1omlRbE7PiJ")]
    #[case::nomusic("nomusic", "PL1J-6JOckZtE_P9Xx8D3b2O6w0idhuKBe")]
    #[case::two_columns("20240228_twoColumns", "RDCLAK5uy_kb7EBi6y3GrtJri4_ZH56Ms786DFEimbM")]
    #[case::n_album("20240228_album", "OLAK5uy_kdSWBZ-9AZDkYkuy0QCc3p0KO9DEHVNH0")]
    #[case::facepile("20241125_facepile", "PL1J-6JOckZtE_P9Xx8D3b2O6w0idhuKBe")]
    fn map_music_playlist(#[case] name: &str, #[case] id: &str) {
        let json_path = path!(*TESTFILES / "music_playlist" / format!("playlist_{name}.json"));
        let json_file = File::open(json_path).unwrap();

        let playlist: response::MusicPlaylist =
            serde_json::from_reader(BufReader::new(json_file)).unwrap();
        let map_res: MapResult<model::MusicPlaylist> =
            playlist.map_response(&MapRespCtx::test(id)).unwrap();

        assert!(
            map_res.warnings.is_empty(),
            "deserialization/mapping warnings: {:?}",
            map_res.warnings
        );
        insta::assert_ron_snapshot!(format!("map_music_playlist_{name}"), map_res.c, {
            ".last_update" => "[date]"
        });
    }

    #[rstest]
    #[case::one_artist("one_artist", "MPREb_nlBWQROfvjo")]
    #[case::various_artists("various_artists", "MPREb_8QkDeEIawvX")]
    #[case::single("single", "MPREb_bHfHGoy7vuv")]
    #[case::description("description", "MPREb_PiyfuVl6aYd")]
    #[case::unavailable("unavailable", "MPREb_AzuWg8qAVVl")]
    #[case::two_columns("20240228_twoColumns", "MPREb_bHfHGoy7vuv")]
    #[case::recommends("20250225_recommends", "MPREb_u1I69lSAe5v")]
    fn map_music_album(#[case] name: &str, #[case] id: &str) {
        let json_path = path!(*TESTFILES / "music_playlist" / format!("album_{name}.json"));
        let json_file = File::open(json_path).unwrap();

        let playlist: response::MusicPlaylist =
            serde_json::from_reader(BufReader::new(json_file)).unwrap();
        let map_res: MapResult<model::MusicAlbum> =
            playlist.map_response(&MapRespCtx::test(id)).unwrap();

        assert!(
            map_res.warnings.is_empty(),
            "deserialization/mapping warnings: {:?}",
            map_res.warnings
        );
        insta::assert_ron_snapshot!(format!("map_music_album_{name}"), map_res.c);
    }
}