#favicon #website #url #picker #reqwest #blocking #default

favicon-picker

用于从网站获取favicon的库

1 个稳定版本

1.0.0 2024年4月2日

#1454网页编程

Download history 202/week @ 2024-04-14 71/week @ 2024-04-21 269/week @ 2024-04-28 14/week @ 2024-05-05 41/week @ 2024-05-19 66/week @ 2024-05-26 23/week @ 2024-06-02 439/week @ 2024-06-23 103/week @ 2024-06-30 102/week @ 2024-07-07 235/week @ 2024-07-14 139/week @ 2024-07-21 191/week @ 2024-07-28

667 每月下载量

自定义许可协议

29KB
207

favicon-picker

Crates.io Documentation Crates.io (recent)

这是一个允许您获取网站favicon的小型库。

这个库与site_icons类似,但我只想有一个简单灵活的库,用于各种边项目。

用法

示例

简单

use favicon_picker::get_favicons_from_url;
use reqwest::{header::{HeaderMap, HeaderValue, USER_AGENT}, Client};
use url::Url;

#[tokio::main(flavor = "current_thread")]
async fn main() -> anyhow::Result<()> {
    let client = Client::builder().default_headers({
        // the `User-Agent` is not required here
        let mut headers = HeaderMap::new();
        headers.append(USER_AGENT, HeaderValue::from_str("favicon-picker/1.0.0")?);
        headers
    }).build()?;
    let base_url = Url::parse("https://comic-walker.com/")?;
    for favs in get_favicons_from_url(&client, &base_url).await.map_err(|e| anyhow::Error::msg(e.to_string()))? {
        // I just printed the value to the standard output
        println!("{:#?}", favs);
    }
    Ok(())
}

下载

use std::{
    fs::{create_dir_all, File},
    io::{BufWriter, Write},
    path::Path,
};

use favicon_picker::get_favicons_from_url;
use reqwest::{
    header::{HeaderMap, HeaderValue, USER_AGENT},
    Client,
};
use url::Url;

#[tokio::main(flavor = "current_thread")]
async fn main() -> anyhow::Result<()> {
    let dowload_path = Path::new("output");
    create_dir_all(dowload_path)?;
    let client = Client::builder()
        .default_headers({
            // the `User-Agent` is not required here
            let mut headers = HeaderMap::new();
            headers.append(USER_AGENT, HeaderValue::from_str("favicon-picker/1.0.0")?);
            headers
        })
        .build()?;
    let base_url = Url::parse("https://comic-walker.com/")?;
    /// get existing favicons on the page
    for favs in get_favicons_from_url(&client, &base_url)
        .await
        .map_err(|e| anyhow::Error::msg(e.to_string()))?
    {
        // i just need the favicon file name here
        let file_name = favs
            .href
            .path_segments()
            .ok_or_else(|| anyhow::Error::msg("cannot be base"))?
            .last()
            .ok_or_else(|| anyhow::Error::msg("can't get the last url fragments"))?;
        let filepath = dowload_path.join(file_name);
        let data = favs.get_image_bytes(&client).await?;
        let mut file = BufWriter::new(File::create(filepath)?);
        file.write_all(&data)?;
        file.flush()?;
    }
    Ok(())
}

它是如何工作的 ??

当你调用favicon_picker::get_favicons_from_url时,它将只获取并解析现有的HTML页面。然后它会查找现有的<link rel='icon'/>,然后就这样。

如果没有找到favicon,它会给你默认的favicon url,即http://<some-website-domain>/favicon.ico

功能标志

  • non_exhaustive:将使Favicon结构体具有#[non_exhaustive] 默认启用
  • serde:允许Favicon结构体使用serde框架进行序列化和反序列化
  • blocking:允许您使用reqwest::blocking::Client。如果您不想使用async/await,则很有用

贡献

欢迎所有pull request!

许可协议

GNU通用公共许可证版本3

依赖项

~6–17MB
~259K SLoC