This commit is contained in:
Dominic Grimm 2023-02-22 17:40:39 +01:00
parent a177f2a5d4
commit 027aad58f9
No known key found for this signature in database
GPG key ID: 6F294212DEAAC530
6 changed files with 110 additions and 90 deletions

View file

@ -1,10 +1,11 @@
use anyhow::{bail, Context, Result};
use anyhow::{Context, Result};
use celery::error::TaskError;
use celery::task::TaskResult;
use chrono::prelude::*;
use diesel::prelude::*;
use lazy_static::lazy_static;
use r2d2_redis::redis;
use regex::Regex;
use std::ops::DerefMut;
use std::thread;
use std::time::Duration;
@ -269,16 +270,19 @@ async fn fetch_holidays(
Ok(())
}
lazy_static! {
static ref TITLE_SELECTOR: scraper::Selector = scraper::Selector::parse(".mon_title").unwrap();
}
async fn fetch_substitutions(
client: &untis::Client,
db_conn: &mut PgConnection,
redis_conn: &mut cache::ConnectionPool,
schoolyear_id: i32,
) -> Result<()> {
lazy_static! {
static ref TITLE_SELECTOR: scraper::Selector =
scraper::Selector::parse(".mon_title").unwrap();
static ref DATE_REGEX: Regex = Regex::new(r"([^\s]+)").unwrap();
static ref WEEK_TYPE_REGEX: Regex = Regex::new(r"\bWoche\s+\K\S+").unwrap();
}
let (date, week_type) = {
let html = reqwest::Client::new()
.get(&config::CONFIG.untis_vplan_url)
@ -302,30 +306,36 @@ async fn fetch_substitutions(
.context("No element in vplan html which is selectable class \"mon_title\"")?
.text()
.next()
.context("\"mon_title\" element is empty")?
.split_once(',')
.context("Could not split \"mon_title\" string")?;
.context("\"mon_title\" element is empty")?;
dbg!(title);
dbg!(DATE_REGEX.captures(title));
dbg!(WEEK_TYPE_REGEX.captures(title));
(
chrono::NaiveDate::parse_from_str(
title
.0
.split_whitespace()
.next()
.context("Could not find date")?,
"%d.%m.%Y",
)?,
match title
.1
.split_whitespace()
.last()
.context("Could not find week type indicator")?
{
"A" => db::models::WeekType::A,
"B" => db::models::WeekType::B,
x => bail!("Invalid week type: {:?}", x),
},
chrono::NaiveDate::parse_from_str("18.1.2023", "%d.%m.%Y")?,
db::models::WeekType::A,
)
// (
// chrono::NaiveDate::parse_from_str(
// title
// .0
// .split_whitespace()
// .next()
// .context("Could not find date")?,
// "%d.%m.%Y",
// )?,
// match title
// .1
// .split_whitespace()
// .last()
// .context("Could not find week type indicator")?
// {
// "A" => db::models::WeekType::A,
// "B" => db::models::WeekType::B,
// x => bail!("Invalid week type: {:?}", x),
// },
// )
};
let substitution_query_id = diesel::insert_into(db::schema::substitution_queries::table)