2020-10-26 09:25:59 +13:00
|
|
|
// CRATES
|
|
|
|
use actix_web::{get, web, HttpResponse, Result};
|
|
|
|
use askama::Template;
|
|
|
|
use chrono::{TimeZone, Utc};
|
|
|
|
|
|
|
|
// STRUCTS
|
|
|
|
#[derive(Template)]
|
|
|
|
#[template(path = "user.html", escape = "none")]
|
|
|
|
struct UserTemplate {
|
|
|
|
user: User,
|
|
|
|
posts: Vec<Post>,
|
2020-10-26 16:57:19 +13:00
|
|
|
sort: String,
|
2020-10-26 09:25:59 +13:00
|
|
|
}
|
|
|
|
|
2020-11-17 15:49:08 +13:00
|
|
|
// Post flair with text, background color and foreground color
|
|
|
|
pub struct Flair(String, String, String);
|
|
|
|
|
2020-10-26 09:25:59 +13:00
|
|
|
pub struct Post {
|
|
|
|
pub title: String,
|
|
|
|
pub community: String,
|
|
|
|
pub author: String,
|
|
|
|
pub score: String,
|
|
|
|
pub image: String,
|
|
|
|
pub url: String,
|
2020-10-26 16:57:19 +13:00
|
|
|
pub time: String,
|
2020-11-17 15:49:08 +13:00
|
|
|
pub flair: Flair,
|
2020-10-26 09:25:59 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct User {
|
|
|
|
pub name: String,
|
|
|
|
pub icon: String,
|
|
|
|
pub karma: i64,
|
|
|
|
pub banner: String,
|
2020-10-26 16:57:19 +13:00
|
|
|
pub description: String,
|
2020-10-26 09:25:59 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
async fn render(username: String, sort: String) -> Result<HttpResponse> {
|
|
|
|
let user: User = user(&username).await;
|
|
|
|
let posts: Vec<Post> = posts(username, &sort).await;
|
2020-10-26 16:57:19 +13:00
|
|
|
|
|
|
|
let s = UserTemplate { user: user, posts: posts, sort: sort }.render().unwrap();
|
2020-10-26 09:25:59 +13:00
|
|
|
Ok(HttpResponse::Ok().content_type("text/html").body(s))
|
|
|
|
}
|
|
|
|
|
|
|
|
// SERVICES
|
|
|
|
#[get("/u/{username}")]
|
|
|
|
async fn page(web::Path(username): web::Path<String>) -> Result<HttpResponse> {
|
|
|
|
render(username, "hot".to_string()).await
|
|
|
|
}
|
|
|
|
|
|
|
|
#[get("/u/{username}/{sort}")]
|
|
|
|
async fn sorted(web::Path((username, sort)): web::Path<(String, String)>) -> Result<HttpResponse> {
|
|
|
|
render(username, sort).await
|
|
|
|
}
|
|
|
|
|
|
|
|
// UTILITIES
|
2020-10-26 16:57:19 +13:00
|
|
|
async fn user_val(j: &serde_json::Value, k: &str) -> String {
|
|
|
|
String::from(j["data"]["subreddit"][k].as_str().unwrap())
|
|
|
|
}
|
|
|
|
async fn post_val(j: &serde_json::Value, k: &str) -> String {
|
|
|
|
String::from(j["data"][k].as_str().unwrap_or("Comment"))
|
|
|
|
}
|
2020-10-26 09:25:59 +13:00
|
|
|
|
|
|
|
// USER
|
|
|
|
async fn user(name: &String) -> User {
|
|
|
|
let url: String = format!("https://www.reddit.com/user/{}/about.json", name);
|
|
|
|
let resp: String = reqwest::get(&url).await.unwrap().text().await.unwrap();
|
|
|
|
|
|
|
|
let data: serde_json::Value = serde_json::from_str(resp.as_str()).expect("Failed to parse JSON");
|
2020-10-26 16:57:19 +13:00
|
|
|
|
2020-10-26 09:25:59 +13:00
|
|
|
User {
|
|
|
|
name: name.to_string(),
|
|
|
|
icon: user_val(&data, "icon_img").await,
|
|
|
|
karma: data["data"]["total_karma"].as_i64().unwrap(),
|
|
|
|
banner: user_val(&data, "banner_img").await,
|
2020-10-26 16:57:19 +13:00
|
|
|
description: user_val(&data, "public_description").await,
|
2020-10-26 09:25:59 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// POSTS
|
|
|
|
async fn posts(sub: String, sort: &String) -> Vec<Post> {
|
|
|
|
let url: String = format!("https://www.reddit.com/u/{}/.json?sort={}", sub, sort);
|
|
|
|
let resp: String = reqwest::get(&url).await.unwrap().text().await.unwrap();
|
2020-10-26 16:57:19 +13:00
|
|
|
|
2020-10-26 09:25:59 +13:00
|
|
|
let popular: serde_json::Value = serde_json::from_str(resp.as_str()).expect("Failed to parse JSON");
|
|
|
|
let post_list = popular["data"]["children"].as_array().unwrap();
|
|
|
|
|
|
|
|
let mut posts: Vec<Post> = Vec::new();
|
2020-10-26 16:57:19 +13:00
|
|
|
|
2020-10-26 09:25:59 +13:00
|
|
|
for post in post_list.iter() {
|
2020-10-26 16:57:19 +13:00
|
|
|
let img = if post_val(post, "thumbnail").await.starts_with("https:/") {
|
|
|
|
post_val(post, "thumbnail").await
|
|
|
|
} else {
|
|
|
|
String::new()
|
|
|
|
};
|
2020-10-26 09:25:59 +13:00
|
|
|
let unix_time: i64 = post["data"]["created_utc"].as_f64().unwrap().round() as i64;
|
|
|
|
let score = post["data"]["score"].as_i64().unwrap();
|
|
|
|
posts.push(Post {
|
2020-10-26 15:05:09 +13:00
|
|
|
title: post_val(post, "title").await,
|
2020-10-26 09:25:59 +13:00
|
|
|
community: post_val(post, "subreddit").await,
|
|
|
|
author: post_val(post, "author").await,
|
2020-10-26 16:57:19 +13:00
|
|
|
score: if score > 1000 { format!("{}k", score / 1000) } else { score.to_string() },
|
2020-10-26 09:25:59 +13:00
|
|
|
image: img,
|
|
|
|
url: post_val(post, "permalink").await,
|
2020-10-26 16:57:19 +13:00
|
|
|
time: Utc.timestamp(unix_time, 0).format("%b %e '%y").to_string(),
|
2020-11-17 15:49:08 +13:00
|
|
|
flair: Flair(
|
|
|
|
post_val(post, "link_flair_text").await,
|
|
|
|
post_val(post, "link_flair_background_color").await,
|
|
|
|
if post_val(post, "link_flair_text_color").await == "dark" { "black".to_string() } else { "white".to_string() }
|
|
|
|
),
|
2020-10-26 09:25:59 +13:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
posts
|
2020-10-26 16:57:19 +13:00
|
|
|
}
|