-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExample.java
More file actions
59 lines (49 loc) · 2.1 KB
/
Copy pathExample.java
File metadata and controls
59 lines (49 loc) · 2.1 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
// Gemini Scraper — Scrapeless LLM Chat Scraper (Java example)
//
// Docs: https://docs.scrapeless.com/en/llm-chat-scraper/quickstart/introduction/
// Token: https://app.scrapeless.com/passport/login?redirect=/quick-start
//
// Run:
// export SCRAPELESS_API_TOKEN="your_api_token"
// java Example.java // Java 11+ (single-file source mode)
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.time.Duration;
public class Example {
private static final String API_URL = "https://api.scrapeless.com/api/v2/scraper/execute";
public static void main(String[] args) throws Exception {
String token = System.getenv("SCRAPELESS_API_TOKEN");
if (token == null || token.isEmpty()) {
token = "YOUR_API_TOKEN";
}
String payload = """
{
"actor": "scraper.gemini",
"input": {
"prompt": "Recommended attractions in New York",
"country": "US"
}
}
""";
HttpClient client = HttpClient.newBuilder()
// A cold call takes roughly 15 seconds, so keep the timeout generous.
.connectTimeout(Duration.ofSeconds(30))
.build();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(API_URL))
.timeout(Duration.ofSeconds(180))
.header("Content-Type", "application/json")
.header("x-api-token", token)
.POST(HttpRequest.BodyPublishers.ofString(payload))
.build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
if (response.statusCode() != 200) {
throw new RuntimeException("HTTP " + response.statusCode() + ": " + response.body());
}
// The raw JSON envelope is { status, task_id, task_result }.
// Add Jackson or Gson to map `task_result` into typed objects.
System.out.println(response.body());
}
}