#image #search #google #async #google-api

image_search

一个基于提供的参数搜索Google图片的crate

10个版本

0.4.5 2024年4月11日
0.4.4 2024年4月7日
0.4.3 2023年4月28日
0.4.2 2023年3月16日
0.2.2 2022年12月19日

#1253 in 异步

Download history 2/week @ 2024-04-18 6/week @ 2024-05-16 4/week @ 2024-05-23 1/week @ 2024-06-06 1/week @ 2024-06-20

637 每月下载量

MIT许可证

41KB
757 代码行

Google图片搜索

Crates.io docs.rs Crates.io

一个基于提供的参数搜索Google图片的crate。由于仅使用单个请求获取图片的限制,每次请求最多只能找到约100张图片。这些图片可能受版权保护,因此您不应该对它们进行任何可受惩罚的操作,如用于商业用途。

参数

有2个必需参数,以及各种不同的参数。

参数 类型 描述
query &str 要搜索的关键词。
limit usize 要获取的最大图片数量。不能获取超过100张。
thumbnails bool 使urlsdownloads函数使用缩略图的URL而不是图片的URL。
timeout Option<Duration> 设置download函数的超时时间。不建议设置为None,因为在极少数情况下,图片可能无法下载但不会抛出错误,导致download函数永远不会返回。
directory Option<PathBuf>

搜索参数

这些是可选参数,Google可以使用这些参数过滤图片,有助于缩小搜索范围。它们通过Arguments结构体的各种方法使用。每个参数都包含在一个包含所有可能选项的enum中。

参数 选项 描述
颜色 RedOrangeYellowGreenTealBluePurplePinkWhiteGrayBlackBrown 通过主要颜色过滤图片。
ColorType ColorGrayscaleTransparent 通过颜色类型过滤图像。
许可证 CreativeCommonsOther 通过使用许可证过滤图像。
类型 FacePhotoClipartLineartAnimated 通过要搜索的图像类型进行过滤。
时间 DayWeekMonthYear 只查找指定时间内发布的图像。
长宽比 TallSquareWidePanoramic 指定图像的长宽比。
格式 JpgGifPngBmpSvgWebpIcoRaw 过滤掉指定格式的图像。如果您想以特定格式下载图像,请使用 download_format 参数。

示例

使用异步 API 需要某种异步运行时,通常是 tokio,这可以像这样添加到您的 Cargo.toml

[dependencies]
image_search = "0.4"
tokio = { version = "1", features = ["full"] }

它可以这样使用

extern crate tokio;
extern crate image_search;

use std::path::PathBuf;
use image_search::{Arguments, Color, urls, search, download};
 
#[tokio::main]
async fn main() -> Result<(), image_search::Error> {
    let args = Arguments::new("example", 10)
        .color(Color::Gray)
        .directory(PathBuf::from("downloads")); // Only affects the download function
     
    let _image_urls = urls(args.clone()).await?;
    let _images = search(args.clone()).await?;
    let _paths = download(args).await?;
 
    Ok(())
}

阻塞

有一个可选的 "阻塞" API 可以启用

[dependencies]
image_search = { version = "0.4", features = ["blocking"] }

它可以这样调用

extern crate image_search;

use std::path::PathBuf;
use image_search::{Arguments, Time, blocking::{urls, search, download}};

fn main() -> Result<(), image_search::Error> {
    let args = Arguments::new("example", 10)
        .time(Time::Month)
        .directory(PathBuf::from("downloads")); // Only affects the download function
    
    let _image_urls = urls(args.clone())?;
    let _images = search(args.clone())?;
    let _paths = download(args)?;

    Ok(())
}

客户端

这个crate使用 surf 来进行 HTTP 请求,以便允许自定义用于 HTTP 请求的客户端。这可以使程序通过 CURL 与 C 交互,通过 hyperasync-h1 以纯 Rust 交互,甚至 WASM。与 surf 一样,可以通过功能自定义使用的客户端。为了更改,您必须在 Cargo.toml 中设置 default-features=false,因为默认使用 curl。可能的后端在此列出

  • curl:通过 isahc 使用 CURL 通过 CURL 作为 HTTP 后端。
  • hyper (默认):使用 hyper 作为 HTTP 后端。
  • wasm:使用 window.fetch 作为 HTTP 后端。
  • h1:使用 async-h1 作为具有原生 TLS 的 HTTPS 的 HTTP 后端。
  • rustls:使用 async-h1 作为 HTTP 后端,并使用 rustls 进行 HTTPS。

依赖关系

~10–27MB
~463K SLoC