-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.go
More file actions
96 lines (84 loc) · 2.26 KB
/
Copy pathexample.go
File metadata and controls
96 lines (84 loc) · 2.26 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
// Copilot Scraper — Scrapeless LLM Chat Scraper (Go 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"
// go run example.go
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"time"
)
const apiURL = "https://api.scrapeless.com/api/v2/scraper/execute"
func main() {
apiToken := os.Getenv("SCRAPELESS_API_TOKEN")
if apiToken == "" {
apiToken = "YOUR_API_TOKEN"
}
payload := map[string]any{
"actor": "scraper.copilot",
"input": map[string]any{
"prompt": "Recommended attractions in New York",
"country": "US",
"mode": "search",
},
// Optional: receive the result via webhook instead of the sync response.
// "webhook": map[string]any{"url": "https://www.your-webhook.com"},
}
body, err := json.Marshal(payload)
if err != nil {
panic(err)
}
req, err := http.NewRequest(http.MethodPost, apiURL, bytes.NewReader(body))
if err != nil {
panic(err)
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("x-api-token", apiToken)
client := &http.Client{Timeout: 180 * time.Second}
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
raw, err := io.ReadAll(resp.Body)
if err != nil {
panic(err)
}
if resp.StatusCode >= 300 {
panic(fmt.Sprintf("request failed: %d %s", resp.StatusCode, raw))
}
var data struct {
Status string `json:"status"`
TaskID string `json:"task_id"`
TaskResult struct {
Mode string `json:"mode"`
ResultText string `json:"result_text"`
Citations []struct {
Title string `json:"title"`
URL string `json:"url"`
} `json:"citations"`
} `json:"task_result"`
}
if err := json.Unmarshal(raw, &data); err != nil {
panic(err)
}
fmt.Println("Status: ", data.Status)
fmt.Println("Task ID:", data.TaskID)
fmt.Println("Mode: ", data.TaskResult.Mode)
fmt.Println("\nAnswer:\n", data.TaskResult.ResultText)
for _, citation := range data.TaskResult.Citations {
fmt.Printf("- %s -> %s\n", citation.Title, citation.URL)
}
var pretty bytes.Buffer
_ = json.Indent(&pretty, raw, "", " ")
fmt.Println("\nRaw response:\n", pretty.String())
}