forvolib/
lib.rs

1/*
2 * Author(s):   Francesco Urbani
3 * Date:        some evening between January-Februrary 2021
4 */
5
6use regex::Regex;
7mod tests;
8
9
10/// Pass a word to this function and return a list of URLs you can use (i.e. visit/download) to 
11/// listen its pronunciation on [Forvo](https://forvo.com/).
12pub fn retrieve_audios(word: &str) -> Result<Vec<String>, std::io::Error> {
13
14    let url = format!("https://forvo.com/search/{}/", word);
15
16    let content = reqwest::get(&url.to_string())
17                    .expect("Could not make request.")
18                    .text()
19                    .expect("Could not read text.");
20    
21    // println!("{}", content);
22
23    /*
24    let regex_num_results_found = Regex::new(r"(>)(\d+)( words found)").unwrap();
25    for caps in regex_num_results_found.captures_iter(content.as_str()) {
26        let num_results = caps.get(2).unwrap().as_str();
27        println!("{} words found.", num_results);
28        println!("\tPress enter (↵) to play them, one after another.");
29        println!("\tPress ^C (ctrl-C / cmd-C) to quit prematurely.");
30    }
31    */
32
33    let mut pronunciations = vec![];
34
35    let regex_sequence_pattern = Regex::new(r"(Play\(\w+,')(\w+=*)").unwrap(); 
36    for caps in regex_sequence_pattern.captures_iter(content.as_str()) {
37        let code_sequence = caps.get(2).unwrap().as_str();
38
39        pronunciations.push(code_sequence.to_string());
40
41    }
42
43    Ok(pronunciations)
44}