Skip to content

Commit 5f66062

Browse files
author
stlc-bot
committed
feat(scrape): add cache age support to byte downloads (#1171)
feat(scrape): support country for image scraping (#1172) Stainless-Generated-From: 96d81bf9343cf24cae5bbd53710bfd102942a1b6
1 parent efaf864 commit 5f66062

2 files changed

Lines changed: 17 additions & 2 deletions

File tree

‎pkg/cmd/web.go‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -783,7 +783,7 @@ var webWebCrawlMd = requestflag.WithInnerFlags(cli.Command{
783783

784784
var webWebScrapeBytes = requestflag.WithInnerFlags(cli.Command{
785785
Name: "web-scrape-bytes",
786-
Usage: "Downloads a resource and returns its bytes as base64. Without waitForMs, returns\nthe original HTTP response without image conversion, text extraction, or\ncharacter-encoding changes. HTTP compression is decoded before base64 encoding.\nSupply waitForMs to render HTML with JavaScript in the browser and return the\nresulting HTML as UTF-8 bytes after the wait. Non-HTML resources, including\nimages and PDFs, keep their original bytes and do not incur a browser wait.\nFollows public redirects and retries failed downloads through ISP and\nresidential proxies, with a direct fallback. When country is specified, only a\nresidential proxy in that country is used. Supply headers such as Referer for\nimages that require a referring page. Downloads are not cached. Maximum decoded\nresource size: 20 MiB (20971520 bytes), before base64 encoding. Successful\nrequests cost 1 credit; errors are not billed.",
786+
Usage: "Downloads a resource and returns its bytes as base64. Without waitForMs, returns\nthe original HTTP response without image conversion, text extraction, or\ncharacter-encoding changes. HTTP compression is decoded before base64 encoding.\nSupply waitForMs to render HTML with JavaScript in the browser and return the\nresulting HTML as UTF-8 bytes after the wait. Non-HTML resources, including\nimages and PDFs, keep their original bytes and do not incur a browser wait.\nFollows public redirects and retries failed downloads through ISP and\nresidential proxies, with a direct fallback. When country is specified, only a\nresidential proxy in that country is used. Supply headers such as Referer for\nimages that require a referring page. Cached results are reused according to\nmaxAgeMs (default: 1 day; maximum: 30 days). Set maxAgeMs=0 to fetch fresh and\nrefresh the cache. Cache identity includes the exact URL, country, waitForMs,\nand normalized outbound headers. Credential-bearing headers and zero data\nretention bypass cache reads and writes. cache_metadata reports hit, miss, or\nzdr and the cached result age in milliseconds. Maximum decoded resource size: 20\nMiB (20971520 bytes), before base64 encoding. Successful requests cost 1 credit;\nerrors are not billed.",
787787
Suggest: true,
788788
Flags: []cli.Flag{
789789
&requestflag.Flag[string]{
@@ -799,9 +799,15 @@ var webWebScrapeBytes = requestflag.WithInnerFlags(cli.Command{
799799
},
800800
&requestflag.Flag[map[string]any]{
801801
Name: "headers",
802-
Usage: "Optional outbound HTTP headers, such as Referer, Cookie, or Authorization. Send as a JSON object or deep-object query params such as headers[Referer]=https://example.com/. Host, Content-Length, and hop-by-hop transport headers are rejected. Authorization and cookies are removed when a redirect changes origin.",
802+
Usage: "Optional outbound HTTP headers, such as Referer, Cookie, or Authorization. Send as a JSON object or deep-object query params such as headers[Referer]=https://example.com/. Host, Content-Length, and hop-by-hop transport headers are rejected. Authorization and cookies are removed when a redirect changes origin. Credential-bearing headers bypass cache reads and writes; other headers are included in the cache key.",
803803
QueryPath: "headers",
804804
},
805+
&requestflag.Flag[*int64]{
806+
Name: "max-age-ms",
807+
Usage: "Return a cached result if a prior scrape for the same parameters exists and is younger than this many milliseconds. Defaults to 1 day (86400000 ms) when omitted. Max is 30 days (2592000000 ms). Set to 0 to always scrape fresh.",
808+
Default: requestflag.Ptr[int64](86400000),
809+
QueryPath: "maxAgeMs",
810+
},
805811
&requestflag.Flag[[]string]{
806812
Name: "tag",
807813
Usage: "Comma-separated tags for tracking request usage. Up to 20 tags, each 1-50 characters.",
@@ -989,6 +995,11 @@ var webWebScrapeImages = requestflag.WithInnerFlags(cli.Command{
989995
Usage: "Optional browser actions executed in array order after the page loads and before content is captured. Requires a paid plan. Send a JSON array in the query parameter. Maximum: 5 actions.",
990996
QueryPath: "actions",
991997
},
998+
&requestflag.Flag[string]{
999+
Name: "country",
1000+
Usage: "Fetch the target page through a residential proxy in this country (ISO 3166-1 alpha-2).",
1001+
QueryPath: "country",
1002+
},
9921003
&requestflag.Flag[bool]{
9931004
Name: "dedupe",
9941005
Usage: "When true, visually duplicate images are removed: every image is loaded and perceptually hashed, and only the highest-resolution copy of each duplicate group is kept. Images that cannot be downloaded or hashed are kept. Default: false.",

‎pkg/cmd/web_test.go‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,7 @@ func TestWebWebScrapeBytes(t *testing.T) {
563563
"--url", "https://example.com",
564564
"--country", "de",
565565
"--headers", "{foo: J!}",
566+
"--max-age-ms", "0",
566567
"--tag", "production",
567568
"--tag", "team-alpha",
568569
"--timeout-opts", "{milliseconds: 1, behavior: fail}",
@@ -583,6 +584,7 @@ func TestWebWebScrapeBytes(t *testing.T) {
583584
"--url", "https://example.com",
584585
"--country", "de",
585586
"--headers", "{foo: J!}",
587+
"--max-age-ms", "0",
586588
"--tag", "production",
587589
"--tag", "team-alpha",
588590
"--timeout-opts.milliseconds", "1",
@@ -663,6 +665,7 @@ func TestWebWebScrapeImages(t *testing.T) {
663665
"web", "web-scrape-images",
664666
"--url", "https://example.com",
665667
"--action", "[{do: wait, timeMs: 0}]",
668+
"--country", "de",
666669
"--dedupe=true",
667670
"--enrichment", "{classification: true, hostedUrl: true, maxTimePerMs: 1, resolution: true}",
668671
"--headers", "{foo: J!}",
@@ -686,6 +689,7 @@ func TestWebWebScrapeImages(t *testing.T) {
686689
"web", "web-scrape-images",
687690
"--url", "https://example.com",
688691
"--action", "[{do: wait, timeMs: 0}]",
692+
"--country", "de",
689693
"--dedupe=true",
690694
"--enrichment.classification=true",
691695
"--enrichment.hosted-url=true",

0 commit comments

Comments
 (0)