Add GitHub RSS feed
All checks were successful
Build and push container image / build (push) Successful in 12m39s

This commit is contained in:
toast 2024-08-15 10:51:17 +10:00
parent 025a65eefb
commit 7c368404e4
4 changed files with 298 additions and 93 deletions

124
Cargo.lock generated
View File

@ -64,7 +64,7 @@ checksum = "6e0c28dcc82d7c8ead5cb13beb15405b57b8546e93215673ff8ca0349a028107"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.72", "syn 2.0.74",
] ]
[[package]] [[package]]
@ -229,9 +229,12 @@ dependencies = [
[[package]] [[package]]
name = "cc" name = "cc"
version = "1.1.8" version = "1.1.11"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "504bdec147f2cc13c8b57ed9401fd8a147cc66b67ad5cb241394244f2c947549" checksum = "5fb8dd288a69fc53a1996d7ecfbf4a20d59065bff137ce7e56bbd620de191189"
dependencies = [
"shlex",
]
[[package]] [[package]]
name = "cfg-if" name = "cfg-if"
@ -278,15 +281,15 @@ dependencies = [
[[package]] [[package]]
name = "core-foundation-sys" name = "core-foundation-sys"
version = "0.8.6" version = "0.8.7"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "06ea2b9bc92be3c2baa9334a323ebca2d6f074ff852cd1d7b11064035cd3868f" checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b"
[[package]] [[package]]
name = "cpufeatures" name = "cpufeatures"
version = "0.2.12" version = "0.2.13"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "53fe5e26ff1b7aef8bca9c6080520cfb8d9333c7568e1829cef191a9723e5504" checksum = "51e852e6dc9a5bed1fae92dd2375037bf2b768725bf3be87811edee3249d09ad"
dependencies = [ dependencies = [
"libc", "libc",
] ]
@ -365,7 +368,7 @@ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"strsim", "strsim",
"syn 2.0.72", "syn 2.0.74",
] ]
[[package]] [[package]]
@ -376,7 +379,7 @@ checksum = "d336a2a514f6ccccaa3e09b02d41d35330c07ddf03a62165fcec10bb561c7806"
dependencies = [ dependencies = [
"darling_core", "darling_core",
"quote", "quote",
"syn 2.0.72", "syn 2.0.74",
] ]
[[package]] [[package]]
@ -455,7 +458,7 @@ dependencies = [
"heck", "heck",
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.72", "syn 2.0.74",
] ]
[[package]] [[package]]
@ -596,7 +599,7 @@ checksum = "87750cf4b7a4c0625b1529e4c543c2182106e4dedc60a2a6455e00d212c489ac"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.72", "syn 2.0.74",
] ]
[[package]] [[package]]
@ -977,9 +980,9 @@ dependencies = [
[[package]] [[package]]
name = "indexmap" name = "indexmap"
version = "2.3.0" version = "2.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "de3fc2e30ba82dd1b3911c8de1ffc143c74a914a14e99514d7637e3099df5ea0" checksum = "93ead53efc7ea8ed3cfb0c79fc8023fbb782a5432b52830b6518941cebe6505c"
dependencies = [ dependencies = [
"equivalent", "equivalent",
"hashbrown", "hashbrown",
@ -1011,16 +1014,16 @@ checksum = "49f1f14873335454500d59611f1cf4a4b0f786f9ac11f4312a78e4cf2566695b"
[[package]] [[package]]
name = "js-sys" name = "js-sys"
version = "0.3.69" version = "0.3.70"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "29c15563dc2726973df627357ce0c9ddddbea194836909d655df6a75d2cf296d" checksum = "1868808506b929d7b0cfa8f75951347aa71bb21144b7791bae35d9bccfcfe37a"
dependencies = [ dependencies = [
"wasm-bindgen", "wasm-bindgen",
] ]
[[package]] [[package]]
name = "kon" name = "kon"
version = "0.3.13" version = "0.3.14"
dependencies = [ dependencies = [
"bb8", "bb8",
"bb8-postgres", "bb8-postgres",
@ -1153,9 +1156,9 @@ dependencies = [
[[package]] [[package]]
name = "mio" name = "mio"
version = "1.0.1" version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4569e456d394deccd22ce1c1913e6ea0e54519f577285001215d33557431afe4" checksum = "80e04d1dcff3aae0704555fe5fee3bcfaf3d1fdf8a7e521d5b9d2b42acb52cec"
dependencies = [ dependencies = [
"hermit-abi", "hermit-abi",
"libc", "libc",
@ -1261,7 +1264,7 @@ checksum = "a948666b637a0f465e8564c73e89d4dde00d72d4d473cc972f390fc3dcee7d9c"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.72", "syn 2.0.74",
] ]
[[package]] [[package]]
@ -1346,7 +1349,7 @@ checksum = "2f38a4412a78282e09a2cf38d195ea5420d15ba0602cb375210efbc877243965"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.72", "syn 2.0.74",
] ]
[[package]] [[package]]
@ -1393,7 +1396,7 @@ dependencies = [
"darling", "darling",
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.72", "syn 2.0.74",
] ]
[[package]] [[package]]
@ -1903,9 +1906,9 @@ dependencies = [
[[package]] [[package]]
name = "serde" name = "serde"
version = "1.0.205" version = "1.0.207"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e33aedb1a7135da52b7c21791455563facbbcc43d0f0f66165b42c21b3dfb150" checksum = "5665e14a49a4ea1b91029ba7d3bca9f299e1f7cfa194388ccc20f14743e784f2"
dependencies = [ dependencies = [
"serde_derive", "serde_derive",
] ]
@ -1921,20 +1924,20 @@ dependencies = [
[[package]] [[package]]
name = "serde_derive" name = "serde_derive"
version = "1.0.205" version = "1.0.207"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "692d6f5ac90220161d6774db30c662202721e64aed9058d2c394f451261420c1" checksum = "6aea2634c86b0e8ef2cfdc0c340baede54ec27b1e46febd7f80dffb2aa44a00e"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.72", "syn 2.0.74",
] ]
[[package]] [[package]]
name = "serde_json" name = "serde_json"
version = "1.0.122" version = "1.0.124"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "784b6203951c57ff748476b126ccb5e8e2959a5c19e5c617ab1956be3dbc68da" checksum = "66ad62847a56b3dba58cc891acd13884b9c61138d330c0d7b6181713d4fce38d"
dependencies = [ dependencies = [
"itoa", "itoa",
"memchr", "memchr",
@ -2018,6 +2021,12 @@ dependencies = [
"digest", "digest",
] ]
[[package]]
name = "shlex"
version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64"
[[package]] [[package]]
name = "signal-hook-registry" name = "signal-hook-registry"
version = "1.4.2" version = "1.4.2"
@ -2121,9 +2130,9 @@ dependencies = [
[[package]] [[package]]
name = "syn" name = "syn"
version = "2.0.72" version = "2.0.74"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dc4b9b9bf2add8093d3f2c0204471e951b2285580335de42f9d2534f3ae7a8af" checksum = "1fceb41e3d546d0bd83421d3409b1460cc7444cd389341a4c880fe7a042cb3d7"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
@ -2213,7 +2222,7 @@ checksum = "a4558b58466b9ad7ca0f102865eccc95938dca1a74a856f2b57b6629050da261"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.72", "syn 2.0.74",
] ]
[[package]] [[package]]
@ -2301,7 +2310,7 @@ checksum = "693d596312e88961bc67d7f1f97af8a70227d9f90c31bba5806eec004978d752"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.72", "syn 2.0.74",
] ]
[[package]] [[package]]
@ -2452,15 +2461,15 @@ dependencies = [
[[package]] [[package]]
name = "tower-layer" name = "tower-layer"
version = "0.3.2" version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c20c8dbed6283a09604c3e69b4b7eeb54e298b8a600d4d5ecb5ad39de609f1d0" checksum = "121c2a6cda46980bb0fcd1647ffaf6cd3fc79a013de288782836f6df9c48780e"
[[package]] [[package]]
name = "tower-service" name = "tower-service"
version = "0.3.2" version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b6bc1c9ce2b5135ac7f93c72918fc37feb872bdc6a5533a8b85eb4b86bfdae52" checksum = "8df9b6e13f2d32c91b9bd719c00d1958837bc7dec474d94952798cc8e69eeec3"
[[package]] [[package]]
name = "tracing" name = "tracing"
@ -2482,7 +2491,7 @@ checksum = "34704c8d6ebcbc939824180af020566b01a7c01f80641264eba0999f6c2b6be7"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.72", "syn 2.0.74",
] ]
[[package]] [[package]]
@ -2611,7 +2620,7 @@ checksum = "905e88c2a4cc27686bd57e495121d451f027e441388a67f773be729ad4be1ea8"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.72", "syn 2.0.74",
] ]
[[package]] [[package]]
@ -2739,34 +2748,35 @@ checksum = "b8dad83b4f25e74f184f64c43b150b91efe7647395b42289f38e50566d82855b"
[[package]] [[package]]
name = "wasm-bindgen" name = "wasm-bindgen"
version = "0.2.92" version = "0.2.93"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4be2531df63900aeb2bca0daaaddec08491ee64ceecbee5076636a3b026795a8" checksum = "a82edfc16a6c469f5f44dc7b571814045d60404b55a0ee849f9bcfa2e63dd9b5"
dependencies = [ dependencies = [
"cfg-if", "cfg-if",
"once_cell",
"wasm-bindgen-macro", "wasm-bindgen-macro",
] ]
[[package]] [[package]]
name = "wasm-bindgen-backend" name = "wasm-bindgen-backend"
version = "0.2.92" version = "0.2.93"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "614d787b966d3989fa7bb98a654e369c762374fd3213d212cfc0251257e747da" checksum = "9de396da306523044d3302746f1208fa71d7532227f15e347e2d93e4145dd77b"
dependencies = [ dependencies = [
"bumpalo", "bumpalo",
"log", "log",
"once_cell", "once_cell",
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.72", "syn 2.0.74",
"wasm-bindgen-shared", "wasm-bindgen-shared",
] ]
[[package]] [[package]]
name = "wasm-bindgen-futures" name = "wasm-bindgen-futures"
version = "0.4.42" version = "0.4.43"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "76bc14366121efc8dbb487ab05bcc9d346b3b5ec0eaa76e46594cabbe51762c0" checksum = "61e9300f63a621e96ed275155c108eb6f843b6a26d053f122ab69724559dc8ed"
dependencies = [ dependencies = [
"cfg-if", "cfg-if",
"js-sys", "js-sys",
@ -2776,9 +2786,9 @@ dependencies = [
[[package]] [[package]]
name = "wasm-bindgen-macro" name = "wasm-bindgen-macro"
version = "0.2.92" version = "0.2.93"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a1f8823de937b71b9460c0c34e25f3da88250760bec0ebac694b49997550d726" checksum = "585c4c91a46b072c92e908d99cb1dcdf95c5218eeb6f3bf1efa991ee7a68cccf"
dependencies = [ dependencies = [
"quote", "quote",
"wasm-bindgen-macro-support", "wasm-bindgen-macro-support",
@ -2786,22 +2796,22 @@ dependencies = [
[[package]] [[package]]
name = "wasm-bindgen-macro-support" name = "wasm-bindgen-macro-support"
version = "0.2.92" version = "0.2.93"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e94f17b526d0a461a191c78ea52bbce64071ed5c04c9ffe424dcb38f74171bb7" checksum = "afc340c74d9005395cf9dd098506f7f44e38f2b4a21c6aaacf9a105ea5e1e836"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.72", "syn 2.0.74",
"wasm-bindgen-backend", "wasm-bindgen-backend",
"wasm-bindgen-shared", "wasm-bindgen-shared",
] ]
[[package]] [[package]]
name = "wasm-bindgen-shared" name = "wasm-bindgen-shared"
version = "0.2.92" version = "0.2.93"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "af190c94f2773fdb3729c55b007a722abb5384da03bc0986df4c289bf5567e96" checksum = "c62a0a307cb4a311d3a07867860911ca130c3494e8c2719593806c08bc5d0484"
[[package]] [[package]]
name = "wasm-streams" name = "wasm-streams"
@ -2818,9 +2828,9 @@ dependencies = [
[[package]] [[package]]
name = "web-sys" name = "web-sys"
version = "0.3.69" version = "0.3.70"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "77afa9a11836342370f4817622a2f0f418b134426d91a82dfb48f532d2ec13ef" checksum = "26fdeaafd9bd129f65e7c031593c24d62186301e0c72c8978fa1678be7d532c0"
dependencies = [ dependencies = [
"js-sys", "js-sys",
"wasm-bindgen", "wasm-bindgen",
@ -2928,7 +2938,7 @@ checksum = "9107ddc059d5b6fbfbffdfa7a7fe3e22a226def0b2608f72e9d552763d3e1ad7"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.72", "syn 2.0.74",
] ]
[[package]] [[package]]
@ -2939,7 +2949,7 @@ checksum = "29bee4b38ea3cde66011baa44dba677c432a78593e202392d1e9070cf2a7fca7"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.72", "syn 2.0.74",
] ]
[[package]] [[package]]
@ -3146,7 +3156,7 @@ checksum = "fa4f8080344d4671fb4e831a13ad1e68092748387dfc4f55e356242fae12ce3e"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.72", "syn 2.0.74",
] ]
[[package]] [[package]]

View File

@ -1,6 +1,6 @@
[package] [package]
name = "kon" name = "kon"
version = "0.3.13" version = "0.3.14"
edition = "2021" edition = "2021"
[dependencies] [dependencies]
@ -13,8 +13,8 @@ once_cell = "1.19.0"
poise = "0.6.1" poise = "0.6.1"
regex = "1.10.6" regex = "1.10.6"
reqwest = { version = "0.12.5", features = ["json"] } reqwest = { version = "0.12.5", features = ["json"] }
serde = "1.0.205" serde = "1.0.207"
serde_json = "1.0.122" serde_json = "1.0.124"
sysinfo = "0.31.2" sysinfo = "0.31.2"
tokenservice-client = { version = "0.3.3", registry = "gitea" } tokenservice-client = { version = "0.3.3", registry = "gitea" }
tokio = { version = "1.39.2", features = ["macros", "signal", "rt-multi-thread"] } tokio = { version = "1.39.2", features = ["macros", "signal", "rt-multi-thread"] }

View File

@ -5,6 +5,8 @@ use reqwest::{
Error Error
}; };
const ERROR_PREFIX: &str = "HTTPClient[Error]:";
pub struct HttpClient(Client); pub struct HttpClient(Client);
impl HttpClient { impl HttpClient {
@ -13,14 +15,25 @@ impl HttpClient {
} }
pub async fn get(&self, url: &str, ua: &str) -> Result<Response, Error> { pub async fn get(&self, url: &str, ua: &str) -> Result<Response, Error> {
Ok( let response = self.0.get(url).header(
self.0.get(url).header(
reqwest::header::USER_AGENT, reqwest::header::USER_AGENT,
format!("Kon ({}) - {}/reqwest", super::utils::BOT_VERSION.as_str(), ua) format!("Kon ({}-{}) - {}/reqwest", super::utils::BOT_VERSION.as_str(), crate::GIT_COMMIT_HASH, ua)
) )
.timeout(Duration::from_secs(15)) .timeout(Duration::from_secs(30))
.send() .send()
.await? .await;
)
match response {
Ok(res) => Ok(res),
Err(y) if y.is_timeout() => {
eprintln!("{ERROR_PREFIX} Request timed out for \"{}\"", url);
Err(y)
},
Err(y) if y.is_connect() => {
eprintln!("{ERROR_PREFIX} Connection failed for \"{}\"", url);
Err(y)
},
Err(y) => Err(y)
}
} }
} }

View File

@ -108,6 +108,30 @@ async fn save_to_redis(key: &str, value: &str) -> Result<(), Error> {
Ok(()) Ok(())
} }
fn embed(
color: u32,
title: String,
url: String,
description: String,
timestamp: Timestamp
) -> CreateEmbed {
CreateEmbed::new()
.color(color)
.title(title)
.url(url)
.description(description)
.timestamp(timestamp)
}
const MAX_CONTENT_LENGTH: usize = 2048;
fn trim_old_content(s: &str) -> String {
if s.len() > MAX_CONTENT_LENGTH {
s[..MAX_CONTENT_LENGTH].to_string()
} else {
s.to_string()
}
}
async fn esxi_embed() -> Result<Option<CreateEmbed>, Error> { async fn esxi_embed() -> Result<Option<CreateEmbed>, Error> {
let redis = get_redis().await; let redis = get_redis().await;
let rkey = "RSS_ESXi"; let rkey = "RSS_ESXi";
@ -196,6 +220,22 @@ async fn gportal_embed() -> Result<Option<CreateEmbed>, Error> {
let cached_incident = redis.get(&rkey).await.unwrap().unwrap_or_default(); let cached_incident = redis.get(&rkey).await.unwrap().unwrap_or_default();
let new_content = format_html_to_discord(article.content.unwrap().body.unwrap()); let new_content = format_html_to_discord(article.content.unwrap().body.unwrap());
let mut color: u32 = 0x243C32;
let update_patt = Regex::new(r"(?i)\bupdate\b").unwrap();
let investigating_patt = Regex::new(r"(?i)\binvestigating\b").unwrap();
let monitoring_patt = Regex::new(r"(?i)\bmonitoring\b").unwrap();
let resolved_patt = Regex::new(r"(?i)\bresolved\b").unwrap();
if update_patt.is_match(&new_content) {
color = 0xFFAD33;
} else if investigating_patt.is_match(&new_content) {
color = 0x16AAEB;
} else if monitoring_patt.is_match(&new_content) {
color = 0x243C32;
} else if resolved_patt.is_match(&new_content) {
color = 0x57F287;
}
if cached_incident.is_empty() { if cached_incident.is_empty() {
redis.set(&rkey, &get_incident_id(&article.links[0].href).unwrap()).await.unwrap(); redis.set(&rkey, &get_incident_id(&article.links[0].href).unwrap()).await.unwrap();
redis.set(&rkey_content, &new_content).await.unwrap(); redis.set(&rkey_content, &new_content).await.unwrap();
@ -207,30 +247,30 @@ async fn gportal_embed() -> Result<Option<CreateEmbed>, Error> {
if let Some(incident) = get_incident_id(&article.links[0].href) { if let Some(incident) = get_incident_id(&article.links[0].href) {
if incident == cached_incident { if incident == cached_incident {
let cached_content: String = redis.get(&format!("{}_content", rkey)).await.unwrap().unwrap_or_default(); let cached_content: String = redis.get(&rkey_content).await.unwrap().unwrap_or_default();
if cached_content == new_content { if cached_content == new_content {
return Ok(None); return Ok(None);
} else { } else {
redis.set(&rkey_content, &new_content).await.unwrap(); redis.set(&rkey_content, &new_content).await.unwrap();
redis.expire(&rkey_content, 21600).await.unwrap(); redis.expire(&rkey_content, 21600).await.unwrap();
return Ok(Some(CreateEmbed::new() return Ok(Some(embed(
.color(0xC23EE8) color,
.title(article.title.unwrap().content) article.title.unwrap().content,
.url(incident_page) incident_page,
.description(new_content) new_content,
.timestamp(Timestamp::from(article.updated.unwrap())) Timestamp::from(article.updated.unwrap())
)); )));
} }
} else { } else {
save_to_redis(&rkey, &incident).await?; save_to_redis(&rkey, &incident).await?;
redis.set(&rkey_content, &new_content).await.unwrap(); redis.set(&rkey_content, &new_content).await.unwrap();
return Ok(Some(CreateEmbed::new() return Ok(Some(embed(
.color(0xC23EE8) color,
.title(article.title.unwrap().content) article.title.unwrap().content,
.url(incident_page) incident_page,
.description(new_content) new_content,
.timestamp(Timestamp::from(article.updated.unwrap())) Timestamp::from(article.updated.unwrap())
)); )));
} }
} else { } else {
task_err("RSS:GPortal", &format!("Incident ID does not match the expected RegEx pattern! ({})", &article.links[0].href)); task_err("RSS:GPortal", &format!("Incident ID does not match the expected RegEx pattern! ({})", &article.links[0].href));
@ -238,6 +278,85 @@ async fn gportal_embed() -> Result<Option<CreateEmbed>, Error> {
} }
} }
async fn github_embed() -> Result<Option<CreateEmbed>, Error> {
let redis = get_redis().await;
let rkey = "RSS_GitHub";
let rkey_content = format!("{}_Content", rkey);
let url = "https://www.githubstatus.com/history.atom";
let res = fetch_feed(url).await?;
let data = res.text().await?;
let cursor = Cursor::new(data);
let feed = parse(cursor).unwrap();
let incident_page = feed.links[0].clone().href;
let article = feed.entries[0].clone();
fn get_incident_id(input: &str) -> Option<String> {
let re = Regex::new(r#"/incidents/([a-zA-Z0-9]+)$"#).unwrap();
if let Some(caps) = re.captures(input) {
Some(caps[1].to_string())
} else {
None
}
}
let cached_incident = redis.get(&rkey).await.unwrap().unwrap_or_default();
let new_content = format_html_to_discord(article.content.unwrap().body.unwrap());
let mut color: u32 = 0x243C32;
let update_patt = Regex::new(r"(?i)\bupdate\b").unwrap();
let resolved_patt = Regex::new(r"(?i)\bresolved\b").unwrap();
if update_patt.is_match(&new_content) {
color = 0xFFAD33;
} else if resolved_patt.is_match(&new_content) {
color = 0x57F287;
}
if cached_incident.is_empty() {
redis.set(&rkey, &get_incident_id(&article.links[0].href).unwrap()).await.unwrap();
redis.set(&rkey_content, &new_content).await.unwrap();
if let Err(y) = redis.expire(&rkey, REDIS_EXPIRY_SECS).await {
task_err("RSS", format!("[RedisExpiry]: {}", y).as_str());
}
return Ok(None);
}
if let Some(incident) = get_incident_id(&article.links[0].href) {
if incident == cached_incident {
let cached_content: String = redis.get(&rkey_content).await.unwrap().unwrap_or_default();
if cached_content == new_content {
return Ok(None);
} else {
redis.set(&rkey_content, &new_content).await.unwrap();
redis.expire(&rkey_content, 21600).await.unwrap();
return Ok(Some(embed(
color,
article.title.unwrap().content,
incident_page,
trim_old_content(&new_content),
Timestamp::from(article.updated.unwrap())
)));
}
} else {
save_to_redis(&rkey, &incident).await?;
redis.set(&rkey_content, &new_content).await.unwrap();
return Ok(Some(embed(
color,
article.title.unwrap().content,
incident_page,
trim_old_content(&new_content),
Timestamp::from(article.updated.unwrap())
)));
}
} else {
task_err("RSS:GitHub", &format!("Incident ID does not match the expected RegEx pattern! ({})", &article.links[0].href));
Ok(None)
}
}
async fn rust_message() -> Result<Option<String>, Error> { async fn rust_message() -> Result<Option<String>, Error> {
let redis = get_redis().await; let redis = get_redis().await;
let rkey = "RSS_RustBlog"; let rkey = "RSS_RustBlog";
@ -281,7 +400,10 @@ async fn rust_message() -> Result<Option<String>, Error> {
pub async fn rss(ctx: Arc<Context>) -> Result<(), Error> { pub async fn rss(ctx: Arc<Context>) -> Result<(), Error> {
let task_name = "RSS"; let task_name = "RSS";
let mut interval = interval(Duration::from_secs(900)); #[cfg(feature = "production")]
let mut interval = interval(Duration::from_secs(300)); // Check feeds every 5 mins
#[cfg(not(feature = "production"))]
let mut interval = interval(Duration::from_secs(30)); // Check feeds every 30 secs
task_info(&task_name, "Task loaded!"); task_info(&task_name, "Task loaded!");
loop { loop {
@ -306,17 +428,77 @@ pub async fn rss(ctx: Arc<Context>) -> Result<(), Error> {
let channel = ChannelId::new(BINARY_PROPERTIES.rss_channel); let channel = ChannelId::new(BINARY_PROPERTIES.rss_channel);
// Check if the message ID is in Redis // Check if the message ID is in Redis
if let Ok(Some(msg_id_key)) = redis.get(&rkey).await { match redis.get(&rkey).await {
if let Ok(msg_id) = msg_id_key.parse::<u64>() { Ok(Some(msg_id_key)) => {
// Attempt to edit the message // Fetch the cached content
if let Ok(mut message) = channel.message(&ctx.http, msg_id).await { let cached_content: Option<String> = redis.get("RSS_GPortal_Content").await.unwrap_or(None);
message.edit(&ctx.http, EditMessage::new().embed(embed)).await.unwrap();
if let Ok(msg_id) = msg_id_key.parse::<u64>() {
// Attempt to edit the message
if let Ok(mut message) = channel.message(&ctx.http, msg_id).await {
let new_desc = message.embeds[0].description.clone().unwrap();
if cached_content.as_deref() != Some(&new_desc) {
message.edit(&ctx.http, EditMessage::new().embed(embed)).await.unwrap();
}
}
} else {
// If the message is invalid ID, send a new message instead
let message = channel.send_message(&ctx.http, CreateMessage::new()
.content("*Uh-oh! G-Portal is having issues!*").add_embed(embed)
).await.unwrap();
redis.set(&rkey, &message.id.to_string()).await.unwrap();
redis.expire(&rkey, 36000).await.unwrap();
} }
} else { },
// If the message is not found or invalid ID, send a new message instead Ok(None) | Err(_) => {
let message = channel.send_message(&ctx.http, CreateMessage::new() // If the message is not found, send a new message instead
.content("*Uh-oh! G-Portal is having issues!*").add_embed(embed) let message = channel.send_message(&ctx.http, CreateMessage::new()
).await.unwrap(); .content("*Uh-oh! G-Portal is having issues!*").add_embed(embed)
).await.unwrap();
redis.set(&rkey, &message.id.to_string()).await.unwrap();
redis.expire(&rkey, 36000).await.unwrap();
}
}
},
Ok(None) => (),
Err(y) => {
log_msgs.push(format!("**[RSS:GPortal:Error]:** Feed failed with the following error:```\n{}\n```", y));
task_err(&task_name, &y.to_string())
}
}
match github_embed().await {
Ok(Some(embed)) => {
let redis = get_redis().await;
let rkey = "RSS_GitHub_MsgID";
let channel = ChannelId::new(BINARY_PROPERTIES.rss_channel);
// Check if the message ID is in Redis
match redis.get(&rkey).await {
Ok(Some(msg_id_key)) => {
// Fetch the cached content
let cached_content: Option<String> = redis.get("RSS_GitHub_Content").await.unwrap_or(None);
if let Ok(msg_id) = msg_id_key.parse::<u64>() {
// Attempt to edit the message
if let Ok(mut message) = channel.message(&ctx.http, msg_id).await {
let new_desc = message.embeds[0].description.clone().unwrap();
if cached_content.as_deref() != Some(&new_desc) {
message.edit(&ctx.http, EditMessage::new().embed(embed)).await.unwrap();
}
}
} else {
// If the message is invalid ID, send a new message instead
let message = channel.send_message(&ctx.http, CreateMessage::new().add_embed(embed)).await.unwrap();
redis.set(&rkey, &message.id.to_string()).await.unwrap();
redis.expire(&rkey, 36000).await.unwrap();
}
},
Ok(None) | Err(_) => {
// If the message is not found, send a new message instead
let message = channel.send_message(&ctx.http, CreateMessage::new().add_embed(embed)).await.unwrap();
redis.set(&rkey, &message.id.to_string()).await.unwrap(); redis.set(&rkey, &message.id.to_string()).await.unwrap();
redis.expire(&rkey, 36000).await.unwrap(); redis.expire(&rkey, 36000).await.unwrap();
} }
@ -324,7 +506,7 @@ pub async fn rss(ctx: Arc<Context>) -> Result<(), Error> {
}, },
Ok(None) => (), Ok(None) => (),
Err(y) => { Err(y) => {
log_msgs.push(format!("**[RSS:GPortal:Error]:** Feed failed with the following error:```\n{}\n```", y)); log_msgs.push(format!("**[RSS:GitHub:Error]:** Feed failed with the following error:```\n{}\n```", y));
task_err(&task_name, &y.to_string()) task_err(&task_name, &y.to_string())
} }
} }