CrawlUrls

 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
using var api = new FirecrawlApp(apiKey);
using var cancellationTokenSource = new CancellationTokenSource(TimeSpan.FromMinutes(5));
var cancellationToken = cancellationTokenSource.Token;

var response = await api.Crawling.CrawlUrlsAsync(
    url: "https://docs.firecrawl.dev/",
    crawlerOptions: new CrawlUrlsRequestCrawlerOptions
    {
        Limit = 1,
    },
    pageOptions: new CrawlUrlsRequestPageOptions
    {
        IncludeHtml = true,
        OnlyMainContent = true,
    },
    cancellationToken: cancellationToken);

Console.WriteLine($"Success: {response.Success}");
Console.WriteLine($"Id: {response.Id}");
Console.WriteLine($"Url: {response.Url}");


GetCrawlStatusResponse? statusResponse = null;
while (!cancellationToken.IsCancellationRequested)
{
    await Task.Delay(TimeSpan.FromSeconds(5), cancellationToken);

    statusResponse = await api.Crawl.GetCrawlStatusAsync(
        jobId: response.Id!,
        cancellationToken: cancellationToken);
    if (statusResponse.Status == "completed")
    {
        break;
    }
}


var index = 0;
foreach (var data in statusResponse.Data ?? [])
{

    var fileInfo = new FileInfo($"output{++index}.md");
    await File.WriteAllTextAsync(fileInfo.FullName, data.Markdown, cancellationToken);
    Console.WriteLine($"Output file: {new Uri(fileInfo.FullName).AbsoluteUri}");
}