Skip to main content

rsynko_yt/
response.rs

1use crate::YoutubeFormatSource;
2use ambassador::delegatable_trait;
3
4/// Specifies decoding transport bytes into Youtube response observations.
5#[delegatable_trait]
6pub trait YoutubeResponseAlg {
7    /// Denotes decoding failure.
8    type Error;
9
10    /// Decodes one watch page.
11    ///
12    /// # Errors
13    ///
14    /// Returns interpreter-specific decoding failure.
15    fn decode_youtube_watch(&self, bytes: &[u8]) -> Result<YoutubeWatchPage, Self::Error>;
16
17    /// Decodes one player response.
18    ///
19    /// # Errors
20    ///
21    /// Returns interpreter-specific decoding failure.
22    fn decode_youtube_player(&self, bytes: &[u8]) -> Result<YoutubePlayer, Self::Error>;
23}
24
25/// Denotes the Youtube observations recovered from a watch page.
26#[derive(Clone, Debug, PartialEq, Eq)]
27pub struct YoutubeWatchPage {
28    /// Preserves whether an initial player response was present and parseable.
29    pub has_player_response: bool,
30    /// Names the player API key when present.
31    pub api_key: Option<String>,
32    /// Locates the player program the page states, when it states one.
33    pub player_url: Option<String>,
34    /// Names the session the page issued to this client, when it issued one.
35    pub visitor_data: Option<String>,
36    /// Preserves the initial player title when present.
37    pub title: Option<String>,
38}
39
40/// Denotes one format described by the Youtube player response.
41#[derive(Clone, Debug, PartialEq, Eq)]
42pub struct YoutubeFormat {
43    /// Identifies the format by `itag`.
44    pub id: String,
45    /// States how its media representation is located and what it is guarded by.
46    pub source: YoutubeFormatSource,
47    /// Names its container extension when known.
48    pub extension: Option<String>,
49    /// Denotes whether the representation contains audio.
50    pub has_audio: bool,
51    /// Denotes whether the representation contains video.
52    pub has_video: bool,
53    /// Preserves the displayed quality when known.
54    pub quality: Option<String>,
55    /// Preserves coded width when known.
56    pub width: Option<i64>,
57    /// Preserves coded height when known.
58    pub height: Option<i64>,
59    /// Preserves average bitrate when known.
60    pub bitrate: Option<i64>,
61    /// Preserves expected bytes when known.
62    pub content_length: Option<i64>,
63    /// Preserves codec names when known.
64    pub codecs: Option<String>,
65}
66
67/// Denotes the Youtube observations recovered from a player response.
68#[derive(Clone, Debug, PartialEq, Eq)]
69pub struct YoutubePlayer {
70    /// Names the playability status.
71    pub status: Option<String>,
72    /// Explains an unavailable status when supplied.
73    pub reason: Option<String>,
74    /// Names the media title when supplied.
75    pub title: Option<String>,
76    /// Preserves described formats before challenge resolution and preference ordering.
77    pub formats: Vec<YoutubeFormat>,
78    /// Counts the described formats this interpreter could not read.
79    ///
80    /// A response may describe a format this interpreter cannot use — one stating no location, or
81    /// carrying neither audio nor video. Counting them keeps the difference between what the
82    /// response described and what the catalog offers an observation rather than a silence.
83    pub unreadable: usize,
84}