This repository has been archived on 2025-01-30. You can view files and clone it, but cannot push or open issues or pull requests.
site_icons/src/lib.rs

72 lines
1.6 KiB
Rust
Raw Normal View History

2021-01-30 19:12:13 +00:00
//! # site_icons
//! An efficient website icon scraper.
//!
//! ## Usage
//! ```rust
//! use site_icons::Icons;
//!
2022-12-24 18:16:04 +01:00
//! async fn run() {
2022-05-04 13:56:48 +03:00
//! let mut icons = Icons::new();
2021-01-30 19:12:13 +00:00
//! // scrape the icons from a url
2022-12-24 18:16:04 +01:00
//! icons.load_website("https://github.com").await.unwrap();
2021-01-30 19:12:13 +00:00
//!
//! // fetch all icons, ensuring they exist & determining size
//! let entries = icons.entries().await;
//!
//! // entries are sorted from highest to lowest resolution
//! for icon in entries {
2022-05-04 13:56:48 +03:00
//! println!("{:?}", icon)
2021-01-30 19:12:13 +00:00
//! }
2022-12-24 18:16:04 +01:00
//! }
2021-01-30 19:12:13 +00:00
//! ```
2021-01-29 12:23:15 +00:00
#[macro_use]
extern crate serde_with;
#[macro_use]
extern crate log;
2021-01-30 19:12:13 +00:00
#[macro_use]
mod macros;
2021-01-29 12:23:15 +00:00
mod icon;
mod icon_info;
mod icon_size;
mod icons;
2021-01-29 16:26:04 +00:00
mod utils;
2021-01-29 12:23:15 +00:00
pub use icon::*;
pub use icon_info::*;
pub use icons::*;
use once_cell::sync::Lazy;
use reqwest::{
header::{HeaderMap, HeaderValue, USER_AGENT},
Client,
};
static CLIENT: Lazy<Client> = Lazy::new(|| {
let mut headers = HeaderMap::new();
headers.insert(USER_AGENT, HeaderValue::from_str("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.104 Safari/537.36").unwrap());
2022-07-24 21:14:34 +01:00
Client::builder().default_headers(headers).build().unwrap()
2021-01-29 12:23:15 +00:00
});
2022-05-06 16:40:25 +03:00
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn test_icons() {
let mut icons = Icons::new();
// scrape the icons from a url
icons.load_website("https://github.com").await.unwrap();
// fetch all icons, ensuring they exist & determining size
let entries = icons.entries().await;
// entries are sorted from highest to lowest resolution
for icon in &entries {
println!("{:?}", icon)
}
assert_eq!(entries.len() > 0, true);
}
}