-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathProgram.cs
More file actions
95 lines (84 loc) · 2.9 KB
/
Copy pathProgram.cs
File metadata and controls
95 lines (84 loc) · 2.9 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
// Price Monitor: track product prices across shopping engines.
// Use case: E-commerce teams monitoring competitor pricing in real time.
// Runs concurrent searches on Google Shopping and Walmart.
using SerpApi;
using System.Text.Json;
var apiKey = args.Length > 0 ? args[0] : Environment.GetEnvironmentVariable("SERPAPI_KEY");
if (string.IsNullOrEmpty(apiKey))
{
Console.WriteLine("Usage: dotnet run -- <API_KEY>");
Console.WriteLine(" or: SERPAPI_KEY=... dotnet run");
return;
}
using var client = new SerpApiClient(apiKey, new SerpApiClientOptions
{
Timeout = TimeSpan.FromSeconds(30)
});
var product = "sony wh-1000xm5";
Console.WriteLine($"Monitoring prices for: \"{product}\"\n");
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(20));
// Search Google Shopping and Walmart concurrently
var googleShopping = client.SearchAsync(new Dictionary<string, string>
{
["engine"] = "google_shopping",
["q"] = product
}, cts.Token);
var walmart = client.SearchAsync(new Dictionary<string, string>
{
["engine"] = "walmart",
["query"] = product
}, cts.Token);
try
{
await Task.WhenAll(googleShopping, walmart);
// Google Shopping results
Console.WriteLine("=== Google Shopping ===");
var gResults = await googleShopping;
var shopping = gResults["shopping_results"];
if (shopping is { } items)
{
foreach (var item in items.EnumerateArray().Take(5))
{
var title = item.TryGetProperty("title", out var t) ? t.GetString() : "?";
var price = item.TryGetProperty("extracted_price", out var p) ? $"${p}" : "N/A";
var source = item.TryGetProperty("source", out var s) ? s.GetString() : "";
Console.WriteLine($" {price,-8} {source,-15} {title}");
}
}
else Console.WriteLine(" No results");
// Walmart results
Console.WriteLine("\n=== Walmart ===");
var wResults = await walmart;
var organic = wResults["organic_results"];
if (organic is { } walmartItems)
{
foreach (var item in walmartItems.EnumerateArray().Take(5))
{
var title = item.TryGetProperty("title", out var t) ? t.GetString() : "?";
var price = item.TryGetProperty("primary_offer", out var po)
&& po.TryGetProperty("offer_price", out var op)
? $"${op}"
: "N/A";
Console.WriteLine($" {price,-8} {title}");
}
}
else Console.WriteLine(" No results");
}
catch (OperationCanceledException) when (cts.IsCancellationRequested)
{
Console.WriteLine("Price search timed out after 20 seconds.");
}
catch (SerpApiException ex)
{
Console.WriteLine($"Search error: {ex.Message}");
}
finally
{
DisposeCompleted(googleShopping);
DisposeCompleted(walmart);
}
static void DisposeCompleted(Task<SerpApiResponse> task)
{
if (task.Status == TaskStatus.RanToCompletion)
task.Result.Dispose();
}