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 ureq::{get, Error as ReqError};
8use html_escape::{decode_html_entities};
9use percent_encoding::{percent_encode, NON_ALPHANUMERIC};
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).to_string()
41                .replace("\\n", "\n")
42                .replace("\\t", "\t")
43                .replace("\\r", "\r")
44}
45
46/// Applies basic encoding for use as an argument for a URL
47/// 
48/// ## Example:
49/// ```
50/// use ug_scraper::network::encode_string;
51/// 
52/// let encoded_string: String = encode_string("This is an example & \"");
53/// // Returns:
54/// // "This%20is%20an%20example%20&amp;%20&quot;"
55/// ```
56pub fn encode_string(string: &str) -> String {
57        //encode_double_quoted_attribute(string).to_string().replace(" ", "%20")
58        percent_encode(string.as_bytes(), NON_ALPHANUMERIC).to_string()
59}