From 56d7f7ed7df04fd2823eb5051b993a2855524aaa Mon Sep 17 00:00:00 2001 From: urvishthakker15 Date: Tue, 29 Oct 2024 11:30:47 -0700 Subject: [PATCH 1/7] Add js example for list query usage --- .../examples/list-query-usage/README.md | 18 +++++++++ .../list-query-usage/list-query-usage.js | 37 +++++++++++++++++++ .../list-query-usage/products_data.csv | 17 +++++++++ 3 files changed, 72 insertions(+) create mode 100644 javascript-sdk/examples/list-query-usage/products_data.csv diff --git a/javascript-sdk/examples/list-query-usage/README.md b/javascript-sdk/examples/list-query-usage/README.md index e69de29..c0da98d 100644 --- a/javascript-sdk/examples/list-query-usage/README.md +++ b/javascript-sdk/examples/list-query-usage/README.md @@ -0,0 +1,18 @@ +# Example script: querying a list of items with AgentQL + +This example demonstrates how to query a list of items on the page. + + +## Run the script + +- [Install AgentQL SDK](https://agentql-docs-opvm8e9cd-tiny-fish.vercel.app/javascript-sdk/installation) +- Save this javascript file locally as **list-query-usage.js** +- Run the following command from the project's folder: + +```bash +node list-query-usage.js +``` + +## Play with the query + +Install the [AgentQL Debugger Chrome extension](https://docs.agentql.com/installation/chrome-extension-installation) to play with the AgentQL query. [Learn more about the AgentQL query language](https://docs.agentql.com/agentql-query/query-intro) diff --git a/javascript-sdk/examples/list-query-usage/list-query-usage.js b/javascript-sdk/examples/list-query-usage/list-query-usage.js index e69de29..19cdb30 100644 --- a/javascript-sdk/examples/list-query-usage/list-query-usage.js +++ b/javascript-sdk/examples/list-query-usage/list-query-usage.js @@ -0,0 +1,37 @@ +const { wrap, configure } = require('agentql'); +const { chromium } = require('playwright'); +const path = require('path'); +const fs = require('fs'); + +(async () => { + // Set the AgentQL API key via the `configure` method. + configure({ apiKey: 'Y6oyGdvZD_wRCMTVMQ9SEQoJTkUhZUaDyICLsXiaSeyDLx_FfrF9jA' }); + + const browser = await chromium.launch({ headless: false }); + const page = wrap(await browser.newPage()); // Wraps the Playwright Page to access AgentQL's features. + + await page.goto('https://scrapeme.live/shop/'); + + const QUERY = ` + { + products[] + { + product_name + price + } + } + `; + + const response = await page.queryData(QUERY); + + const scriptDir = path.dirname(__filename); + const csvFilePath = path.join(scriptDir, 'products_data.csv'); + let csvContent = 'Products Name, Price\n'; + + response.products.forEach((product) => { + csvContent += `${product.product_name},${product.price}\n`; + }); + + fs.writeFileSync(csvFilePath, csvContent, 'utf-8'); + await browser.close(); +})(); diff --git a/javascript-sdk/examples/list-query-usage/products_data.csv b/javascript-sdk/examples/list-query-usage/products_data.csv new file mode 100644 index 0000000..ae5d8c8 --- /dev/null +++ b/javascript-sdk/examples/list-query-usage/products_data.csv @@ -0,0 +1,17 @@ +Products Name, Price +Bulbasaur,£63.00 +Ivysaur,£87.00 +Venusaur,£105.00 +Charmander,£48.00 +Charmeleon,£165.00 +Charizard,£156.00 +Squirtle,£130.00 +Wartortle,£123.00 +Blastoise,£76.00 +Caterpie,£73.00 +Metapod,£148.00 +Butterfree,£162.00 +Weedle,£25.00 +Kakuna,£148.00 +Beedrill,£168.00 +Pidgey,£159.00 From 4c197d850998845efcf2bb3751f97ff4bcc1675b Mon Sep 17 00:00:00 2001 From: urvishthakker15 Date: Tue, 29 Oct 2024 11:32:54 -0700 Subject: [PATCH 2/7] lint --- javascript-sdk/examples/list-query-usage/README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/javascript-sdk/examples/list-query-usage/README.md b/javascript-sdk/examples/list-query-usage/README.md index c0da98d..1d9c8b9 100644 --- a/javascript-sdk/examples/list-query-usage/README.md +++ b/javascript-sdk/examples/list-query-usage/README.md @@ -2,7 +2,6 @@ This example demonstrates how to query a list of items on the page. - ## Run the script - [Install AgentQL SDK](https://agentql-docs-opvm8e9cd-tiny-fish.vercel.app/javascript-sdk/installation) From 1d9491f24bb5fb5d1b5eb0042015ebe8e8dcdc62 Mon Sep 17 00:00:00 2001 From: urvishthakker15 Date: Tue, 29 Oct 2024 12:00:16 -0700 Subject: [PATCH 3/7] Address security concerns --- javascript-sdk/examples/list-query-usage/list-query-usage.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/javascript-sdk/examples/list-query-usage/list-query-usage.js b/javascript-sdk/examples/list-query-usage/list-query-usage.js index 19cdb30..79d313b 100644 --- a/javascript-sdk/examples/list-query-usage/list-query-usage.js +++ b/javascript-sdk/examples/list-query-usage/list-query-usage.js @@ -4,8 +4,9 @@ const path = require('path'); const fs = require('fs'); (async () => { + // Set the AgentQL API key via the `configure` method. - configure({ apiKey: 'Y6oyGdvZD_wRCMTVMQ9SEQoJTkUhZUaDyICLsXiaSeyDLx_FfrF9jA' }); + configure({ apiKey: process.env.AGENTQL_API_KEY }); const browser = await chromium.launch({ headless: false }); const page = wrap(await browser.newPage()); // Wraps the Playwright Page to access AgentQL's features. From 2b5b1332e981e6fae4c001606ae6dbb4accf3a2e Mon Sep 17 00:00:00 2001 From: urvishthakker15 Date: Tue, 29 Oct 2024 12:01:25 -0700 Subject: [PATCH 4/7] lint --- javascript-sdk/examples/list-query-usage/list-query-usage.js | 1 - 1 file changed, 1 deletion(-) diff --git a/javascript-sdk/examples/list-query-usage/list-query-usage.js b/javascript-sdk/examples/list-query-usage/list-query-usage.js index 79d313b..0d26abd 100644 --- a/javascript-sdk/examples/list-query-usage/list-query-usage.js +++ b/javascript-sdk/examples/list-query-usage/list-query-usage.js @@ -4,7 +4,6 @@ const path = require('path'); const fs = require('fs'); (async () => { - // Set the AgentQL API key via the `configure` method. configure({ apiKey: process.env.AGENTQL_API_KEY }); From 1089f2913fcd2509c4cb512c2a9607fa6fc82847 Mon Sep 17 00:00:00 2001 From: urvishthakker15 Date: Tue, 29 Oct 2024 14:51:35 -0700 Subject: [PATCH 5/7] Add review comments --- javascript-sdk/examples/list-query-usage/README.md | 4 ++-- javascript-sdk/examples/list-query-usage/list-query-usage.js | 2 -- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/javascript-sdk/examples/list-query-usage/README.md b/javascript-sdk/examples/list-query-usage/README.md index 1d9c8b9..59cc3ed 100644 --- a/javascript-sdk/examples/list-query-usage/README.md +++ b/javascript-sdk/examples/list-query-usage/README.md @@ -4,8 +4,8 @@ This example demonstrates how to query a list of items on the page. ## Run the script -- [Install AgentQL SDK](https://agentql-docs-opvm8e9cd-tiny-fish.vercel.app/javascript-sdk/installation) -- Save this javascript file locally as **list-query-usage.js** +- [Install AgentQL SDK](https://docs.agentql.com/javascript-sdk/installation) +- Save this Javascript file locally as **list-query-usage.js** - Run the following command from the project's folder: ```bash diff --git a/javascript-sdk/examples/list-query-usage/list-query-usage.js b/javascript-sdk/examples/list-query-usage/list-query-usage.js index 0d26abd..4bf905b 100644 --- a/javascript-sdk/examples/list-query-usage/list-query-usage.js +++ b/javascript-sdk/examples/list-query-usage/list-query-usage.js @@ -4,8 +4,6 @@ const path = require('path'); const fs = require('fs'); (async () => { - // Set the AgentQL API key via the `configure` method. - configure({ apiKey: process.env.AGENTQL_API_KEY }); const browser = await chromium.launch({ headless: false }); const page = wrap(await browser.newPage()); // Wraps the Playwright Page to access AgentQL's features. From 92c8a2377ad807b758e84ddd1976e77428662c13 Mon Sep 17 00:00:00 2001 From: urvishthakker15 Date: Tue, 29 Oct 2024 14:52:59 -0700 Subject: [PATCH 6/7] Lint --- javascript-sdk/examples/list-query-usage/list-query-usage.js | 1 - 1 file changed, 1 deletion(-) diff --git a/javascript-sdk/examples/list-query-usage/list-query-usage.js b/javascript-sdk/examples/list-query-usage/list-query-usage.js index 4bf905b..c2bb337 100644 --- a/javascript-sdk/examples/list-query-usage/list-query-usage.js +++ b/javascript-sdk/examples/list-query-usage/list-query-usage.js @@ -4,7 +4,6 @@ const path = require('path'); const fs = require('fs'); (async () => { - const browser = await chromium.launch({ headless: false }); const page = wrap(await browser.newPage()); // Wraps the Playwright Page to access AgentQL's features. From 7ce4b0065cc8b8177d459859405881217d164838 Mon Sep 17 00:00:00 2001 From: urvishthakker15 Date: Tue, 29 Oct 2024 14:55:06 -0700 Subject: [PATCH 7/7] Lint --- javascript-sdk/examples/list-query-usage/list-query-usage.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/javascript-sdk/examples/list-query-usage/list-query-usage.js b/javascript-sdk/examples/list-query-usage/list-query-usage.js index c2bb337..4abcf78 100644 --- a/javascript-sdk/examples/list-query-usage/list-query-usage.js +++ b/javascript-sdk/examples/list-query-usage/list-query-usage.js @@ -1,4 +1,4 @@ -const { wrap, configure } = require('agentql'); +const { wrap } = require('agentql'); const { chromium } = require('playwright'); const path = require('path'); const fs = require('fs');