Skip to content

Instantly share code, notes, and snippets.

@flesch

flesch/db.json Secret

Last active June 29, 2021 20:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save flesch/b1c0c984ef7dc1d6a292c2609d8f1818 to your computer and use it in GitHub Desktop.
Save flesch/b1c0c984ef7dc1d6a292c2609d8f1818 to your computer and use it in GitHub Desktop.
Old Navy: undefined • undefined
{
"url": "https://oldnavy.gap.com/browse/product.do?pid=550822002",
"date": "2021-06-29T20:44:09.008Z"
}
const { JSDOM } = require('jsdom');
const Gists = require('gists');
const IFTTT = require('node-ifttt-maker');
const gists = new Gists({ token: process.env.GITHUB_GIST_ACCESS_TOKEN });
const ifttt = new IFTTT(process.env.IFTTT_MAKER_KEY);
(async () => {
try {
const url = 'https://oldnavy.gap.com/browse/product.do?pid=139537002';
const { window } = await JSDOM.fromURL(url);
const [name, currentPrice] = Array.from(window.document.querySelectorAll('h1.product-title, h5.product-price')).map((node) => {
return node.textContent.trim();
});
const { body } = await gists.get('b1c0c984ef7dc1d6a292c2609d8f1818');
const { price: previousPrice } = JSON.parse(body.files['db.json'].content);
if (currentPrice !== previousPrice) {
const iftttResponse = await ifttt.request({
event: 'old_navy_price_change',
params: {
value1: name,
value2: currentPrice,
value3: previousPrice,
},
});
}
console.log({ name, currentPrice, previousPrice });
const db = await gists.edit('b1c0c984ef7dc1d6a292c2609d8f1818', {
description: `Old Navy: ${name} • ${currentPrice}`,
files: {
'db.json': {
content: JSON.stringify({ name, price: currentPrice, url, date: new Date().toISOString() }, null, 2),
},
},
});
} catch (e) {
console.error(e);
}
})();
{
"name": "old-navy-mlb-shirt",
"version": "1.0.0",
"description": "Trigger an IFTTT notification if the price of my favorite shirt changes.",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "John Flesch <john@fles.ch> (https://fles.ch)",
"license": "MIT",
"dependencies": {
"gists": "^2.0.0",
"jsdom": "^12.0.0",
"node-ifttt-maker": "^1.0.2"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment