Skip to content

Instantly share code, notes, and snippets.

@jaredwy
Created August 8, 2019 00:23
Show Gist options
  • Save jaredwy/1a9017c5d6f829c689cd23c0e0a47b3e to your computer and use it in GitHub Desktop.
Save jaredwy/1a9017c5d6f829c689cd23c0e0a47b3e to your computer and use it in GitHub Desktop.
const fs = require("fs");
const https = require("https");
const UNKNOWN = "UNKNOWN";
function getValue(obj) {
if(!obj) {
return UNKNOWN;
}
return obj.Value;
}
function getPrice(element) {
return getValue(element.Prices.singleprice);
}
function getValueFromAdditional(element, key) {
const obj = element.AdditionalDetails.find(x => {
return x.Name == key;
});
return getValue(obj);
}
function hasAward(product) {
const obj = element.AdditionalDetails.find(x => {
return x.Name == "awardWinner";
});
}
function get_gin_info(gins) {
return gins.Bundles.map(element => {
const product = element.Products[0];
return {
name: getValueFromAdditional(product, "producttitle"),
company: getValueFromAdditional(product, "webbrandname"),
country: getValueFromAdditional(product, "webcountryoforigin"),
abv: getValueFromAdditional(product, "webalcoholpercentage"),
cost: getPrice(product),
size: getValueFromAdditional(product, "webliquorsize"),
has_award: getValueFromAdditional(product, "awardwinner") != UNKNOWN
}
});
}
const data = {
"department":"spirits",
"subDepartment":"gin",
"filters":[],
"pageNumber":1,
"pageSize":20000,
"sortType":"Relevance",
"Location":"ListerFacet"
};
const options = {
hostname: "api.danmurphys.com.au",
path: "/apis/ui/Browse",
method: "POST",
headers: {
"Content-Type": "application/json",
"Referer": "https://www.danmurphys.com.au/spirits/gin",
"Content-Length" : Buffer.byteLength(JSON.stringify(data), "utf-8")
},
port: 443,
protocol: "https:"
};
const req = https.request(options, (res) => {
let json = "";
res.setEncoding('utf8');
res.on("data", (chunk) => {
json += chunk;
});
res.on('end', () => {
const gin_info = get_gin_info(JSON.parse(json));
fs.writeFileSync("gin_network.json", JSON.stringify(gin_info, null, 2));
});
});
req.write(JSON.stringify(data),"utf-8");
req.end();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment