Unify Post Fetching into 1 Function
This commit is contained in:
parent
f75b48fdc9
commit
0f2a3713ef
@ -2,13 +2,9 @@
|
|||||||
use actix_web::{get, web, HttpResponse, Result};
|
use actix_web::{get, web, HttpResponse, Result};
|
||||||
use askama::Template;
|
use askama::Template;
|
||||||
|
|
||||||
#[path = "subreddit.rs"]
|
|
||||||
mod subreddit;
|
|
||||||
use subreddit::{posts, Post};
|
|
||||||
|
|
||||||
#[path = "utils.rs"]
|
#[path = "utils.rs"]
|
||||||
mod utils;
|
mod utils;
|
||||||
use utils::{ErrorTemplate, Params};
|
use utils::{fetch_posts, ErrorTemplate, Params, Post};
|
||||||
|
|
||||||
// STRUCTS
|
// STRUCTS
|
||||||
#[derive(Template)]
|
#[derive(Template)]
|
||||||
@ -33,7 +29,7 @@ async fn render(sub_name: String, sort: Option<String>, ends: (Option<String>, O
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
let items_result = posts(url).await;
|
let items_result = fetch_posts(url, String::new()).await;
|
||||||
|
|
||||||
if items_result.is_err() {
|
if items_result.is_err() {
|
||||||
let s = ErrorTemplate {
|
let s = ErrorTemplate {
|
||||||
|
@ -1,11 +1,10 @@
|
|||||||
// CRATES
|
// CRATES
|
||||||
use actix_web::{get, web, HttpResponse, Result};
|
use actix_web::{get, web, HttpResponse, Result};
|
||||||
use askama::Template;
|
use askama::Template;
|
||||||
use chrono::{TimeZone, Utc};
|
|
||||||
|
|
||||||
#[path = "utils.rs"]
|
#[path = "utils.rs"]
|
||||||
mod utils;
|
mod utils;
|
||||||
pub use utils::{request, val, ErrorTemplate, Flair, Params, Post, Subreddit};
|
pub use utils::{request, val, fetch_posts, ErrorTemplate, Flair, Params, Post, Subreddit};
|
||||||
|
|
||||||
// STRUCTS
|
// STRUCTS
|
||||||
#[derive(Template)]
|
#[derive(Template)]
|
||||||
@ -38,7 +37,7 @@ pub async fn render(sub_name: String, sort: Option<String>, ends: (Option<String
|
|||||||
};
|
};
|
||||||
|
|
||||||
let sub_result = subreddit(&sub_name).await;
|
let sub_result = subreddit(&sub_name).await;
|
||||||
let items_result = posts(url).await;
|
let items_result = fetch_posts(url, String::new()).await;
|
||||||
|
|
||||||
if sub_result.is_err() || items_result.is_err() {
|
if sub_result.is_err() || items_result.is_err() {
|
||||||
let s = ErrorTemplate {
|
let s = ErrorTemplate {
|
||||||
@ -98,53 +97,3 @@ async fn subreddit(sub: &String) -> Result<Subreddit, &'static str> {
|
|||||||
|
|
||||||
Ok(sub)
|
Ok(sub)
|
||||||
}
|
}
|
||||||
|
|
||||||
// POSTS
|
|
||||||
pub async fn posts(url: String) -> Result<(Vec<Post>, String), &'static str> {
|
|
||||||
// Send a request to the url, receive JSON in response
|
|
||||||
let req = request(url).await;
|
|
||||||
|
|
||||||
// If the Reddit API returns an error, exit this function
|
|
||||||
if req.is_err() {
|
|
||||||
return Err(req.err().unwrap());
|
|
||||||
}
|
|
||||||
|
|
||||||
// Otherwise, grab the JSON output from the request
|
|
||||||
let res = req.unwrap();
|
|
||||||
|
|
||||||
// Fetch the list of posts from the JSON response
|
|
||||||
let post_list = res["data"]["children"].as_array().unwrap();
|
|
||||||
|
|
||||||
let mut posts: Vec<Post> = Vec::new();
|
|
||||||
|
|
||||||
for post in post_list.iter() {
|
|
||||||
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();
|
|
||||||
posts.push(Post {
|
|
||||||
title: val(post, "title").await,
|
|
||||||
community: val(post, "subreddit").await,
|
|
||||||
body: String::new(),
|
|
||||||
author: val(post, "author").await,
|
|
||||||
score: if score > 1000 { format!("{}k", score / 1000) } else { score.to_string() },
|
|
||||||
media: img,
|
|
||||||
url: val(post, "permalink").await,
|
|
||||||
time: Utc.timestamp(unix_time, 0).format("%b %e '%y").to_string(),
|
|
||||||
flair: Flair(
|
|
||||||
val(post, "link_flair_text").await,
|
|
||||||
val(post, "link_flair_background_color").await,
|
|
||||||
if val(post, "link_flair_text_color").await == "dark" {
|
|
||||||
"black".to_string()
|
|
||||||
} else {
|
|
||||||
"white".to_string()
|
|
||||||
},
|
|
||||||
),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok((posts, res["data"]["after"].as_str().unwrap_or("").to_string()))
|
|
||||||
}
|
|
||||||
|
64
src/user.rs
64
src/user.rs
@ -1,11 +1,10 @@
|
|||||||
// CRATES
|
// CRATES
|
||||||
use actix_web::{get, web, HttpResponse, Result};
|
use actix_web::{get, web, HttpResponse, Result};
|
||||||
use askama::Template;
|
use askama::Template;
|
||||||
use chrono::{TimeZone, Utc};
|
|
||||||
|
|
||||||
#[path = "utils.rs"]
|
#[path = "utils.rs"]
|
||||||
mod utils;
|
mod utils;
|
||||||
use utils::{nested_val, request, val, ErrorTemplate, Flair, Params, Post, User};
|
use utils::{nested_val, request, fetch_posts, ErrorTemplate, Params, Post, User};
|
||||||
|
|
||||||
// STRUCTS
|
// STRUCTS
|
||||||
#[derive(Template)]
|
#[derive(Template)]
|
||||||
@ -17,8 +16,11 @@ struct UserTemplate {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async fn render(username: String, sort: String) -> Result<HttpResponse> {
|
async fn render(username: String, sort: String) -> Result<HttpResponse> {
|
||||||
|
// Build the Reddit JSON API url
|
||||||
|
let url: String = format!("https://www.reddit.com/user/{}/.json?sort={}", username, sort);
|
||||||
|
|
||||||
let user = user(&username).await;
|
let user = user(&username).await;
|
||||||
let posts = posts(username, &sort).await;
|
let posts = fetch_posts(url, "Comment".to_string()).await;
|
||||||
|
|
||||||
if user.is_err() || posts.is_err() {
|
if user.is_err() || posts.is_err() {
|
||||||
let s = ErrorTemplate {
|
let s = ErrorTemplate {
|
||||||
@ -30,7 +32,7 @@ async fn render(username: String, sort: String) -> Result<HttpResponse> {
|
|||||||
} else {
|
} else {
|
||||||
let s = UserTemplate {
|
let s = UserTemplate {
|
||||||
user: user.unwrap(),
|
user: user.unwrap(),
|
||||||
posts: posts.unwrap(),
|
posts: posts.unwrap().0,
|
||||||
sort: sort,
|
sort: sort,
|
||||||
}
|
}
|
||||||
.render()
|
.render()
|
||||||
@ -73,57 +75,3 @@ async fn user(name: &String) -> Result<User, &'static str> {
|
|||||||
description: nested_val(&res, "subreddit", "public_description").await,
|
description: nested_val(&res, "subreddit", "public_description").await,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// POSTS
|
|
||||||
async fn posts(sub: String, sort: &String) -> Result<Vec<Post>, &'static str> {
|
|
||||||
// Build the Reddit JSON API url
|
|
||||||
let url: String = format!("https://www.reddit.com/u/{}/.json?sort={}", sub, sort);
|
|
||||||
|
|
||||||
// Send a request to the url, receive JSON in response
|
|
||||||
let req = request(url).await;
|
|
||||||
|
|
||||||
// If the Reddit API returns an error, exit this function
|
|
||||||
if req.is_err() {
|
|
||||||
return Err(req.err().unwrap());
|
|
||||||
}
|
|
||||||
|
|
||||||
// Otherwise, grab the JSON output from the request
|
|
||||||
let res = req.unwrap();
|
|
||||||
|
|
||||||
let post_list = res["data"]["children"].as_array().unwrap();
|
|
||||||
|
|
||||||
let mut posts: Vec<Post> = Vec::new();
|
|
||||||
|
|
||||||
for post in post_list.iter() {
|
|
||||||
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();
|
|
||||||
let title = val(post, "title").await;
|
|
||||||
|
|
||||||
posts.push(Post {
|
|
||||||
title: if title.is_empty() { "Comment".to_string() } else { title },
|
|
||||||
community: val(post, "subreddit").await,
|
|
||||||
body: String::new(),
|
|
||||||
author: val(post, "author").await,
|
|
||||||
score: if score > 1000 { format!("{}k", score / 1000) } else { score.to_string() },
|
|
||||||
media: img,
|
|
||||||
url: val(post, "permalink").await,
|
|
||||||
time: Utc.timestamp(unix_time, 0).format("%b %e '%y").to_string(),
|
|
||||||
flair: Flair(
|
|
||||||
val(post, "link_flair_text").await,
|
|
||||||
val(post, "link_flair_background_color").await,
|
|
||||||
if val(post, "link_flair_text_color").await == "dark" {
|
|
||||||
"black".to_string()
|
|
||||||
} else {
|
|
||||||
"white".to_string()
|
|
||||||
},
|
|
||||||
),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(posts)
|
|
||||||
}
|
|
||||||
|
59
src/utils.rs
59
src/utils.rs
@ -1,3 +1,8 @@
|
|||||||
|
//
|
||||||
|
// CRATES
|
||||||
|
//
|
||||||
|
use chrono::{TimeZone, Utc};
|
||||||
|
|
||||||
//
|
//
|
||||||
// STRUCTS
|
// STRUCTS
|
||||||
//
|
//
|
||||||
@ -78,6 +83,58 @@ pub async fn nested_val(j: &serde_json::Value, n: &str, k: &str) -> String {
|
|||||||
String::from(j["data"][n][k].as_str().unwrap())
|
String::from(j["data"][n][k].as_str().unwrap())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
|
pub async fn fetch_posts(url: String, fallback_title: String) -> Result<(Vec<Post>, String), &'static str> {
|
||||||
|
// Send a request to the url, receive JSON in response
|
||||||
|
let req = request(url).await;
|
||||||
|
|
||||||
|
// If the Reddit API returns an error, exit this function
|
||||||
|
if req.is_err() {
|
||||||
|
return Err(req.err().unwrap());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Otherwise, grab the JSON output from the request
|
||||||
|
let res = req.unwrap();
|
||||||
|
|
||||||
|
// Fetch the list of posts from the JSON response
|
||||||
|
let post_list = res["data"]["children"].as_array().unwrap();
|
||||||
|
|
||||||
|
let mut posts: Vec<Post> = Vec::new();
|
||||||
|
|
||||||
|
for post in post_list.iter() {
|
||||||
|
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();
|
||||||
|
let title = val(post, "title").await;
|
||||||
|
|
||||||
|
posts.push(Post {
|
||||||
|
title: if title.is_empty() { fallback_title.to_owned() } else { title },
|
||||||
|
community: val(post, "subreddit").await,
|
||||||
|
body: String::new(),
|
||||||
|
author: val(post, "author").await,
|
||||||
|
score: if score > 1000 { format!("{}k", score / 1000) } else { score.to_string() },
|
||||||
|
media: img,
|
||||||
|
url: val(post, "permalink").await,
|
||||||
|
time: Utc.timestamp(unix_time, 0).format("%b %e '%y").to_string(),
|
||||||
|
flair: Flair(
|
||||||
|
val(post, "link_flair_text").await,
|
||||||
|
val(post, "link_flair_background_color").await,
|
||||||
|
if val(post, "link_flair_text_color").await == "dark" {
|
||||||
|
"black".to_string()
|
||||||
|
} else {
|
||||||
|
"white".to_string()
|
||||||
|
},
|
||||||
|
),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok((posts, res["data"]["after"].as_str().unwrap_or("").to_string()))
|
||||||
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
// NETWORKING
|
// NETWORKING
|
||||||
//
|
//
|
||||||
@ -104,6 +161,8 @@ pub async fn request(url: String) -> Result<serde_json::Value, &'static str> {
|
|||||||
let success = res.status().is_success();
|
let success = res.status().is_success();
|
||||||
let body = res.body_string().await.unwrap();
|
let body = res.body_string().await.unwrap();
|
||||||
|
|
||||||
|
dbg!(url.clone());
|
||||||
|
|
||||||
// --- reqwest ---
|
// --- reqwest ---
|
||||||
// let res = reqwest::get(&url).await.unwrap();
|
// let res = reqwest::get(&url).await.unwrap();
|
||||||
// // Read the status from the response
|
// // Read the status from the response
|
||||||
|
Loading…
Reference in New Issue
Block a user