redsunlib/src/utils.rs

126 lines
3.0 KiB
Rust
Raw Normal View History

2020-11-20 17:42:18 +13:00
//
// STRUCTS
//
2020-11-18 13:03:28 +13:00
#[allow(dead_code)]
2020-11-18 08:37:40 +13:00
// Post flair with text, background color and foreground color
pub struct Flair(pub String, pub String, pub String);
2020-11-18 13:03:28 +13:00
#[allow(dead_code)]
2020-11-18 08:37:40 +13:00
// Post containing content, metadata and media
pub struct Post {
pub title: String,
pub community: String,
pub body: String,
pub author: String,
pub url: String,
pub score: String,
pub media: String,
pub time: String,
pub flair: Flair,
}
#[allow(dead_code)]
// Comment with content, post, score and data/time that it was posted
pub struct Comment {
pub body: String,
pub author: String,
pub score: String,
pub time: String,
}
#[allow(dead_code)]
// User struct containing metadata about user
pub struct User {
pub name: String,
pub icon: String,
pub karma: i64,
pub banner: String,
pub description: String,
}
#[allow(dead_code)]
// Subreddit struct containing metadata about community
pub struct Subreddit {
pub name: String,
pub title: String,
pub description: String,
pub icon: String,
}
2020-11-20 10:49:32 +13:00
// Parser for query params, used in sorting (eg. /r/rust/?sort=hot)
#[derive(serde::Deserialize)]
pub struct Params {
pub sort: Option<String>,
pub after: Option<String>,
pub before: Option<String>,
}
2020-11-20 17:42:18 +13:00
// Error template
#[derive(askama::Template)]
#[template(path = "error.html", escape = "none")]
pub struct ErrorTemplate {
pub message: String,
}
//
// JSON PARSING
//
2020-11-18 13:03:28 +13:00
#[allow(dead_code)]
2020-11-18 08:37:40 +13:00
// val() function used to parse JSON from Reddit APIs
pub async fn val(j: &serde_json::Value, k: &str) -> String {
String::from(j["data"][k].as_str().unwrap_or(""))
}
#[allow(dead_code)]
// nested_val() function used to parse JSON from Reddit APIs
pub async fn nested_val(j: &serde_json::Value, n: &str, k: &str) -> String {
String::from(j["data"][n][k].as_str().unwrap())
2020-11-18 13:03:28 +13:00
}
2020-11-20 17:42:18 +13:00
//
// NETWORKING
//
2020-11-19 15:50:59 +13:00
// Make a request to a Reddit API and parse the JSON response
#[allow(dead_code)]
2020-11-20 17:42:18 +13:00
pub async fn request(url: String) -> Result<serde_json::Value, &'static str> {
2020-11-19 15:50:59 +13:00
// --- actix-web::client ---
// let client = actix_web::client::Client::default();
// let res = client
// .get(url)
// .send()
// .await?
// .body()
// .limit(1000000)
// .await?;
// let body = std::str::from_utf8(res.as_ref())?; // .as_ref converts Bytes to [u8]
// --- surf ---
// let req = surf::get(url);
// let client = surf::client().with(surf::middleware::Redirect::new(5));
// let mut res = client.send(req).await.unwrap();
// let body = res.body_string().await.unwrap();
// --- reqwest ---
2020-11-20 17:42:18 +13:00
let res = reqwest::get(&url).await.unwrap();
// Read the status from the response
let success = res.status().is_success();
// Read the body of the response
let body = res.text().await.unwrap();
2020-11-19 15:50:59 +13:00
// Parse the response from Reddit as JSON
2020-11-20 17:42:18 +13:00
let json: serde_json::Value = serde_json::from_str(body.as_str()).unwrap_or(serde_json::Value::Null);
2020-11-20 10:49:32 +13:00
2020-11-20 17:42:18 +13:00
if !success {
Ok(json)
} else if json == serde_json::Value::Null {
println!("! {} - {}", url, "Failed to parse page JSON data");
Err("Failed to parse page JSON data")
} else {
println!("! {} - {}", url, "Page not found");
Err("Page not found")
}
2020-11-19 15:50:59 +13:00
}