use super::Error;
use super::{ request , _request};
use std::{
fs,io,io::Write
};
#[derive(Debug)]
pub struct Image {
pub url : Option<String>,
pub bytes : bytes::Bytes
}
impl Image {
pub fn save_as_new(&self, path : &str) -> Result<fs::File, io::Error> {
let mut file = fs::File::create(path)?;
file.write_all(&self.bytes)?;
Ok(file)
}
pub fn save_on_existing(&self, path : &str) -> Result<fs::File, io::Error> {
let mut file = fs::File::open(path)?;
file.write_all(&self.bytes)?;
Ok(file)
}
}
#[derive(Debug)]
pub struct Meme {
pub image : Image,
pub subreddit : String,
pub score : u64,
pub id : String,
pub self_text : String,
pub is_nsfw : bool
}
impl Meme {
pub fn get(topic : &str) -> Result<Meme, Error> {
let bytes = request(&format!("meme/{}",topic).to_string())?;
let json : serde_json::Value = serde_json::from_slice(&bytes).unwrap();
let bytes = _request(json["data"]["url"].as_str().unwrap())?;
let image = Image {
url : Some(json["data"]["url"].as_str().unwrap().to_string()),
bytes : bytes
};
Ok(
Meme {
image : image,
subreddit : json["data"]["subreddit"].as_str().unwrap().to_string(),
score : json["data"]["score"].as_u64().unwrap(),
id : json["data"]["id"].as_str().unwrap().to_string(),
self_text : json["data"]["selftext"].as_str().unwrap().to_string(),
is_nsfw : json["data"]["is_nsfw"].as_bool().unwrap()
}
)
}
}
pub fn get_random_meme() -> Result<Image, Error> {
let bytes = request("meme")?;
Ok(
Image {
url : None,
bytes : bytes
}
)
}
pub fn eightball() -> Result<String, Error> {
let bytes = request("8ball?simple=True")?;
Ok(
String::from_utf8(bytes.as_ref().to_vec()).unwrap()
)
}
pub fn would_you_rather() -> Result< (String , String) , Error> {
let bytes = request("wyr")?;
let json : serde_json::Value = serde_json::from_slice(&bytes).unwrap();
Ok(
(
json["data"]["Would you rather"][0].as_str().unwrap().to_string(),
json["data"]["Would you rather"][1].as_str().unwrap().to_string()
)
)
}
pub fn truth_or_dare() -> Result< (String, String) , Error> {
let bytes = request("truthordare")?;
let json : serde_json::Value = serde_json::from_slice(&bytes).unwrap();
Ok(
(
json["data"]["Truth"].as_str().unwrap().to_string(),
json["data"]["Dare"].as_str().unwrap().to_string(),
)
)
}
pub fn truth() -> Result<String , Error> {
let bytes = request("truth?simple=True")?;
Ok(
String::from_utf8(bytes.as_ref().to_vec()).unwrap()
)
}
pub fn dare() -> Result<String , Error> {
let bytes = request("dare?simple=True")?;
Ok(
String::from_utf8(bytes.as_ref().to_vec()).unwrap()
)
}
pub fn fact() -> Result<String, Error> {
let bytes = request("fact?simple=True")?;
Ok(
String::from_utf8(bytes.as_ref().to_vec()).unwrap()
)
}
pub fn roast() -> Result<String, Error> {
let bytes = request("roast?simple=True")?;
Ok(
String::from_utf8(bytes.as_ref().to_vec()).unwrap()
)
}
#[derive(Debug)]
pub struct Trivia {
pub ques : String,
pub ans : String
}
impl Trivia {
pub fn get() -> Result<Trivia, Error> {
let bytes = request("trivia")?;
let json : serde_json::Value = serde_json::from_slice(&bytes).unwrap();
Ok(
Trivia {
ques : json["data"]["Question"].as_str().unwrap().to_string(),
ans : json["data"]["Answer"].as_str().unwrap().to_string()
}
)
}
}
pub fn joke() -> Result<String, Error> {
let bytes = request("joke?simple=True")?;
Ok(
String::from_utf8(bytes.as_ref().to_vec()).unwrap()
)
}
pub fn compliment() -> Result<String, Error> {
let bytes = request("compliment?simple=True")?;
Ok(
String::from_utf8(bytes.as_ref().to_vec()).unwrap()
)
}
pub fn neverhaveiever() -> Result<String, Error> {
let bytes = request("neverhaveiever?simple=True")?;
Ok(
String::from_utf8(bytes.as_ref().to_vec()).unwrap()
)
}
pub fn topic() -> Result<String, Error> {
let bytes = request("topic?simple=True")?;
Ok(
String::from_utf8(bytes.as_ref().to_vec()).unwrap()
)
}