#[cfg(feature = "rand")]
use crate::random_usize_vec;
use serde::Deserialize;
use super::mini_data::{MiniPost, MiniPosts};
#[derive(Default, Debug, Clone, PartialEq, Deserialize)]
pub struct Posts(Vec<Post>);
#[derive(Default, Debug, Clone, PartialEq, Deserialize)]
pub struct Post {
pub preview_url: String,
pub sample_url: String,
pub file_url: String,
pub hash: String,
pub width: i64,
pub height: i64,
pub id: i64,
pub image: String,
pub owner: String,
pub parent_id: i64,
pub sample: bool,
pub sample_height: i64,
pub sample_width: i64,
pub score: i64,
pub tags: String,
pub has_notes: bool,
}
impl From<Vec<Post>> for Posts {
#[inline]
fn from(value: Vec<Post>) -> Self {
Self(value)
}
}
impl From<()> for Posts {
#[inline]
fn from(_: ()) -> Self {
Posts::default()
}
}
impl Posts {
pub fn get_p_urls(&self) -> Vec<&str> {
let mut urls: Vec<&str> = vec![];
self.0.iter().for_each(|x| urls.push(&x.preview_url));
urls
}
pub fn get_s_urls(&self) -> Vec<&str> {
let mut urls: Vec<&str> = vec![];
self.0.iter().for_each(|x| urls.push(&x.sample_url));
urls
}
pub fn get_f_urls(&self) -> Vec<&str> {
let mut urls: Vec<&str> = vec![];
self.0.iter().for_each(|x| urls.push(&x.file_url));
urls
}
#[inline]
pub fn get_urls_ext(&self) -> MiniPosts {
self.into()
}
#[inline]
pub fn get_url_ext(&self) -> Option<MiniPost> {
self.0.first().map(|x| x.into())
}
#[inline]
pub fn get_p_url(&self) -> Option<&str> {
self.0.first().map(|x| x.preview_url.as_str())
}
#[inline]
pub fn get_s_url(&self) -> Option<&str> {
self.0.first().map(|x| x.sample_url.as_str())
}
#[inline]
pub fn get_f_url(&self) -> Option<&str> {
self.0.first().map(|x| x.file_url.as_str())
}
#[inline]
pub fn data_ref(&self) -> &Vec<Post> {
&self.0
}
#[inline]
pub fn data(self) -> Vec<Post> {
self.0
}
#[cfg(feature = "rand")]
#[inline]
pub fn get_random_f_urls(&self, size: usize) -> Vec<&str> {
random_usize_vec!(self.get_f_urls(), size)
}
#[cfg(feature = "rand")]
#[inline]
pub fn get_random_p_urls(&self, size: usize) -> Vec<&str> {
random_usize_vec!(self.get_p_urls(), size)
}
#[cfg(feature = "rand")]
#[inline]
pub fn get_random_s_urls(&self, size: usize) -> Vec<&str> {
random_usize_vec!(self.get_s_urls(), size)
}
#[cfg(feature = "rand")]
#[inline]
pub fn shuffle(&self) -> Posts {
use crate::random_usize_vec_cloned;
random_usize_vec_cloned!(self.0, self.0.len())
}
#[inline]
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
#[inline]
pub fn len(&self) -> usize {
self.0.len()
}
}
#[cfg(test)]
mod tests {
use uller::JsonDownload;
use crate::prelude::*;
#[tokio::test]
async fn create_make_link_search_with_id() {
let binding = R34Params::init().id(10542274).download().await.unwrap();
let result = binding.get_f_urls();
println!("{:#?}", result);
assert!(!result.is_empty())
}
#[tokio::test]
async fn check_error() {
let binding = R34Params::init().limit(10).download().await.unwrap();
let result = binding.get_f_urls();
println!("{:#?}", result);
assert!(!result.is_empty());
let result = binding.get_random_f_urls(3);
println!("{:#?}", result);
}
}