1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
use lazy_static::lazy_static; use tokio::runtime::Runtime; use url::Url; use utils::Client; pub mod error; pub mod utils; pub mod website; pub use crate::error::*; pub type FinaResult<T = Finata> = Result<T, Error>; lazy_static! { static ref RUNTIME: Runtime = Runtime::new().unwrap(); } #[async_trait::async_trait] pub trait Extract { async fn extract(&mut self) -> FinaResult; } pub trait Config { fn client(&self) -> &Client; fn client_mut(&mut self) -> &mut Client; } pub trait ExtractSync { fn extract_sync(&mut self) -> FinaResult; } impl<T: Extract> ExtractSync for T { fn extract_sync(&mut self) -> FinaResult { RUNTIME.block_on(async { self.extract().await }) } } #[derive(Debug, PartialEq, Clone)] pub enum Track { Video(Url), Audio(Url), Text(Url), Image(Url), } #[derive(Debug, PartialEq, Clone)] pub struct Origin { pub tracks: Vec<Track>, pub title: String, } #[derive(Debug, PartialEq)] pub struct Finata { raws: Vec<Origin>, title: String, } impl Track { pub fn as_url(&self) -> &Url { match self { Self::Video(url) | Self::Audio(url) | Self::Image(url) | Self::Text(url) => url, } } } impl Origin { pub fn new(tracks: Vec<Track>, title: String) -> Self { Self { tracks, title } } pub fn video(url: Url, title: String) -> Self { Self::new(vec![Track::Video(url)], title) } pub fn audio(url: Url, title: String) -> Self { Self::new(vec![Track::Audio(url)], title) } pub fn image(url: Url, title: String) -> Self { Self::new(vec![Track::Image(url)], title) } } impl Finata { pub fn new(raws: Vec<Origin>, title: String) -> Self { Self { raws, title } } pub fn raws(&self) -> &[Origin] { &self.raws } pub fn title(&self) -> &str { &self.title } pub fn into_title(self) -> String { self.title } pub fn into_parts(self) -> (Vec<Origin>, String) { (self.raws, self.title) } }