Commit 1c3d249741
Unsigned
Layout: unified · split
src/anilist.rs added +122
| @@ -0,0 +1,122 @@ | |||
| 1 | |||
| 2 | use reqwest::{header::{HeaderMap, HeaderValue, AUTHORIZATION, CONTENT_TYPE}, Error}; | ||
| 3 | use serde::{Serialize, Deserialize}; | ||
| 4 | |||
| 5 | #[derive(Serialize)] | ||
| 6 | struct Query { | ||
| 7 | query: String, | ||
| 8 | variables: Variables, | ||
| 9 | } | ||
| 10 | |||
| 11 | #[derive(Serialize)] | ||
| 12 | struct Variables { | ||
| 13 | search: String, | ||
| 14 | } | ||
| 15 | |||
| 16 | #[derive(Deserialize, Debug)] | ||
| 17 | struct Media { | ||
| 18 | id: i32, | ||
| 19 | title: Title, | ||
| 20 | } | ||
| 21 | |||
| 22 | #[derive(Deserialize, Debug)] | ||
| 23 | struct Title { | ||
| 24 | romaji: String, | ||
| 25 | english: Option<String>, | ||
| 26 | native: String, | ||
| 27 | } | ||
| 28 | |||
| 29 | #[derive(Deserialize, Debug)] | ||
| 30 | struct Page { | ||
| 31 | media: Vec<Media>, | ||
| 32 | } | ||
| 33 | |||
| 34 | #[derive(Deserialize, Debug)] | ||
| 35 | struct ResponseData { | ||
| 36 | Page: Page, | ||
| 37 | } | ||
| 38 | |||
| 39 | #[derive(Deserialize, Debug)] | ||
| 40 | struct ApiResponse { | ||
| 41 | data: ResponseData, | ||
| 42 | } | ||
| 43 | |||
| 44 | pub struct AnimeApi { | ||
| 45 | token: String, | ||
| 46 | } | ||
| 47 | |||
| 48 | impl AnimeApi { | ||
| 49 | // Public constructor for the struct | ||
| 50 | pub fn new(token: String) -> Self { | ||
| 51 | AnimeApi { token } | ||
| 52 | } | ||
| 53 | |||
| 54 | pub fn search_anime_anilist(&self, query: &str) -> Result<ApiResponse, reqwest::Error> { | ||
| 55 | let url = "https://graphql.anilist.co"; | ||
| 56 | |||
| 57 | let query_string = r#" | ||
| 58 | query ($search: String) { | ||
| 59 | Page(page: 1, perPage: 10) { | ||
| 60 | media(search: $search, type: ANIME) { | ||
| 61 | id | ||
| 62 | title { | ||
| 63 | romaji | ||
| 64 | english | ||
| 65 | native | ||
| 66 | } | ||
| 67 | } | ||
| 68 | } | ||
| 69 | } | ||
| 70 | "#; | ||
| 71 | |||
| 72 | let variables = Variables { | ||
| 73 | search: query.to_string(), | ||
| 74 | }; | ||
| 75 | |||
| 76 | let request_body = Query { | ||
| 77 | query: query_string.to_string(), | ||
| 78 | variables, | ||
| 79 | }; | ||
| 80 | |||
| 81 | let mut headers = HeaderMap::new(); | ||
| 82 | headers.insert(AUTHORIZATION, HeaderValue::from_str(&format!("Bearer {}", self.token)).unwrap()); | ||
| 83 | headers.insert(CONTENT_TYPE, HeaderValue::from_static("application/json")); | ||
| 84 | |||
| 85 | // Send the request | ||
| 86 | let client = reqwest::blocking::Client::new(); | ||
| 87 | let response = client | ||
| 88 | .post(url) | ||
| 89 | .headers(headers) | ||
| 90 | .json(&request_body) | ||
| 91 | .send()?; | ||
| 92 | |||
| 93 | // Check if the response is successful | ||
| 94 | if response.status().is_success() { | ||
| 95 | let api_response: ApiResponse = response.json()?; | ||
| 96 | Ok(api_response) | ||
| 97 | } else { | ||
| 98 | Err(Error) | ||
| 99 | } | ||
| 100 | } | ||
| 101 | } | ||
| 102 | |||
| 103 | |||
| 104 | // pub fn parse_search_anime_anilist(response_text: &String) -> Result<Vec<SkipTime>, Box<dyn Error>>{ | ||
| 105 | // // Deserialize the JSON into AniskipResponse | ||
| 106 | // let parsed_data: AniskipResponse = serde_json::from_str(response_text)?; | ||
| 107 | |||
| 108 | // // If no skips are found, return an empty vector | ||
| 109 | // if !parsed_data.found { | ||
| 110 | // return Ok(Vec::new()); | ||
| 111 | // } | ||
| 112 | // // Convert the parsed results into SkipTime structs | ||
| 113 | // let skip_times: Vec<SkipTime> = parsed_data.results.into_iter().map(|result| { | ||
| 114 | // SkipTime::new( | ||
| 115 | // result.kind, | ||
| 116 | // result.interval.start as i32, | ||
| 117 | // result.interval.end as i32, | ||
| 118 | // ) | ||
| 119 | // }).collect(); | ||
| 120 | |||
| 121 | // Ok(skip_times) | ||
| 122 | // }\ No newline at end of file | ||
src/aniskip.rs +19 −1
| @@ -4,7 +4,7 @@ use std::{error::Error}; | |||
| 4 | use serde_json; | 4 | use serde_json; |
| 5 | 5 | ||
| 6 | // Define the enum for SkipType | 6 | // Define the enum for SkipType |
| 7 | #[derive(Debug, Deserialize)] | 7 | #[derive(Debug, Deserialize, PartialEq)] |
| 8 | pub enum SkipType { | 8 | pub enum SkipType { |
| 9 | #[serde(rename = "op")] | 9 | #[serde(rename = "op")] |
| 10 | Op, | 10 | Op, |
| @@ -100,6 +100,24 @@ pub fn parse_aniskip_response(response_text: &String) -> Result<Vec<SkipTime>, B | |||
| 100 | Ok(skip_times) | 100 | Ok(skip_times) |
| 101 | } | 101 | } |
| 102 | 102 | ||
| 103 | pub fn get_skip_times(anime_id: i32, episode_number: i32) -> Result<Vec<SkipTime>, Box<dyn Error>> { | ||
| 104 | |||
| 105 | let mut response_text = get_aniskip_data(anime_id, episode_number); | ||
| 106 | |||
| 107 | match response_text{ | ||
| 108 | Ok(response_text) => { | ||
| 109 | let skip_times = parse_aniskip_response(&response_text); | ||
| 110 | // println!("{:?}", skip_times); | ||
| 111 | skip_times | ||
| 112 | |||
| 113 | }, | ||
| 114 | Err(e) => { | ||
| 115 | // eprintln!("Error fetching Aniskip data: {}", e); | ||
| 116 | Err(e) | ||
| 117 | } | ||
| 118 | } | ||
| 119 | } | ||
| 120 | |||
| 103 | // Unit tests | 121 | // Unit tests |
| 104 | #[cfg(test)] | 122 | #[cfg(test)] |
| 105 | mod tests { | 123 | mod tests { |
src/lib.rs +58 −1
| @@ -1 +1,58 @@ | |||
| 1 | pub mod aniskip;\ No newline at end of file | 1 | pub mod aniskip; |
| 2 | use aniskip::SkipTime; | ||
| 3 | |||
| 4 | pub struct Anime { | ||
| 5 | pub anilist_id: Option<i32>, | ||
| 6 | pub mal_id: Option<i32>, | ||
| 7 | pub allanime_id: Option<String>, | ||
| 8 | pub watching_ep: Option<i32>, | ||
| 9 | pub playing_time: Option<i32>, | ||
| 10 | pub skip_times: Option<Vec<SkipTime>>, | ||
| 11 | } | ||
| 12 | |||
| 13 | impl Anime { | ||
| 14 | // Constructor to create a new instance with fields initially set to None | ||
| 15 | pub fn new() -> Self { | ||
| 16 | Anime { | ||
| 17 | anilist_id: None, | ||
| 18 | mal_id: None, | ||
| 19 | allanime_id: None, | ||
| 20 | watching_ep: None, | ||
| 21 | playing_time: None, | ||
| 22 | skip_times: None, | ||
| 23 | } | ||
| 24 | } | ||
| 25 | |||
| 26 | pub fn anilist_id(&mut self, id: i32){ | ||
| 27 | self.anilist_id = Some(id); | ||
| 28 | } | ||
| 29 | |||
| 30 | // Method to set the `mal_id` | ||
| 31 | pub fn mal_id(&mut self, id: i32){ | ||
| 32 | self.mal_id = Some(id); | ||
| 33 | } | ||
| 34 | |||
| 35 | // Method to set the `allanime_id` | ||
| 36 | pub fn allanime_id(&mut self, id: String){ | ||
| 37 | self.allanime_id = Some(id); | ||
| 38 | } | ||
| 39 | |||
| 40 | // Method to set the `watching_ep` | ||
| 41 | pub fn watching_ep(&mut self, ep: i32){ | ||
| 42 | self.watching_ep = Some(ep); | ||
| 43 | } | ||
| 44 | |||
| 45 | // Method to set the `playing_time` | ||
| 46 | pub fn playing_time(&mut self, time: i32){ | ||
| 47 | self.playing_time = Some(time); | ||
| 48 | } | ||
| 49 | |||
| 50 | // Method to set the `skip_times` | ||
| 51 | pub fn skip_times(&mut self, times: Vec<SkipTime>){ | ||
| 52 | self.skip_times = Some(times); | ||
| 53 | } | ||
| 54 | } | ||
| 55 | |||
| 56 | pub struct Mpv { | ||
| 57 | pub speed: f32, | ||
| 58 | }\ No newline at end of file | ||
src/main.rs +40 −19
| @@ -1,30 +1,51 @@ | |||
| 1 | use std::process::exit; | 1 | // use std::process::exit; |
| 2 | use curd::aniskip::{get_aniskip_data, parse_aniskip_response, SkipInterval, SkipType, SkipResult, SkipTime}; | 2 | use curd::aniskip::{get_skip_times, SkipType, SkipTime}; |
| 3 | use curd::Anime; | ||
| 4 | // mod anilist; | ||
| 3 | 5 | ||
| 4 | fn main() { | 6 | fn main() { |
| 5 | let anime_id = 21; | 7 | // let query = "Naruto"; |
| 6 | let episode_no = 2; | 8 | // let token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImp0aSI6IjY5NTU3NWExMzY5NjE5ZWUxZWYwNTY5OTA0MDUxZDc0NTE5ZTM2MmZmZDViOTI5YjE1YjMwMTBjZWQyNDMyZDJiYmZlNTcxZmMxNGU0OGQ0In0.eyJhdWQiOiIyMDY4NiIsImp0aSI6IjY5NTU3NWExMzY5NjE5ZWUxZWYwNTY5OTA0MDUxZDc0NTE5ZTM2MmZmZDViOTI5YjE1YjMwMTBjZWQyNDMyZDJiYmZlNTcxZmMxNGU0OGQ0IiwiaWF0IjoxNzI3MjcyNDkxLCJuYmYiOjE3MjcyNzI0OTEsImV4cCI6MTc1ODgwODQ5MSwic3ViIjoiNjcxOTUyNiIsInNjb3BlcyI6W119.cpypIpuRu1D0LfNZpKgNVyGGViEVeaB9IZ2zkWcoudfzmrT8xIweYgpCLjK7pCliPFX4UhT3oC8vYh1kwDmUKGkr6TcyJ4zWEDpRVZUZwtT6jt6y9cotNu5Wifk2FRSKQ4R9zLTsrgFRiSxHQC5gMk6-qbVsv94AO77QNIRXNTcjGY0260S-EQFsm988s77yEsWiGL27OJCU6D02ik89mS0vQqdifh3HvnJQI7rJOctg0zRouHSbhj8csevjzOcdU2zjwO669rn9zZZUerA99Sb1fUEQuAt-c7A8TdAJwekdX50bPNOETs-pHxH4maOW_5yV-osTVvdC8WoTSWr8rmGN3pvxhGJ-Z97nCw5L-RCyoJV93DkGZie4esfUPEKoewzB4l6TUtbuUFT0EYF-QD4Wh7dpzBcMXiVJUVyhfEuf25LpFknn3r3L2ATPj9MM-tgGLJQCqHKgEn0MVys6vbZJmYxPNAnQ9wej0hrLno1JoP8ap7obnU8_BG3cx9PDzgRHT_X6goH4V74tgxspt6gIUblDJC4xNjodnKbgrz3CCHNhb_HVWY7OtsdenBn69nNNz1iN4ZzH2ioksYzpfEm0tVoHmzIxGNDRJMe5yPKYO_ccOTviez7uIY4r2w2t9roNt-4pT5UkevKr-vrTKlx_LtlWl42u8KXfv1ta9fY"; // Replace with your token |
| 7 | 9 | ||
| 8 | let mut response_text = get_aniskip_data(anime_id, episode_no); | 10 | // match anilist::search_anime_anilist(query, token) { |
| 11 | // Ok(response) => { | ||
| 12 | // println!("{:#?}", response); | ||
| 13 | // } | ||
| 14 | // Err(err) => { | ||
| 15 | // eprintln!("Error: {:?}", err); | ||
| 16 | // } | ||
| 17 | // } | ||
| 9 | 18 | ||
| 10 | match response_text{ | 19 | let mut anime = Anime::new(); |
| 11 | Ok(response_text) => { | 20 | anime.anilist_id(21); |
| 12 | let skip_times = parse_aniskip_response(&response_text); | 21 | anime.watching_ep(2); |
| 13 | // println!("{:?}", skip_times); | 22 | // let episode_no = 2; |
| 14 | match skip_times{ | 23 | anime.skip_times( |
| 15 | Ok(skip_times) => { | 24 | match get_skip_times(anime.anilist_id.unwrap(), anime.watching_ep.unwrap()) { |
| 16 | for skip in skip_times { | 25 | Ok(skips) => { |
| 17 | println!("{:?} skips from {} to {}", skip.kind, skip.intervals.start, skip.intervals.end); | 26 | skips |
| 18 | } | 27 | }, |
| 28 | Err(_) => { | ||
| 29 | println!("Failed to get skip times"); | ||
| 30 | vec![SkipTime::new(SkipType::Op, 0, 0), SkipTime::new(SkipType::Ed, 0, 0)] | ||
| 31 | } | ||
| 32 | } | ||
| 33 | ); | ||
| 34 | |||
| 35 | match anime.skip_times { | ||
| 36 | Some(anime_skip) => { | ||
| 37 | for skip in anime_skip{ | ||
| 38 | // println!("{:?}", anime.skip_times); | ||
| 39 | if skip.kind == SkipType::Op { | ||
| 40 | println!("Opening: {} to {}", skip.intervals.start, skip.intervals.end); | ||
| 19 | } | 41 | } |
| 20 | Err(e) => { | 42 | else if skip.kind == SkipType::Ed { |
| 21 | eprintln!("Error: {e}") | 43 | println!("Ending: {} to {}", skip.intervals.start, skip.intervals.end); |
| 22 | } | 44 | } |
| 23 | } | 45 | } |
| 24 | }, | 46 | }, |
| 25 | Err(e) => { | 47 | None => { |
| 26 | eprintln!("Error fetching Aniskip data: {}", e); | 48 | println!("Failed to get skip times"); |
| 27 | } | 49 | } |
| 28 | } | 50 | } |
| 29 | |||
| 30 | }\ No newline at end of file | 51 | }\ No newline at end of file |