Skip to content

Commit

Permalink
fix example, stable build and documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
Victor Benarbia committed Sep 18, 2022
1 parent a1d7288 commit 92712c5
Show file tree
Hide file tree
Showing 6 changed files with 286 additions and 58 deletions.
97 changes: 93 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ serpapi-search-rust="0.1.0"

Basic application.
```rust
use serpapi_search_rust::serp_api_search::SerpApiSearch;
use serpapi_search_rust::serp_api_search::SerpApiClient;
use std::collections::HashMap;
use std::env;

Expand All @@ -32,7 +32,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
params.insert("location".to_string(), "Austin, TX, Texas, United States".to_string());

// initialize the search engine
let search = SerpApiSearch::google(params, api_key);
let search = SerpApiClient::google(params, api_key);

// search returns a JSON as serde_json::Value which can be accessed like a HashMap.
println!("waiting...");
Expand All @@ -56,7 +56,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
// println!("--- JSON search with a different location ---");
// params = HashMap::<String, String>::new();
// params.insert("location".to_string(), "Destin, Florida, United States".to_string());
// search = SerpApiSearch::google(params, api_key);
// search = SerpApiClient::google(params, api_key);
// let results = search.json().await?;
// println!(">> search_parameters: {}", results["search_parameters"]);
// let places = results["local_results"]["places"].as_array().unwrap();
Expand Down Expand Up @@ -106,4 +106,93 @@ For more information how to build a paramaters HashMap see [serpapi.com document
### TODO
- [ ] more test to close code coverage (each search engine)
- [ ] add more examples
- [ ] better documentation
- [ ] better documentation

### Search bing
<%= snippet('rust', 'examples/example_bing_search.rs') %>
see: [https://serpapi.com/bing-search-api](https://serpapi.com/bing-search-api)

### Search baidu
<%= snippet('rust', 'examples/example_baidu_search.rs') %>
see: [https://serpapi.com/baidu-search-api](https://serpapi.com/baidu-search-api)

### Search yahoo
<%= snippet('rust', 'examples/example_yahoo_search.rs') %>
see: [https://serpapi.com/yahoo-search-api](https://serpapi.com/yahoo-search-api)

### Search youtube
<%= snippet('rust', 'examples/example_youtube_search.rs') %>
see: [https://serpapi.com/youtube-search-api](https://serpapi.com/youtube-search-api)

### Search walmart
<%= snippet('rust', 'examples/example_walmart_search.rs') %>
see: [https://serpapi.com/walmart-search-api](https://serpapi.com/walmart-search-api)

### Search ebay
<%= snippet('rust', 'examples/example_ebay_search.rs') %>
see: [https://serpapi.com/ebay-search-api](https://serpapi.com/ebay-search-api)

### Search naver
<%= snippet('rust', 'examples/example_naver_search.rs') %>
see: [https://serpapi.com/naver-search-api](https://serpapi.com/naver-search-api)

### Search home depot
<%= snippet('rust', 'examples/example_home_depot_search.rs') %>
see: [https://serpapi.com/home-depot-search-api](https://serpapi.com/home-depot-search-api)

### Search apple app store
<%= snippet('rust', 'examples/example_apple_app_store_search.rs') %>
see: [https://serpapi.com/apple-app-store](https://serpapi.com/apple-app-store)

### Search duckduckgo
<%= snippet('rust', 'examples/example_duckduckgo_search.rs') %>
see: [https://serpapi.com/duckduckgo-search-api](https://serpapi.com/duckduckgo-search-api)

### Search google search
<%= snippet('rust', 'examples/example_google_search_search.rs') %>
see: [https://serpapi.com/search-api](https://serpapi.com/search-api)

### Search google scholar
<%= snippet('rust', 'examples/example_google_scholar_search.rs') %>
see: [https://serpapi.com/google-scholar-api](https://serpapi.com/google-scholar-api)

### Search google autocomplete
<%= snippet('rust', 'examples/example_google_autocomplete_search.rs') %>
see: [https://serpapi.com/google-autocomplete-api](https://serpapi.com/google-autocomplete-api)

### Search google product
<%= snippet('rust', 'examples/example_google_product_search.rs') %>
see: [https://serpapi.com/google-product-api](https://serpapi.com/google-product-api)

### Search google reverse image
<%= snippet('rust', 'examples/example_google_reverse_image_search.rs') %>
see: [https://serpapi.com/google-reverse-image](https://serpapi.com/google-reverse-image)

### Search google events
<%= snippet('rust', 'examples/example_google_events_search.rs') %>
see: [https://serpapi.com/google-events-api](https://serpapi.com/google-events-api)

### Search google local services
<%= snippet('rust', 'examples/example_google_local_services_search.rs') %>
see: [https://serpapi.com/google-local-services-api](https://serpapi.com/google-local-services-api)

### Search google maps
<%= snippet('rust', 'examples/example_google_maps_search.rs') %>
see: [https://serpapi.com/google-maps-api](https://serpapi.com/google-maps-api)

### Search google jobs
<%= snippet('rust', 'examples/example_google_jobs_search.rs') %>
see: [https://serpapi.com/google-jobs-api](https://serpapi.com/google-jobs-api)

### Search google play
<%= snippet('rust', 'examples/example_google_play_search.rs') %>
see: [https://serpapi.com/google-play-api](https://serpapi.com/google-play-api)

### Search google images
<%= snippet('rust', 'examples/example_google_images_search.rs') %>
see: [https://serpapi.com/images-results](https://serpapi.com/images-results)

Reference
----

https://github.com/serde-rs/json
8 changes: 6 additions & 2 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,15 @@ task :test do
end

task :doc do
sh("cargo doc")
sh("cargo doc --no-deps --examples")
end

task :oobt do
sh("cargo run --example example_google_search")
Dir.glob('examples/*.rs').each do |path|
name = File.basename(path, '.rs')
ENV['RUST_BACKTRACE'] = '1'
sh("cargo run --example #{name}")
end
end

task :release => [:fmt, :test, :oobt] do
Expand Down
68 changes: 68 additions & 0 deletions examples/reference_example_google.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// reference example google search
use serpapi::serpapi::Client;
use std::collections::HashMap;
use std::env;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// // read secret api key from environment variable
// // To get the key simply copy/paste from https://serpapi.com/dashboard.
// let api_key = match env::var_os("API_KEY") {
// Some(v) => v.into_string().unwrap(),
// None => panic!("$API_KEY is not set"),
// };

// println!("let's search about coffee on google");
// let mut default = HashMap::new();
// default.insert("api_key".to_string(), api_key);
// default.insert("engine".to_string(), "google".to_string());
// // initialize the search engine
// let client = Client::new(default);

// let mut parameter = HashMap::new();
// parameter.insert(
// "location".to_string(),
// "Austin, TX, Texas, United States".to_string(),
// );
// parameter.insert("q".to_string(), "coffee".to_string());
// let mut html_parameter = HashMap::new();
// html_parameter.clone_from(&parameter);

// // search returns a JSON as serde_json::Value which can be accessed like a HashMap.
// println!("waiting...");
// let results = client.search(parameter).await?;
// let organic_results = results["organic_results"].as_array().unwrap();
// println!("results received");
// println!("--- JSON ---");
// println!(" - number of organic results: {}", organic_results.len());
// println!(
// " - organic_results first result description: {}",
// results["organic_results"][0]["about_this_result"]["source"]["description"]
// );
// let places = results["local_results"]["places"].as_array().unwrap();
// println!("number of local_results: {}", places.len());
// println!(" - local_results first address: {}", places[0]["address"]);

// // search returns text
// println!("--- HTML search ---");
// let raw = client.html(html_parameter).await?;
// print!(" - raw HTML size {} bytes\n", raw.len());
// print!(
// " - async search completed with {}\n",
// results["search_parameters"]["engine"]
// );

// // // edit the location in the search
// // println!("--- JSON search with a different location ---");
// // parameter = HashMap::<String, String>::new();
// // parameter.insert("location".to_string(), "Destin, Florida, United States".to_string());
// // client = Client::google(parameter, api_key);
// // let results = client.json().await?;
// // println!(">> search_parameters: {}", results["search_parameters"]);
// // let places = results["local_results"]["places"].as_array().unwrap();
// // println!("number of local_results: {}\n", places.len());
// // println!("local_results first address: {}\n", places[0]["address"]);

print!("ok");
Ok(())
}
42 changes: 21 additions & 21 deletions examples/example_google_search.rs → examples/youtube_search.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use serpapi::serpapi::SerpApiClient;
// reference example google search
use serpapi::serpapi::Client;
use std::collections::HashMap;
use std::env;

Expand All @@ -14,48 +15,47 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("let's search about coffee on google");
let mut default = HashMap::new();
default.insert("api_key".to_string(), api_key);
default.insert("q".to_string(), "coffee".to_string());
default.insert("engine".to_string(), "youtube".to_string());
// initialize the search engine
let client = SerpApiClient::new(default);
let client = Client::new(default);

let mut parameter = HashMap::new();
parameter.insert(
"location".to_string(),
"Austin, TX, Texas, United States".to_string(),
);
parameter.insert("search_query".to_string(), "coffee".to_string());

let mut html_parameter = HashMap::new();
println!("{:?}", parameter);
html_parameter.clone_from(&parameter);
println!("{:?}", html_parameter);

// search returns a JSON as serde_json::Value which can be accessed like a HashMap.
println!("waiting...");
let results = client.search(parameter).await?;
let organic_results = results["organic_results"].as_array().unwrap();
let video_results = results["video_results"].as_array().unwrap();
println!("results received");
println!("--- JSON ---");
println!(" - number of organic results: {}", organic_results.len());
println!(" - number of video results: {}", video_results.len());
println!(
" - organic_results first result description: {}",
results["organic_results"][0]["about_this_result"]["source"]["description"]
" - video_results first result description: {}",
results["video_results"][0]["about_this_result"]["source"]["description"]
);
print!(
" - async search completed with {:?}\n",
results["search_parameters"]
);
let places = results["local_results"]["places"].as_array().unwrap();
println!("number of local_results: {}", places.len());
println!(" - local_results first address: {}", places[0]["address"]);

// search returns text
println!("--- HTML search ---");
println!("{:?}", html_parameter);
let raw = client.html(html_parameter).await?;
println!(">> {:?}", raw);
print!(" - raw HTML size {} bytes\n", raw.len());
print!(
" - async search completed with {}\n",
results["search_parameters"]["engine"]
);

// // edit the location in the search
// println!("--- JSON search with a different location ---");
// parameter = HashMap::<String, String>::new();
// parameter.insert("location".to_string(), "Destin, Florida, United States".to_string());
// search = SerpApiSearch::google(parameter, api_key);
// let results = search.json().await?;
// client = Client::google(parameter, api_key);
// let results = client.json().await?;
// println!(">> search_parameters: {}", results["search_parameters"]);
// let places = results["local_results"]["places"].as_array().unwrap();
// println!("number of local_results: {}\n", places.len());
Expand Down

0 comments on commit 92712c5

Please sign in to comment.