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
use html_extractor::*;

/// Gets the url of Bing wallpaper.
///
/// # Example
/// ```
/// # #[tokio::main]
/// # async fn main() {
/// let url = bing_wallpaper_url::get_url().await.unwrap();
/// println!("{}", url);
/// # }
/// ```
pub async fn get_url() -> Result<String, Error> {
    let html = reqwest::get("https://www.bing.com/").await?.text().await?;
    let bing = BingTopPage::extract_from_str(&html)?;
    match &*bing.wallpaper_url {
        "#" => Err(Error::NotAvailable),
        url => Ok(format!(
            "https://www.bing.com{}",
            url.replace("qlt=50", "qlt=100")
        )),
    }
}
html_extractor! {
    BingTopPage {
        wallpaper_url: String = (attr["href"] of "#preloadBg"),
    }
}

#[derive(Debug)]
pub enum Error {
    Network(reqwest::Error),
    InvalidResponse(html_extractor::Error),
    NotAvailable,
}
impl From<reqwest::Error> for Error {
    fn from(e: reqwest::Error) -> Self {
        Error::Network(e)
    }
}
impl From<html_extractor::Error> for Error {
    fn from(e: html_extractor::Error) -> Self {
        Error::InvalidResponse(e)
    }
}