@@ -44,25 +44,44 @@ pub mod skm {
44
44
& pattern_slice[ 0 ..id_offset_end]
45
45
}
46
46
47
- fn get_message ( & self , body : & str , station : & str ) -> String {
47
+ fn get_message ( & self , body : & str , station : & str , curr_time : chrono :: NaiveTime ) -> String {
48
48
// We connstruct search pattern to get remaining time. for example:
49
49
// Najbliższa kolejka za</p>
50
50
//<h3 class="no-print">28 min</h3>
51
51
52
52
let search_phrase = "Najbl" . to_string ( ) ;
53
53
let return_string: String = match body. find ( & search_phrase) {
54
54
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"
57
58
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}" )
59
62
. 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
+ } ) ;
64
80
} ) ;
65
81
82
+
83
+
84
+
66
85
" (" . to_string ( ) + station + " --> ) departs in " + & next_train_minutes
67
86
}
68
87
None => "No connections today" . to_owned ( ) ,
@@ -117,7 +136,7 @@ pub mod skm {
117
136
messages
118
137
. borrow_mut ( )
119
138
. 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 ( ) ) ;
121
140
messages
122
141
. borrow_mut ( )
123
142
. push ( ( mystring, ( order_number << 1 ) + 1 ) ) ;
@@ -195,7 +214,7 @@ pub mod skm {
195
214
#[ test]
196
215
fn test_parsing_message ( ) -> GenericResult < ( ) > {
197
216
// 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" ) ?;
199
218
200
219
let mut s = String :: new ( ) ;
201
220
file. read_to_string ( & mut s) ?;
@@ -205,14 +224,14 @@ pub mod skm {
205
224
None ,
206
225
vec ! [ (
207
226
vec![
208
- "Gdansk Wrzeszcz" . to_string( ) ,
209
227
"Gdansk Port Lotniczy" . to_string( ) ,
228
+ "Gdansk Wrzeszcz" . to_string( ) ,
210
229
] ,
211
230
format!( "Train to work:" ) ,
212
231
) ] ,
213
232
)
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, " ;
216
235
assert_eq ! ( response, expected_response) ;
217
236
Ok ( ( ) )
218
237
}
@@ -236,7 +255,7 @@ pub mod skm {
236
255
format!( "Train to work " ) ,
237
256
) ] ,
238
257
)
239
- . get_message ( & s, "Gdansk Wrzeszcz" ) ;
258
+ . get_message ( & s, "Gdansk Wrzeszcz" , chrono :: NaiveTime :: from_hms ( 13 , 48 , 0 ) ) ;
240
259
let expected_response = "No connections today" ;
241
260
assert_eq ! ( response, expected_response) ;
242
261
Ok ( ( ) )
0 commit comments