Rewrite Reddit Links to Libreddit
This commit is contained in:
parent
779de6f8af
commit
4ec529cdb8
1
Cargo.lock
generated
1
Cargo.lock
generated
@ -1013,6 +1013,7 @@ dependencies = [
|
||||
"async-recursion",
|
||||
"base64 0.13.0",
|
||||
"chrono",
|
||||
"regex",
|
||||
"reqwest",
|
||||
"serde",
|
||||
"serde_json",
|
||||
|
@ -20,4 +20,5 @@ serde = "1.0.117"
|
||||
serde_json = "1.0"
|
||||
chrono = "0.4.19"
|
||||
async-recursion = "0.3.1"
|
||||
url = "2.2.0"
|
||||
url = "2.2.0"
|
||||
regex = "1"
|
@ -59,6 +59,8 @@ async fn main() -> std::io::Result<()> {
|
||||
.route("/u/{username}/", web::get().to(user::profile))
|
||||
.route("/user/{username}/", web::get().to(user::profile))
|
||||
// WIKI SERVICES
|
||||
.route("/wiki/", web::get().to(subreddit::wiki))
|
||||
.route("/wiki/{page}/", web::get().to(subreddit::wiki))
|
||||
.route("/r/{sub}/wiki/", web::get().to(subreddit::wiki))
|
||||
.route("/r/{sub}/wiki/{page}/", web::get().to(subreddit::wiki))
|
||||
// SUBREDDIT SERVICES
|
||||
|
@ -1,5 +1,5 @@
|
||||
// CRATES
|
||||
use crate::utils::{error, format_num, format_url, param, request, val, Comment, Flags, Flair, Post};
|
||||
use crate::utils::{error, format_num, format_url, param, request, rewrite_url, val, Comment, Flags, Flair, Post};
|
||||
use actix_web::{HttpRequest, HttpResponse, Result};
|
||||
|
||||
use async_recursion::async_recursion;
|
||||
@ -79,7 +79,7 @@ async fn parse_post(json: &serde_json::Value) -> Result<Post, &'static str> {
|
||||
let post = Post {
|
||||
title: val(post_data, "title"),
|
||||
community: val(post_data, "subreddit"),
|
||||
body: val(post_data, "selftext_html"),
|
||||
body: rewrite_url(&val(post_data, "selftext_html")).await,
|
||||
author: val(post_data, "author"),
|
||||
author_flair: Flair(
|
||||
val(post_data, "author_flair_text"),
|
||||
@ -125,7 +125,7 @@ async fn parse_comments(json: &serde_json::Value) -> Result<Vec<Comment>, &'stat
|
||||
}
|
||||
|
||||
let score = comment["data"]["score"].as_i64().unwrap_or(0);
|
||||
let body = val(comment, "body_html");
|
||||
let body = rewrite_url(&val(comment, "body_html")).await;
|
||||
|
||||
let replies: Vec<Comment> = if comment["data"]["replies"].is_object() {
|
||||
parse_comments(&comment["data"]["replies"]).await.unwrap_or_default()
|
||||
|
@ -1,5 +1,5 @@
|
||||
// CRATES
|
||||
use crate::utils::{error, fetch_posts, format_num, format_url, param, request, val, Post, Subreddit};
|
||||
use crate::utils::{error, fetch_posts, format_num, format_url, param, request, rewrite_url, val, Post, Subreddit};
|
||||
use actix_web::{HttpRequest, HttpResponse, Result};
|
||||
use askama::Template;
|
||||
|
||||
@ -18,7 +18,7 @@ struct SubredditTemplate {
|
||||
struct WikiTemplate {
|
||||
sub: String,
|
||||
wiki: String,
|
||||
page: String
|
||||
page: String,
|
||||
}
|
||||
|
||||
// SERVICES
|
||||
@ -50,7 +50,7 @@ pub async fn page(req: HttpRequest) -> Result<HttpResponse> {
|
||||
}
|
||||
|
||||
pub async fn wiki(req: HttpRequest) -> Result<HttpResponse> {
|
||||
let sub = req.match_info().get("sub").unwrap();
|
||||
let sub = req.match_info().get("sub").unwrap_or("reddit.com");
|
||||
let page = req.match_info().get("page").unwrap_or("index");
|
||||
let path: String = format!("r/{}/wiki/{}.json?raw_json=1", sub, page);
|
||||
|
||||
@ -58,8 +58,8 @@ pub async fn wiki(req: HttpRequest) -> Result<HttpResponse> {
|
||||
Ok(res) => {
|
||||
let s = WikiTemplate {
|
||||
sub: sub.to_string(),
|
||||
wiki: res["data"]["content_html"].as_str().unwrap().to_string(),
|
||||
page: page.to_string()
|
||||
wiki: rewrite_url(res["data"]["content_html"].as_str().unwrap()).await,
|
||||
page: page.to_string(),
|
||||
}
|
||||
.render()
|
||||
.unwrap();
|
||||
@ -90,7 +90,7 @@ async fn subreddit(sub: &str) -> Result<Subreddit, &'static str> {
|
||||
name: val(&res, "display_name"),
|
||||
title: val(&res, "title"),
|
||||
description: val(&res, "public_description"),
|
||||
info: val(&res, "description_html").replace("\\", ""),
|
||||
info: rewrite_url(&val(&res, "description_html").replace("\\", "")).await,
|
||||
icon: format_url(icon).await,
|
||||
members: format_num(members),
|
||||
active: format_num(active),
|
||||
|
@ -4,6 +4,7 @@
|
||||
use actix_web::{http::StatusCode, HttpResponse, Result};
|
||||
use askama::Template;
|
||||
use chrono::{TimeZone, Utc};
|
||||
use regex::Regex;
|
||||
use serde_json::from_str;
|
||||
use url::Url;
|
||||
// use surf::{client, get, middleware::Redirect};
|
||||
@ -114,6 +115,12 @@ pub async fn format_url(url: String) -> String {
|
||||
return url.to_string();
|
||||
}
|
||||
|
||||
// Rewrite Reddit links to Libreddit in body of text
|
||||
pub async fn rewrite_url(text: &str) -> String {
|
||||
let re = Regex::new(r#"href="(https://|http://|)(www.|)(reddit).(com)/"#).unwrap();
|
||||
re.replace_all(text, r#"href="/"#).to_string()
|
||||
}
|
||||
|
||||
// Append `m` and `k` for millions and thousands respectively
|
||||
pub fn format_num(num: i64) -> String {
|
||||
if num > 1000000 {
|
||||
@ -175,7 +182,7 @@ pub async fn fetch_posts(path: &str, fallback_title: String) -> Result<(Vec<Post
|
||||
posts.push(Post {
|
||||
title: if title.is_empty() { fallback_title.to_owned() } else { title },
|
||||
community: val(post, "subreddit"),
|
||||
body: val(post, "body_html"),
|
||||
body: rewrite_url(&val(post, "body_html")).await,
|
||||
author: val(post, "author"),
|
||||
author_flair: Flair(
|
||||
val(post, "author_flair_text"),
|
||||
|
Loading…
Reference in New Issue
Block a user