-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic_search.rs
More file actions
135 lines (110 loc) · 3.93 KB
/
basic_search.rs
File metadata and controls
135 lines (110 loc) · 3.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
use serp_sdk::{SearchQuery, SerpClient};
use std::env;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Initialize logging
tracing_subscriber::fmt::init();
// Get API key from environment or command line
let api_key = env::args()
.nth(1)
.or_else(|| env::var("SERP_API_KEY").ok())
.expect("Please provide API key as argument or set SERP_API_KEY environment variable");
// Initialize client with builder pattern
let client = SerpClient::builder()
.api_key(api_key)
.timeout(std::time::Duration::from_secs(30))
.build()?;
println!("🔍 Searching for 'Rust programming language'...\n");
// Execute search with fluent query builder
let results = client
.search(
SearchQuery::new("Rust programming language")
.language("en")
.country("us")
.limit(10)?,
)
.await?;
// Display metadata
println!("✅ Search completed successfully!");
println!("📊 Search ID: {}", results.search_metadata.id);
if let Some(time) = results.search_metadata.total_time_taken {
println!("⏱️ Total time: {:.2}s", time);
}
if let Some(url) = &results.search_metadata.google_url {
println!("🌐 Google URL: {}", url);
}
println!();
// Display organic results
if let Some(organic) = results.organic_results {
println!("📋 Found {} organic results:\n", organic.len());
for (i, result) in organic.iter().enumerate() {
println!("{}. 📎 {}", i + 1, result.title);
println!(" 🔗 {}", result.link);
if let Some(snippet) = &result.snippet {
println!(" 📄 {}", snippet);
}
if let Some(date) = &result.date {
println!(" 📅 {}", date);
}
println!();
}
} else {
println!("❌ No organic results found");
}
// Display answer box if available
if let Some(answer_box) = results.answer_box {
println!("💡 Answer Box:");
println!(" Type: {}", answer_box.answer_type);
if let Some(title) = answer_box.title {
println!(" Title: {}", title);
}
if let Some(answer) = answer_box.answer {
println!(" Answer: {}", answer);
}
if let Some(snippet) = answer_box.snippet {
println!(" Snippet: {}", snippet);
}
println!();
}
// Display knowledge graph if available
if let Some(kg) = results.knowledge_graph {
println!("🧠 Knowledge Graph:");
println!(" Title: {}", kg.title);
if let Some(description) = kg.description {
println!(" Description: {}", description);
}
if let Some(kg_type) = kg.knowledge_type {
println!(" Type: {}", kg_type);
}
println!();
}
// Display related searches
if let Some(related) = results.related_searches {
println!("🔗 Related searches:");
for search in related.iter().take(5) {
match search {
serp_sdk::response::RelatedSearch::Simple { query, .. } => {
println!(" • {}", query);
}
serp_sdk::response::RelatedSearch::Block { items, .. } => {
for item in items.iter().take(5) {
if let Some(name) = &item.name {
println!(" • {}", name);
}
}
}
}
}
println!();
}
// Display pagination info
if let Some(pagination) = results.pagination {
println!("📄 Pagination:");
println!(" Current page: {}", pagination.current);
if let Some(next) = pagination.next {
println!(" Next page available: {}", next);
}
println!();
}
Ok(())
}