-
Notifications
You must be signed in to change notification settings - Fork 4
/
firecrawl.go
805 lines (723 loc) · 23.9 KB
/
firecrawl.go
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
// Package firecrawl provides a client for interacting with the Firecrawl API.
package firecrawl
import (
"bytes"
"encoding/json"
"fmt"
"io"
"math"
"net/http"
"os"
"time"
)
type StringOrStringSlice []string
func (s *StringOrStringSlice) UnmarshalJSON(data []byte) error {
var single string
if err := json.Unmarshal(data, &single); err == nil {
*s = []string{single}
return nil
}
var list []string
if err := json.Unmarshal(data, &list); err == nil {
*s = list
return nil
}
return fmt.Errorf("field is neither a string nor a list of strings")
}
// FirecrawlDocumentMetadata represents metadata for a Firecrawl document
type FirecrawlDocumentMetadata struct {
Title *string `json:"title,omitempty"`
Description *StringOrStringSlice `json:"description,omitempty"`
Language *string `json:"language,omitempty"`
Keywords *StringOrStringSlice `json:"keywords,omitempty"`
Robots *StringOrStringSlice `json:"robots,omitempty"`
OGTitle *string `json:"ogTitle,omitempty"`
OGDescription *string `json:"ogDescription,omitempty"`
OGURL *string `json:"ogUrl,omitempty"`
OGImage *string `json:"ogImage,omitempty"`
OGAudio *string `json:"ogAudio,omitempty"`
OGDeterminer *string `json:"ogDeterminer,omitempty"`
OGLocale *string `json:"ogLocale,omitempty"`
OGLocaleAlternate []*string `json:"ogLocaleAlternate,omitempty"`
OGSiteName *string `json:"ogSiteName,omitempty"`
OGVideo *string `json:"ogVideo,omitempty"`
DCTermsCreated *string `json:"dctermsCreated,omitempty"`
DCDateCreated *string `json:"dcDateCreated,omitempty"`
DCDate *string `json:"dcDate,omitempty"`
DCTermsType *string `json:"dctermsType,omitempty"`
DCType *string `json:"dcType,omitempty"`
DCTermsAudience *string `json:"dctermsAudience,omitempty"`
DCTermsSubject *string `json:"dctermsSubject,omitempty"`
DCSubject *string `json:"dcSubject,omitempty"`
DCDescription *string `json:"dcDescription,omitempty"`
DCTermsKeywords *string `json:"dctermsKeywords,omitempty"`
ModifiedTime *string `json:"modifiedTime,omitempty"`
PublishedTime *string `json:"publishedTime,omitempty"`
ArticleTag *string `json:"articleTag,omitempty"`
ArticleSection *string `json:"articleSection,omitempty"`
SourceURL *string `json:"sourceURL,omitempty"`
StatusCode *int `json:"statusCode,omitempty"`
Error *string `json:"error,omitempty"`
}
// FirecrawlDocument represents a document in Firecrawl
type FirecrawlDocument struct {
Markdown string `json:"markdown,omitempty"`
HTML string `json:"html,omitempty"`
RawHTML string `json:"rawHtml,omitempty"`
Screenshot string `json:"screenshot,omitempty"`
Links []string `json:"links,omitempty"`
Metadata *FirecrawlDocumentMetadata `json:"metadata,omitempty"`
}
// ScrapeParams represents the parameters for a scrape request.
type ScrapeParams struct {
Formats []string `json:"formats,omitempty"`
Headers *map[string]string `json:"headers,omitempty"`
IncludeTags []string `json:"includeTags,omitempty"`
ExcludeTags []string `json:"excludeTags,omitempty"`
OnlyMainContent *bool `json:"onlyMainContent,omitempty"`
WaitFor *int `json:"waitFor,omitempty"`
ParsePDF *bool `json:"parsePDF,omitempty"`
Timeout *int `json:"timeout,omitempty"`
}
// ScrapeResponse represents the response for scraping operations
type ScrapeResponse struct {
Success bool `json:"success"`
Data *FirecrawlDocument `json:"data,omitempty"`
}
// CrawlParams represents the parameters for a crawl request.
type CrawlParams struct {
ScrapeOptions ScrapeParams `json:"scrapeOptions"`
Webhook *string `json:"webhook,omitempty"`
Limit *int `json:"limit,omitempty"`
IncludePaths []string `json:"includePaths,omitempty"`
ExcludePaths []string `json:"excludePaths,omitempty"`
MaxDepth *int `json:"maxDepth,omitempty"`
AllowBackwardLinks *bool `json:"allowBackwardLinks,omitempty"`
AllowExternalLinks *bool `json:"allowExternalLinks,omitempty"`
IgnoreSitemap *bool `json:"ignoreSitemap,omitempty"`
}
// CrawlResponse represents the response for crawling operations
type CrawlResponse struct {
Success bool `json:"success"`
ID string `json:"id,omitempty"`
URL string `json:"url,omitempty"`
}
// CrawlStatusResponse (old JobStatusResponse) represents the response for checking crawl job
type CrawlStatusResponse struct {
Status string `json:"status"`
Total int `json:"total,omitempty"`
Completed int `json:"completed,omitempty"`
CreditsUsed int `json:"creditsUsed,omitempty"`
ExpiresAt string `json:"expiresAt,omitempty"`
Next *string `json:"next,omitempty"`
Data []*FirecrawlDocument `json:"data,omitempty"`
}
// CancelCrawlJobResponse represents the response for canceling a crawl job
type CancelCrawlJobResponse struct {
Success bool `json:"success"`
Status string `json:"status"`
}
// MapParams represents the parameters for a map request.
type MapParams struct {
IncludeSubdomains *bool `json:"includeSubdomains,omitempty"`
Search *string `json:"search,omitempty"`
IgnoreSitemap *bool `json:"ignoreSitemap,omitempty"`
Limit *int `json:"limit,omitempty"`
}
// MapResponse represents the response for mapping operations
type MapResponse struct {
Success bool `json:"success"`
Links []string `json:"links,omitempty"`
Error string `json:"error,omitempty"`
}
// requestOptions represents options for making requests.
type requestOptions struct {
retries int
backoff int
}
// requestOption is a functional option type for requestOptions.
type requestOption func(*requestOptions)
// newRequestOptions creates a new requestOptions instance with the provided options.
//
// Parameters:
// - opts: Optional request options.
//
// Returns:
// - *requestOptions: A new instance of requestOptions with the provided options.
func newRequestOptions(opts ...requestOption) *requestOptions {
options := &requestOptions{retries: 1}
for _, opt := range opts {
opt(options)
}
return options
}
// withRetries sets the number of retries for a request.
//
// Parameters:
// - retries: The number of retries to be performed.
//
// Returns:
// - requestOption: A functional option that sets the number of retries for a request.
func withRetries(retries int) requestOption {
return func(opts *requestOptions) {
opts.retries = retries
}
}
// withBackoff sets the backoff interval for a request.
//
// Parameters:
// - backoff: The backoff interval (in milliseconds) to be used for retries.
//
// Returns:
// - requestOption: A functional option that sets the backoff interval for a request.
func withBackoff(backoff int) requestOption {
return func(opts *requestOptions) {
opts.backoff = backoff
}
}
// FirecrawlApp represents a client for the Firecrawl API.
type FirecrawlApp struct {
APIKey string
APIURL string
Client *http.Client
Version string
}
// NewFirecrawlApp creates a new instance of FirecrawlApp with the provided API key and API URL.
// If the API key or API URL is not provided, it attempts to retrieve them from environment variables.
// If the API key is still not found, it returns an error.
//
// Parameters:
// - apiKey: The API key for authenticating with the Firecrawl API. If empty, it will be retrieved from the FIRECRAWL_API_KEY environment variable.
// - apiURL: The base URL for the Firecrawl API. If empty, it will be retrieved from the FIRECRAWL_API_URL environment variable, defaulting to "https://api.firecrawl.dev".
//
// Returns:
// - *FirecrawlApp: A new instance of FirecrawlApp configured with the provided or retrieved API key and API URL.
// - error: An error if the API key is not provided or retrieved.
func NewFirecrawlApp(apiKey, apiURL string) (*FirecrawlApp, error) {
if apiKey == "" {
apiKey = os.Getenv("FIRECRAWL_API_KEY")
if apiKey == "" {
return nil, fmt.Errorf("no API key provided")
}
}
if apiURL == "" {
apiURL = os.Getenv("FIRECRAWL_API_URL")
if apiURL == "" {
apiURL = "https://api.firecrawl.dev"
}
}
client := &http.Client{
Timeout: 60 * time.Second,
}
return &FirecrawlApp{
APIKey: apiKey,
APIURL: apiURL,
Client: client,
}, nil
}
// ScrapeURL scrapes the content of the specified URL using the Firecrawl API.
//
// Parameters:
// - url: The URL to be scraped.
// - params: Optional parameters for the scrape request, including extractor options for LLM extraction.
//
// Returns:
// - *FirecrawlDocument or *FirecrawlDocumentV0: The scraped document data depending on the API version.
// - error: An error if the scrape request fails.
func (app *FirecrawlApp) ScrapeURL(url string, params *ScrapeParams) (*FirecrawlDocument, error) {
headers := app.prepareHeaders(nil)
scrapeBody := map[string]any{"url": url}
// if params != nil {
// if extractorOptions, ok := params["extractorOptions"].(ExtractorOptions); ok {
// if schema, ok := extractorOptions.ExtractionSchema.(interface{ schema() any }); ok {
// extractorOptions.ExtractionSchema = schema.schema()
// }
// if extractorOptions.Mode == "" {
// extractorOptions.Mode = "llm-extraction"
// }
// scrapeBody["extractorOptions"] = extractorOptions
// }
// for key, value := range params {
// if key != "extractorOptions" {
// scrapeBody[key] = value
// }
// }
// }
if params != nil {
if params.Formats != nil {
scrapeBody["formats"] = params.Formats
}
if params.Headers != nil {
scrapeBody["headers"] = params.Headers
}
if params.IncludeTags != nil {
scrapeBody["includeTags"] = params.IncludeTags
}
if params.ExcludeTags != nil {
scrapeBody["excludeTags"] = params.ExcludeTags
}
if params.OnlyMainContent != nil {
scrapeBody["onlyMainContent"] = params.OnlyMainContent
}
if params.WaitFor != nil {
scrapeBody["waitFor"] = params.WaitFor
}
if params.ParsePDF != nil {
scrapeBody["parsePDF"] = params.ParsePDF
}
if params.Timeout != nil {
scrapeBody["timeout"] = params.Timeout
}
}
resp, err := app.makeRequest(
http.MethodPost,
fmt.Sprintf("%s/v1/scrape", app.APIURL),
scrapeBody,
headers,
"scrape URL",
)
if err != nil {
return nil, err
}
var scrapeResponse ScrapeResponse
err = json.Unmarshal(resp, &scrapeResponse)
if scrapeResponse.Success {
return scrapeResponse.Data, nil
}
if err != nil {
return nil, err
}
return nil, fmt.Errorf("failed to scrape URL")
}
// CrawlURL starts a crawl job for the specified URL using the Firecrawl API.
//
// Parameters:
// - url: The URL to crawl.
// - params: Optional parameters for the crawl request.
// - idempotencyKey: An optional idempotency key to ensure the request is idempotent (can be nil).
// - pollInterval: An optional interval (in seconds) at which to poll the job status. Default is 2 seconds.
//
// Returns:
// - CrawlStatusResponse: The crawl result if the job is completed.
// - error: An error if the crawl request fails.
func (app *FirecrawlApp) CrawlURL(url string, params *CrawlParams, idempotencyKey *string, pollInterval ...int) (*CrawlStatusResponse, error) {
var key string
if idempotencyKey != nil {
key = *idempotencyKey
}
headers := app.prepareHeaders(&key)
crawlBody := map[string]any{"url": url}
if params != nil {
if params.ScrapeOptions.Formats != nil {
crawlBody["scrapeOptions"] = params.ScrapeOptions
}
if params.Webhook != nil {
crawlBody["webhook"] = params.Webhook
}
if params.Limit != nil {
crawlBody["limit"] = params.Limit
}
if params.IncludePaths != nil {
crawlBody["includePaths"] = params.IncludePaths
}
if params.ExcludePaths != nil {
crawlBody["excludePaths"] = params.ExcludePaths
}
if params.MaxDepth != nil {
crawlBody["maxDepth"] = params.MaxDepth
}
if params.AllowBackwardLinks != nil {
crawlBody["allowBackwardLinks"] = params.AllowBackwardLinks
}
if params.AllowExternalLinks != nil {
crawlBody["allowExternalLinks"] = params.AllowExternalLinks
}
if params.IgnoreSitemap != nil {
crawlBody["ignoreSitemap"] = params.IgnoreSitemap
}
}
actualPollInterval := 2
if len(pollInterval) > 0 {
actualPollInterval = pollInterval[0]
}
resp, err := app.makeRequest(
http.MethodPost,
fmt.Sprintf("%s/v1/crawl", app.APIURL),
crawlBody,
headers,
"start crawl job",
withRetries(3),
withBackoff(500),
)
if err != nil {
return nil, err
}
var crawlResponse CrawlResponse
err = json.Unmarshal(resp, &crawlResponse)
if err != nil {
return nil, err
}
return app.monitorJobStatus(crawlResponse.ID, headers, actualPollInterval)
}
// CrawlURL starts a crawl job for the specified URL using the Firecrawl API.
//
// Parameters:
// - url: The URL to crawl.
// - params: Optional parameters for the crawl request.
// - idempotencyKey: An optional idempotency key to ensure the request is idempotent.
//
// Returns:
// - *CrawlResponse: The crawl response with id.
// - error: An error if the crawl request fails.
func (app *FirecrawlApp) AsyncCrawlURL(url string, params *CrawlParams, idempotencyKey *string) (*CrawlResponse, error) {
var key string
if idempotencyKey != nil {
key = *idempotencyKey
}
headers := app.prepareHeaders(&key)
crawlBody := map[string]any{"url": url}
if params != nil {
if params.ScrapeOptions.Formats != nil {
crawlBody["scrapeOptions"] = params.ScrapeOptions
}
if params.Webhook != nil {
crawlBody["webhook"] = params.Webhook
}
if params.Limit != nil {
crawlBody["limit"] = params.Limit
}
if params.IncludePaths != nil {
crawlBody["includePaths"] = params.IncludePaths
}
if params.ExcludePaths != nil {
crawlBody["excludePaths"] = params.ExcludePaths
}
if params.MaxDepth != nil {
crawlBody["maxDepth"] = params.MaxDepth
}
if params.AllowBackwardLinks != nil {
crawlBody["allowBackwardLinks"] = params.AllowBackwardLinks
}
if params.AllowExternalLinks != nil {
crawlBody["allowExternalLinks"] = params.AllowExternalLinks
}
if params.IgnoreSitemap != nil {
crawlBody["ignoreSitemap"] = params.IgnoreSitemap
}
}
resp, err := app.makeRequest(
http.MethodPost,
fmt.Sprintf("%s/v1/crawl", app.APIURL),
crawlBody,
headers,
"start crawl job",
withRetries(3),
withBackoff(500),
)
if err != nil {
return nil, err
}
var crawlResponse CrawlResponse
err = json.Unmarshal(resp, &crawlResponse)
if err != nil {
return nil, err
}
if crawlResponse.ID == "" {
return nil, fmt.Errorf("failed to get job ID")
}
return &crawlResponse, nil
}
// CheckCrawlStatus checks the status of a crawl job using the Firecrawl API.
//
// Parameters:
// - ID: The ID of the crawl job to check.
//
// Returns:
// - *CrawlStatusResponse: The status of the crawl job.
// - error: An error if the crawl status check request fails.
func (app *FirecrawlApp) CheckCrawlStatus(ID string) (*CrawlStatusResponse, error) {
headers := app.prepareHeaders(nil)
apiURL := fmt.Sprintf("%s/v1/crawl/%s", app.APIURL, ID)
resp, err := app.makeRequest(
http.MethodGet,
apiURL,
nil,
headers,
"check crawl status",
withRetries(3),
withBackoff(500),
)
if err != nil {
return nil, err
}
var jobStatusResponse CrawlStatusResponse
err = json.Unmarshal(resp, &jobStatusResponse)
if err != nil {
return nil, err
}
return &jobStatusResponse, nil
}
// CancelCrawlJob cancels a crawl job using the Firecrawl API.
//
// Parameters:
// - ID: The ID of the crawl job to cancel.
//
// Returns:
// - string: The status of the crawl job after cancellation.
// - error: An error if the crawl job cancellation request fails.
func (app *FirecrawlApp) CancelCrawlJob(ID string) (string, error) {
headers := app.prepareHeaders(nil)
apiURL := fmt.Sprintf("%s/v1/crawl/%s", app.APIURL, ID)
resp, err := app.makeRequest(
http.MethodDelete,
apiURL,
nil,
headers,
"cancel crawl job",
)
if err != nil {
return "", err
}
var cancelCrawlJobResponse CancelCrawlJobResponse
err = json.Unmarshal(resp, &cancelCrawlJobResponse)
if err != nil {
return "", err
}
return cancelCrawlJobResponse.Status, nil
}
// MapURL initiates a mapping operation for a URL using the Firecrawl API.
//
// Parameters:
// - url: The URL to map.
// - params: Optional parameters for the mapping request.
//
// Returns:
// - *MapResponse: The response from the mapping operation.
// - error: An error if the mapping request fails.
func (app *FirecrawlApp) MapURL(url string, params *MapParams) (*MapResponse, error) {
headers := app.prepareHeaders(nil)
jsonData := map[string]any{"url": url}
if params != nil {
if params.IncludeSubdomains != nil {
jsonData["includeSubdomains"] = params.IncludeSubdomains
}
if params.Search != nil {
jsonData["search"] = params.Search
}
if params.IgnoreSitemap != nil {
jsonData["ignoreSitemap"] = params.IgnoreSitemap
}
if params.Limit != nil {
jsonData["limit"] = params.Limit
}
}
resp, err := app.makeRequest(
http.MethodPost,
fmt.Sprintf("%s/v1/map", app.APIURL),
jsonData,
headers,
"map",
)
if err != nil {
return nil, err
}
var mapResponse MapResponse
err = json.Unmarshal(resp, &mapResponse)
if err != nil {
return nil, err
}
if mapResponse.Success {
return &mapResponse, nil
} else {
return nil, fmt.Errorf("map operation failed: %s", mapResponse.Error)
}
}
// SearchURL searches for a URL using the Firecrawl API.
//
// Parameters:
// - url: The URL to search for.
// - params: Optional parameters for the search request.
// - error: An error if the search request fails.
//
// Search is not implemented in API version 1.0.0.
func (app *FirecrawlApp) Search(query string, params *any) (any, error) {
return nil, fmt.Errorf("Search is not implemented in API version 1.0.0")
}
// prepareHeaders prepares the headers for an HTTP request.
//
// Parameters:
// - idempotencyKey: A string representing the idempotency key to be included in the headers.
// If the idempotency key is an empty string, it will not be included in the headers.
//
// Returns:
// - map[string]string: A map containing the headers for the HTTP request.
func (app *FirecrawlApp) prepareHeaders(idempotencyKey *string) map[string]string {
headers := map[string]string{
"Content-Type": "application/json",
"Authorization": fmt.Sprintf("Bearer %s", app.APIKey),
}
if idempotencyKey != nil {
headers["x-idempotency-key"] = *idempotencyKey
}
return headers
}
// makeRequest makes a request to the specified URL with the provided method, data, headers, and options.
//
// Parameters:
// - method: The HTTP method to use for the request (e.g., "GET", "POST", "DELETE").
// - url: The URL to send the request to.
// - data: The data to be sent in the request body.
// - headers: The headers to be included in the request.
// - action: A string describing the action being performed.
// - opts: Optional request options.
//
// Returns:
// - []byte: The response body from the request.
// - error: An error if the request fails.
func (app *FirecrawlApp) makeRequest(method, url string, data map[string]any, headers map[string]string, action string, opts ...requestOption) ([]byte, error) {
var body []byte
var err error
if data != nil {
body, err = json.Marshal(data)
if err != nil {
return nil, err
}
}
req, err := http.NewRequest(method, url, bytes.NewBuffer(body))
if err != nil {
return nil, err
}
for key, value := range headers {
req.Header.Set(key, value)
}
var resp *http.Response
options := newRequestOptions(opts...)
for i := 0; i < options.retries; i++ {
resp, err = app.Client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != 502 {
break
}
time.Sleep(time.Duration(math.Pow(2, float64(i))) * time.Duration(options.backoff) * time.Millisecond)
}
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
statusCode := resp.StatusCode
if statusCode != 200 {
return nil, app.handleError(statusCode, respBody, action)
}
return respBody, nil
}
// monitorJobStatus monitors the status of a crawl job using the Firecrawl API.
//
// Parameters:
// - ID: The ID of the crawl job to monitor.
// - headers: The headers to be included in the request.
// - pollInterval: The interval (in seconds) at which to poll the job status.
//
// Returns:
// - *CrawlStatusResponse: The crawl result if the job is completed.
// - error: An error if the crawl status check request fails.
func (app *FirecrawlApp) monitorJobStatus(ID string, headers map[string]string, pollInterval int) (*CrawlStatusResponse, error) {
attempts := 3
for {
resp, err := app.makeRequest(
http.MethodGet,
fmt.Sprintf("%s/v1/crawl/%s", app.APIURL, ID),
nil,
headers,
"check crawl status",
withRetries(3),
withBackoff(500),
)
if err != nil {
return nil, err
}
var statusData CrawlStatusResponse
err = json.Unmarshal(resp, &statusData)
if err != nil {
return nil, err
}
status := statusData.Status
if status == "" {
return nil, fmt.Errorf("invalid status in response")
}
if status == "completed" {
if statusData.Data != nil {
allData := statusData.Data
for statusData.Next != nil {
resp, err := app.makeRequest(
http.MethodGet,
*statusData.Next,
nil,
headers,
"fetch next page of crawl status",
withRetries(3),
withBackoff(500),
)
if err != nil {
return nil, err
}
err = json.Unmarshal(resp, &statusData)
if err != nil {
return nil, err
}
if statusData.Data != nil {
allData = append(allData, statusData.Data...)
}
}
statusData.Data = allData
return &statusData, nil
} else {
attempts++
if attempts > 3 {
return nil, fmt.Errorf("crawl job completed but no data was returned")
}
}
} else if status == "active" || status == "paused" || status == "pending" || status == "queued" || status == "waiting" || status == "scraping" {
pollInterval = max(pollInterval, 2)
time.Sleep(time.Duration(pollInterval) * time.Second)
} else {
return nil, fmt.Errorf("crawl job failed or was stopped. Status: %s", status)
}
}
}
// handleError handles errors returned by the Firecrawl API.
//
// Parameters:
// - resp: The HTTP response object.
// - body: The response body from the HTTP response.
// - action: A string describing the action being performed.
//
// Returns:
// - error: An error describing the failure reason.
func (app *FirecrawlApp) handleError(statusCode int, body []byte, action string) error {
var errorData map[string]any
err := json.Unmarshal(body, &errorData)
if err != nil {
return fmt.Errorf("failed to parse error response: %v", err)
}
errorMessage, _ := errorData["error"].(string)
if errorMessage == "" {
errorMessage = "No additional error details provided."
}
var message string
switch statusCode {
case 402:
message = fmt.Sprintf("Payment Required: Failed to %s. %s", action, errorMessage)
case 408:
message = fmt.Sprintf("Request Timeout: Failed to %s as the request timed out. %s", action, errorMessage)
case 409:
message = fmt.Sprintf("Conflict: Failed to %s due to a conflict. %s", action, errorMessage)
case 500:
message = fmt.Sprintf("Internal Server Error: Failed to %s. %s", action, errorMessage)
default:
message = fmt.Sprintf("Unexpected error during %s: Status code %d. %s", action, statusCode, errorMessage)
}
return fmt.Errorf(message)
}