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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
|
use actix_cors::Cors;
use actix_web::{get, post, web, App, HttpServer, Responder};
use rand::Rng;
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet};
use std::hash::{Hash, Hasher};
use std::sync::{Arc, Mutex};
#[derive(Debug, Clone, Serialize)]
struct CrawledResource {
url: String,
title: String,
description: String,
priority: u32,
word: Arc<String>,
}
//We implement PartialEq, Eq and Hash to ignore the priority field.
impl PartialEq for CrawledResource {
fn eq(&self, other: &Self) -> bool {
self.url == other.url && self.word == other.word
}
}
impl Eq for CrawledResource {}
impl Hash for CrawledResource {
fn hash<H: Hasher>(&self, state: &mut H) {
self.url.hash(state);
self.word.hash(state);
}
}
struct AppState {
database: Mutex<HashMap<String, HashSet<CrawledResource>>>,
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
println!("Hello, world! Im the indexer!");
serve_http_endpoint("0.0.0.0", 4444).await
}
async fn serve_http_endpoint(address: &str, port: u16) -> std::io::Result<()> {
let shared_state = web::Data::new(AppState {
database: Mutex::new(HashMap::new()),
});
HttpServer::new(move || {
let cors = Cors::permissive();
App::new()
.wrap(cors)
.app_data(shared_state.clone())
.service(no_search)
.service(search)
.service(add_resource)
})
.bind((address, port))?
.run()
.await
}
//we need to rename stuff
#[derive(Deserialize, Debug)]
struct Resource {
url: String,
content: String,
}
#[post("/resource")]
async fn add_resource(data: web::Data<AppState>, resource: web::Json<Resource>) -> impl Responder {
//parse content
let document = scraper::Html::parse_document(resource.content.as_str());
let text = html2text::from_read(resource.content.as_str().as_bytes(), resource.content.len());
let split_words = text.split(' ');
//fixup words (remove words with non alphabetic chars, empty words, transform to lowercase...)
let fixed_words: Vec<String> = split_words
.filter(|w| !w.chars().any(|c| !c.is_ascii_alphabetic()))
.filter(|w| !w.is_empty() && *w != " ")
.map(|w| w.to_ascii_lowercase())
.collect();
println!("xd: {:?}", fixed_words);
let title_selector = scraper::Selector::parse("title").unwrap();
let description_selector = scraper::Selector::parse("meta").unwrap();
let page_title: String = document
.select(&title_selector)
.map(|e| e.inner_html())
.take(1)
.collect();
let page_description: String = document
.select(&description_selector)
.filter(|e| e.value().attr("name") == Some("description"))
.filter_map(|e| e.value().attr("content"))
.take(1)
.collect();
//and for each changed content word we add it to the db (word -> list.append(url))
let mut database = data.database.lock().unwrap();
for word in fixed_words {
let resource_to_add = CrawledResource {
url: resource.url.clone(),
priority: calculate_word_priority(&word, resource.content.as_str()),
word: Arc::new(word.clone()),
title: page_title.clone(),
description: page_description.clone(),
};
match database.get_mut(&word) {
Some(resources) => _ = resources.insert(resource_to_add),
None => _ = database.insert(word.clone(), HashSet::from([resource_to_add])),
}
}
println!("Added resource! {:?}", database.len());
format!("{:?}", resource)
}
#[get("/search")]
async fn no_search(_data: web::Data<AppState>) -> impl Responder {
"[]".to_string()
}
#[get("/search/{term}")]
async fn search(data: web::Data<AppState>, term: web::Path<String>) -> impl Responder {
let query: Vec<&str> = term.split(' ').collect();
let database = data.database.lock().unwrap();
//percentage of valid words
let mut valid_results: Option<HashSet<CrawledResource>> = None;
for w in query {
let curr_word_results = match search_word_in_db(&database, w.to_string()) {
None => return "[]".to_string(),
Some(curr_results) => curr_results,
};
match valid_results {
//Initialise valid_results
None => {
valid_results = Some(curr_word_results.to_owned());
}
Some(results) => {
let intersection: HashSet<CrawledResource> = curr_word_results
.intersection(&results)
.map(|s| s.to_owned())
.collect();
valid_results = Some(intersection);
}
}
}
serde_json::to_string(&valid_results.unwrap()).unwrap()
}
fn search_word_in_db(
db: &HashMap<String, HashSet<CrawledResource>>,
word: String,
) -> Option<&HashSet<CrawledResource>> {
db.get(&word)
}
//TODO!
fn calculate_word_priority(_word: &str, _html_site: &str) -> u32 {
rand::thread_rng().gen::<u32>()
}
|