1use crate::parser::VideoInfo;
2use kodik_utils::KodikError;
3use reqwest::{
4 Client,
5 header::{ACCEPT, HeaderName, ORIGIN, REFERER, USER_AGENT},
6};
7use serde::Deserialize;
8
9#[derive(Debug, Deserialize)]
10pub struct Response {
12 pub links: Links,
14}
15
16#[derive(Debug, Deserialize)]
17pub struct Links {
19 #[serde(rename = "360")]
21 pub quality_360: Vec<Link>,
22 #[serde(rename = "480")]
24 pub quality_480: Vec<Link>,
25 #[serde(rename = "720")]
27 pub quality_720: Vec<Link>,
28}
29
30#[derive(Debug, Deserialize)]
31pub struct Link {
33 pub src: String,
35 pub r#type: String,
37}
38
39pub async fn get(client: &Client, url: &str) -> Result<String, KodikError> {
40 let agent = kodik_utils::random_user_agent();
41
42 log::info!("GET to {url}...");
43
44 let html = client
45 .get(url)
46 .header(USER_AGENT, agent)
47 .send()
48 .await?
49 .text()
50 .await?;
51
52 log::trace!("Fetched to {url}, response: {html}");
53
54 Ok(html)
55}
56
57pub async fn post(
58 client: &Client,
59 domain: &str,
60 endpoint: &str,
61 video_info: &VideoInfo<'_>,
62) -> Result<Response, KodikError> {
63 let user_agent = kodik_utils::random_user_agent();
64 let url = format!("https://{domain}{endpoint}");
65
66 log::info!("POST to {url}...");
67
68 let kodik_response = client
69 .post(url)
70 .header(ORIGIN, format!("https://{domain}"))
71 .header(ACCEPT, "application/json, text/javascript, */*; q=0.01")
72 .header(REFERER, format!("https://{domain}"))
73 .header(USER_AGENT, user_agent)
74 .header(
75 HeaderName::from_static("x-requested-with"),
76 "XMLHttpRequest",
77 )
78 .form(&video_info)
79 .send()
80 .await?
81 .json()
82 .await?;
83
84 log::trace!("POST Response: {kodik_response:#?}");
85
86 Ok(kodik_response)
87}