Giter Club home page Giter Club logo

image_search's Introduction

Google Image Search

Crates.io docs.rs Crates.io

A crate designed to search Google Images based on provided arguments. Due to the limitations of using only a single request to fetch images, only a max of about 100 images can be found per request. These images may be protected under copyright, and you shouldn't do anything punishable with them, like using them for commercial use.

Arguments

There are 2 required parameters, along with a variety of different arguments.

Argument Type Description
query &str The keyword(s) to search for.
limit usize The maximum amount of images to fetch. Cannot fetch more than 100.
thumbnails bool Causes the urls and downloads functions to use the urls of the thumbnails instead of the urls of the images.
timeout Option<Duration> Sets the timeout for the download function. Setting to None is not recommended, since in rare cases images can fail to download but not throw an error, causing the download function to never return.
directory Option<PathBuf>

Search Arguments

These are optional arguments that Google can use to filter images, useful for narrowing your search. They are used via the various methods on the Arguments struct. Each argument is contained in an enum which contains all possible options.

Argument Options Description
Color Red, Orange, Yellow, Green, Teal, Blue, Purple, Pink, White, Gray, Black, Brown Filter images by the dominant color.
ColorType Color, Grayscale, Transparent Filter images by the color type.
License CreativeCommons, Other Filter images by the usage license.
Type Face, Photo, Clipart, Lineart, Animated Filters by the type of images to search for.
Time Day, Week, Month, Year Only finds images posted in the time specified.
AspectRatio Tall, Square, Wide, Panoramic Specifies the aspect ratio of the images.
Format Jpg, Gif, Png, Bmp, Svg, Webp, Ico, Raw Filters out images that are not a specified format. If you would like to download images as a specific format, use the download_format argument instead.

Examples

Using the asynchronous API requires some sort of async runtime, usually tokio, which can be added to your Cargo.toml like so:

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

It can be used like this:

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

Blocking

There is an optional "blocking" API that can be enabled:

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

This is called like so:

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

Clients

This crate uses surf for HTTP requests in order to allow for the customization of the client used for HTTP requests. This can allow programs to interface with C via CURL, pure Rust via hyper or async-h1, or even WASM. As with surf, the client used can be customized via features. In order to change you will have to set default-features=false in your Cargo.toml, since curl is used by default. The possible backends are listed here:

  • curl: Uses CURL through isahc as the HTTP backend.
  • hyper (default): Uses hyper as the HTTP backend.
  • wasm: Uses window.fetch as the HTTP backend.
  • h1: Uses async-h1 as the HTTP backend with native TLS for HTTPS.
  • rustls: Uses async-h1 as the HTTP backend with rustls for HTTPS.

image_search's People

Contributors

commonkestrel avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar

image_search's Issues

The dependencies used seems to use a very old version of tokio, preventing to use with a more recent one

Hi,

I am new to rust, but I tried to use your crates to download some pictures from google images (is there a way to pick a random one ?).

But my problem is that in my dependencies I am already have tokio as version 1.26.0.

When starting a simple image search (async) like in your sample, I have this error :

thread 'main' panicked at 'there is no timer running, must be called from the context of a Tokio 0.2.x runtime', /home/ixo/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-0.2.25/src/time/driver/handle.rs:24:32

If I use cargo tree, I see that :

├── image_search v0.4.1
│ ├── async-std v1.12.0
│ │ ├── async-channel v1.8.0
│ │ │ ├── concurrent-queue v2.1.0
│ │ │ │ └── crossbeam-utils v0.8.15 ()
│ │ │ ├── event-listener v2.5.3
│ │ │ └── futures-core v0.3.26
│ │ ├── async-global-executor v2.3.1
│ │ │ ├── async-channel v1.8.0 (
)
│ │ │ ├── async-executor v1.5.0
│ │ │ │ ├── async-lock v2.7.0
│ │ │ │ │ └── event-listener v2.5.3
│ │ │ │ ├── async-task v4.3.0
│ │ │ │ ├── concurrent-queue v2.1.0 (*)
│ │ │ │ .... snip ....
│ │ │ ├── once_cell v1.17.1
│ │ │ └── tokio v0.2.25
...

What can we do ?

Broken image fetch

Hello!

Currently I'm getting the error:

Unable to parse images from json. Google may have changed the way their data is stored

As the message says, the json format probably changed and the package is unable to fetch the urls.

Google reply format change ?

Hi,

since the v0.4.2 I can now use this crate, thanks, but it seems that the way Google is now replying prevent for it to works :

11:54:08 [DEBUG] (52) hyper::client::connect::dns: resolving host="www.google.com"
11:54:08 [DEBUG] (48) hyper::client::connect::http: connecting to 142.250.179.68:443
11:54:08 [DEBUG] (48) hyper::client::connect::http: connected to 142.250.179.68:443
11:54:08 [DEBUG] (26) hyper::proto::h1::io: flushed 217 bytes
11:54:08 [DEBUG] (26) hyper::proto::h1::io: read 2764 bytes
11:54:08 [DEBUG] (26) hyper::proto::h1::io: parsed 23 headers
11:54:08 [DEBUG] (26) hyper::proto::h1::conn: incoming body is empty
11:54:08 [DEBUG] (50) hyper::client::pool: pooling idle connection for ("https", www.google.com)
Error: Unable to parse images from json. Google may have changed the way their data is stored

Is it just me ?

Parse Error In the examples provided in docs.rs

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

fn main() -> Result<(), image_search::Error> {
let args = Arguments::new("example", 10)
.directory(PathBuf::from("downloads"));

let _image_urls = urls(args.clone())?;
let _images = search(args.clone())?;
let _paths = download(args)?;
Ok(())
}

The above code is from the docs page. I keep getting a Parse error. Not sure why. My Cargo.toml only includes this dependency. Any help would be appreciated.
image_search = { version = "0.4.3", features = ["blocking"] }

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.