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

View File

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

View File

@@ -1,6 +1,6 @@
[package]
name = "provider-engels-polytechnic"
version = "0.1.0"
version = "0.2.0"
edition = "2024"
[features]
@@ -20,7 +20,7 @@ derive_more = { version = "2.0.1", features = ["error", "display"] }
utoipa = { version = "5.4.0", features = ["macros", "chrono"] }
calamine = "0.30.0"
calamine = "0.30"
async-trait = "0.1.89"
reqwest = "0.12.23"
@@ -28,5 +28,5 @@ ua_generator = "0.5.22"
regex = "1.11.2"
strsim = "0.11.1"
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::CourseProject),
("защита курсового проекта", 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(|| {
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()
});
@@ -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());
let text = CLEAN_RE
.replace(&text.replace([' ', '\t', '\n'], " "), " ")
.replace(&text.replace([' ', '\t', '\n'], " ").replace(",", ""), " ")
.to_string();
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];
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
.map_or(0, |index| name[(index + 1)..(index + 2)].parse().unwrap());
let number: u8 =
digit_index.map_or(0, |index| name[(index)..(index + 1)].parse().unwrap());
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!(
@@ -525,7 +528,7 @@ fn parse_name_and_subgroups(text: &str) -> Result<ParsedLessonName, Error> {
if result.is_none() {
#[cfg(not(debug_assertions))]
sentry::capture_message(
&*format!("Не удалось угадать тип пары '{}'!", extra),
&format!("Не удалось угадать тип пары '{}'!", extra),
sentry::Level::Warning,
);

View File

@@ -46,14 +46,17 @@ pub mod error {
/// problems with the Yandex Cloud Function invocation.
#[display("An error occurred during the request to the Yandex Cloud API: {_0}")]
RequestFailed(reqwest::Error),
#[display("Unable to fetch Uri in 3 retries")]
UriFetchFailed,
}
/// Errors that may occur during the creation of a schedule snapshot.
#[derive(Debug, Display, Error)]
pub enum SnapshotCreationError {
/// The URL is the same as the one already being used (no update needed).
#[display("The URL is the same as the one already being used.")]
SameUrl,
/// The ETag is the same (no update needed).
#[display("The ETag is the same.")]
Same,
/// 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}")]
@@ -86,10 +89,6 @@ impl Updater {
downloader: &mut XlsDownloader,
url: String,
) -> 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| {
if let FetchError::Unknown(error) = &error {
sentry::capture_error(&error);
@@ -98,6 +97,10 @@ impl Updater {
SnapshotCreationError::FetchFailed(error)
})?;
if downloader.etag == Some(head_result.etag) {
return Err(SnapshotCreationError::Same);
}
let xls_data = downloader
.fetch(false)
.await
@@ -144,7 +147,21 @@ impl Updater {
async fn query_url(api_key: &str, func_id: &str) -> Result<String, QueryUrlError> {
let client = reqwest::Client::new();
let uri = client
let uri = {
// вот бы добавили named-scopes как в котлине,
// чтоб мне не пришлось такой хуйнёй страдать.
#[allow(unused_assignments)]
let mut uri = String::new();
let mut counter = 0;
loop {
if counter == 3 {
return Err(QueryUrlError::UriFetchFailed);
}
counter += 1;
uri = client
.post(format!(
"https://functions.yandexcloud.net/{}?integration=raw",
func_id
@@ -157,6 +174,17 @@ impl Updater {
.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()))
}
@@ -249,7 +277,7 @@ impl Updater {
let snapshot = match Self::new_snapshot(&mut self.downloader, url).await {
Ok(snapshot) => snapshot,
Err(SnapshotCreationError::SameUrl) => {
Err(SnapshotCreationError::Same) => {
let mut clone = current_snapshot.clone();
clone.update();

View File

@@ -66,25 +66,30 @@ pub struct FetchOk {
/// Date data received.
pub requested_at: DateTime<Utc>,
/// Etag.
pub etag: String,
/// File data.
pub data: Option<Vec<u8>>,
}
impl FetchOk {
/// Result without file content.
pub fn head(uploaded_at: DateTime<Utc>) -> Self {
pub fn head(uploaded_at: DateTime<Utc>, etag: String) -> Self {
FetchOk {
uploaded_at,
requested_at: Utc::now(),
etag,
data: None,
}
}
/// 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 {
uploaded_at,
requested_at: Utc::now(),
etag,
data: Some(data),
}
}
@@ -94,11 +99,15 @@ pub type FetchResult = Result<FetchOk, FetchError>;
pub struct XlsDownloader {
pub url: Option<String>,
pub etag: Option<String>,
}
impl XlsDownloader {
pub fn new() -> Self {
XlsDownloader { url: None }
XlsDownloader {
url: None,
etag: None,
}
}
async fn fetch_specified(url: &str, head: bool) -> FetchResult {
@@ -124,9 +133,12 @@ impl XlsDownloader {
.get("Content-Type")
.ok_or(FetchError::bad_headers("Content-Type"))?;
if !headers.contains_key("etag") {
return Err(FetchError::bad_headers("etag"));
}
let etag = headers
.get("etag")
.ok_or(FetchError::bad_headers("etag"))?
.to_str()
.or(Err(FetchError::bad_headers("etag")))?
.to_string();
let last_modified = headers
.get("last-modified")
@@ -141,9 +153,13 @@ impl XlsDownloader {
.with_timezone(&Utc);
Ok(if head {
FetchOk::head(last_modified)
FetchOk::head(last_modified, etag)
} 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 serde::{Deserialize, Serialize};
#[derive(Deserialize, Serialize)]
struct TokenData {
iis: String,
sub: i32,
app: i32,
exp: i32,
iat: i32,
jti: i32,
}
#[derive(Debug, Serialize, Deserialize)]
struct Claims {
sub: i32,

View File

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

View File

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