ug_scraper/
network.rs

1// UG-Scraper - A basic rust API for getting data from Ultimate Guitar
2// Copyright (C) 2025  Linus Tibert
3//
4// This program was originally published under the MIT licence as seen
5// here: https://github.com/Lich-Corals/ug-tab-scraper-rs/blob/mistress/LICENCE
6
7use html_escape::decode_html_entities;
8use percent_encoding::{percent_encode, NON_ALPHANUMERIC};
9use ureq::{get, Error as ReqError};
10
11/// Returns the raw HTML of a given URL wraped in an Result.
12///
13/// ## Example:
14/// ```
15/// use ug_scraper::network::get_raw_html;
16///
17/// let raw_html: String = get_raw_html("https://tabs.ultimate-guitar.com/tab/rick-astley/never-gonna-give-you-up-chords-521741").unwrap();
18/// ```
19///
20/// ## Possible errors
21/// * `ureq::Error::*`
22pub fn get_raw_html(url: &str) -> Result<String, ReqError> {
23        let mut response = get(url).call()?;
24        let raw_html = response.body_mut().read_to_string()?;
25        Ok(raw_html)
26}
27
28/// Returns a String with common escaped characters unescaped.
29///
30/// ## Example:
31/// ```
32/// use ug_scraper::network::unescape_string;
33///
34/// let escaped_string: &str = "This\\t is a tab.";
35/// let clean_string: String = unescape_string(escaped_string);
36/// // Returns:
37/// // "This     is a tab"
38/// ```
39pub fn unescape_string(string: &str) -> String {
40        decode_html_entities(string)
41                .to_string()
42                .replace("\\n", "\n")
43                .replace("\\t", "\t")
44                .replace("\\r", "\r")
45                .replace("\\\"", "\"")
46}
47
48/// Applies basic encoding for use as an argument for a URL
49///
50/// ## Example:
51/// ```
52/// use ug_scraper::network::encode_string;
53///
54/// let encoded_string: String = encode_string("This is an example & \"");
55/// // Returns:
56/// // "This%20is%20an%20example%20&amp;%20&quot;"
57/// ```
58pub fn encode_string(string: &str) -> String {
59        //encode_double_quoted_attribute(string).to_string().replace(" ", "%20")
60        percent_encode(string.as_bytes(), NON_ALPHANUMERIC).to_string()
61}