youtubei_rs/types/
client.rs

1use serde::{Deserialize, Serialize};
2use reqwest::Client;
3use reqwest::header;
4/// Represents the client that is used in requests to the api.
5/// For example the following represent the web client:
6/// Note that gl and hl are added a later point as they depend on other information
7/// ```not_rust
8///
9///  "client": {
10///  "clientName": "WEB",
11///  "clientScreen": "WATCH_FULL_SCREEN",
12///  "clientVersion": "2.20210721.00.00",
13///   "gl": "US", 
14///   "hl": "en"
15/// },
16/// ```
17#[derive(Serialize, Deserialize)]
18pub struct ClientType{
19   pub  name: String,
20   pub  version: String,
21   pub  api_key: String,
22   pub  screen: String,
23 }
24 #[derive(Serialize, Deserialize)]
25 pub enum ClientTypes{
26    Web,
27    WebEmbeddedPlayer,
28    WebMobile,
29    WebScreenEmbed,
30    Android,
31    AndroidEmbeddedPlayer,
32    AndroidScreenEmbed,
33    TvHtml5ScreenEmbed,
34}
35
36impl ClientTypes {
37/// Returns the client type based on the enum value
38/// ```
39///  use youtubei_rs::types::client::ClientTypes;
40///  let client = ClientTypes::Web;
41///  assert_eq!(client.get_client_type().name, "WEB");
42/// ```
43 pub fn get_client_type(&self) -> ClientType{
44    match self {
45        ClientTypes::Web =>  ClientType{
46              name:    "WEB".to_string(),
47              version: "2.20210721.00.00".to_string(),
48              api_key: "AIzaSyAO_FJ2SlqU8Q4STEHLGCilw_Y9_11qcW8".to_string(),
49              screen:  "WATCH_FULL_SCREEN".to_string(),
50            },
51        ClientTypes::WebEmbeddedPlayer => ClientType{      
52            name:    "WEB_EMBEDDED_PLAYER".to_string(),
53            version: "1.20210721.1.0".to_string(),
54            api_key: "AIzaSyAO_FJ2SlqU8Q4STEHLGCilw_Y9_11qcW8".to_string(),
55            screen:  "EMBED".to_string(),
56        },
57        ClientTypes::WebMobile => ClientType{
58            name:    "MWEB".to_string(),
59            version: "2.20210726.08.00".to_string(),
60            api_key: "AIzaSyAO_FJ2SlqU8Q4STEHLGCilw_Y9_11qcW8".to_string(),
61            screen:  "".to_string(),
62        },
63        ClientTypes::WebScreenEmbed => ClientType{      
64            name:    "WEB".to_string(),
65            version: "2.20210721.00.00".to_string(),
66            api_key: "AIzaSyAO_FJ2SlqU8Q4STEHLGCilw_Y9_11qcW8".to_string(),
67            screen:  "EMBED".to_string(),
68        },
69        ClientTypes::Android => ClientType{
70            name:    "ANDROID".to_string(),
71            version: "16.20".to_string(),
72            api_key: "AIzaSyA8eiZmM1FaDVjRy-df2KTyQ_vz_yYM39w".to_string(),
73            screen:  "".to_string(),
74        },
75        ClientTypes::AndroidEmbeddedPlayer => ClientType{
76            name:    "ANDROID_EMBEDDED_PLAYER".to_string(),
77            version: "16.20".to_string(),
78            api_key: "AIzaSyAO_FJ2SlqU8Q4STEHLGCilw_Y9_11qcW8".to_string(),
79            screen:  "".to_string(),
80        },
81        ClientTypes::AndroidScreenEmbed => ClientType{
82            name:    "ANDROID".to_string(),
83            version: "16.20".to_string(),
84            api_key: "AIzaSyAO_FJ2SlqU8Q4STEHLGCilw_Y9_11qcW8".to_string(),
85            screen:  "EMBED".to_string(),
86        },
87        ClientTypes::TvHtml5ScreenEmbed => ClientType{
88            name:    "TVHTML5_SIMPLY_EMBEDDED_PLAYER".to_string(),
89            version: "2.0".to_string(),
90            api_key: "AIzaSyAO_FJ2SlqU8Q4STEHLGCilw_Y9_11qcW8".to_string(),
91            screen:  "EMBED".to_string(),
92        },
93    }
94 }
95}
96/// Represents the client_config used by the endpoint functions
97/// to determine gl and hl params in the request context. 
98/// Will later be used to choose a appropriate proxy.
99pub struct ClientConfig{
100   pub client_type: ClientTypes,
101   pub region: String,
102   pub proxy_region:String,
103   pub http_client: Client
104}
105
106impl ClientConfig {
107    /// Constructs a new ClientConfig with the client and all required headers
108    pub fn new(client_type: ClientTypes, region: String, proxy_region: String) -> Self {
109        let mut headers = header::HeaderMap::new();
110        headers.insert("Content-Type", header::HeaderValue::from_static("application/json; charset=UTF-8"));
111        headers.insert("Accept-Encoding", header::HeaderValue::from_static("gzip"));
112        headers.insert("accept", header::HeaderValue::from_static("text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"));
113        headers.insert("accept-charset", header::HeaderValue::from_static("ISO-8859-1,utf-8;q=0.7,*;q=0.7"));
114        headers.insert("accept-language", header::HeaderValue::from_static("en-us,en;q=0.5"));
115        headers.insert("x-youtube-client-name", header::HeaderValue::from_static("1"));
116        headers.insert("x-youtube-client-version", header::HeaderValue::from_static( "2.20200609"));
117        headers.insert("cookie",header::HeaderValue::from_static("CONSENT=YES+"));
118        let _http_client = reqwest::ClientBuilder::new()
119        .https_only(true)
120        .user_agent("Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97 Safari/537.36")
121        .default_headers(headers)
122        .gzip(true)
123        .build().unwrap();
124        Self { client_type, region, proxy_region, http_client: _http_client} 
125    }
126
127    pub fn name(&self) ->  String {
128       self.client_type.get_client_type().name
129    }
130    pub fn version(&self) ->  String {
131      self.client_type.get_client_type().version
132    }
133    pub fn region(&self) -> &str{
134        return &self.region
135    }
136}