?query=whatever

This commit is contained in:
lda committed 2025-05-18 00:18:08 +07:00
1 parent 3ca6797a06
commit 52f1a602cf
3 files changed
+54 -32

No files matched your search

Generated
+27 -1
View File
@@ -287,6 +287,21 @@ version = "0.22.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6"
[[package]]
name = "bit-set"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "08807e080ed7f9d5433fa9b275196cfc35414f66a0c79d864dc51a0d825231a3"
dependencies = [
"bit-vec",
]
[[package]]
name = "bit-vec"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5e764a1d40d510daf35e07be9eb06e75770908c27d411ee6c92109c9840eaaf7"
[[package]]
name = "bitflags"
version = "2.9.1"
@@ -351,8 +366,8 @@ dependencies = [
"actix-cors",
"actix-web",
"chrono",
"fancy-regex",
"icalendar",
"regex",
"reqwest",
"tokio",
]
@@ -519,6 +534,17 @@ dependencies = [
"windows-sys 0.59.0",
]
[[package]]
name = "fancy-regex"
version = "0.14.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6e24cb5a94bcae1e5408b0effca5cd7172ea3c5755049c5f3af4cd283a165298"
dependencies = [
"bit-set",
"regex-automata",
"regex-syntax",
]
[[package]]
name = "fastrand"
version = "2.3.0"
+1 -1
View File
@@ -8,7 +8,7 @@ publish = ["gitea"]
actix-cors = "0.7"
actix-web = "4"
chrono = "0.4.41"
fancy-regex = "0.14.0"
icalendar = "0.16.13"
regex = "1.11.1"
reqwest = {version = "0.12", features = ["blocking"]}
tokio = { version = "1", features = ["full"] }
+26 -30
View File
@@ -1,20 +1,25 @@
use std::collections::HashMap;
use actix_cors::Cors;
use actix_web::{App, HttpResponse, HttpServer, Responder, get, http::header, web};
use chrono::{NaiveDate, TimeDelta};
use fancy_regex::{self, Regex};
use icalendar::{Calendar, CalendarComponent, Component};
use reqwest::Client;
mod funny_timer;
// use regex::Regex;
const ICS_URL: &str = "https://calendar.google.com/calendar/ical/i7p4ol56gi8sqe0dq7uc3m0o4c%40group.calendar.google.com/public/basic.ics";
#[get("/filtered-ics")]
async fn filtered_ics(now: web::Data<NaiveDate>) -> impl Responder {
async fn filtered_ics(
now: web::Data<NaiveDate>,
query: web::Query<HashMap<String, String>>,
) -> impl Responder {
let cumah = funny_timer::LdaTimer::new("total time idfk");
let now: NaiveDate = **now;
let client = Client::new();
let ics_url = ICS_URL;
let ics_url = query.get("url").map_or(ICS_URL, String::as_str);
let timer = tokio::time::Instant::now();
// Fetch the .ics content
@@ -49,9 +54,17 @@ async fn filtered_ics(now: web::Data<NaiveDate>) -> impl Responder {
let start_date = now - TimeDelta::days(365);
let end_date = now + TimeDelta::days(365);
let regex_rules = query
.get("regex")
.and_then(|r| Regex::new(r).inspect_err(|e| eprintln!("{e}")).ok()); // silently discard a faulty regex
// or
// let include = query.get("").map(|c|c.split(",")); // ?
// let exclude = query.get("").map(|c|c.split(",")); // ?
// Filter events by summary and date range
let timer = tokio::time::Instant::now();
let filtered_events = filter_events_by_date_and_regex(calendar, start_date, end_date);
let filtered_events =
filter_events_by_date_and_regex(calendar, regex_rules, start_date, end_date);
println!("filtering took {:?}", timer.elapsed());
cumah.ping("function time after filtering");
@@ -75,7 +88,7 @@ async fn filtered_ics(now: web::Data<NaiveDate>) -> impl Responder {
fn filter_events_by_date_and_regex(
calendar: Calendar,
// pattern: &str,
pattern: Option<Regex>,
start_date: NaiveDate,
end_date: NaiveDate,
) -> Vec<CalendarComponent> {
@@ -96,7 +109,14 @@ fn filter_events_by_date_and_regex(
let summary = event.get_summary()?;
if summary.contains("CLASS 2") || (2..=6).any(|c| summary.contains(&format!("G{c}"))) {
let kill = if let Some(reg) = &pattern {
reg.is_match(summary).inspect_err(|e|eprintln!("{e}")).unwrap_or(false)
// where is the
} else {
summary.contains("CLASS 2") || (2..=8).any(|c| summary.contains(&format!("G{c}")))
};
if kill {
// not perfect...
None
} else {
@@ -106,30 +126,6 @@ fn filter_events_by_date_and_regex(
.collect()
}
// #[allow(dead_code)]
// 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()
// })
// }
#[actix_web::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(|| {