Fix 'no entry found for key' error

This commit is contained in:
spikecodes 2021-02-07 17:56:06 -08:00
parent ac5ef89dff
commit 402b3149e1
No known key found for this signature in database
GPG Key ID: 004CECFF9B463BCB
4 changed files with 110 additions and 14 deletions

97
Cargo.lock generated
View File

@ -346,6 +346,15 @@ dependencies = [
"toml", "toml",
] ]
[[package]]
name = "async-mutex"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "479db852db25d9dbf6204e6cb6253698f175c15726470f78af0d918e99d6156e"
dependencies = [
"event-listener",
]
[[package]] [[package]]
name = "async-recursion" name = "async-recursion"
version = "0.3.2" version = "0.3.2"
@ -511,6 +520,40 @@ dependencies = [
"bytes 1.0.1", "bytes 1.0.1",
] ]
[[package]]
name = "cached"
version = "0.23.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5e2afe73808fbaac302e39c9754bfc3c4b4d0f99c9c240b9f4e4efc841ad1b74"
dependencies = [
"async-mutex",
"async-trait",
"cached_proc_macro",
"cached_proc_macro_types",
"futures",
"hashbrown",
"once_cell",
]
[[package]]
name = "cached_proc_macro"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bf857ae42d910aede5c5186e62684b0d7a597ce2fe3bd14448ab8f7ef439848c"
dependencies = [
"async-mutex",
"cached_proc_macro_types",
"darling",
"quote",
"syn",
]
[[package]]
name = "cached_proc_macro_types"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3a4f925191b4367301851c6d99b09890311d74b0d43f274c0b34c86d308a3663"
[[package]] [[package]]
name = "cc" name = "cc"
version = "1.0.66" version = "1.0.66"
@ -573,6 +616,41 @@ dependencies = [
"cfg-if 1.0.0", "cfg-if 1.0.0",
] ]
[[package]]
name = "darling"
version = "0.10.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0d706e75d87e35569db781a9b5e2416cff1236a47ed380831f959382ccd5f858"
dependencies = [
"darling_core",
"darling_macro",
]
[[package]]
name = "darling_core"
version = "0.10.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f0c960ae2da4de88a91b2d920c2a7233b400bc33cb28453a2987822d8392519b"
dependencies = [
"fnv",
"ident_case",
"proc-macro2",
"quote",
"strsim",
"syn",
]
[[package]]
name = "darling_macro"
version = "0.10.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d9b5a2f4ac4969822c62224815d069952656cadc7084fdca9751e6d959189b72"
dependencies = [
"darling_core",
"quote",
"syn",
]
[[package]] [[package]]
name = "derive_more" name = "derive_more"
version = "0.99.11" version = "0.99.11"
@ -626,6 +704,12 @@ dependencies = [
"syn", "syn",
] ]
[[package]]
name = "event-listener"
version = "2.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f7531096570974c3a9dcf9e4b8e1cede1ec26cf5046219fb3b9d897503b9be59"
[[package]] [[package]]
name = "flate2" name = "flate2"
version = "1.0.20" version = "1.0.20"
@ -885,6 +969,12 @@ version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b6cab2627acfc432780848602f3f558f7e9dd427352224b0d9324025796d2a5e" checksum = "b6cab2627acfc432780848602f3f558f7e9dd427352224b0d9324025796d2a5e"
[[package]]
name = "ident_case"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39"
[[package]] [[package]]
name = "idna" name = "idna"
version = "0.2.0" version = "0.2.0"
@ -1000,6 +1090,7 @@ dependencies = [
"askama", "askama",
"async-recursion", "async-recursion",
"base64 0.13.0", "base64 0.13.0",
"cached",
"futures", "futures",
"regex", "regex",
"serde", "serde",
@ -1647,6 +1738,12 @@ version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "213701ba3370744dcd1a12960caa4843b3d68b4d1c0a5d575e0d65b2ee9d16c0" checksum = "213701ba3370744dcd1a12960caa4843b3d68b4d1c0a5d575e0d65b2ee9d16c0"
[[package]]
name = "strsim"
version = "0.9.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6446ced80d6c486436db5c078dde11a9f73d42b57fb273121e160b84f63d894c"
[[package]] [[package]]
name = "syn" name = "syn"
version = "1.0.60" version = "1.0.60"

View File

@ -19,3 +19,4 @@ async-recursion = "0.3"
url = "2.2" url = "2.2"
regex = "1.4" regex = "1.4"
time = "0.2" time = "0.2"
cached = "0.23"

View File

@ -95,7 +95,7 @@ pub async fn subscriptions(req: HttpRequest) -> HttpResponse {
// Modify sub list based on action // Modify sub list based on action
if action == "subscribe" && !sub_list.contains(&sub) { if action == "subscribe" && !sub_list.contains(&sub) {
sub_list.push(sub.to_owned()); sub_list.push(sub.to_owned());
sub_list.sort_by(|a, b| a.to_lowercase().cmp(&b.to_lowercase())); sub_list.sort_by_key(|a| a.to_lowercase());
} else if action == "unsubscribe" { } else if action == "unsubscribe" {
sub_list.retain(|s| s != &sub); sub_list.retain(|s| s != &sub);
} }

View File

@ -4,12 +4,12 @@
use actix_web::{cookie::Cookie, HttpRequest, HttpResponse, Result}; use actix_web::{cookie::Cookie, HttpRequest, HttpResponse, Result};
use askama::Template; use askama::Template;
use base64::encode; use base64::encode;
use cached::proc_macro::cached;
use regex::Regex; use regex::Regex;
use serde_json::{from_str, Value}; use serde_json::{from_str, Value};
use std::collections::HashMap; use std::collections::HashMap;
use time::{Duration, OffsetDateTime}; use time::{Duration, OffsetDateTime};
use url::Url; use url::Url;
// use cached::proc_macro::cached;
// //
// STRUCTS // STRUCTS
@ -211,18 +211,16 @@ pub async fn media(data: &Value) -> (String, Media, Vec<GalleryMedia>) {
// Handle images, whether GIFs or pics // Handle images, whether GIFs or pics
} else if data["post_hint"].as_str().unwrap_or("") == "image" { } else if data["post_hint"].as_str().unwrap_or("") == "image" {
let preview = data["preview"]["images"][0].clone(); let preview = data["preview"]["images"][0].clone();
match preview["variants"]["mp4"].as_object() { let mp4 = &preview["variants"]["mp4"];
if mp4.is_object() {
// Return the mp4 if the media is a gif // Return the mp4 if the media is a gif
Some(gif) => {
post_type = "gif"; post_type = "gif";
format_url(gif["source"]["url"].as_str().unwrap_or_default()) format_url(mp4["source"]["url"].as_str().unwrap_or_default())
} } else {
// Return the picture if the media is an image // Return the picture if the media is an image
None => {
post_type = "image"; post_type = "image";
format_url(preview["source"]["url"].as_str().unwrap_or_default()) format_url(preview["source"]["url"].as_str().unwrap_or_default())
} }
}
} else if data["is_self"].as_bool().unwrap_or_default() { } else if data["is_self"].as_bool().unwrap_or_default() {
post_type = "self"; post_type = "self";
data["permalink"].as_str().unwrap_or_default().to_string() data["permalink"].as_str().unwrap_or_default().to_string()
@ -234,7 +232,7 @@ pub async fn media(data: &Value) -> (String, Media, Vec<GalleryMedia>) {
.iter() .iter()
.map(|item| { .map(|item| {
let media_id = item["media_id"].as_str().unwrap_or_default(); let media_id = item["media_id"].as_str().unwrap_or_default();
let image = &data["media_metadata"][media_id].as_object().map(ToOwned::to_owned).unwrap_or_default()["s"]; let image = &data["media_metadata"][media_id]["s"];
GalleryMedia { GalleryMedia {
url: format_url(image["u"].as_str().unwrap_or_default()), url: format_url(image["u"].as_str().unwrap_or_default()),
width: image["x"].as_i64().unwrap_or_default(), width: image["x"].as_i64().unwrap_or_default(),
@ -433,7 +431,7 @@ pub async fn error(msg: String) -> HttpResponse {
} }
// Make a request to a Reddit API and parse the JSON response // Make a request to a Reddit API and parse the JSON response
// #[cached(size=100,time=60, result = true)] #[cached(size = 100, time = 30, result = true)]
pub async fn request(path: String) -> Result<Value, String> { pub async fn request(path: String) -> Result<Value, String> {
let url = format!("https://www.reddit.com{}", path); let url = format!("https://www.reddit.com{}", path);
let user_agent = format!("web:libreddit:{}", env!("CARGO_PKG_VERSION")); let user_agent = format!("web:libreddit:{}", env!("CARGO_PKG_VERSION"));