2020-10-26 09:25:59 +13:00
|
|
|
// CRATES
|
|
|
|
use actix_web::{get, web, HttpResponse, Result};
|
|
|
|
use askama::Template;
|
|
|
|
use chrono::{TimeZone, Utc};
|
2020-10-26 16:57:19 +13:00
|
|
|
use pulldown_cmark::{html, Options, Parser};
|
2020-10-26 09:25:59 +13:00
|
|
|
|
2020-11-18 08:37:40 +13:00
|
|
|
#[path = "utils.rs"]
|
|
|
|
mod utils;
|
|
|
|
use utils::{Comment, Flair, Post, val};
|
|
|
|
|
2020-10-26 09:25:59 +13:00
|
|
|
// STRUCTS
|
|
|
|
#[derive(Template)]
|
|
|
|
#[template(path = "post.html", escape = "none")]
|
|
|
|
struct PostTemplate {
|
|
|
|
comments: Vec<Comment>,
|
|
|
|
post: Post,
|
2020-10-26 16:57:19 +13:00
|
|
|
sort: String,
|
2020-10-26 09:25:59 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
async fn render(id: String, sort: String) -> Result<HttpResponse> {
|
|
|
|
println!("id: {}", id);
|
|
|
|
let post: Post = fetch_post(&id).await;
|
|
|
|
let comments: Vec<Comment> = fetch_comments(id, &sort).await.unwrap();
|
2020-10-26 16:57:19 +13:00
|
|
|
|
2020-10-26 09:25:59 +13:00
|
|
|
let s = PostTemplate {
|
|
|
|
comments: comments,
|
|
|
|
post: post,
|
2020-11-17 17:36:36 +13:00
|
|
|
sort: sort,
|
2020-10-26 09:25:59 +13:00
|
|
|
}
|
|
|
|
.render()
|
|
|
|
.unwrap();
|
2020-10-26 15:05:09 +13:00
|
|
|
|
|
|
|
// println!("{}", s);
|
2020-10-26 16:57:19 +13:00
|
|
|
|
2020-10-26 09:25:59 +13:00
|
|
|
Ok(HttpResponse::Ok().content_type("text/html").body(s))
|
|
|
|
}
|
|
|
|
|
|
|
|
// SERVICES
|
|
|
|
#[get("/{id}")]
|
|
|
|
async fn short(web::Path(id): web::Path<String>) -> Result<HttpResponse> {
|
|
|
|
render(id.to_string(), "confidence".to_string()).await
|
|
|
|
}
|
|
|
|
|
2020-10-26 13:52:57 +13:00
|
|
|
#[get("/r/{sub}/comments/{id}/{title}/")]
|
2020-10-26 09:25:59 +13:00
|
|
|
async fn page(web::Path((_sub, id)): web::Path<(String, String)>) -> Result<HttpResponse> {
|
|
|
|
render(id.to_string(), "confidence".to_string()).await
|
|
|
|
}
|
|
|
|
|
|
|
|
#[get("/r/{sub}/comments/{id}/{title}/{sort}")]
|
|
|
|
async fn sorted(web::Path((_sub, id, _title, sort)): web::Path<(String, String, String, String)>) -> Result<HttpResponse> {
|
|
|
|
render(id.to_string(), sort).await
|
|
|
|
}
|
|
|
|
|
|
|
|
// UTILITIES
|
|
|
|
async fn media(data: &serde_json::Value) -> String {
|
|
|
|
let post_hint: &str = data["data"]["post_hint"].as_str().unwrap_or("");
|
|
|
|
let has_media: bool = data["data"]["media"].is_object();
|
|
|
|
|
2020-10-26 16:57:19 +13:00
|
|
|
let media: String = if !has_media {
|
|
|
|
format!(r#"<h4 class="post_body"><a href="{u}">{u}</a></h4>"#, u = data["data"]["url"].as_str().unwrap())
|
|
|
|
} else {
|
|
|
|
format!(r#"<img class="post_image" src="{}.png"/>"#, data["data"]["url"].as_str().unwrap())
|
|
|
|
};
|
2020-10-26 09:25:59 +13:00
|
|
|
|
|
|
|
match post_hint {
|
2020-10-26 16:57:19 +13:00
|
|
|
"hosted:video" => format!(
|
|
|
|
r#"<video class="post_image" src="{}" controls/>"#,
|
|
|
|
data["data"]["media"]["reddit_video"]["fallback_url"].as_str().unwrap()
|
|
|
|
),
|
2020-10-26 09:25:59 +13:00
|
|
|
"image" => format!(r#"<img class="post_image" src="{}"/>"#, data["data"]["url"].as_str().unwrap()),
|
|
|
|
"self" => String::from(""),
|
2020-10-26 16:57:19 +13:00
|
|
|
_ => media,
|
2020-10-26 09:25:59 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-26 15:55:00 +13:00
|
|
|
async fn markdown_to_html(md: &str) -> String {
|
|
|
|
let mut options = Options::empty();
|
|
|
|
options.insert(Options::ENABLE_TABLES);
|
|
|
|
options.insert(Options::ENABLE_FOOTNOTES);
|
|
|
|
options.insert(Options::ENABLE_STRIKETHROUGH);
|
|
|
|
options.insert(Options::ENABLE_TASKLISTS);
|
|
|
|
let parser = Parser::new_ext(md, options);
|
|
|
|
|
|
|
|
// Write to String buffer.
|
|
|
|
let mut html_output = String::new();
|
|
|
|
html::push_html(&mut html_output, parser);
|
|
|
|
html_output
|
|
|
|
}
|
|
|
|
|
2020-10-26 13:52:57 +13:00
|
|
|
// POSTS
|
2020-10-26 16:57:19 +13:00
|
|
|
async fn fetch_post(id: &String) -> Post {
|
2020-10-26 09:25:59 +13:00
|
|
|
let url: String = format!("https://reddit.com/{}.json", id);
|
|
|
|
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 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
|
|
|
let post_data: &serde_json::Value = &data[0]["data"]["children"][0];
|
|
|
|
|
|
|
|
let unix_time: i64 = post_data["data"]["created_utc"].as_f64().unwrap().round() as i64;
|
|
|
|
let score = post_data["data"]["score"].as_i64().unwrap();
|
|
|
|
|
|
|
|
Post {
|
2020-10-26 15:05:09 +13:00
|
|
|
title: val(post_data, "title").await,
|
2020-10-26 09:25:59 +13:00
|
|
|
community: val(post_data, "subreddit").await,
|
2020-10-26 16:57:19 +13:00
|
|
|
body: markdown_to_html(post_data["data"]["selftext"].as_str().unwrap()).await,
|
2020-10-26 09:25:59 +13:00
|
|
|
author: val(post_data, "author").await,
|
|
|
|
url: val(post_data, "permalink").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
|
|
|
media: media(post_data).await,
|
2020-10-26 16:57:19 +13:00
|
|
|
time: Utc.timestamp(unix_time, 0).format("%b %e %Y %H:%M UTC").to_string(),
|
2020-11-17 15:49:08 +13:00
|
|
|
flair: Flair(
|
|
|
|
val(post_data, "link_flair_text").await,
|
|
|
|
val(post_data, "link_flair_background_color").await,
|
2020-11-17 17:36:36 +13:00
|
|
|
if val(post_data, "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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// COMMENTS
|
2020-10-26 16:57:19 +13:00
|
|
|
async fn fetch_comments(id: String, sort: &String) -> Result<Vec<Comment>, Box<dyn std::error::Error>> {
|
2020-10-26 09:25:59 +13:00
|
|
|
let url: String = format!("https://reddit.com/{}.json?sort={}", id, sort);
|
|
|
|
let resp: String = reqwest::get(&url).await?.text().await?;
|
2020-10-26 16:57:19 +13:00
|
|
|
|
2020-10-26 09:25:59 +13:00
|
|
|
let data: serde_json::Value = serde_json::from_str(resp.as_str())?;
|
2020-10-26 16:57:19 +13:00
|
|
|
|
2020-10-26 09:25:59 +13:00
|
|
|
let comment_data = data[1]["data"]["children"].as_array().unwrap();
|
|
|
|
|
|
|
|
let mut comments: Vec<Comment> = Vec::new();
|
2020-10-26 16:57:19 +13:00
|
|
|
|
2020-10-26 09:25:59 +13:00
|
|
|
for comment in comment_data.iter() {
|
|
|
|
let unix_time: i64 = comment["data"]["created_utc"].as_f64().unwrap_or(0.0).round() as i64;
|
2020-10-26 13:52:57 +13:00
|
|
|
let score = comment["data"]["score"].as_i64().unwrap_or(0);
|
2020-10-26 16:57:19 +13:00
|
|
|
let body = markdown_to_html(comment["data"]["body"].as_str().unwrap_or("")).await;
|
2020-10-26 13:52:57 +13:00
|
|
|
|
2020-10-26 15:05:09 +13:00
|
|
|
// println!("{}", body);
|
|
|
|
|
2020-10-26 09:25:59 +13:00
|
|
|
comments.push(Comment {
|
2020-10-26 15:05:09 +13:00
|
|
|
body: body,
|
2020-10-26 09:25:59 +13:00
|
|
|
author: val(comment, "author").await,
|
2020-10-26 16:57:19 +13:00
|
|
|
score: if score > 1000 { format!("{}k", score / 1000) } else { score.to_string() },
|
|
|
|
time: Utc.timestamp(unix_time, 0).format("%b %e %Y %H:%M UTC").to_string(),
|
2020-10-26 09:25:59 +13:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(comments)
|
2020-10-26 16:57:19 +13:00
|
|
|
}
|