diff --git a/Cargo.toml b/Cargo.toml index e206b0e..d1e5a6f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,18 +3,26 @@ name = "transmission-rss" version = "0.1.0" edition = "2021" +[features] +anissia = ["dep:tl", "dep:chrono", "dep:bytes"] + [dependencies] # trname = { path = "../trname" } -trname = { git = "https://github.com/syrflover/trname", rev = "3eee979" } +trname = { git = "https://github.com/syrflover/trname", rev = "3becf44" } + +chrono = { version = "0.4", features = ["serde"], optional = true } +bytes = { version = "1.7.1", optional = true } +tl = { version = "0.7.8", optional = true } -dotenv = "0.15.0" -futures = "0.3.30" -reqwest = "0.12.5" -rss = { version = "2.0.8", features = ["serde"] } -serde = "1.0.203" -serde_json = "1.0.120" -serde_yml = "0.0.11" -thiserror = "1.0.61" -tokio = { version = "1.38.0", features = ["rt-multi-thread", "macros"] } -transmission-rpc = "0.4.2" -url = "2.5.2" +dotenv = "0.15" +futures = "0.3" +reqwest = "0.12" +rss = { version = "2.0.9", features = ["serde"] } +serde = "1.0" +serde_json = "1.0" +serde_yml = "0.0.12" +thiserror = "1.0" +tokio = { version = "1.40", features = ["rt-multi-thread", "macros"] } +transmission-rpc = "0.4" +tap = "1.0.1" +url = "2.5" diff --git a/src/anissia.rs b/src/anissia.rs new file mode 100644 index 0000000..3171f57 --- /dev/null +++ b/src/anissia.rs @@ -0,0 +1,119 @@ +use std::fmt::Debug; + +use bytes::Bytes; +use chrono::{DateTime, Local}; +use reqwest::StatusCode; +use serde::Deserialize; +use tap::Pipe; +use tl::ParserOptions; + +#[derive(Debug, thiserror::Error)] +pub enum Error { + #[error("reqwest: {0}")] + Reqwest(#[from] reqwest::Error), + + #[error("tl: {0}")] + Tl(#[from] tl::ParseError), + + #[error("status: {0} - {1}")] + Status(StatusCode, String), +} + +mod sealed { + use std::fmt::Debug; + + use serde::Deserialize; + + #[derive(Debug, Deserialize)] + pub(super) struct ResponseDataInner { + pub content: T, + } +} + +#[derive(Debug, Deserialize)] +struct ResponseData { + pub data: sealed::ResponseDataInner, +} + +#[derive(Debug, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct CaptionInfo { + pub anime_no: u32, + pub subject: String, + pub episode: String, + #[serde(rename = "updDt")] + pub updated_at: DateTime, + pub website: String, + #[serde(rename = "name")] + pub translator: String, +} + +impl CaptionInfo { + pub async fn download(&self, selector: &str) -> Result, Error> { + let website_resp = reqwest::get(&self.website).await?; + + if !website_resp.status().is_success() { + return Err(Error::Status( + website_resp.status(), + website_resp.text().await.unwrap_or_default(), + )); + } + + let website = website_resp.text().await?; + + let dom = tl::parse(&website, ParserOptions::default())?; + + let parser = dom.parser(); + + let element = match dom + .query_selector(selector) + .and_then(|r| r.next()?.get(&parser)?.as_tag()?.pipe(Some)) + { + Some(r) => r, + None => return Ok(None), + }; + + let href = match element + .attributes() + .get("href") + .and_then(|r| String::from_utf8(r?.as_bytes().to_vec()).ok()) + { + Some(r) => r, + None => return Ok(None), + }; + + let resp = reqwest::get(&href).await?; + + Ok() + } +} + +// TODO: https://github.com/dhku/SMI-Auto-Downloader/blob/main/subs.py + +struct GoogleDrive {} + +impl GoogleDrive { + pub async fn download(url: &str) -> Result {} +} + +const ANISSIA_URL: &str = "https://api.anissia.net"; + +// https://api.anissia.net/anime/caption/recent/0 +// res_json.data.content + +pub async fn get_recent_captions(page: usize) -> Result, Error> { + let url = format!("{}/anime/caption/recent/{}", ANISSIA_URL, page); + + let resp = reqwest::get(url).await?; + + if !resp.status().is_success() { + return Err(Error::Status( + resp.status(), + resp.text().await.unwrap_or_default(), + )); + } + + let res = resp.json::>>().await?; + + Ok(res.data.content) +} diff --git a/src/lib.rs b/src/lib.rs index 4cb697c..d72eca5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,2 +1,4 @@ +#[cfg(feature = "anissia")] +pub mod anissia; pub mod config; pub mod rule;