Skip to content

Instantly share code, notes, and snippets.

@ramann
Created November 13, 2019 00:50
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 ramann/69b843bb20ed9c34a1d99c9b810dc46e to your computer and use it in GitHub Desktop.
Save ramann/69b843bb20ed9c34a1d99c9b810dc46e to your computer and use it in GitHub Desktop.
gotta get that Weller Antique Special Stagg Jr Pappy Van Winkle Reserve Single Barrel 23!!!!!!
/**
* Are you tired of missing out on the latest and greatest alcohols?
* With this one easy script, you can see pretty JSON data about local establishments that cater to your distinguished tastes!
*
* usage: node liquorRun.js zipCodes productId
* example: node liquorRun.js 43240,44118,43065 2941
*/
const request = require('request-promise');
const fs = require('fs');
const vm = require('vm');
const quantity = {
"R": "None Available",
"Y": "Limited Supply",
"G": "In-stock"
};
async function downloadInfo(url, filename) {
const options = {
url: url,
headers: {
'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:70.0) Gecko/20100101 Firefox/70.0'
}
}
ret = "";
await request(options, function (error, response, body) {
if (error) {
console.error('error:', error); // Print the error if one occurred
}
// console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received
// console.log('body:', body);
fs.writeFileSync(filename, body);
ret = body;
});
return ret;
}
function loadStoresInfo(filename) {
const storesSandbox = {};
vm.createContext(storesSandbox);
const storesCode = fs.readFileSync(filename, 'utf8');
vm.runInContext(storesCode, storesSandbox);
stores = {}
for (x of storesSandbox.Agencies) {
store = { "id": x[0],
"name": x[1],
"address": x[2],
"city": x[3],
"state": x[4],
"zip": x[5],
"phone": x[6],
"lat": x[7],
"lon": x[8]
}
stores[store.id] = store;
}
return stores;
}
function loadProductInfo(filename) {
const productSandbox = {};
vm.createContext(productSandbox);
const productCode = fs.readFileSync(filename, 'utf8')
vm.runInContext(productCode,productSandbox);
sizes = { "1.75L": productSandbox.sizeD,
"1L": productSandbox.sizeL,
"750mL": productSandbox.sizeB,
"600mL": productSandbox.sizeM,
"375mL": productSandbox.sizeH,
"200mL": productSandbox.sizeF,
"50mL": productSandbox.sizeE
}
return sizes;
}
function printOutput(stores, products) {
results = []
for (const [key, values] of Object.entries(products)) {
size = key;
for (const [key, value] of Object.entries(values)) {
storeId = key.slice(1)
store = stores[storeId]
/** if (store == undefined) { // this condition occurs when there is a store ID provided for a product, but not a corresponding store in the stores collection
console.log(JSON.stringify(store));
console.log(key);
} */
if (store != undefined && zipCodes.includes(store.zip) && (quantity[value.f] == "Limited Supply" || quantity[value.f] == "In-stock")) {
//console.log(store.name+","+store.address+","+store.zip+","+quantity[value.f]+","+size);
results.push(store);
}
}
}
console.log(results);
}
// Do some basic input error checking
if (process.argv[2].indexOf(',') >= 0) {
process.argv[2].split(',').forEach(function(x) {
if (! /^\d+$/.test(x) ) {
console.log("Exiting. Invalid zip code string.")
process.exit()
}
})
} else if (process.argv[2] == undefined || ! /^\d+$/.test(process.argv[2]) ) {
console.log("Exiting. Invalid zip code string.");
process.exit()
}
zipCodes = process.argv[2].split(',')
if ( process.argv[3] == undefined || ! /^\d+$/.test(process.argv[3]) ) {
console.log("Exiting. Invalid product ID.")
process.exit()
}
(async function() {
try {
await downloadInfo('https://www.ohlq.com/assets/javascripts/jsonDistro.php?d=av2', 'stores.js');
await downloadInfo('https://www.ohlq.com/assets/javascripts/getAgencyInvByProduct.php?code='+process.argv[3], 'product.js');
stores = await loadStoresInfo('stores.js')
sizes = await loadProductInfo('product.js')
printOutput(stores, sizes);
} catch(err) {
console.log(err.stack);
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment