use chrono::{TimeZone, Utc};
use serde_json::{from_str, Value};
#[cfg(feature = "proxy")]
use base64::encode;
pub struct Flair(pub String, pub String, pub String);
pub struct Post {
pub title: String,
pub community: String,
pub body: String,
pub author: String,
pub author_flair: Flair,
pub url: String,
pub score: String,
pub post_type: String,
pub flair: Flair,
pub nsfw: bool,
pub media: String,
pub time: String,
}
pub struct Comment {
pub id: String,
pub body: String,
pub author: String,
pub flair: Flair,
pub score: String,
pub time: String,
pub replies: Vec<Comment>,
}
pub struct User {
pub name: String,
pub icon: String,
pub karma: i64,
pub created: String,
pub banner: String,
pub description: String,
}
#[derive(Default)]
pub struct Subreddit {
pub name: String,
pub title: String,
pub description: String,
pub info: String,
pub icon: String,
pub members: String,
pub active: String,
}
#[derive(serde::Deserialize)]
pub struct Params {
pub sort: Option<String>,
pub after: Option<String>,
pub before: Option<String>,
}
#[derive(askama::Template)]
#[template(path = "error.html", escape = "none")]
pub struct ErrorTemplate {
pub message: String,
}
pub async fn format_url(url: String) -> String {
if url.is_empty() {
return String::new();
};
#[cfg(feature = "proxy")]
return "/proxy/".to_string() + encode(url).as_str();
#[cfg(not(feature = "proxy"))]
return url.to_string();
}
pub fn format_num(num: i64) -> String {
if num > 1000000 {
format!("{}m", num / 1000000)
} else if num > 1000 {
format!("{}k", num / 1000)
} else {
num.to_string()
}
}
pub async fn val(j: &serde_json::Value, k: &str) -> String {
String::from(j["data"][k].as_str().unwrap_or(""))
}
pub async fn nested_val(j: &serde_json::Value, n: &str, k: &str) -> String {
String::from(j["data"][n][k].as_str().unwrap())
}
pub async fn fetch_posts(url: String, fallback_title: String) -> Result<(Vec<Post>, String), &'static str> {
let req = request(url.clone()).await;
if req.is_err() {
return Err(req.err().unwrap());
}
let res = req.unwrap();
let post_list = res["data"]["children"].as_array().unwrap();
let mut posts: Vec<Post> = Vec::new();
for post in post_list {
let img = if val(post, "thumbnail").await.starts_with("https:/") {
format_url(val(post, "thumbnail").await).await
} else {
String::new()
};
let unix_time: i64 = post["data"]["created_utc"].as_f64().unwrap().round() as i64;
let score = post["data"]["score"].as_i64().unwrap();
let title = val(post, "title").await;
posts.push(Post {
title: if title.is_empty() { fallback_title.to_owned() } else { title },
community: val(post, "subreddit").await,
body: val(post, "body_html").await,
author: val(post, "author").await,
author_flair: Flair(
val(post, "author_flair_text").await,
val(post, "author_flair_background_color").await,
val(post, "author_flair_text_color").await,
),
score: format_num(score),
post_type: "link".to_string(),
media: img,
flair: Flair(
val(post, "link_flair_text").await,
val(post, "link_flair_background_color").await,
if val(post, "link_flair_text_color").await == "dark" {
"black".to_string()
} else {
"white".to_string()
},
),
nsfw: post["data"]["over_18"].as_bool().unwrap_or(false),
url: val(post, "permalink").await,
time: Utc.timestamp(unix_time, 0).format("%b %e '%y").to_string(),
});
}
dbg!(url);
Ok((posts, res["data"]["after"].as_str().unwrap_or("").to_string()))
}
pub async fn request(mut url: String) -> Result<serde_json::Value, &'static str> {
url = format!("https://www.reddit.com/{}", url);
let res = reqwest::get(&url).await.unwrap();
let success = res.status().is_success();
let body = res.text().await.unwrap();
let json: Value = from_str(body.as_str()).unwrap_or(Value::Null);
if !success {
println!("! {} - {}", url, "Page not found");
Err("Page not found")
} else if json == Value::Null {
println!("! {} - {}", url, "Failed to parse page JSON data");
Err("Failed to parse page JSON data")
} else {
Ok(json)
}
}