four entire seconds

This commit is contained in:
lda committed 2025-05-16 02:59:28 +07:00
1 parent 0b44c6afe2
commit c55867a6ee
3 files changed
+234 -23

No files matched your search

Generated
+163 -4
View File
@@ -239,6 +239,21 @@ dependencies = [
"alloc-no-stdlib",
]
[[package]]
name = "android-tzdata"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e999941b234f3131b00bc13c22d06e8c5ff726d1b6318ac7eb276997bbb4fef0"
[[package]]
name = "android_system_properties"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "819e7219dbd41043ac279b19830f2efc897156490d7fd6ea916720117ee66311"
dependencies = [
"libc",
]
[[package]]
name = "atomic-waker"
version = "1.1.2"
@@ -335,6 +350,8 @@ version = "0.1.0"
dependencies = [
"actix-cors",
"actix-web",
"icalendar",
"regex",
"reqwest",
"tokio",
]
@@ -356,6 +373,20 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "chrono"
version = "0.4.41"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c469d952047f47f91b68d1cba3f10d63c11d73e4636f24f08daf0278abf01c4d"
dependencies = [
"android-tzdata",
"iana-time-zone",
"js-sys",
"num-traits",
"wasm-bindgen",
"windows-link",
]
[[package]]
name = "cookie"
version = "0.16.2"
@@ -484,7 +515,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cea14ef9355e3beab063703aa9dab15afd25f0667c341310c1e5274bb1d0da18"
dependencies = [
"libc",
"windows-sys 0.52.0",
"windows-sys 0.59.0",
]
[[package]]
@@ -802,6 +833,43 @@ dependencies = [
"tracing",
]
[[package]]
name = "iana-time-zone"
version = "0.1.63"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b0c919e5debc312ad217002b8048a17b7d83f80703865bbfcfebb0458b0b27d8"
dependencies = [
"android_system_properties",
"core-foundation-sys",
"iana-time-zone-haiku",
"js-sys",
"log",
"wasm-bindgen",
"windows-core",
]
[[package]]
name = "iana-time-zone-haiku"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f31827a206f56af32e590ba56d5d2d085f558508192593743f16b2306495269f"
dependencies = [
"cc",
]
[[package]]
name = "icalendar"
version = "0.16.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ab45baec83621017cbc28632fbc4afdb7fb4ee59bdefc7e42eccdf3f9b02c2e4"
dependencies = [
"chrono",
"iso8601",
"nom",
"nom-language",
"uuid",
]
[[package]]
name = "icu_collections"
version = "2.0.0"
@@ -931,6 +999,15 @@ version = "2.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "469fb0b9cefa57e3ef31275ee7cacb78f2fdca44e4765491884a2b119d4eb130"
[[package]]
name = "iso8601"
version = "0.6.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c5c177cff824ab21a6f41079a4c401241c4e8be14f316c4c6b07d5fca351c98d"
dependencies = [
"nom",
]
[[package]]
name = "itoa"
version = "1.0.15"
@@ -1064,12 +1141,39 @@ dependencies = [
"tempfile",
]
[[package]]
name = "nom"
version = "8.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "df9761775871bdef83bee530e60050f7e54b1105350d6884eb0fb4f46c2f9405"
dependencies = [
"memchr",
]
[[package]]
name = "nom-language"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2de2bc5b451bfedaef92c90b8939a8fff5770bdcc1fafd6239d086aab8fa6b29"
dependencies = [
"nom",
]
[[package]]
name = "num-conv"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "51d515d32fb182ee37cda2ccdcb92950d6a3c2893aa280e540671c2cd0f3b1d9"
[[package]]
name = "num-traits"
version = "0.2.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841"
dependencies = [
"autocfg",
]
[[package]]
name = "object"
version = "0.36.7"
@@ -1372,7 +1476,7 @@ dependencies = [
"errno",
"libc",
"linux-raw-sys",
"windows-sys 0.52.0",
"windows-sys 0.59.0",
]
[[package]]
@@ -1636,7 +1740,7 @@ dependencies = [
"getrandom 0.3.3",
"once_cell",
"rustix",
"windows-sys 0.52.0",
"windows-sys 0.59.0",
]
[[package]]
@@ -1848,6 +1952,17 @@ version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b6c140620e7ffbb22c2dee59cafe6084a59b5ffc27a8859a5f0d494b5d52b6be"
[[package]]
name = "uuid"
version = "1.16.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "458f7a779bf54acc9f347480ac654f68407d3aab21269a6e3c9f922acd9e2da9"
dependencies = [
"getrandom 0.3.3",
"js-sys",
"wasm-bindgen",
]
[[package]]
name = "vcpkg"
version = "0.2.15"
@@ -1965,6 +2080,41 @@ dependencies = [
"wasm-bindgen",
]
[[package]]
name = "windows-core"
version = "0.61.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4763c1de310c86d75a878046489e2e5ba02c649d185f21c67d4cf8a56d098980"
dependencies = [
"windows-implement",
"windows-interface",
"windows-link",
"windows-result",
"windows-strings 0.4.0",
]
[[package]]
name = "windows-implement"
version = "0.60.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a47fddd13af08290e67f4acabf4b459f647552718f683a7b415d290ac744a836"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "windows-interface"
version = "0.59.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bd9211b69f8dcdfa817bfd14bf1c97c9188afa36f4750130fcdf3f400eca9fa8"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "windows-link"
version = "0.1.1"
@@ -1978,7 +2128,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4286ad90ddb45071efd1a66dfa43eb02dd0dfbae1545ad6cc3c51cf34d7e8ba3"
dependencies = [
"windows-result",
"windows-strings",
"windows-strings 0.3.1",
"windows-targets 0.53.0",
]
@@ -2000,6 +2150,15 @@ dependencies = [
"windows-link",
]
[[package]]
name = "windows-strings"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7a2ba9642430ee452d5a7aa78d72907ebe8cfda358e8cb7918a2050581322f97"
dependencies = [
"windows-link",
]
[[package]]
name = "windows-sys"
version = "0.52.0"
+3 -1
View File
@@ -7,5 +7,7 @@ publish = ["gitea"]
[dependencies]
actix-cors = "0.7"
actix-web = "4"
icalendar = "0.16.13"
regex = "1.11.1"
reqwest = {version = "0.12", features = ["blocking"]}
tokio = { version = "1.45.0", features = ["full"] }
tokio = { version = "1", features = ["full"] }
+68 -18
View File
@@ -1,27 +1,78 @@
use actix_cors::Cors;
use actix_web::{App, HttpResponse, HttpServer, Responder, get, http::header};
use icalendar::{
Calendar, CalendarComponent, Component,
parser::{read_calendar, read_calendar_simple},
};
use regex::Regex;
use reqwest::Client;
#[get("/fetch-ics")]
async fn fetch_ics() -> impl Responder {
println!("pinged");
let client = Client::new();
let ics_url = "https://calendar.google.com/calendar/ical/i7p4ol56gi8sqe0dq7uc3m0o4c%40group.calendar.google.com/public/basic.ics";
const ICS_URL: &str = "https://calendar.google.com/calendar/ical/i7p4ol56gi8sqe0dq7uc3m0o4c%40group.calendar.google.com/public/basic.ics";
const REGEX_PATTERN: &str = r"(CLASS 1|G1)";
match client.get(ics_url).send().await {
Ok(response) => {
if let Ok(body) = response.text().await {
HttpResponse::Ok()
.insert_header((header::CONTENT_TYPE, "text/calendar"))
.body(body)
} else {
HttpResponse::InternalServerError().body("Failed to read response body")
}
}
Err(_) => HttpResponse::BadGateway().body("Failed to fetch ICS file"),
#[get("/filtered-ics")]
async fn filtered_ics() -> impl Responder {
match fetch_and_filter_calendar(ICS_URL, REGEX_PATTERN).await {
Ok(ics_content) => HttpResponse::Ok()
.content_type("text/calendar")
.body(ics_content),
Err(err_msg) => HttpResponse::InternalServerError().body(err_msg),
}
}
async fn fetch_and_filter_calendar(url: &str, pattern: &str) -> Result<String, String> {
let body = fetch_ics_content(url).await?;
let calendar = parse_calendar(&body)?;
let filtered_calendar = filter_events(calendar, pattern)?;
Ok(serialize_calendar(filtered_calendar))
}
async fn fetch_ics_content(url: &str) -> Result<String, &str> {
let client = Client::new();
let response = client
.get(url)
.send()
.await
.map_err(|_| "Failed to fetch ICS file")?;
response
.text()
.await
.map_err(|_| "Failed to read response body")
}
fn parse_calendar(content: &str) -> Result<Calendar, &str> {
read_calendar(content)
.map(Calendar::from)
.map_err(|_| "Failed to parse ICS file")
}
fn filter_events(calendar: Calendar, pattern: &str) -> Result<Calendar, String> {
let summary_filter = Regex::new(pattern).map_err(|_| "Invalid regex pattern")?;
let filtered_components: Vec<CalendarComponent> = calendar
.components
.into_iter()
.filter_map(|component| match component {
CalendarComponent::Event(event)
if event
.get_summary()
.is_some_and(|s| summary_filter.is_match(s)) =>
{
Some(CalendarComponent::Event(event))
}
_ => None,
})
.collect();
Ok(Calendar {
components: filtered_components,
..Default::default()
})
}
fn serialize_calendar(calendar: Calendar) -> String {
calendar.to_string()
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(|| {
@@ -30,8 +81,7 @@ async fn main() -> std::io::Result<()> {
.allowed_methods(vec!["GET"])
.allowed_headers(vec![header::CONTENT_TYPE])
.max_age(3600);
App::new().wrap(cors).service(fetch_ics)
App::new().wrap(cors).service(filtered_ics)
})
.bind(("127.0.0.1", 8080))?
.run()