scrapebadger 0.81.0 → 0.82.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -24,7 +24,7 @@ The official Node.js/TypeScript client library for the [ScrapeBadger](https://sc
24
24
  - **Typed Exceptions** - Distinct error classes for every failure scenario
25
25
  - **Async requests and webhooks** - Run any call asynchronously, wait for it or get a signed webhook; idempotent resubmits
26
26
  - **37+ Twitter endpoints** - Tweets, users, lists, communities, trends, geo, real-time streams
27
- - **20 Google product APIs** - Search (with optional deferred AI Overview follow-up), Maps, News, Hotels, Trends (incl. topic autocomplete), Jobs, Shopping (+ merchant URL enrichment, barcode offers), Patents, Scholar (search + profiles + author + author citation + cite formats), Images, Videos, Finance, AI Mode, Lens, **Local Pack**, **Shorts**, **Flights**, Products, **Ads Transparency Center**
27
+ - **20 Google product APIs** - Search (with optional deferred AI Overview follow-up and result link resolving), Maps, News, Hotels, Trends (incl. topic autocomplete), Jobs, Shopping (+ merchant URL enrichment, barcode offers), Patents, Scholar (search + profiles + author + author citation + cite formats), Images, Videos, Finance, AI Mode, Lens, **Local Pack**, **Shorts**, **Flights**, Products, **Ads Transparency Center**
28
28
  - **Vinted scraping** - Search items, item details, user profiles, brands, colors, markets
29
29
  - **Web scraping** - Anti-bot bypass, JS rendering, screenshots, and selector/AI data extraction
30
30
 
package/dist/index.cjs CHANGED
@@ -9,7 +9,7 @@ function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
9
9
  var WebSocket__default = /*#__PURE__*/_interopDefault(WebSocket);
10
10
 
11
11
  // src/internal/version.ts
12
- var SDK_VERSION = "0.81.0";
12
+ var SDK_VERSION = "0.82.0";
13
13
 
14
14
  // src/internal/exceptions.ts
15
15
  var ScrapeBadgerError = class _ScrapeBadgerError extends Error {
@@ -3666,6 +3666,9 @@ var ScholarClient = class {
3666
3666
 
3667
3667
  // src/google/search.ts
3668
3668
  var FULL_SEARCH_ONLY = ["mode", "ai_overview", "expand_related_questions"];
3669
+ var RESOLVE_MAX_RESULTS = 25;
3670
+ var DEFAULT_RESOLVE_MAX_WAIT_MS = 24e4;
3671
+ var MIN_RESOLVE_WAIT_MS = 1e3;
3669
3672
  var SearchClient2 = class {
3670
3673
  constructor(client) {
3671
3674
  this.client = client;
@@ -3682,7 +3685,128 @@ var SearchClient2 = class {
3682
3685
  params: light
3683
3686
  });
3684
3687
  }
3688
+ /**
3689
+ * Resolve Google redirect links to the results' own URLs. One
3690
+ * `POST /v1/google/resolve` call, 0 credits.
3691
+ *
3692
+ * Google can return a result's `link` as a redirect
3693
+ * (`https://www.google.com/goto?url=...`) and limits how fast the links of
3694
+ * one result page can be followed. Such a result has a `link_token`. Send
3695
+ * 1 to 25 results that have one; every other field of an item is returned
3696
+ * unchanged on its row, so `organic_results` items can be sent as they
3697
+ * are.
3698
+ *
3699
+ * `results` in the response has one row per item, in the order sent. Each
3700
+ * row is one of:
3701
+ * - resolved: `link` is the result's own URL. The row has no `link_token`,
3702
+ * `retry_after_seconds` or `error`.
3703
+ * - not ready: the row has `link_token` and `retry_after_seconds`. Wait
3704
+ * that many seconds, then send the row's `link_token` again. It can
3705
+ * differ from the one sent, so always keep the latest. `link` is as
3706
+ * sent.
3707
+ * - failed: the row has `link_token` as sent and `error`. Do not send it
3708
+ * again.
3709
+ *
3710
+ * The top-level `retry_after_seconds` is the smallest wait among the rows
3711
+ * that are not ready, or `null` when no row is waiting. A token sent
3712
+ * before its wait is over is not an error: its row comes back not ready
3713
+ * with the wait that is left.
3714
+ *
3715
+ * Polling as `retry_after_seconds` says, all ten links of a result page
3716
+ * are usually known 45 to 70 seconds after the search. Once the result
3717
+ * page is about 160 seconds old, every remaining link resolves in one
3718
+ * call. {@link resolveAll} runs this loop for you.
3719
+ *
3720
+ * @example
3721
+ * ```typescript
3722
+ * const serp = await client.google.search.search({ q: "python 3.13" });
3723
+ * const pending = (serp.organic_results as any[]).filter((r) => r.link_token);
3724
+ * const { results, retry_after_seconds } = await client.google.search.resolve({
3725
+ * results: pending,
3726
+ * });
3727
+ * ```
3728
+ *
3729
+ * @param params - `results`: 1 to 25 items, each with a `link_token`.
3730
+ * @returns The rows in the order sent, and the smallest wait.
3731
+ * @throws ValidationError - 422: no results, more than 25, or an item without a `link_token`.
3732
+ * @throws ServerError - 503 with `Retry-After`: links cannot be resolved right now; try again later.
3733
+ */
3734
+ async resolve(params) {
3735
+ return this.client.request("/v1/google/resolve", {
3736
+ method: "POST",
3737
+ body: { results: params.results }
3738
+ });
3739
+ }
3740
+ /**
3741
+ * Resolve every Google redirect link in a list of results, polling
3742
+ * {@link resolve} until all are known or `maxWaitMs` has passed. Free.
3743
+ *
3744
+ * Pass a search response's `organic_results`. Rows with a `link_token` are
3745
+ * sent, at most 25 per request. Between rounds it waits the
3746
+ * `retry_after_seconds` the API returned (at least one second, never past
3747
+ * the deadline). A row without a `link_token` is returned as it is and
3748
+ * never sent. A row that failed (`error`) is not sent again.
3749
+ *
3750
+ * It does not throw for rows that are still not ready at the deadline:
3751
+ * they are returned with their latest `link_token` and
3752
+ * `retry_after_seconds`, and can be passed to `resolveAll` or
3753
+ * {@link resolve} again. An API error (for example a 503) is thrown as
3754
+ * usual.
3755
+ *
3756
+ * The input array and its objects are not changed.
3757
+ *
3758
+ * @example
3759
+ * ```typescript
3760
+ * const serp = await client.google.search.search({ q: "python 3.13" });
3761
+ * const results = await client.google.search.resolveAll(
3762
+ * serp.organic_results as Record<string, unknown>[]
3763
+ * );
3764
+ * for (const r of results) {
3765
+ * if (r.error) console.log("failed:", r.title, r.error);
3766
+ * else if (r.link_token) console.log("not ready yet:", r.title);
3767
+ * else console.log(r.title, r.link);
3768
+ * }
3769
+ * ```
3770
+ *
3771
+ * @param results - Result objects, normally a search response's `organic_results`.
3772
+ * @param options - `maxWaitMs`: longest time to keep polling (default 240000, four minutes).
3773
+ * @returns A new array with one row per input row, in the same order.
3774
+ */
3775
+ async resolveAll(results, options = {}) {
3776
+ const deadline = Date.now() + (options.maxWaitMs ?? DEFAULT_RESOLVE_MAX_WAIT_MS);
3777
+ const rows = results.map((row) => ({ ...row }));
3778
+ for (; ; ) {
3779
+ const waiting = rows.flatMap((row, index) => isWaiting(row) ? [index] : []);
3780
+ if (waiting.length === 0) return rows;
3781
+ let waitSeconds = null;
3782
+ for (let start = 0; start < waiting.length; start += RESOLVE_MAX_RESULTS) {
3783
+ const chunk = waiting.slice(start, start + RESOLVE_MAX_RESULTS);
3784
+ const response = await this.resolve({
3785
+ results: chunk.map((index) => rows[index])
3786
+ });
3787
+ chunk.forEach((index, n) => {
3788
+ const row = response.results[n];
3789
+ if (row) rows[index] = row;
3790
+ });
3791
+ const retryAfter = response.retry_after_seconds;
3792
+ if (typeof retryAfter === "number" && (waitSeconds === null || retryAfter < waitSeconds)) {
3793
+ waitSeconds = retryAfter;
3794
+ }
3795
+ }
3796
+ if (!rows.some(isWaiting)) return rows;
3797
+ const remainingMs = deadline - Date.now();
3798
+ if (remainingMs <= 0) return rows;
3799
+ const waitMs = Math.max((waitSeconds ?? 0) * 1e3, MIN_RESOLVE_WAIT_MS);
3800
+ await sleep2(Math.min(waitMs, remainingMs));
3801
+ }
3802
+ }
3685
3803
  };
3804
+ function isWaiting(row) {
3805
+ return typeof row.link_token === "string" && row.link_token !== "" && (row.error === void 0 || row.error === null);
3806
+ }
3807
+ function sleep2(ms) {
3808
+ return new Promise((resolve) => setTimeout(resolve, ms));
3809
+ }
3686
3810
 
3687
3811
  // src/google/shopping.ts
3688
3812
  var ShoppingClient = class {
@@ -5337,7 +5461,7 @@ var RequestsClient = class {
5337
5461
  const remainingMs = deadline - Date.now();
5338
5462
  if (remainingMs <= 0) throw new WaitTimeoutError(requestId, timeoutMs);
5339
5463
  const suggestedMs = (state.retryAfterSeconds ?? DEFAULT_POLL_S) * 1e3;
5340
- await sleep2(Math.min(Math.max(suggestedMs, MIN_POLL_MS), remainingMs));
5464
+ await sleep3(Math.min(Math.max(suggestedMs, MIN_POLL_MS), remainingMs));
5341
5465
  }
5342
5466
  }
5343
5467
  /**
@@ -5424,7 +5548,7 @@ function retryAfterSeconds(headers, fromBody) {
5424
5548
  }
5425
5549
  return typeof fromBody === "number" && Number.isFinite(fromBody) && fromBody >= 0 ? fromBody : null;
5426
5550
  }
5427
- function sleep2(ms) {
5551
+ function sleep3(ms) {
5428
5552
  return new Promise((resolve) => setTimeout(resolve, ms));
5429
5553
  }
5430
5554