Last active
November 10, 2017 18:45
-
-
Save adambankin/5ec7eaf471e6935fa48b6e36d95c6dff to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var http = require('http'); | |
var url = require('url'); | |
var querystring = require('querystring'); | |
var stores = [191, 192, 193, 194, 195, 15173, 15171, 15174]; | |
var opts = url.parse('http://www.istocknow.com/live/live.php?type=7Plus&operator=simfree&color=Black&model=128GB&ajax=1&nocache=1476915136992&nobb=false¬arget=false&noradioshack=false&nostock=false'); | |
opts.headers = { | |
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.59 Safari/537.36" | |
}; | |
function createOptions (nyc) { | |
var secret = ''; | |
var postData = querystring.stringify({ 'value1' : nyc.length ? JSON.stringify(nyc) : 'none' }); | |
var options = { | |
hostname: 'maker.ifttt.com', | |
port: 80, | |
path: `/trigger/iphone_7_stock/with/key/${secret}`, | |
method: 'POST', | |
headers: { | |
'Content-Type': 'application/x-www-form-urlencoded', | |
'Content-Length': Buffer.byteLength(postData), | |
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.59 Safari/537.36" | |
} | |
}; | |
return { obj, postData }; | |
} | |
function handleResponse (response) { | |
const nyc = stores | |
.map(num => response.dataz['' + num]) | |
.filter(obj => parseInt(obj.live, 10)); | |
if (nyc.length === 0) { | |
return; | |
} | |
const { obj, postData } = createOptions(nyc); | |
const req = http.request(options, res => { | |
res.setEncoding('utf8'); | |
res.on('data', chunk => { console.log(`BODY: ${chunk}`); }); | |
res.on('end', () => { console.log('No more data in response.'); }); | |
}); | |
req.on('error', (e) => { console.log(`problem with request: ${e.message}`); }); | |
req.write(postData); | |
req.end(); | |
} | |
function getData () { | |
const req = http.request(opts, res => { | |
const stream = []; | |
res.setEncoding('utf8'); | |
res.on('data', chunk => { stream.push(chunk); }); | |
res.on('end', () => { handleResponse(JSON.parse(stream.join(''))); }); | |
}) | |
req.on('error', e => { console.log(`problem with request: ${e.message}`); }); | |
req.end(); | |
} | |
getData(); | |
// setInterval(getData, 60000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment