Rustfmt Code Format
This commit is contained in:
parent
1e7bbb385c
commit
c5b64e2168
@ -1 +1,4 @@
|
|||||||
|
edition = "2018"
|
||||||
tab_spaces = 2
|
tab_spaces = 2
|
||||||
|
hard_tabs = true
|
||||||
|
max_width = 200
|
47
src/main.rs
47
src/main.rs
@ -1,12 +1,12 @@
|
|||||||
// Import Crates
|
// Import Crates
|
||||||
use actix_files::NamedFile;
|
use actix_files::NamedFile;
|
||||||
use actix_web::{get, App, HttpServer, HttpResponse, Result};
|
use actix_web::{get, App, HttpResponse, HttpServer, Result};
|
||||||
|
|
||||||
// Reference local files
|
// Reference local files
|
||||||
mod user;
|
|
||||||
mod popular;
|
mod popular;
|
||||||
mod post;
|
mod post;
|
||||||
mod subreddit;
|
mod subreddit;
|
||||||
|
mod user;
|
||||||
|
|
||||||
// Create Services
|
// Create Services
|
||||||
#[get("/style.css")]
|
#[get("/style.css")]
|
||||||
@ -20,29 +20,28 @@ async fn favicon() -> HttpResponse {
|
|||||||
HttpResponse::Ok().body("")
|
HttpResponse::Ok().body("")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#[actix_web::main]
|
#[actix_web::main]
|
||||||
async fn main() -> std::io::Result<()> {
|
async fn main() -> std::io::Result<()> {
|
||||||
// start http server
|
// start http server
|
||||||
HttpServer::new(|| {
|
HttpServer::new(|| {
|
||||||
App::new()
|
App::new()
|
||||||
// GENERAL SERVICES
|
// GENERAL SERVICES
|
||||||
.service(style)
|
.service(style)
|
||||||
.service(favicon)
|
.service(favicon)
|
||||||
// POST SERVICES
|
// POST SERVICES
|
||||||
.service(post::short)
|
.service(post::short)
|
||||||
.service(post::page)
|
.service(post::page)
|
||||||
.service(post::sorted)
|
.service(post::sorted)
|
||||||
// SUBREDDIT SERVICES
|
// SUBREDDIT SERVICES
|
||||||
.service(subreddit::page)
|
.service(subreddit::page)
|
||||||
.service(subreddit::sorted)
|
.service(subreddit::sorted)
|
||||||
// POPULAR SERVICES
|
// POPULAR SERVICES
|
||||||
.service(popular::page)
|
.service(popular::page)
|
||||||
// .service(popular::sorted)
|
// .service(popular::sorted)
|
||||||
// USER SERVICES
|
// USER SERVICES
|
||||||
.service(user::page)
|
.service(user::page)
|
||||||
})
|
})
|
||||||
.bind("127.0.0.1:8080")?
|
.bind("127.0.0.1:8080")?
|
||||||
.run()
|
.run()
|
||||||
.await
|
.await
|
||||||
}
|
}
|
@ -1,52 +1,37 @@
|
|||||||
// CRATES
|
// CRATES
|
||||||
use actix_web::{get, web, HttpResponse, Result};
|
use actix_web::{get, web, HttpResponse, Result};
|
||||||
use serde::Deserialize;
|
|
||||||
use askama::Template;
|
use askama::Template;
|
||||||
|
use serde::Deserialize;
|
||||||
|
|
||||||
#[path = "subreddit.rs"] mod subreddit;
|
#[path = "subreddit.rs"]
|
||||||
|
mod subreddit;
|
||||||
|
|
||||||
// STRUCTS
|
// STRUCTS
|
||||||
#[derive(Template)]
|
#[derive(Template)]
|
||||||
#[template(path = "popular.html", escape = "none")]
|
#[template(path = "popular.html", escape = "none")]
|
||||||
struct PopularTemplate {
|
struct PopularTemplate {
|
||||||
posts: Vec<subreddit::Post>,
|
posts: Vec<subreddit::Post>,
|
||||||
sort: String
|
sort: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
pub struct Params {
|
pub struct Params {
|
||||||
sort: Option<String>
|
sort: Option<String>,
|
||||||
}
|
|
||||||
|
|
||||||
#[get("/")]
|
|
||||||
pub async fn page(params: web::Query<Params>) -> Result<HttpResponse> {
|
|
||||||
match ¶ms.sort {
|
|
||||||
Some(sort) => render("popular".to_string(), sort.to_string()).await,
|
|
||||||
None => render("popular".to_string(), "hot".to_string()).await,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// RENDER
|
||||||
async fn render(sub_name: String, sort: String) -> Result<HttpResponse> {
|
async fn render(sub_name: String, sort: String) -> Result<HttpResponse> {
|
||||||
let posts: Vec<subreddit::Post> = subreddit::posts(sub_name, &sort).await;
|
let posts: Vec<subreddit::Post> = subreddit::posts(sub_name, &sort).await;
|
||||||
|
|
||||||
let s = PopularTemplate {
|
let s = PopularTemplate { posts: posts, sort: sort }.render().unwrap();
|
||||||
posts: posts,
|
|
||||||
sort: sort
|
|
||||||
}
|
|
||||||
.render()
|
|
||||||
.unwrap();
|
|
||||||
Ok(HttpResponse::Ok().content_type("text/html").body(s))
|
Ok(HttpResponse::Ok().content_type("text/html").body(s))
|
||||||
}
|
}
|
||||||
|
|
||||||
// #[get("/?<sort>")]
|
// SERVICES
|
||||||
// pub fn sorted(sort: String) -> Template {
|
#[get("/")]
|
||||||
// println!("{}", sort);
|
pub async fn page(params: web::Query<Params>) -> Result<HttpResponse> {
|
||||||
// let posts: Vec<subreddit::Post> = subreddit::posts(&"popular".to_string(), &sort).unwrap();
|
match ¶ms.sort {
|
||||||
|
Some(sort) => render("popular".to_string(), sort.to_string()).await,
|
||||||
// let mut context = std::collections::HashMap::new();
|
None => render("popular".to_string(), "hot".to_string()).await,
|
||||||
// context.insert("about", String::new());
|
}
|
||||||
// context.insert("sort", sort);
|
}
|
||||||
// context.insert("posts", subreddit::posts_html(posts));
|
|
||||||
|
|
||||||
// Template::render("popular", context)
|
|
||||||
// }
|
|
||||||
|
44
src/post.rs
44
src/post.rs
@ -1,8 +1,8 @@
|
|||||||
// CRATES
|
// CRATES
|
||||||
use actix_web::{get, web, HttpResponse, Result};
|
use actix_web::{get, web, HttpResponse, Result};
|
||||||
use askama::Template;
|
use askama::Template;
|
||||||
use pulldown_cmark::{Parser, Options, html};
|
|
||||||
use chrono::{TimeZone, Utc};
|
use chrono::{TimeZone, Utc};
|
||||||
|
use pulldown_cmark::{html, Options, Parser};
|
||||||
|
|
||||||
// STRUCTS
|
// STRUCTS
|
||||||
#[derive(Template)]
|
#[derive(Template)]
|
||||||
@ -10,7 +10,7 @@ use chrono::{TimeZone, Utc};
|
|||||||
struct PostTemplate {
|
struct PostTemplate {
|
||||||
comments: Vec<Comment>,
|
comments: Vec<Comment>,
|
||||||
post: Post,
|
post: Post,
|
||||||
sort: String
|
sort: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Post {
|
pub struct Post {
|
||||||
@ -21,14 +21,14 @@ pub struct Post {
|
|||||||
pub url: String,
|
pub url: String,
|
||||||
pub score: String,
|
pub score: String,
|
||||||
pub media: String,
|
pub media: String,
|
||||||
pub time: String
|
pub time: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Comment {
|
pub struct Comment {
|
||||||
pub body: String,
|
pub body: String,
|
||||||
pub author: String,
|
pub author: String,
|
||||||
pub score: String,
|
pub score: String,
|
||||||
pub time: String
|
pub time: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn render(id: String, sort: String) -> Result<HttpResponse> {
|
async fn render(id: String, sort: String) -> Result<HttpResponse> {
|
||||||
@ -39,7 +39,7 @@ async fn render(id: String, sort: String) -> Result<HttpResponse> {
|
|||||||
let s = PostTemplate {
|
let s = PostTemplate {
|
||||||
comments: comments,
|
comments: comments,
|
||||||
post: post,
|
post: post,
|
||||||
sort: sort
|
sort: sort,
|
||||||
}
|
}
|
||||||
.render()
|
.render()
|
||||||
.unwrap();
|
.unwrap();
|
||||||
@ -66,20 +66,28 @@ async fn sorted(web::Path((_sub, id, _title, sort)): web::Path<(String, String,
|
|||||||
}
|
}
|
||||||
|
|
||||||
// UTILITIES
|
// UTILITIES
|
||||||
async fn val (j: &serde_json::Value, k: &str) -> String { String::from(j["data"][k].as_str().unwrap_or("")) }
|
async fn val(j: &serde_json::Value, k: &str) -> String {
|
||||||
|
String::from(j["data"][k].as_str().unwrap_or(""))
|
||||||
|
}
|
||||||
|
|
||||||
async fn media(data: &serde_json::Value) -> String {
|
async fn media(data: &serde_json::Value) -> String {
|
||||||
let post_hint: &str = data["data"]["post_hint"].as_str().unwrap_or("");
|
let post_hint: &str = data["data"]["post_hint"].as_str().unwrap_or("");
|
||||||
let has_media: bool = data["data"]["media"].is_object();
|
let has_media: bool = data["data"]["media"].is_object();
|
||||||
|
|
||||||
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()) }
|
let media: String = if !has_media {
|
||||||
else { format!(r#"<img class="post_image" src="{}.png"/>"#, data["data"]["url"].as_str().unwrap()) };
|
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())
|
||||||
|
};
|
||||||
|
|
||||||
match post_hint {
|
match post_hint {
|
||||||
"hosted:video" => format!(r#"<video class="post_image" src="{}" controls/>"#, data["data"]["media"]["reddit_video"]["fallback_url"].as_str().unwrap()),
|
"hosted:video" => format!(
|
||||||
|
r#"<video class="post_image" src="{}" controls/>"#,
|
||||||
|
data["data"]["media"]["reddit_video"]["fallback_url"].as_str().unwrap()
|
||||||
|
),
|
||||||
"image" => format!(r#"<img class="post_image" src="{}"/>"#, data["data"]["url"].as_str().unwrap()),
|
"image" => format!(r#"<img class="post_image" src="{}"/>"#, data["data"]["url"].as_str().unwrap()),
|
||||||
"self" => String::from(""),
|
"self" => String::from(""),
|
||||||
_ => media
|
_ => media,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -98,7 +106,7 @@ async fn markdown_to_html(md: &str) -> String {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// POSTS
|
// POSTS
|
||||||
async fn fetch_post (id: &String) -> Post {
|
async fn fetch_post(id: &String) -> Post {
|
||||||
let url: String = format!("https://reddit.com/{}.json", id);
|
let url: String = format!("https://reddit.com/{}.json", id);
|
||||||
let resp: String = reqwest::get(&url).await.unwrap().text().await.unwrap();
|
let resp: String = reqwest::get(&url).await.unwrap().text().await.unwrap();
|
||||||
|
|
||||||
@ -112,17 +120,17 @@ async fn fetch_post (id: &String) -> Post {
|
|||||||
Post {
|
Post {
|
||||||
title: val(post_data, "title").await,
|
title: val(post_data, "title").await,
|
||||||
community: val(post_data, "subreddit").await,
|
community: val(post_data, "subreddit").await,
|
||||||
body: markdown_to_html(post_data["data"]["selftext"].as_str().unwrap()).await, //markdown_to_html(post_data["data"]["selftext"].as_str().unwrap(), &ComrakOptions::default()),
|
body: markdown_to_html(post_data["data"]["selftext"].as_str().unwrap()).await,
|
||||||
author: val(post_data, "author").await,
|
author: val(post_data, "author").await,
|
||||||
url: val(post_data, "permalink").await,
|
url: val(post_data, "permalink").await,
|
||||||
score: if score>1000 {format!("{}k",score/1000)} else {score.to_string()},
|
score: if score > 1000 { format!("{}k", score / 1000) } else { score.to_string() },
|
||||||
media: media(post_data).await,
|
media: media(post_data).await,
|
||||||
time: Utc.timestamp(unix_time, 0).format("%b %e %Y %H:%M UTC").to_string()
|
time: Utc.timestamp(unix_time, 0).format("%b %e %Y %H:%M UTC").to_string(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// COMMENTS
|
// COMMENTS
|
||||||
async fn fetch_comments (id: String, sort: &String) -> Result<Vec<Comment>, Box<dyn std::error::Error>> {
|
async fn fetch_comments(id: String, sort: &String) -> Result<Vec<Comment>, Box<dyn std::error::Error>> {
|
||||||
let url: String = format!("https://reddit.com/{}.json?sort={}", id, sort);
|
let url: String = format!("https://reddit.com/{}.json?sort={}", id, sort);
|
||||||
let resp: String = reqwest::get(&url).await?.text().await?;
|
let resp: String = reqwest::get(&url).await?.text().await?;
|
||||||
|
|
||||||
@ -135,15 +143,15 @@ async fn fetch_comments (id: String, sort: &String) -> Result<Vec<Comment>, Box<
|
|||||||
for comment in comment_data.iter() {
|
for comment in comment_data.iter() {
|
||||||
let unix_time: i64 = comment["data"]["created_utc"].as_f64().unwrap_or(0.0).round() as i64;
|
let unix_time: i64 = comment["data"]["created_utc"].as_f64().unwrap_or(0.0).round() as i64;
|
||||||
let score = comment["data"]["score"].as_i64().unwrap_or(0);
|
let score = comment["data"]["score"].as_i64().unwrap_or(0);
|
||||||
let body = markdown_to_html(comment["data"]["body"].as_str().unwrap_or("")).await;// markdown_to_html(comment["data"]["body"].as_str().unwrap_or(""), &ComrakOptions::default());
|
let body = markdown_to_html(comment["data"]["body"].as_str().unwrap_or("")).await;
|
||||||
|
|
||||||
// println!("{}", body);
|
// println!("{}", body);
|
||||||
|
|
||||||
comments.push(Comment {
|
comments.push(Comment {
|
||||||
body: body,
|
body: body,
|
||||||
author: val(comment, "author").await,
|
author: val(comment, "author").await,
|
||||||
score: if score>1000 {format!("{}k",score/1000)} else {score.to_string()},
|
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()
|
time: Utc.timestamp(unix_time, 0).format("%b %e %Y %H:%M UTC").to_string(),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -9,7 +9,7 @@ use chrono::{TimeZone, Utc};
|
|||||||
struct SubredditTemplate {
|
struct SubredditTemplate {
|
||||||
sub: Subreddit,
|
sub: Subreddit,
|
||||||
posts: Vec<Post>,
|
posts: Vec<Post>,
|
||||||
sort: String
|
sort: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Post {
|
pub struct Post {
|
||||||
@ -19,29 +19,27 @@ pub struct Post {
|
|||||||
pub score: String,
|
pub score: String,
|
||||||
pub image: String,
|
pub image: String,
|
||||||
pub url: String,
|
pub url: String,
|
||||||
pub time: String
|
pub time: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Subreddit {
|
pub struct Subreddit {
|
||||||
pub name: String,
|
pub name: String,
|
||||||
pub title: String,
|
pub title: String,
|
||||||
pub description: String,
|
pub description: String,
|
||||||
pub icon: String
|
pub icon: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn render(sub_name: String, sort: String) -> Result<HttpResponse> {
|
async fn render(sub_name: String, sort: String) -> Result<HttpResponse> {
|
||||||
let mut sub: Subreddit = subreddit(&sub_name).await;
|
let mut sub: Subreddit = subreddit(&sub_name).await;
|
||||||
let posts: Vec<Post> = posts(sub_name, &sort).await;
|
let posts: Vec<Post> = posts(sub_name, &sort).await;
|
||||||
|
|
||||||
sub.icon = if sub.icon!="" {format!(r#"<img class="subreddit_icon" src="{}">"#, sub.icon)} else {String::new()};
|
sub.icon = if sub.icon != "" {
|
||||||
|
format!(r#"<img class="subreddit_icon" src="{}">"#, sub.icon)
|
||||||
|
} else {
|
||||||
|
String::new()
|
||||||
|
};
|
||||||
|
|
||||||
let s = SubredditTemplate {
|
let s = SubredditTemplate { sub: sub, posts: posts, sort: sort }.render().unwrap();
|
||||||
sub: sub,
|
|
||||||
posts: posts,
|
|
||||||
sort: sort
|
|
||||||
}
|
|
||||||
.render()
|
|
||||||
.unwrap();
|
|
||||||
Ok(HttpResponse::Ok().content_type("text/html").body(s))
|
Ok(HttpResponse::Ok().content_type("text/html").body(s))
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -59,7 +57,9 @@ async fn sorted(web::Path((sub, sort)): web::Path<(String, String)>) -> Result<H
|
|||||||
}
|
}
|
||||||
|
|
||||||
// UTILITIES
|
// UTILITIES
|
||||||
async fn val (j: &serde_json::Value, k: &str) -> String { String::from(j["data"][k].as_str().unwrap_or("")) }
|
async fn val(j: &serde_json::Value, k: &str) -> String {
|
||||||
|
String::from(j["data"][k].as_str().unwrap_or(""))
|
||||||
|
}
|
||||||
|
|
||||||
// SUBREDDIT
|
// SUBREDDIT
|
||||||
async fn subreddit(sub: &String) -> Subreddit {
|
async fn subreddit(sub: &String) -> Subreddit {
|
||||||
@ -72,7 +72,7 @@ async fn subreddit(sub: &String) -> Subreddit {
|
|||||||
let icon_split: std::str::Split<&str> = icon.split("?");
|
let icon_split: std::str::Split<&str> = icon.split("?");
|
||||||
let icon_parts: Vec<&str> = icon_split.collect();
|
let icon_parts: Vec<&str> = icon_split.collect();
|
||||||
|
|
||||||
Subreddit {
|
Subreddit {
|
||||||
name: val(&data, "display_name").await,
|
name: val(&data, "display_name").await,
|
||||||
title: val(&data, "title").await,
|
title: val(&data, "title").await,
|
||||||
description: val(&data, "public_description").await,
|
description: val(&data, "public_description").await,
|
||||||
@ -91,19 +91,22 @@ pub async fn posts(sub: String, sort: &String) -> Vec<Post> {
|
|||||||
let mut posts: Vec<Post> = Vec::new();
|
let mut posts: Vec<Post> = Vec::new();
|
||||||
|
|
||||||
for post in post_list.iter() {
|
for post in post_list.iter() {
|
||||||
let img = if val(post, "thumbnail").await.starts_with("https:/") { val(post, "thumbnail").await } else { String::new() };
|
let img = if val(post, "thumbnail").await.starts_with("https:/") {
|
||||||
let unix_time: i64 = post["data"]["created_utc"].as_f64().unwrap().round() as i64;
|
val(post, "thumbnail").await
|
||||||
let score = post["data"]["score"].as_i64().unwrap();
|
} 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();
|
||||||
posts.push(Post {
|
posts.push(Post {
|
||||||
title: val(post, "title").await,
|
title: val(post, "title").await,
|
||||||
community: val(post, "subreddit").await,
|
community: val(post, "subreddit").await,
|
||||||
author: val(post, "author").await,
|
author: val(post, "author").await,
|
||||||
score: if score>1000 {format!("{}k",score/1000)} else {score.to_string()},
|
score: if score > 1000 { format!("{}k", score / 1000) } else { score.to_string() },
|
||||||
image: img,
|
image: img,
|
||||||
url: val(post, "permalink").await,
|
url: val(post, "permalink").await,
|
||||||
time: Utc.timestamp(unix_time, 0).format("%b %e '%y").to_string()
|
time: Utc.timestamp(unix_time, 0).format("%b %e '%y").to_string(),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
posts
|
||||||
posts
|
|
||||||
}
|
}
|
35
src/user.rs
35
src/user.rs
@ -9,7 +9,7 @@ use chrono::{TimeZone, Utc};
|
|||||||
struct UserTemplate {
|
struct UserTemplate {
|
||||||
user: User,
|
user: User,
|
||||||
posts: Vec<Post>,
|
posts: Vec<Post>,
|
||||||
sort: String
|
sort: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Post {
|
pub struct Post {
|
||||||
@ -19,7 +19,7 @@ pub struct Post {
|
|||||||
pub score: String,
|
pub score: String,
|
||||||
pub image: String,
|
pub image: String,
|
||||||
pub url: String,
|
pub url: String,
|
||||||
pub time: String
|
pub time: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct User {
|
pub struct User {
|
||||||
@ -27,21 +27,14 @@ pub struct User {
|
|||||||
pub icon: String,
|
pub icon: String,
|
||||||
pub karma: i64,
|
pub karma: i64,
|
||||||
pub banner: String,
|
pub banner: String,
|
||||||
pub description: String
|
pub description: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
async fn render(username: String, sort: String) -> Result<HttpResponse> {
|
async fn render(username: String, sort: String) -> Result<HttpResponse> {
|
||||||
let user: User = user(&username).await;
|
let user: User = user(&username).await;
|
||||||
let posts: Vec<Post> = posts(username, &sort).await;
|
let posts: Vec<Post> = posts(username, &sort).await;
|
||||||
|
|
||||||
let s = UserTemplate {
|
let s = UserTemplate { user: user, posts: posts, sort: sort }.render().unwrap();
|
||||||
user: user,
|
|
||||||
posts: posts,
|
|
||||||
sort: sort
|
|
||||||
}
|
|
||||||
.render()
|
|
||||||
.unwrap();
|
|
||||||
Ok(HttpResponse::Ok().content_type("text/html").body(s))
|
Ok(HttpResponse::Ok().content_type("text/html").body(s))
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -57,8 +50,12 @@ async fn sorted(web::Path((username, sort)): web::Path<(String, String)>) -> Res
|
|||||||
}
|
}
|
||||||
|
|
||||||
// UTILITIES
|
// UTILITIES
|
||||||
async fn user_val (j: &serde_json::Value, k: &str) -> String { String::from(j["data"]["subreddit"][k].as_str().unwrap()) }
|
async fn user_val(j: &serde_json::Value, k: &str) -> String {
|
||||||
async fn post_val (j: &serde_json::Value, k: &str) -> String { String::from(j["data"][k].as_str().unwrap_or("Comment")) }
|
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"))
|
||||||
|
}
|
||||||
|
|
||||||
// USER
|
// USER
|
||||||
async fn user(name: &String) -> User {
|
async fn user(name: &String) -> User {
|
||||||
@ -72,7 +69,7 @@ async fn user(name: &String) -> User {
|
|||||||
icon: user_val(&data, "icon_img").await,
|
icon: user_val(&data, "icon_img").await,
|
||||||
karma: data["data"]["total_karma"].as_i64().unwrap(),
|
karma: data["data"]["total_karma"].as_i64().unwrap(),
|
||||||
banner: user_val(&data, "banner_img").await,
|
banner: user_val(&data, "banner_img").await,
|
||||||
description: user_val(&data, "public_description").await
|
description: user_val(&data, "public_description").await,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -87,17 +84,21 @@ async fn posts(sub: String, sort: &String) -> Vec<Post> {
|
|||||||
let mut posts: Vec<Post> = Vec::new();
|
let mut posts: Vec<Post> = Vec::new();
|
||||||
|
|
||||||
for post in post_list.iter() {
|
for post in post_list.iter() {
|
||||||
let img = if post_val(post, "thumbnail").await.starts_with("https:/") { post_val(post, "thumbnail").await } else { String::new() };
|
let img = if post_val(post, "thumbnail").await.starts_with("https:/") {
|
||||||
|
post_val(post, "thumbnail").await
|
||||||
|
} else {
|
||||||
|
String::new()
|
||||||
|
};
|
||||||
let unix_time: i64 = post["data"]["created_utc"].as_f64().unwrap().round() as i64;
|
let unix_time: i64 = post["data"]["created_utc"].as_f64().unwrap().round() as i64;
|
||||||
let score = post["data"]["score"].as_i64().unwrap();
|
let score = post["data"]["score"].as_i64().unwrap();
|
||||||
posts.push(Post {
|
posts.push(Post {
|
||||||
title: post_val(post, "title").await,
|
title: post_val(post, "title").await,
|
||||||
community: post_val(post, "subreddit").await,
|
community: post_val(post, "subreddit").await,
|
||||||
author: post_val(post, "author").await,
|
author: post_val(post, "author").await,
|
||||||
score: if score>1000 {format!("{}k",score/1000)} else {score.to_string()},
|
score: if score > 1000 { format!("{}k", score / 1000) } else { score.to_string() },
|
||||||
image: img,
|
image: img,
|
||||||
url: post_val(post, "permalink").await,
|
url: post_val(post, "permalink").await,
|
||||||
time: Utc.timestamp(unix_time, 0).format("%b %e '%y").to_string()
|
time: Utc.timestamp(unix_time, 0).format("%b %e '%y").to_string(),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user