Skip to content

Commit

Permalink
Compare Product Prices Example (#78)
Browse files Browse the repository at this point in the history
* Update

* Update

* Update

* Update

* Update

* Update

* Update

* Update

---------

Co-authored-by: R L Nabors <rachelnabors@users.noreply.github.com>
  • Loading branch information
jinyangTF and rachelnabors authored Oct 31, 2024
1 parent 2b5f1b4 commit 2bb7af2
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
17 changes: 17 additions & 0 deletions javascript-sdk/examples/compare-product-prices/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Example script: comparing product price across websites with AgentQL

This example demonstrates how to compare product prices across websites with queryData() method.

## Run the script

- [Install AgentQL SDK](https://docs.agentql.com/javascript-sdk/installation)
- Save this JavaScript file locally as **compare-product-prices.js**
- Run the following command from the project's folder:

```bash
node compare-product-prices.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)
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
const { wrap } = require('agentql');
const { chromium } = require('playwright');

// Set the URL to the desired website
const BESTBUY_URL = 'https://www.bestbuy.com';
const EBAY_URL = 'https://www.ebay.com';
const TELQUEST_URL = 'https://www.telquestintl.com';

// Define the queries to interact with the page
const HOME_PAGE_QUERY = `
{
search_input
search_button
}
`;

const PRODUCT_INFO_QUERY = `
{
product_price (for Nintendo Switch - OLed Model - w/ White Joy-Con)
}
`;

/**
* Open the given URL in a new tab and fetch the price of the product.
*/
async function fetchPrice(context, sessionUrl) {
// Create a page in a new tab in the broswer context and wrap it to get access to the AgentQL's querying API
const page = wrap(await context.newPage());
await page.goto(sessionUrl);

// Search for the product
await page.waitForLoadState('networkidle');
const homeResponse = await page.queryElements(HOME_PAGE_QUERY);
await homeResponse.search_input.fill('Nintendo Switch - OLED Model White');
await homeResponse.search_button.click();

// Fetch the price data from the page
const data = await page.queryData(PRODUCT_INFO_QUERY);
return data.product_price;
}

/**
* Fetch prices concurrently in the same browser session from multiple websites.
*/
async function getPriceAcrossWebsites() {
const browser = await chromium.launch({ headless: false });
const context = await browser.newContext();

// Open multiple tabs in the same browser context to fetch prices concurrently
const [bestbuyPrice, ebayPrice, telquestPrice] = await Promise.all([
fetchPrice(context, BESTBUY_URL),
fetchPrice(context, EBAY_URL),
fetchPrice(context, TELQUEST_URL),
]);

console.log(`
Price at BestBuy: ${bestbuyPrice}
Price at eBay: ${ebayPrice}
Price at Telquest: ${telquestPrice}
`);

await browser.close();
}

getPriceAcrossWebsites();

0 comments on commit 2bb7af2

Please sign in to comment.