stophy 1.2.2 → 1.4.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 +68 -56
- package/dist/index.cjs +203 -436
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +4961 -15450
- package/dist/index.d.ts +4961 -15450
- package/dist/index.js +203 -436
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -13,118 +13,130 @@ npm install stophy
|
|
|
13
13
|
```ts
|
|
14
14
|
import { Stophy } from "stophy";
|
|
15
15
|
|
|
16
|
-
const
|
|
17
|
-
|
|
16
|
+
const stophy = new Stophy();
|
|
17
|
+
|
|
18
|
+
const page = await stophy.scrape({ url: "https://example.com", formats: ["markdown"] });
|
|
19
|
+
console.log(page.data.markdown);
|
|
20
|
+
|
|
21
|
+
const results = await stophy.search({ query: "bun runtime", limit: 3 });
|
|
22
|
+
console.log(results.data?.web);
|
|
18
23
|
```
|
|
19
24
|
|
|
20
|
-
|
|
25
|
+
`scrape`, `search`, `map` and `parse` work without a key, within a small free allowance, and so do `parsers`, `endpoints` and `signup`. Every other method throws a `StophyError` with the code `apiKeyRequired`.
|
|
21
26
|
|
|
22
27
|
## Use an API key
|
|
23
28
|
|
|
24
|
-
Get a key
|
|
29
|
+
Get a key at [stophy.dev/signup](https://stophy.dev/signup). Set it as `STOPHY_API_KEY`, or pass it in:
|
|
25
30
|
|
|
26
31
|
```ts
|
|
27
32
|
const stophy = new Stophy({ apiKey: "st_..." });
|
|
28
|
-
|
|
29
|
-
const videos = await stophy.youtube.search({ query: "bun runtime" });
|
|
30
|
-
console.log(videos.data.results);
|
|
31
33
|
```
|
|
32
34
|
|
|
33
35
|
`new Stophy("st_...")` works too.
|
|
34
36
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
To point at one thing, send its link or its id, never both: `stophy.youtube.video({ videoUrl })` or `stophy.youtube.video({ videoId })`. Methods that take a person send `userUrl` or `username`.
|
|
38
|
-
|
|
39
|
-
Every response is `{ success, data, creditsUsed, requestId }`. `data` is one flat object. Lists are in `data.results`.
|
|
37
|
+
Each method sends one call to the [Stophy API](https://api.stophy.dev/v1/endpoints) and returns its JSON as is. `scrape` returns `{ success, data }`, and `map` returns `{ success, links }`.
|
|
40
38
|
|
|
41
|
-
##
|
|
39
|
+
## Read a site with a parser
|
|
42
40
|
|
|
43
|
-
A
|
|
41
|
+
A parser reads a site's page and returns its data as JSON: Reddit posts, Google Maps places, Amazon products, YouTube videos and more. Send the page URL and the parser's name:
|
|
44
42
|
|
|
45
43
|
```ts
|
|
46
|
-
const
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
console.log(transcript.data.text, transcript.data.segments);
|
|
53
|
-
|
|
54
|
-
const posts = await stophy.reddit.search({ query: "bun runtime", sort: "top", within: "month" });
|
|
55
|
-
console.log(posts.data.results);
|
|
44
|
+
const places = await stophy.parsed(
|
|
45
|
+
"google-maps",
|
|
46
|
+
"https://www.google.com/maps/search/dentists+in+Ljubljana",
|
|
47
|
+
);
|
|
48
|
+
console.log(places.data.json);
|
|
49
|
+
```
|
|
56
50
|
|
|
57
|
-
|
|
58
|
-
const placeId = places.data.results[0]?.placeId;
|
|
59
|
-
if (placeId) {
|
|
60
|
-
const reviews = await stophy.google.maps.reviews({ placeId });
|
|
61
|
-
console.log(reviews.data.results);
|
|
62
|
-
}
|
|
51
|
+
`parsed` calls `scrape` with the format `{ type: "json", parser }`. The parser name is typed, so your editor lists every parser. `await stophy.parsers()` returns each parser with the page it reads and what it returns.
|
|
63
52
|
|
|
64
|
-
|
|
65
|
-
console.log(profile.data.followers, profile.data.results);
|
|
53
|
+
The third argument sends the parser's options, such as the next page:
|
|
66
54
|
|
|
67
|
-
|
|
68
|
-
|
|
55
|
+
```ts
|
|
56
|
+
const next = await stophy.parsed(
|
|
57
|
+
"google-maps",
|
|
58
|
+
"https://www.google.com/maps/search/dentists+in+Ljubljana",
|
|
59
|
+
{ page: 2 },
|
|
60
|
+
);
|
|
69
61
|
```
|
|
70
62
|
|
|
71
|
-
|
|
63
|
+
Paging needs an API key.
|
|
72
64
|
|
|
73
|
-
|
|
65
|
+
## Crawl a site
|
|
74
66
|
|
|
75
|
-
|
|
67
|
+
`crawl` answers at once with an id. Ask `crawl.status` for the pages:
|
|
76
68
|
|
|
77
69
|
```ts
|
|
78
|
-
const
|
|
79
|
-
|
|
70
|
+
const job = await stophy.crawl({ url: "https://bun.com/docs", limit: 20 });
|
|
71
|
+
if (job.id) {
|
|
72
|
+
const status = await stophy.crawl.status({ id: job.id });
|
|
73
|
+
console.log(status.status, status.completed, status.total, status.data);
|
|
74
|
+
|
|
75
|
+
const cursor = status.next && new URL(status.next).searchParams.get("cursor");
|
|
76
|
+
if (cursor) {
|
|
77
|
+
const more = await stophy.crawl.status({ id: job.id, cursor });
|
|
78
|
+
console.log(more.data);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
80
81
|
```
|
|
81
82
|
|
|
82
|
-
|
|
83
|
+
`status` is `scraping` until the crawl is `completed`. While pages are left to read, the answer has `next`, a link with a `cursor`. Send that cursor with the same id to get only the pages finished since. `crawl.cancel({ id })` stops the crawl and keeps the pages already read. A crawl and its pages are kept for 24 hours.
|
|
84
|
+
|
|
85
|
+
`batch.scrape` takes a list of URLs and works the same way, with `batch.scrape.status` and `batch.scrape.cancel`.
|
|
86
|
+
|
|
87
|
+
## Extract fields from pages
|
|
83
88
|
|
|
84
89
|
```ts
|
|
85
|
-
const
|
|
86
|
-
|
|
87
|
-
|
|
90
|
+
const job = await stophy.extract({
|
|
91
|
+
urls: ["https://example.com"],
|
|
92
|
+
prompt: "What is this page for?",
|
|
93
|
+
});
|
|
94
|
+
if (job.id) {
|
|
95
|
+
const result = await stophy.extract.result({ id: job.id });
|
|
96
|
+
console.log(result.data, result.confidence);
|
|
88
97
|
}
|
|
89
98
|
```
|
|
90
99
|
|
|
100
|
+
`extract.result` throws a `StophyError` with the code `notFound` until the result is ready. Results are kept for 24 hours.
|
|
101
|
+
|
|
91
102
|
## Handle errors
|
|
92
103
|
|
|
93
104
|
```ts
|
|
94
105
|
import { StophyError } from "stophy";
|
|
95
106
|
|
|
96
107
|
try {
|
|
97
|
-
await stophy.
|
|
108
|
+
await stophy.scrape({ url: "not a url" });
|
|
98
109
|
} catch (error) {
|
|
99
110
|
if (error instanceof StophyError) {
|
|
100
|
-
console.log(error.code, error.
|
|
111
|
+
console.log(error.code, error.message, error.retryable);
|
|
101
112
|
}
|
|
102
113
|
}
|
|
103
114
|
```
|
|
104
115
|
|
|
105
|
-
`StophyError` has `code`, `message`, `retryable`, `retryAfterSeconds`, `status`, and `
|
|
116
|
+
`StophyError` has `code`, `message`, `retryable`, `retryAfterSeconds`, `status`, `requestId`, and `details`. The message says what went wrong and what to do next.
|
|
106
117
|
|
|
107
|
-
The SDK retries network errors and
|
|
118
|
+
The SDK retries network errors, timeouts, and every error the API marks `retryable`, and it waits as long as the API asks. If the API asks for a wait longer than 60 seconds, the SDK throws right away with `retryAfterSeconds` set.
|
|
108
119
|
|
|
109
120
|
## Options
|
|
110
121
|
|
|
111
|
-
| Option | Default | What it does
|
|
112
|
-
| ------------ | ------------------------------------ |
|
|
113
|
-
| `apiKey` | `STOPHY_API_KEY` | Your API key
|
|
114
|
-
| `baseUrl` | `STOPHY_BASE_URL`, or the Stophy API | The server to call
|
|
115
|
-
| `maxRetries` | `2` | Retries per request. `0` turns retries off.
|
|
116
|
-
| `timeoutMs` | `70000` | Time limit for each attempt. A timed-out attempt is
|
|
122
|
+
| Option | Default | What it does |
|
|
123
|
+
| ------------ | ------------------------------------ | --------------------------------------------------------------------------------------------- |
|
|
124
|
+
| `apiKey` | `STOPHY_API_KEY` | Your API key |
|
|
125
|
+
| `baseUrl` | `STOPHY_BASE_URL`, or the Stophy API | The server to call |
|
|
126
|
+
| `maxRetries` | `2` | Retries per request. `0` turns retries off. |
|
|
127
|
+
| `timeoutMs` | `70000` | Time limit for each attempt. A timed-out attempt is retried when the call carries an API key. |
|
|
117
128
|
|
|
118
|
-
Each method also takes `signal` to cancel the request
|
|
129
|
+
Each method also takes `signal` to cancel the request, and `idempotencyKey` to choose the key of one `POST`.
|
|
119
130
|
|
|
120
131
|
## Check your account
|
|
121
132
|
|
|
122
133
|
```ts
|
|
134
|
+
const limits = await stophy.limits();
|
|
123
135
|
const usage = await stophy.usage();
|
|
124
|
-
const logs = await stophy.logs({ days: 7
|
|
136
|
+
const logs = await stophy.logs({ days: 7 });
|
|
125
137
|
```
|
|
126
138
|
|
|
127
|
-
`
|
|
139
|
+
`limits` returns your balance in dollars as `balanceUsd`, and your rate limit. `usage` returns your all-time usage. `logs` returns your recent calls. All three need an API key.
|
|
128
140
|
|
|
129
141
|
## License
|
|
130
142
|
|