Skip to main content

kodik_parser/
scraper.rs

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)]
10/// Response structure for player data containing video links
11pub struct Response {
12    /// Available video links organized by quality
13    pub links: Links,
14}
15
16#[derive(Debug, Deserialize)]
17/// Container for video links organized by different quality levels
18pub struct Links {
19    /// Video links for 360p quality
20    #[serde(rename = "360")]
21    pub quality_360: Vec<Link>,
22    /// Video links for 480p quality
23    #[serde(rename = "480")]
24    pub quality_480: Vec<Link>,
25    /// Video links for 720p quality
26    #[serde(rename = "720")]
27    pub quality_720: Vec<Link>,
28}
29
30#[derive(Debug, Deserialize)]
31/// Individual video link with source URL and content type
32pub struct Link {
33    /// Source URL of the video stream
34    pub src: String,
35    /// MIME type of the video content
36    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}