From 402b3149e19f82e2565535ce660a6b60574088fc Mon Sep 17 00:00:00 2001 From: spikecodes <19519553+spikecodes@users.noreply.github.com> Date: Sun, 7 Feb 2021 17:56:06 -0800 Subject: [PATCH] Fix 'no entry found for key' error --- Cargo.lock | 97 ++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 3 +- src/subreddit.rs | 2 +- src/utils.rs | 22 +++++------ 4 files changed, 110 insertions(+), 14 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5b1f54e..ba72f38 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -346,6 +346,15 @@ dependencies = [ "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]] name = "async-recursion" version = "0.3.2" @@ -511,6 +520,40 @@ dependencies = [ "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]] name = "cc" version = "1.0.66" @@ -573,6 +616,41 @@ dependencies = [ "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]] name = "derive_more" version = "0.99.11" @@ -626,6 +704,12 @@ dependencies = [ "syn", ] +[[package]] +name = "event-listener" +version = "2.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f7531096570974c3a9dcf9e4b8e1cede1ec26cf5046219fb3b9d897503b9be59" + [[package]] name = "flate2" version = "1.0.20" @@ -885,6 +969,12 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b6cab2627acfc432780848602f3f558f7e9dd427352224b0d9324025796d2a5e" +[[package]] +name = "ident_case" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39" + [[package]] name = "idna" version = "0.2.0" @@ -1000,6 +1090,7 @@ dependencies = [ "askama", "async-recursion", "base64 0.13.0", + "cached", "futures", "regex", "serde", @@ -1647,6 +1738,12 @@ version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "213701ba3370744dcd1a12960caa4843b3d68b4d1c0a5d575e0d65b2ee9d16c0" +[[package]] +name = "strsim" +version = "0.9.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6446ced80d6c486436db5c078dde11a9f73d42b57fb273121e160b84f63d894c" + [[package]] name = "syn" version = "1.0.60" diff --git a/Cargo.toml b/Cargo.toml index 63b1fec..6d08408 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,4 +18,5 @@ serde_json = "1.0" async-recursion = "0.3" url = "2.2" regex = "1.4" -time = "0.2" \ No newline at end of file +time = "0.2" +cached = "0.23" \ No newline at end of file diff --git a/src/subreddit.rs b/src/subreddit.rs index e0de60f..8dd1096 100644 --- a/src/subreddit.rs +++ b/src/subreddit.rs @@ -95,7 +95,7 @@ pub async fn subscriptions(req: HttpRequest) -> HttpResponse { // Modify sub list based on action if action == "subscribe" && !sub_list.contains(&sub) { 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" { sub_list.retain(|s| s != &sub); } diff --git a/src/utils.rs b/src/utils.rs index d225972..8a605a9 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -4,12 +4,12 @@ use actix_web::{cookie::Cookie, HttpRequest, HttpResponse, Result}; use askama::Template; use base64::encode; +use cached::proc_macro::cached; use regex::Regex; use serde_json::{from_str, Value}; use std::collections::HashMap; use time::{Duration, OffsetDateTime}; use url::Url; -// use cached::proc_macro::cached; // // STRUCTS @@ -211,17 +211,15 @@ pub async fn media(data: &Value) -> (String, Media, Vec) { // Handle images, whether GIFs or pics } else if data["post_hint"].as_str().unwrap_or("") == "image" { 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 - Some(gif) => { - post_type = "gif"; - format_url(gif["source"]["url"].as_str().unwrap_or_default()) - } + post_type = "gif"; + format_url(mp4["source"]["url"].as_str().unwrap_or_default()) + } else { // Return the picture if the media is an image - None => { - post_type = "image"; - format_url(preview["source"]["url"].as_str().unwrap_or_default()) - } + post_type = "image"; + format_url(preview["source"]["url"].as_str().unwrap_or_default()) } } else if data["is_self"].as_bool().unwrap_or_default() { post_type = "self"; @@ -234,7 +232,7 @@ pub async fn media(data: &Value) -> (String, Media, Vec) { .iter() .map(|item| { 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 { url: format_url(image["u"].as_str().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 -// #[cached(size=100,time=60, result = true)] +#[cached(size = 100, time = 30, result = true)] pub async fn request(path: String) -> Result { let url = format!("https://www.reddit.com{}", path); let user_agent = format!("web:libreddit:{}", env!("CARGO_PKG_VERSION"));