2020-10-26 09:25:59 +13:00
|
|
|
// CRATES
|
|
|
|
use actix_web::{get, web, HttpResponse, Result};
|
|
|
|
use askama::Template;
|
|
|
|
use chrono::{TimeZone, Utc};
|
|
|
|
|
2020-11-18 08:37:40 +13:00
|
|
|
#[path = "utils.rs"]
|
|
|
|
mod utils;
|
2020-11-18 13:03:28 +13:00
|
|
|
pub use utils::{Params, Flair, Post, Subreddit, val};
|
2020-11-18 08:37:40 +13:00
|
|
|
|
2020-10-26 09:25:59 +13:00
|
|
|
// STRUCTS
|
|
|
|
#[derive(Template)]
|
|
|
|
#[template(path = "subreddit.html", escape = "none")]
|
|
|
|
struct SubredditTemplate {
|
|
|
|
sub: Subreddit,
|
|
|
|
posts: Vec<Post>,
|
2020-10-26 16:57:19 +13:00
|
|
|
sort: String,
|
2020-10-26 09:25:59 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
async fn render(sub_name: String, sort: String) -> Result<HttpResponse> {
|
|
|
|
let mut sub: Subreddit = subreddit(&sub_name).await;
|
|
|
|
let posts: Vec<Post> = posts(sub_name, &sort).await;
|
|
|
|
|
2020-10-26 16:57:19 +13:00
|
|
|
sub.icon = if sub.icon != "" {
|
|
|
|
format!(r#"<img class="subreddit_icon" src="{}">"#, sub.icon)
|
|
|
|
} else {
|
|
|
|
String::new()
|
|
|
|
};
|
|
|
|
|
|
|
|
let s = SubredditTemplate { sub: sub, posts: posts, sort: sort }.render().unwrap();
|
2020-10-26 09:25:59 +13:00
|
|
|
Ok(HttpResponse::Ok().content_type("text/html").body(s))
|
|
|
|
}
|
|
|
|
|
|
|
|
// SERVICES
|
|
|
|
#[allow(dead_code)]
|
|
|
|
#[get("/r/{sub}")]
|
2020-11-18 13:03:28 +13:00
|
|
|
async fn page(web::Path(sub): web::Path<String>, params: web::Query<Params>) -> Result<HttpResponse> {
|
|
|
|
match ¶ms.sort {
|
|
|
|
Some(sort) => render(sub, sort.to_string()).await,
|
|
|
|
None => render(sub, "hot".to_string()).await,
|
|
|
|
}
|
2020-10-26 09:25:59 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
// SUBREDDIT
|
|
|
|
async fn subreddit(sub: &String) -> Subreddit {
|
2020-11-18 13:03:28 +13:00
|
|
|
// Make a GET request to the Reddit's JSON API for the metadata of this subreddit
|
2020-10-26 09:25:59 +13:00
|
|
|
let url: String = format!("https://www.reddit.com/r/{}/about.json", sub);
|
|
|
|
let resp: String = reqwest::get(&url).await.unwrap().text().await.unwrap();
|
|
|
|
|
2020-11-18 13:03:28 +13:00
|
|
|
// Parse the response from Reddit as JSON
|
2020-10-26 09:25:59 +13:00
|
|
|
let data: serde_json::Value = serde_json::from_str(resp.as_str()).expect("Failed to parse JSON");
|
|
|
|
|
|
|
|
let icon: String = String::from(data["data"]["community_icon"].as_str().unwrap()); //val(&data, "community_icon");
|
|
|
|
let icon_split: std::str::Split<&str> = icon.split("?");
|
|
|
|
let icon_parts: Vec<&str> = icon_split.collect();
|
|
|
|
|
2020-10-26 16:57:19 +13:00
|
|
|
Subreddit {
|
2020-10-26 09:25:59 +13:00
|
|
|
name: val(&data, "display_name").await,
|
2020-10-26 15:05:09 +13:00
|
|
|
title: val(&data, "title").await,
|
|
|
|
description: val(&data, "public_description").await,
|
2020-10-26 09:25:59 +13:00
|
|
|
icon: String::from(icon_parts[0]),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// POSTS
|
|
|
|
pub async fn posts(sub: String, sort: &String) -> Vec<Post> {
|
2020-11-18 13:03:28 +13:00
|
|
|
// Make a GET request to the Reddit's JSON API for the content of this subreddit
|
2020-10-26 09:25:59 +13:00
|
|
|
let url: String = format!("https://www.reddit.com/r/{}/{}.json", sub, sort);
|
|
|
|
let resp: String = reqwest::get(&url).await.unwrap().text().await.unwrap();
|
2020-10-26 16:57:19 +13:00
|
|
|
|
2020-11-18 13:03:28 +13:00
|
|
|
// Parse the response from Reddit as JSON
|
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 val(post, "thumbnail").await.starts_with("https:/") {
|
|
|
|
val(post, "thumbnail").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();
|
2020-10-26 09:25:59 +13:00
|
|
|
posts.push(Post {
|
2020-10-26 15:05:09 +13:00
|
|
|
title: val(post, "title").await,
|
2020-10-26 09:25:59 +13:00
|
|
|
community: val(post, "subreddit").await,
|
2020-11-18 08:37:40 +13:00
|
|
|
body: String::new(),
|
2020-10-26 09:25:59 +13:00
|
|
|
author: 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-11-18 08:37:40 +13:00
|
|
|
media: img,
|
2020-10-26 09:25:59 +13:00
|
|
|
url: 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(
|
|
|
|
val(post, "link_flair_text").await,
|
|
|
|
val(post, "link_flair_background_color").await,
|
2020-11-17 17:36:36 +13:00
|
|
|
if val(post, "link_flair_text_color").await == "dark" {
|
|
|
|
"black".to_string()
|
|
|
|
} else {
|
|
|
|
"white".to_string()
|
|
|
|
},
|
2020-11-17 15:49:08 +13:00
|
|
|
),
|
2020-10-26 09:25:59 +13:00
|
|
|
});
|
2020-10-26 16:57:19 +13:00
|
|
|
}
|
|
|
|
posts
|
|
|
|
}
|