7 Commits

10 changed files with 582 additions and 446 deletions

863
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@@ -3,7 +3,7 @@ members = ["actix-macros", "actix-test", "providers"]
[package] [package]
name = "schedule-parser-rusted" name = "schedule-parser-rusted"
version = "1.2.2" version = "1.3.0"
edition = "2024" edition = "2024"
publish = false publish = false
@@ -21,7 +21,7 @@ actix-macros = { path = "actix-macros" }
actix-web = "4.11.0" actix-web = "4.11.0"
# basic # basic
chrono = { version = "0.4.41", features = ["serde"] } chrono = { version = "0.4.42", features = ["serde"] }
derive_more = { version = "2.0.1", features = ["full"] } derive_more = { version = "2.0.1", features = ["full"] }
dotenvy = "0.15.7" dotenvy = "0.15.7"
@@ -48,13 +48,13 @@ reqwest = { version = "0.12.23", features = ["json"] }
mime = "0.3.17" mime = "0.3.17"
# error handling # error handling
sentry = "0.42.0" sentry = "0.43.0"
sentry-actix = "0.42.0" sentry-actix = "0.43.0"
# [de]serializing # [de]serializing
serde = { version = "1.0.219", features = ["derive"] } serde = { version = "1", features = ["derive"] }
serde_json = "1.0.143" serde_json = "1"
serde_with = "3.14.0" serde_with = "3.14"
sha1 = "0.11.0-rc.2" sha1 = "0.11.0-rc.2"
@@ -65,12 +65,12 @@ utoipa-actix-web = "0.1.2"
uuid = { version = "1.18.1", features = ["v4"] } uuid = { version = "1.18.1", features = ["v4"] }
hex-literal = "1" hex-literal = "1"
log = "0.4.27" log = "0.4.28"
# telegram webdata deciding and verify # telegram webdata deciding and verify
base64 = "0.22.1" base64 = "0.22.1"
percent-encoding = "2.3.2" percent-encoding = "2.3.2"
ed25519-dalek = "3.0.0-pre.0" ed25519-dalek = "3.0.0-pre.1"
# development tracing # development tracing
console-subscriber = { version = "0.4.1", optional = true } console-subscriber = { version = "0.4.1", optional = true }

View File

@@ -100,6 +100,9 @@ pub enum LessonType {
/// Защита курсового проекта. /// Защита курсового проекта.
CourseProjectDefense, CourseProjectDefense,
/// Практическое занятие.
Practice
} }
#[derive(Clone, Hash, Debug, Serialize, Deserialize, ToSchema)] #[derive(Clone, Hash, Debug, Serialize, Deserialize, ToSchema)]

View File

@@ -1,6 +1,6 @@
[package] [package]
name = "provider-engels-polytechnic" name = "provider-engels-polytechnic"
version = "0.1.0" version = "0.2.0"
edition = "2024" edition = "2024"
[features] [features]
@@ -20,7 +20,7 @@ derive_more = { version = "2.0.1", features = ["error", "display"] }
utoipa = { version = "5.4.0", features = ["macros", "chrono"] } utoipa = { version = "5.4.0", features = ["macros", "chrono"] }
calamine = "0.30.0" calamine = "0.30"
async-trait = "0.1.89" async-trait = "0.1.89"
reqwest = "0.12.23" reqwest = "0.12.23"
@@ -28,5 +28,5 @@ ua_generator = "0.5.22"
regex = "1.11.2" regex = "1.11.2"
strsim = "0.11.1" strsim = "0.11.1"
log = "0.4.27" log = "0.4.27"
sentry = "0.42.0" sentry = "0.43.0"

View File

@@ -240,6 +240,7 @@ fn guess_lesson_type(text: &str) -> Option<LessonType> {
("экзамен", LessonType::ExamDefault), ("экзамен", LessonType::ExamDefault),
("курсовой проект", LessonType::CourseProject), ("курсовой проект", LessonType::CourseProject),
("защита курсового проекта", LessonType::CourseProjectDefense), ("защита курсового проекта", LessonType::CourseProjectDefense),
("практическое занятие", LessonType::Practice),
]) ])
}); });
@@ -447,7 +448,7 @@ fn parse_name_and_subgroups(text: &str) -> Result<ParsedLessonName, Error> {
static NAMES_REGEX: LazyLock<Regex> = LazyLock::new(|| { static NAMES_REGEX: LazyLock<Regex> = LazyLock::new(|| {
Regex::new( Regex::new(
r"(?:[А-Я][а-я]+\s?(?:[А-Я][\s.]*){2}(?:\(\s*\d\s*[а-я\s]+\))?(?:[\s,]+)?){1,2}+[\s.,]*", r"(?:[А-Я][а-я]+\s?(?:[А-Я][\s.]*){2}(?:\(?\s*\d\s*[а-я\s]+\)?)?(?:[\s,.]+)?){1,2}+[\s.,]*",
) )
.unwrap() .unwrap()
}); });
@@ -456,7 +457,7 @@ fn parse_name_and_subgroups(text: &str) -> Result<ParsedLessonName, Error> {
static CLEAN_RE: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"[\s\n\t]+").unwrap()); static CLEAN_RE: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"[\s\n\t]+").unwrap());
let text = CLEAN_RE let text = CLEAN_RE
.replace(&text.replace([' ', '\t', '\n'], " "), " ") .replace(&text.replace([' ', '\t', '\n'], " ").replace(",", ""), " ")
.to_string(); .to_string();
let (lesson_name, subgroups, lesson_type) = match NAMES_REGEX.captures(&text) { let (lesson_name, subgroups, lesson_type) = match NAMES_REGEX.captures(&text) {
@@ -470,13 +471,15 @@ fn parse_name_and_subgroups(text: &str) -> Result<ParsedLessonName, Error> {
let mut subgroups: [Option<LessonSubGroup>; 2] = [None, None]; let mut subgroups: [Option<LessonSubGroup>; 2] = [None, None];
for name in src.split(',') { for name in src.split(',') {
let open_bracket_index = name.find('('); let digit_index = name.find(|c: char| c.is_ascii_digit());
let number: u8 = open_bracket_index let number: u8 =
.map_or(0, |index| name[(index + 1)..(index + 2)].parse().unwrap()); digit_index.map_or(0, |index| name[(index)..(index + 1)].parse().unwrap());
let teacher_name = { let teacher_name = {
let name_end = open_bracket_index.unwrap_or(name.len()); let name_end = name
.find(|c: char| !c.is_alphabetic())
.unwrap_or(name.len());
// Я ебал. Как же я долго до этого доходил. // Я ебал. Как же я долго до этого доходил.
format!( format!(
@@ -525,7 +528,7 @@ fn parse_name_and_subgroups(text: &str) -> Result<ParsedLessonName, Error> {
if result.is_none() { if result.is_none() {
#[cfg(not(debug_assertions))] #[cfg(not(debug_assertions))]
sentry::capture_message( sentry::capture_message(
&*format!("Не удалось угадать тип пары '{}'!", extra), &format!("Не удалось угадать тип пары '{}'!", extra),
sentry::Level::Warning, sentry::Level::Warning,
); );

View File

@@ -46,14 +46,17 @@ pub mod error {
/// problems with the Yandex Cloud Function invocation. /// problems with the Yandex Cloud Function invocation.
#[display("An error occurred during the request to the Yandex Cloud API: {_0}")] #[display("An error occurred during the request to the Yandex Cloud API: {_0}")]
RequestFailed(reqwest::Error), RequestFailed(reqwest::Error),
#[display("Unable to fetch Uri in 3 retries")]
UriFetchFailed,
} }
/// Errors that may occur during the creation of a schedule snapshot. /// Errors that may occur during the creation of a schedule snapshot.
#[derive(Debug, Display, Error)] #[derive(Debug, Display, Error)]
pub enum SnapshotCreationError { pub enum SnapshotCreationError {
/// The URL is the same as the one already being used (no update needed). /// The ETag is the same (no update needed).
#[display("The URL is the same as the one already being used.")] #[display("The ETag is the same.")]
SameUrl, Same,
/// The URL query for the XLS file failed to execute, either due to network issues or invalid API parameters. /// The URL query for the XLS file failed to execute, either due to network issues or invalid API parameters.
#[display("Failed to fetch URL: {_0}")] #[display("Failed to fetch URL: {_0}")]
@@ -86,10 +89,6 @@ impl Updater {
downloader: &mut XlsDownloader, downloader: &mut XlsDownloader,
url: String, url: String,
) -> Result<ScheduleSnapshot, SnapshotCreationError> { ) -> Result<ScheduleSnapshot, SnapshotCreationError> {
if downloader.url.as_ref().is_some_and(|_url| _url.eq(&url)) {
return Err(SnapshotCreationError::SameUrl);
}
let head_result = downloader.set_url(&url).await.map_err(|error| { let head_result = downloader.set_url(&url).await.map_err(|error| {
if let FetchError::Unknown(error) = &error { if let FetchError::Unknown(error) = &error {
sentry::capture_error(&error); sentry::capture_error(&error);
@@ -98,6 +97,10 @@ impl Updater {
SnapshotCreationError::FetchFailed(error) SnapshotCreationError::FetchFailed(error)
})?; })?;
if downloader.etag == Some(head_result.etag) {
return Err(SnapshotCreationError::Same);
}
let xls_data = downloader let xls_data = downloader
.fetch(false) .fetch(false)
.await .await
@@ -144,18 +147,43 @@ impl Updater {
async fn query_url(api_key: &str, func_id: &str) -> Result<String, QueryUrlError> { async fn query_url(api_key: &str, func_id: &str) -> Result<String, QueryUrlError> {
let client = reqwest::Client::new(); let client = reqwest::Client::new();
let uri = client let uri = {
.post(format!( // вот бы добавили named-scopes как в котлине,
"https://functions.yandexcloud.net/{}?integration=raw", // чтоб мне не пришлось такой хуйнёй страдать.
func_id #[allow(unused_assignments)]
)) let mut uri = String::new();
.header("Authorization", format!("Api-Key {}", api_key)) let mut counter = 0;
.send()
.await loop {
.map_err(QueryUrlError::RequestFailed)? if counter == 3 {
.text() return Err(QueryUrlError::UriFetchFailed);
.await }
.map_err(QueryUrlError::RequestFailed)?;
counter += 1;
uri = client
.post(format!(
"https://functions.yandexcloud.net/{}?integration=raw",
func_id
))
.header("Authorization", format!("Api-Key {}", api_key))
.send()
.await
.map_err(QueryUrlError::RequestFailed)?
.text()
.await
.map_err(QueryUrlError::RequestFailed)?;
if uri.is_empty() {
log::warn!("[{}] Unable to get uri! Retrying in 5 seconds...", counter);
continue;
}
break;
}
uri
};
Ok(format!("https://politehnikum-eng.ru{}", uri.trim())) Ok(format!("https://politehnikum-eng.ru{}", uri.trim()))
} }
@@ -249,7 +277,7 @@ impl Updater {
let snapshot = match Self::new_snapshot(&mut self.downloader, url).await { let snapshot = match Self::new_snapshot(&mut self.downloader, url).await {
Ok(snapshot) => snapshot, Ok(snapshot) => snapshot,
Err(SnapshotCreationError::SameUrl) => { Err(SnapshotCreationError::Same) => {
let mut clone = current_snapshot.clone(); let mut clone = current_snapshot.clone();
clone.update(); clone.update();

View File

@@ -66,25 +66,30 @@ pub struct FetchOk {
/// Date data received. /// Date data received.
pub requested_at: DateTime<Utc>, pub requested_at: DateTime<Utc>,
/// Etag.
pub etag: String,
/// File data. /// File data.
pub data: Option<Vec<u8>>, pub data: Option<Vec<u8>>,
} }
impl FetchOk { impl FetchOk {
/// Result without file content. /// Result without file content.
pub fn head(uploaded_at: DateTime<Utc>) -> Self { pub fn head(uploaded_at: DateTime<Utc>, etag: String) -> Self {
FetchOk { FetchOk {
uploaded_at, uploaded_at,
requested_at: Utc::now(), requested_at: Utc::now(),
etag,
data: None, data: None,
} }
} }
/// Full result. /// Full result.
pub fn get(uploaded_at: DateTime<Utc>, data: Vec<u8>) -> Self { pub fn get(uploaded_at: DateTime<Utc>, etag: String, data: Vec<u8>) -> Self {
FetchOk { FetchOk {
uploaded_at, uploaded_at,
requested_at: Utc::now(), requested_at: Utc::now(),
etag,
data: Some(data), data: Some(data),
} }
} }
@@ -94,11 +99,15 @@ pub type FetchResult = Result<FetchOk, FetchError>;
pub struct XlsDownloader { pub struct XlsDownloader {
pub url: Option<String>, pub url: Option<String>,
pub etag: Option<String>,
} }
impl XlsDownloader { impl XlsDownloader {
pub fn new() -> Self { pub fn new() -> Self {
XlsDownloader { url: None } XlsDownloader {
url: None,
etag: None,
}
} }
async fn fetch_specified(url: &str, head: bool) -> FetchResult { async fn fetch_specified(url: &str, head: bool) -> FetchResult {
@@ -124,9 +133,12 @@ impl XlsDownloader {
.get("Content-Type") .get("Content-Type")
.ok_or(FetchError::bad_headers("Content-Type"))?; .ok_or(FetchError::bad_headers("Content-Type"))?;
if !headers.contains_key("etag") { let etag = headers
return Err(FetchError::bad_headers("etag")); .get("etag")
} .ok_or(FetchError::bad_headers("etag"))?
.to_str()
.or(Err(FetchError::bad_headers("etag")))?
.to_string();
let last_modified = headers let last_modified = headers
.get("last-modified") .get("last-modified")
@@ -141,9 +153,13 @@ impl XlsDownloader {
.with_timezone(&Utc); .with_timezone(&Utc);
Ok(if head { Ok(if head {
FetchOk::head(last_modified) FetchOk::head(last_modified, etag)
} else { } else {
FetchOk::get(last_modified, response.bytes().await.unwrap().to_vec()) FetchOk::get(
last_modified,
etag,
response.bytes().await.unwrap().to_vec(),
)
}) })
} }

View File

@@ -2,16 +2,6 @@ use jsonwebtoken::errors::ErrorKind;
use jsonwebtoken::{Algorithm, DecodingKey, Validation, decode}; use jsonwebtoken::{Algorithm, DecodingKey, Validation, decode};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
#[derive(Deserialize, Serialize)]
struct TokenData {
iis: String,
sub: i32,
app: i32,
exp: i32,
iat: i32,
jti: i32,
}
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
struct Claims { struct Claims {
sub: i32, sub: i32,

View File

@@ -185,7 +185,7 @@ mod tests {
id: Set(id.clone()), id: Set(id.clone()),
username: Set(username), username: Set(username),
password: Set(Some( password: Set(Some(
bcrypt::hash("example".to_string(), bcrypt::DEFAULT_COST).unwrap(), bcrypt::hash("example", bcrypt::DEFAULT_COST).unwrap(),
)), )),
vk_id: Set(None), vk_id: Set(None),
telegram_id: Set(None), telegram_id: Set(None),

View File

@@ -24,14 +24,13 @@ static ENCODING_KEY: LazyLock<EncodingKey> = LazyLock::new(|| {
}); });
/// Token verification errors. /// Token verification errors.
#[allow(dead_code)]
#[derive(Debug)] #[derive(Debug)]
pub enum Error { pub enum Error {
/// The token has a different signature. /// The token has a different signature.
InvalidSignature, InvalidSignature,
/// Token reading error. /// Token reading error.
InvalidToken(ErrorKind), InvalidToken,
/// Token expired. /// Token expired.
Expired, Expired,
@@ -82,7 +81,7 @@ pub fn verify_and_decode(token: &str) -> Result<String, Error> {
Err(err) => Err(match err.into_kind() { Err(err) => Err(match err.into_kind() {
ErrorKind::InvalidSignature => Error::InvalidSignature, ErrorKind::InvalidSignature => Error::InvalidSignature,
ErrorKind::ExpiredSignature => Error::Expired, ErrorKind::ExpiredSignature => Error::Expired,
kind => Error::InvalidToken(kind), _ => Error::InvalidToken,
}), }),
} }
} }
@@ -115,7 +114,7 @@ mod tests {
fn test_encode() { fn test_encode() {
test_env(); test_env();
assert_eq!(encode(&"test".to_string()).is_empty(), false); assert!(!encode("test").is_empty());
} }
#[test] #[test]
@@ -128,7 +127,7 @@ mod tests {
assert!(result.is_err()); assert!(result.is_err());
assert_eq!( assert_eq!(
result.err().unwrap(), result.err().unwrap(),
Error::InvalidToken(ErrorKind::InvalidToken) Error::InvalidToken
); );
} }