1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
use scraper::{Html, Selector};
use crate::api::vtop::types::GetFaculty;
pub fn parse_faculty_search(html: String) -> GetFaculty {
let document = Html::parse_document(&html);
let rows_selector = Selector::parse("tr").unwrap();
// Skip header row and find the data row
for row in document.select(&rows_selector).skip(1) {
let cells: Vec<_> = row.select(&Selector::parse("td").unwrap()).collect();
if cells.len() >= 4 {
// Extract employee ID from button onclick attribute
let button_selector = Selector::parse("button").unwrap();
let emp_id = if let Some(button) = row.select(&button_selector).next() {
if let Some(onclick) = button.value().attr("onclick") {
// Extract ID from onclick - handle both " and regular quotes
let extracted_id = if onclick.contains(""") {
onclick
.split(""")
.nth(1)
.unwrap_or("")
.to_string()
} else if onclick.contains('"') {
// Handle regular quotes: getEmployeeIdNo("30058");
onclick
.split('"')
.nth(1)
.unwrap_or("")
.to_string()
} else {
// Fallback: extract numbers from the string
onclick
.chars()
.filter(|c| c.is_ascii_digit())
.collect::<String>()
};
// println!("Extracted emp_id from onclick: {}, ID: {}", onclick, extracted_id);
extracted_id
} else if let Some(id) = button.value().attr("id") {
id.to_string()
} else {
String::new()
}
} else {
String::new()
};
return GetFaculty {
faculty_name: cells[0]
.text()
.collect::<Vec<_>>()
.join("")
.trim()
.replace("\t", "")
.replace("\n", ""),
designation: cells[1]
.text()
.collect::<Vec<_>>()
.join("")
.trim()
.replace("\t", "")
.replace("\n", ""),
school_or_centre: cells[2]
.text()
.collect::<Vec<_>>()
.join("")
.trim()
.replace("\t", "")
.replace("\n", ""),
emp_id,
};
}
}
// Return empty faculty if no data found
GetFaculty {
faculty_name: String::new(),
designation: String::new(),
school_or_centre: String::new(),
emp_id: String::new(),
}
}