s8/
lib.rs

1use rust_json::JsonParseErr;
2
3#[derive(Debug)]
4pub struct Player(rust_json::JsonElem);
5
6#[derive(Debug)]
7pub enum Error {
8   HTTP(attohttpc::Error),
9   JSON(JsonParseErr)
10}
11
12impl From<attohttpc::Error> for Error {
13   fn from(err: attohttpc::Error) -> Self {
14      Self::HTTP(err)
15   }
16}
17
18impl From<JsonParseErr> for Error {
19   fn from(err: JsonParseErr) -> Self {
20      Self::JSON(err)
21   }
22}
23
24const YOUTUBEI: &str = "https://www.youtube.com/youtubei/v1/player";
25
26impl Player {
27   pub fn new() -> Result<Self, Error> {
28      let body = rust_json::json!({
29         "videoId": "aGCdLKXNF3w",
30         "context": {
31            "client": {
32               "clientName": "ANDROID",
33               "clientVersion": "17.32.35"
34            }
35         }
36      });
37      let res = attohttpc::post(YOUTUBEI).
38         header("X-Goog-API-Key", "AIzaSyAO_FJ2SlqU8Q4STEHLGCilw_Y9_11qcW8").
39         bytes(body.to_string()).
40         send()?;
41      let body = res.text()?;
42      let play = rust_json::json_parse(&body)?;
43      Ok(Self(play))
44   }
45   pub fn title(self) -> Option<String> {
46      self.0["videoDetails"]["title"].clone().get()
47   }
48}