Skip to content

Instantly share code, notes, and snippets.

@drewfranz
Created June 21, 2018 22:43
Show Gist options
  • Save drewfranz/d256b7be9a421d186dc8d5b49a2ddf62 to your computer and use it in GitHub Desktop.
Save drewfranz/d256b7be9a421d186dc8d5b49a2ddf62 to your computer and use it in GitHub Desktop.
var request = require("request-json");
var url = require("url");
const fetch = require('node-fetch');
const logger = require('heroku-logger');
const newrelic = require ('newrelic');
const express = require('express')
const path = require('path')
const PORT = process.env.PORT || 5000
var TILL_URL = url.parse(process.env.TILL_URL);
var TILL_BASE = TILL_URL.protocol + "//" + TILL_URL.host;
var TILL_PATH = TILL_URL.pathname;
const timeout = (60 * 1000) * 20; //20 minutes
//const timeout = (60 * 1000) * 1; //1 minutes
var counter = 0;
var id = encodeURI('MEC02');
const intervalObj = setInterval(intervalFunc, timeout);
if(TILL_URL.query != null) {
TILL_PATH += "?"+TILL_URL.query;
}
function intervalFunc() {
fetch("https://shop.fantasyflightgames.com/api/v1/stockrecord/" + id + "/level/").then(function(res) {
return res.json();
}).then(function(data) {
// Check the response data for in stock value.
logger.log('info', 'message', { in_stock: data.in_stock });
// If out of stock jump out of function.
if (data.in_stock === "outofstock") {
return false;
}
// If it's in stock we want to track how many times we send SMS messages.
counter++;
if (counter > 5) {
// After 5 SMS messages stop the process.
clearInterval(intervalObj);
logger.log('info', 'clearing intervalObj', { counter: counter });
}
// Send the SMS
request.createClient(TILL_BASE).post(TILL_PATH, {
"phone": ["14253084426"],
"text": "FFG - LOTR is " + data.in_stock,
}, function(err, res, body) {
return logger.log('info', 'message', { code: res.statusCode });
});
});
};
intervalFunc();
express()
.use(express.static(path.join(__dirname, 'public')))
.set('views', path.join(__dirname, 'views'))
.set('view engine', 'ejs')
.get('/', (req, res) => res.render('pages/index'))
.get('/page1', (req, res) => res.render('pages/page1'))
.listen(PORT, () => console.log(`Listening on ${ PORT }`));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment