libreddit/src/utils.rs

488 lines
14 KiB
Rust
Raw Normal View History

2020-11-21 07:05:27 +01:00
//
// CRATES
//
2021-01-01 06:03:44 +01:00
use askama::Template;
2021-01-03 05:50:23 +01:00
use base64::encode;
2021-02-08 02:56:06 +01:00
use cached::proc_macro::cached;
2021-01-02 19:58:21 +01:00
use regex::Regex;
2021-01-12 22:43:03 +01:00
use serde_json::{from_str, Value};
2021-01-09 02:35:04 +01:00
use std::collections::HashMap;
use tide::{http::url::Url, http::Cookie, Request, Response};
2021-01-13 21:52:00 +01:00
use time::{Duration, OffsetDateTime};
2020-11-21 07:05:27 +01:00
2020-11-20 05:42:18 +01:00
//
// STRUCTS
//
2021-01-17 00:02:24 +01:00
2021-01-12 22:43:03 +01:00
// Post flair with content, background color and foreground color
2021-01-13 21:52:00 +01:00
pub struct Flair {
pub flair_parts: Vec<FlairPart>,
2021-01-12 22:43:03 +01:00
pub background_color: String,
pub foreground_color: String,
}
2021-01-15 00:13:52 +01:00
// Part of flair, either emoji or text
2021-01-13 21:52:00 +01:00
pub struct FlairPart {
pub flair_part_type: String,
2021-01-12 22:43:03 +01:00
pub value: String,
}
2021-01-17 00:02:24 +01:00
pub struct Author {
pub name: String,
pub flair: Flair,
pub distinguished: String,
}
2020-12-30 04:01:02 +01:00
// Post flags with nsfw and stickied
pub struct Flags {
pub nsfw: bool,
2021-01-01 00:54:13 +01:00
pub stickied: bool,
2020-12-30 04:01:02 +01:00
}
2020-11-17 20:37:40 +01:00
2021-01-17 21:58:12 +01:00
pub struct Media {
pub url: String,
pub width: i64,
pub height: i64,
pub poster: String,
2021-01-17 21:58:12 +01:00
}
2021-02-06 21:05:11 +01:00
pub struct GalleryMedia {
pub url: String,
pub width: i64,
pub height: i64,
pub caption: String,
2021-02-08 02:33:54 +01:00
pub outbound_url: String,
2021-02-06 21:05:11 +01:00
}
2020-11-17 20:37:40 +01:00
// Post containing content, metadata and media
pub struct Post {
pub id: String,
2020-11-17 20:37:40 +01:00
pub title: String,
pub community: String,
pub body: String,
2021-01-17 00:02:24 +01:00
pub author: Author,
pub permalink: String,
2020-11-17 20:37:40 +01:00
pub score: String,
pub upvote_ratio: i64,
pub post_type: String,
2020-12-23 03:29:43 +01:00
pub flair: Flair,
2020-12-30 04:01:02 +01:00
pub flags: Flags,
2021-01-17 21:58:12 +01:00
pub thumbnail: Media,
pub media: Media,
2021-01-11 23:08:12 +01:00
pub domain: String,
pub rel_time: String,
pub created: String,
pub comments: String,
2021-02-06 21:05:11 +01:00
pub gallery: Vec<GalleryMedia>,
2020-11-17 20:37:40 +01:00
}
#[derive(Template)]
#[template(path = "comment.html", escape = "none")]
2020-11-17 20:37:40 +01:00
// Comment with content, post, score and data/time that it was posted
pub struct Comment {
pub id: String,
pub kind: String,
pub parent_id: String,
pub parent_kind: String,
pub post_link: String,
pub post_author: String,
2020-11-17 20:37:40 +01:00
pub body: String,
2021-01-17 00:02:24 +01:00
pub author: Author,
2020-11-17 20:37:40 +01:00
pub score: String,
pub rel_time: String,
pub created: String,
2020-12-20 04:54:46 +01:00
pub replies: Vec<Comment>,
pub highlighted: bool,
2020-11-17 20:37:40 +01:00
}
2021-01-09 05:55:40 +01:00
#[derive(Default)]
2020-11-17 20:37:40 +01:00
// User struct containing metadata about user
pub struct User {
pub name: String,
pub title: String,
2020-11-17 20:37:40 +01:00
pub icon: String,
pub karma: i64,
2020-12-24 07:16:04 +01:00
pub created: String,
2020-11-17 20:37:40 +01:00
pub banner: String,
2020-11-30 03:50:29 +01:00
pub description: String,
2020-11-17 20:37:40 +01:00
}
2020-12-29 03:42:46 +01:00
#[derive(Default)]
2020-11-17 20:37:40 +01:00
// Subreddit struct containing metadata about community
pub struct Subreddit {
pub name: String,
pub title: String,
pub description: String,
2020-12-29 03:42:46 +01:00
pub info: String,
2020-11-17 20:37:40 +01:00
pub icon: String,
2020-11-23 01:43:23 +01:00
pub members: String,
2020-11-30 03:50:29 +01:00
pub active: String,
2021-01-02 07:21:43 +01:00
pub wiki: bool,
2020-11-17 20:37:40 +01:00
}
2020-11-19 22:49:32 +01:00
// Parser for query params, used in sorting (eg. /r/rust/?sort=hot)
#[derive(serde::Deserialize)]
pub struct Params {
2020-12-30 02:11:47 +01:00
pub t: Option<String>,
2021-01-01 00:54:13 +01:00
pub q: Option<String>,
2020-11-19 22:49:32 +01:00
pub sort: Option<String>,
pub after: Option<String>,
2020-11-30 03:50:29 +01:00
pub before: Option<String>,
2020-11-19 22:49:32 +01:00
}
2020-11-20 05:42:18 +01:00
// Error template
2021-01-01 06:03:44 +01:00
#[derive(Template)]
2020-11-20 05:42:18 +01:00
#[template(path = "error.html", escape = "none")]
pub struct ErrorTemplate {
2021-01-14 18:53:54 +01:00
pub msg: String,
2021-01-11 03:15:34 +01:00
pub prefs: Preferences,
2020-11-20 05:42:18 +01:00
}
2021-01-11 03:15:34 +01:00
#[derive(Default)]
2021-01-09 02:35:04 +01:00
pub struct Preferences {
2021-01-11 03:15:34 +01:00
pub theme: String,
2021-01-09 05:55:40 +01:00
pub front_page: String,
2021-01-09 02:35:04 +01:00
pub layout: String,
2021-01-10 22:08:36 +01:00
pub wide: String,
2021-01-31 06:43:46 +01:00
pub show_nsfw: String,
2021-01-09 02:50:03 +01:00
pub comment_sort: String,
pub subscriptions: Vec<String>,
2021-01-09 02:35:04 +01:00
}
2020-12-01 06:10:08 +01:00
//
2020-12-07 19:53:22 +01:00
// FORMATTING
2020-12-01 06:10:08 +01:00
//
2021-01-09 02:35:04 +01:00
// Build preferences from cookies
pub fn prefs(req: Request<()>) -> Preferences {
2021-01-09 02:35:04 +01:00
Preferences {
2021-01-11 03:15:34 +01:00
theme: cookie(&req, "theme"),
2021-01-09 05:55:40 +01:00
front_page: cookie(&req, "front_page"),
2021-01-09 02:50:03 +01:00
layout: cookie(&req, "layout"),
2021-01-10 22:08:36 +01:00
wide: cookie(&req, "wide"),
2021-01-31 06:43:46 +01:00
show_nsfw: cookie(&req, "show_nsfw"),
2021-01-09 02:50:03 +01:00
comment_sort: cookie(&req, "comment_sort"),
2021-02-14 00:02:38 +01:00
subscriptions: cookie(&req, "subscriptions").split('+').map(String::from).filter(|s| !s.is_empty()).collect(),
2021-01-09 02:35:04 +01:00
}
}
2021-01-01 00:54:13 +01:00
// Grab a query param from a url
2021-01-01 21:33:57 +01:00
pub fn param(path: &str, value: &str) -> String {
2021-01-14 23:56:28 +01:00
match Url::parse(format!("https://libredd.it/{}", path).as_str()) {
2021-02-14 00:02:38 +01:00
Ok(url) => url.query_pairs().into_owned().collect::<HashMap<_, _>>().get(value).unwrap_or(&String::new()).to_owned(),
2021-01-14 23:56:28 +01:00
_ => String::new(),
}
2021-01-01 00:54:13 +01:00
}
2021-01-07 06:27:24 +01:00
// Parse Cookie value from request
pub fn cookie(req: &Request<()>, name: &str) -> String {
let cookie = req.cookie(name).unwrap_or_else(|| Cookie::named(name));
cookie.value().to_string()
2021-01-06 03:04:49 +01:00
}
2021-01-03 05:50:23 +01:00
2020-12-26 03:06:33 +01:00
// Direct urls to proxy if proxy is enabled
2021-01-12 02:47:14 +01:00
pub fn format_url(url: &str) -> String {
2021-01-09 02:35:04 +01:00
if url.is_empty() || url == "self" || url == "default" || url == "nsfw" || url == "spoiler" {
2021-01-05 04:26:41 +01:00
String::new()
} else {
format!("/proxy/{}/", encode(url).as_str())
2021-01-05 04:26:41 +01:00
}
2020-12-01 06:10:08 +01:00
}
2021-01-02 19:58:21 +01:00
// Rewrite Reddit links to Libreddit in body of text
2021-02-10 06:56:38 +01:00
pub fn rewrite_urls(text: &str) -> String {
2021-02-04 06:42:43 +01:00
let re = Regex::new(r#"href="(https://|http://|)(www.|old.|np.|)(reddit).(com)/"#).unwrap();
2021-01-02 19:58:21 +01:00
re.replace_all(text, r#"href="/"#).to_string()
}
2020-12-26 03:06:33 +01:00
// Append `m` and `k` for millions and thousands respectively
2020-12-07 19:53:22 +01:00
pub fn format_num(num: i64) -> String {
2021-01-17 21:59:40 +01:00
if num >= 1_000_000 {
2021-01-09 02:35:04 +01:00
format!("{}m", num / 1_000_000)
2021-01-17 21:59:40 +01:00
} else if num >= 1000 {
2021-01-09 02:35:04 +01:00
format!("{}k", num / 1_000)
2020-12-07 20:36:05 +01:00
} else {
num.to_string()
}
2020-12-07 19:53:22 +01:00
}
2021-02-06 21:05:11 +01:00
pub async fn media(data: &Value) -> (String, Media, Vec<GalleryMedia>) {
2021-01-06 03:04:49 +01:00
let post_type: &str;
2021-02-06 21:05:11 +01:00
let mut gallery = Vec::new();
2021-01-15 00:13:52 +01:00
// If post is a video, return the video
let url = if data["preview"]["reddit_video_preview"]["fallback_url"].is_string() {
2021-01-06 03:04:49 +01:00
post_type = "video";
2021-01-12 02:47:14 +01:00
format_url(data["preview"]["reddit_video_preview"]["fallback_url"].as_str().unwrap_or_default())
2021-01-15 00:13:52 +01:00
} else if data["secure_media"]["reddit_video"]["fallback_url"].is_string() {
2021-01-06 03:04:49 +01:00
post_type = "video";
2021-01-12 02:47:14 +01:00
format_url(data["secure_media"]["reddit_video"]["fallback_url"].as_str().unwrap_or_default())
2021-01-06 03:04:49 +01:00
} else if data["post_hint"].as_str().unwrap_or("") == "image" {
2021-02-14 00:02:38 +01:00
// Handle images, whether GIFs or pics
2021-01-12 02:47:14 +01:00
let preview = data["preview"]["images"][0].clone();
2021-02-08 02:56:06 +01:00
let mp4 = &preview["variants"]["mp4"];
if mp4.is_object() {
2021-01-15 00:13:52 +01:00
// Return the mp4 if the media is a gif
2021-02-08 02:56:06 +01:00
post_type = "gif";
format_url(mp4["source"]["url"].as_str().unwrap_or_default())
} else {
2021-01-15 00:13:52 +01:00
// Return the picture if the media is an image
2021-02-08 02:56:06 +01:00
post_type = "image";
format_url(preview["source"]["url"].as_str().unwrap_or_default())
2021-01-12 02:47:14 +01:00
}
2021-01-12 01:35:50 +01:00
} else if data["is_self"].as_bool().unwrap_or_default() {
2021-01-11 23:08:12 +01:00
post_type = "self";
data["permalink"].as_str().unwrap_or_default().to_string()
2021-02-06 21:05:11 +01:00
} else if data["is_gallery"].as_bool().unwrap_or_default() {
2021-02-08 02:33:54 +01:00
post_type = "gallery";
2021-02-06 21:05:11 +01:00
gallery = data["gallery_data"]["items"]
.as_array()
2021-02-08 02:33:54 +01:00
.unwrap_or(&Vec::<Value>::new())
2021-02-06 21:05:11 +01:00
.iter()
.map(|item| {
let media_id = item["media_id"].as_str().unwrap_or_default();
2021-02-08 02:56:06 +01:00
let image = &data["media_metadata"][media_id]["s"];
2021-02-06 21:05:11 +01:00
GalleryMedia {
2021-02-08 02:33:54 +01:00
url: format_url(image["u"].as_str().unwrap_or_default()),
width: image["x"].as_i64().unwrap_or_default(),
height: image["y"].as_i64().unwrap_or_default(),
2021-02-06 21:05:11 +01:00
caption: item["caption"].as_str().unwrap_or_default().to_string(),
outbound_url: item["outbound_url"].as_str().unwrap_or_default().to_string(),
}
})
.collect::<Vec<GalleryMedia>>();
data["url"].as_str().unwrap_or_default().to_string()
2021-01-06 03:04:49 +01:00
} else {
post_type = "link";
data["url"].as_str().unwrap_or_default().to_string()
};
2021-01-17 21:58:12 +01:00
(
post_type.to_string(),
Media {
url,
width: data["preview"]["images"][0]["source"]["width"].as_i64().unwrap_or_default(),
height: data["preview"]["images"][0]["source"]["height"].as_i64().unwrap_or_default(),
poster: format_url(data["preview"]["images"][0]["source"]["url"].as_str().unwrap_or_default()),
2021-01-17 21:58:12 +01:00
},
2021-02-06 21:05:11 +01:00
gallery,
2021-01-17 21:58:12 +01:00
)
2021-01-06 03:04:49 +01:00
}
2021-01-13 08:23:48 +01:00
pub fn parse_rich_flair(flair_type: String, rich_flair: Option<&Vec<Value>>, text_flair: Option<&str>) -> Vec<FlairPart> {
2021-01-15 00:13:52 +01:00
// Parse type of flair
2021-01-14 03:19:40 +01:00
match flair_type.as_str() {
2021-01-15 00:13:52 +01:00
// If flair contains emojis and text
"richtext" => match rich_flair {
Some(rich) => rich
.iter()
2021-01-15 00:13:52 +01:00
// For each part of the flair, extract text and emojis
.map(|part| {
let value = |name: &str| part[name].as_str().unwrap_or_default();
FlairPart {
flair_part_type: value("e").to_string(),
value: match value("e") {
"text" => value("t").to_string(),
"emoji" => format_url(value("u")),
_ => String::new(),
},
2021-01-14 03:19:40 +01:00
}
})
.collect::<Vec<FlairPart>>(),
None => Vec::new(),
2021-01-14 03:19:40 +01:00
},
2021-01-15 00:13:52 +01:00
// If flair contains only text
"text" => match text_flair {
2021-01-14 03:19:40 +01:00
Some(text) => vec![FlairPart {
flair_part_type: "text".to_string(),
value: text.to_string(),
}],
None => Vec::new(),
2021-01-14 03:19:40 +01:00
},
_ => Vec::new(),
2021-01-12 22:43:03 +01:00
}
}
pub fn time(created: f64) -> (String, String) {
let time = OffsetDateTime::from_unix_timestamp(created.round() as i64);
2021-01-12 19:59:32 +01:00
let time_delta = OffsetDateTime::now_utc() - time;
2021-01-15 00:13:52 +01:00
// If the time difference is more than a month, show full date
let rel_time = if time_delta > Duration::days(30) {
2021-01-15 00:13:52 +01:00
time.format("%b %d '%y")
// Otherwise, show relative date/time
2021-01-14 01:31:24 +01:00
} else if time_delta.whole_days() > 0 {
format!("{}d ago", time_delta.whole_days())
2021-01-12 19:59:32 +01:00
} else if time_delta.whole_hours() > 0 {
format!("{}h ago", time_delta.whole_hours())
} else {
format!("{}m ago", time_delta.whole_minutes())
};
(rel_time, time.format("%b %d %Y, %H:%M UTC"))
2021-01-12 19:59:32 +01:00
}
2020-11-20 05:42:18 +01:00
//
// JSON PARSING
//
2020-11-17 20:37:40 +01:00
// val() function used to parse JSON from Reddit APIs
2021-01-14 18:53:54 +01:00
pub fn val(j: &Value, k: &str) -> String {
2021-01-22 06:25:51 +01:00
j["data"][k].as_str().unwrap_or_default().to_string()
2020-11-17 20:37:40 +01:00
}
2021-01-07 06:27:24 +01:00
// Fetch posts of a user or subreddit and return a vector of posts and the "after" value
2021-01-14 18:53:54 +01:00
pub async fn fetch_posts(path: &str, fallback_title: String) -> Result<(Vec<Post>, String), String> {
2021-01-02 00:28:13 +01:00
let res;
let post_list;
// Send a request to the url
2021-01-23 10:48:33 +01:00
match request(path.to_string()).await {
2021-01-02 00:28:13 +01:00
// If success, receive JSON in response
2021-01-07 17:38:05 +01:00
Ok(response) => {
res = response;
}
2021-01-02 00:28:13 +01:00
// If the Reddit API returns an error, exit this function
2021-01-02 07:21:43 +01:00
Err(msg) => return Err(msg),
2020-11-21 07:05:27 +01:00
}
// Fetch the list of posts from the JSON response
2021-01-02 00:28:13 +01:00
match res["data"]["children"].as_array() {
2021-01-02 07:21:43 +01:00
Some(list) => post_list = list,
2021-01-14 18:53:54 +01:00
None => return Err("No posts found".to_string()),
2021-01-02 00:28:13 +01:00
}
2020-11-21 07:05:27 +01:00
let mut posts: Vec<Post> = Vec::new();
2021-01-05 04:26:41 +01:00
// For each post from posts list
2020-12-23 03:29:43 +01:00
for post in post_list {
let (rel_time, created) = time(post["data"]["created_utc"].as_f64().unwrap_or_default());
2021-01-02 00:28:13 +01:00
let score = post["data"]["score"].as_i64().unwrap_or_default();
let ratio: f64 = post["data"]["upvote_ratio"].as_f64().unwrap_or(1.0) * 100.0;
2021-01-01 21:33:57 +01:00
let title = val(post, "title");
2020-11-21 07:05:27 +01:00
2021-01-06 03:04:49 +01:00
// Determine the type of media along with the media URL
2021-02-06 21:05:11 +01:00
let (post_type, media, gallery) = media(&post["data"]).await;
2021-01-06 03:04:49 +01:00
2020-11-21 07:05:27 +01:00
posts.push(Post {
id: val(post, "id"),
2020-11-21 07:05:27 +01:00
title: if title.is_empty() { fallback_title.to_owned() } else { title },
2021-01-01 21:33:57 +01:00
community: val(post, "subreddit"),
2021-02-10 06:56:38 +01:00
body: rewrite_urls(&val(post, "body_html")),
2021-01-17 00:02:24 +01:00
author: Author {
name: val(post, "author"),
flair: Flair {
flair_parts: parse_rich_flair(
val(post, "author_flair_type"),
post["data"]["author_flair_richtext"].as_array(),
post["data"]["author_flair_text"].as_str(),
),
background_color: val(post, "author_flair_background_color"),
foreground_color: val(post, "author_flair_text_color"),
},
distinguished: val(post, "distinguished"),
2021-01-12 22:43:03 +01:00
},
2021-01-31 23:10:13 +01:00
score: if post["data"]["hide_score"].as_bool().unwrap_or_default() {
"".to_string()
} else {
format_num(score)
},
upvote_ratio: ratio as i64,
2021-01-07 06:27:24 +01:00
post_type,
2021-01-17 21:58:12 +01:00
thumbnail: Media {
url: format_url(val(post, "thumbnail").as_str()),
width: post["data"]["thumbnail_width"].as_i64().unwrap_or_default(),
height: post["data"]["thumbnail_height"].as_i64().unwrap_or_default(),
poster: "".to_string(),
2021-01-17 21:58:12 +01:00
},
2021-01-07 06:27:24 +01:00
media,
2021-01-11 23:08:12 +01:00
domain: val(post, "domain"),
2021-01-13 21:52:00 +01:00
flair: Flair {
flair_parts: parse_rich_flair(
val(post, "link_flair_type"),
post["data"]["link_flair_richtext"].as_array(),
post["data"]["link_flair_text"].as_str(),
),
2021-01-12 22:43:03 +01:00
background_color: val(post, "link_flair_background_color"),
foreground_color: if val(post, "link_flair_text_color") == "dark" {
2020-11-21 07:05:27 +01:00
"black".to_string()
} else {
"white".to_string()
},
2021-01-12 22:43:03 +01:00
},
2020-12-30 04:01:02 +01:00
flags: Flags {
2021-01-05 04:26:41 +01:00
nsfw: post["data"]["over_18"].as_bool().unwrap_or_default(),
stickied: post["data"]["stickied"].as_bool().unwrap_or_default(),
2020-12-30 04:01:02 +01:00
},
permalink: val(post, "permalink"),
rel_time,
2021-01-16 20:50:12 +01:00
created,
comments: format_num(post["data"]["num_comments"].as_i64().unwrap_or_default()),
2021-02-06 21:05:11 +01:00
gallery,
2020-11-21 07:05:27 +01:00
});
}
2021-01-05 04:26:41 +01:00
Ok((posts, res["data"]["after"].as_str().unwrap_or_default().to_string()))
2020-11-21 07:05:27 +01:00
}
2020-11-20 05:42:18 +01:00
//
// NETWORKING
//
2021-02-14 00:02:38 +01:00
pub fn template(t: impl Template) -> tide::Result {
Ok(Response::builder(200).content_type("text/html").body(t.render().unwrap_or_default()).build())
}
pub fn redirect(path: String) -> Response {
Response::builder(302)
.content_type("text/html")
.header("Location", &path)
.body(format!("Redirecting to <a href=\"{0}\">{0}</a>...", path))
.build()
}
pub async fn error(msg: String) -> tide::Result {
2021-01-11 03:15:34 +01:00
let body = ErrorTemplate {
2021-01-14 18:53:54 +01:00
msg,
2021-01-11 03:15:34 +01:00
prefs: Preferences::default(),
}
.render()
.unwrap_or_default();
Ok(Response::builder(404).content_type("text/html").body(body).build())
2021-01-01 06:03:44 +01:00
}
2020-11-19 03:50:59 +01:00
// Make a request to a Reddit API and parse the JSON response
2021-02-08 02:56:06 +01:00
#[cached(size = 100, time = 30, result = true)]
2021-01-23 10:48:33 +01:00
pub async fn request(path: String) -> Result<Value, String> {
2021-01-11 19:33:48 +01:00
let url = format!("https://www.reddit.com{}", path);
// Build reddit-compliant user agent for Libreddit
2021-01-17 00:13:34 +01:00
let user_agent = format!("web:libreddit:{}", env!("CARGO_PKG_VERSION"));
2021-01-11 19:33:48 +01:00
// Send request using surf
let req = surf::get(&url).header("User-Agent", user_agent.as_str());
let client = surf::client().with(surf::middleware::Redirect::new(5));
let res = client.send(req).await;
let body = res.unwrap().take_body().into_string().await;
match body {
2021-01-16 06:26:51 +01:00
// If response is success
Ok(response) => {
// Parse the response from Reddit as JSON
match from_str(&response) {
2021-01-16 06:26:51 +01:00
Ok(json) => Ok(json),
2021-01-28 02:48:58 +01:00
Err(e) => {
println!("{} - Failed to parse page JSON data: {}", url, e);
2021-01-16 06:26:51 +01:00
Err("Failed to parse page JSON data".to_string())
}
}
}
// If failed to send request
2021-01-28 02:48:58 +01:00
Err(e) => {
println!("{} - Couldn't send request to Reddit: {}", url, e);
Err("Couldn't send request to Reddit".to_string())
2021-01-16 06:26:51 +01:00
}
}
2020-11-19 03:50:59 +01:00
}