Giter Club home page Giter Club logo

favicon-picker's Introduction

favicon-picker

Crates.io Crates.io (recent)

This is just a small library that allows you to get websites favicons.

This library is similar to site_icons but i just wanted something simple and flexible to use for various side-project.

Usage

Just add favicon-picker into your cargo dependencies or run:

cargo add favicon-picker

Example

Simple

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(())
}

Download

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(())
}

How does it work ??

When you call the favicon_picker::get_favicons_from_url, it will just fetch, and parse the existing HTML page. Then it will lookup to existing <link rel='icon'/> and that's it.

If it finds no favicon, it will give you the default favicon url which is http://<some-website-domain>/favicon.ico

Feature flags

  • non_exhaustive: Will make the Favicon struct #[non_exhaustive] Enabled by default
  • serde: Allows the Favicon struct to be serialized and deserialized with the serde framework
  • blocking: Allows you to use the reqwest::blocking::Client. Useful if you don't want to use async/await

Contribution

All pull request are welcome!!

License

GNU GENERAL PUBLIC LICENSE version 3

favicon-picker's People

Contributors

tonymushah avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.