|
| 1 | +import { loadSync } from "https://deno.land/std@0.173.0/dotenv/mod.ts"; |
| 2 | +import { config, getJson, GoogleParameters } from "../../../mod.ts"; |
| 3 | + |
| 4 | +const { API_KEY: apiKey } = loadSync(); |
| 5 | + |
| 6 | +const extractLinks = (results: { link: string }[]) => |
| 7 | + results.map((r) => r.link); |
| 8 | + |
| 9 | +const params = { |
| 10 | + q: "Coffee", |
| 11 | + api_key: apiKey, |
| 12 | +} satisfies GoogleParameters; |
| 13 | + |
| 14 | +// Pagination (async/await) |
| 15 | +let page1 = await getJson("google", params); |
| 16 | +console.log( |
| 17 | + "First page links", |
| 18 | + extractLinks(page1.organic_results), |
| 19 | +); |
| 20 | +let page2 = await page1.next?.(); |
| 21 | +console.log( |
| 22 | + "Second page links", |
| 23 | + extractLinks(page2?.organic_results), |
| 24 | +); |
| 25 | + |
| 26 | +// Pagination (callback) |
| 27 | +getJson("google", params, (page1) => { |
| 28 | + console.log( |
| 29 | + "First page links", |
| 30 | + extractLinks(page1.organic_results), |
| 31 | + ); |
| 32 | + page1.next?.((page2) => { |
| 33 | + console.log( |
| 34 | + "Second page links", |
| 35 | + extractLinks(page2.organic_results), |
| 36 | + ); |
| 37 | + }); |
| 38 | +}); |
| 39 | + |
| 40 | +// Use global config |
| 41 | +config.api_key = apiKey; |
| 42 | +page1 = await getJson("google", { q: "Coffee" }); |
| 43 | +page2 = await page1.next?.(); |
| 44 | +console.log( |
| 45 | + "Second page links", |
| 46 | + extractLinks(page2?.organic_results), |
| 47 | +); |
| 48 | + |
| 49 | +// Pagination loop (async/await) |
| 50 | +let links: string[] = []; |
| 51 | +let page; |
| 52 | +page = await getJson("google", { q: "Coffee" }); |
| 53 | +while (page) { |
| 54 | + links.push(...extractLinks(page.organic_results)); |
| 55 | + if (links.length >= 30) break; |
| 56 | + page = await page.next?.(); |
| 57 | +} |
| 58 | +console.log(links); |
| 59 | + |
| 60 | +// Pagination loop (callback) |
| 61 | +links = []; |
| 62 | +getJson("google", { q: "Coffee" }, (page) => { |
| 63 | + links.push(...extractLinks(page.organic_results)); |
| 64 | + if (links.length < 30 && page.next) { |
| 65 | + page.next(); |
| 66 | + } else { |
| 67 | + console.log(links); |
| 68 | + } |
| 69 | +}); |
0 commit comments