Skip to content

Commit f64b220

Browse files
authored
Merge pull request #23 from jczaja/prv-fix
Updated code to new HTML format
2 parents 8628029 + 4c779f8 commit f64b220

File tree

3 files changed

+3319
-15
lines changed

3 files changed

+3319
-15
lines changed

data/test_data-3.txt

+3,286
Large diffs are not rendered by default.

src/main.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use macroquad::prelude::*;
2-
use std::cell::RefCell;
32
use std::sync::mpsc;
43

54
#[macroquad::main("Train-Helper")]

src/skm.rs

+33-14
Original file line numberDiff line numberDiff line change
@@ -44,25 +44,44 @@ pub mod skm {
4444
&pattern_slice[0..id_offset_end]
4545
}
4646

47-
fn get_message(&self, body: &str, station: &str) -> String {
47+
fn get_message(&self, body: &str, station: &str, curr_time : chrono::NaiveTime) -> String {
4848
// We connstruct search pattern to get remaining time. for example:
4949
// Najbliższa kolejka za</p>
5050
//<h3 class="no-print">28 min</h3>
5151

5252
let search_phrase = "Najbl".to_string();
5353
let return_string: String = match body.find(&search_phrase) {
5454
Some(start_offset) => {
55-
let pattern_slice = &body[start_offset..start_offset + 400]; // 400 characters should be enough
56-
// find first two "dd min"
55+
let pattern_slice = &body[start_offset..];
56+
57+
// Find a first time right after "timetable-shown"
5758
let mut next_train_minutes: String = "".to_owned();
58-
Regex::new(r"[0-9]+\s[m][i][n]")
59+
60+
// Match exacly phrase "timetable-shown" (three times)
61+
Regex::new(r"timetable-shown(.|\n){0,300}")
5962
.unwrap()
60-
.find_iter(pattern_slice)
61-
.for_each(|x| {
62-
next_train_minutes += x.as_str();
63-
next_train_minutes += ", "
63+
.find_iter(pattern_slice).take(3)
64+
.for_each(|roi| {
65+
66+
Regex::new(r"[0-9]+[:][0-9]+") // for example 14:04
67+
.unwrap()
68+
.find_iter(roi.as_str())
69+
.for_each(|x| {
70+
71+
// lets convert 14:04 into NaiveTime and subtract curr_time from it
72+
let nt = chrono::NaiveTime::parse_from_str(x.as_str(),"%H:%M").expect("Unable to parse time into NaiveDate");
73+
74+
75+
let remaining_time = nt.signed_duration_since(curr_time).num_minutes();
76+
77+
next_train_minutes += &format!("{remaining_time}");
78+
next_train_minutes += " min, "
79+
});
6480
});
6581

82+
83+
84+
6685
" (".to_string() + station + " --> ) departs in " + &next_train_minutes
6786
}
6887
None => "No connections today".to_owned(),
@@ -117,7 +136,7 @@ pub mod skm {
117136
messages
118137
.borrow_mut()
119138
.push((msg_prefix.to_owned(), order_number << 1));
120-
let mystring = self.get_message(&actual_response, &x[0]);
139+
let mystring = self.get_message(&actual_response, &x[0],chrono::Local::now().time());
121140
messages
122141
.borrow_mut()
123142
.push((mystring, (order_number << 1) + 1));
@@ -195,7 +214,7 @@ pub mod skm {
195214
#[test]
196215
fn test_parsing_message() -> GenericResult<()> {
197216
// Let's read data to parse from stored file
198-
let mut file = std::fs::File::open("data/test_data.txt")?;
217+
let mut file = std::fs::File::open("data/test_data-3.txt")?;
199218

200219
let mut s = String::new();
201220
file.read_to_string(&mut s)?;
@@ -205,14 +224,14 @@ pub mod skm {
205224
None,
206225
vec![(
207226
vec![
208-
"Gdansk Wrzeszcz".to_string(),
209227
"Gdansk Port Lotniczy".to_string(),
228+
"Gdansk Wrzeszcz".to_string(),
210229
],
211230
format!("Train to work:"),
212231
)],
213232
)
214-
.get_message(&s, "Gdansk Wrzeszcz");
215-
let expected_response = " (Gdansk Wrzeszcz --> ) departs in 16 min, 26 min, 80 min, ";
233+
.get_message(&s, "Gdansk Wrzeszcz", chrono::NaiveTime::from_hms(7,47,0));
234+
let expected_response = " (Gdansk Wrzeszcz --> ) departs in 16 min, 22 min, 37 min, ";
216235
assert_eq!(response, expected_response);
217236
Ok(())
218237
}
@@ -236,7 +255,7 @@ pub mod skm {
236255
format!("Train to work "),
237256
)],
238257
)
239-
.get_message(&s, "Gdansk Wrzeszcz");
258+
.get_message(&s, "Gdansk Wrzeszcz",chrono::NaiveTime::from_hms(13,48,0));
240259
let expected_response = "No connections today";
241260
assert_eq!(response, expected_response);
242261
Ok(())

0 commit comments

Comments
 (0)