check avail

node v14.18.1
version: 5.2.1
endpointsharetweet
const models = { hk: { "watch7": { "offers": [ { "@type": "Offer", "priceCurrency": "HKD", "price": 3399.00, "sku": "MKN83ZP/A" }, { "@type": "Offer", "priceCurrency": "HKD", "price": 3399.00, "sku": "MKN63ZP/A" }, { "@type": "Offer", "priceCurrency": "HKD", "price": 3399.00, "sku": "MKN53ZP/A" } ] }, }, tw: {} } const stores = { hk: [ "R409", "R428", "R499", "R673", "R485", "R610" ], tw: [ 'R713', 'R694' ] }
const axios = require("axios") const REGION = 'hk' const CONFIG = { region: REGION, models: models[REGION]['watch7'].offers.map(x => x.sku), stores: stores[REGION], headers: { 'X-Ma-Pcmh': 'REL-5.13.0', 'X-Deviceconfiguration': 'ss=3.00;dim=1125x2436;m=iPhone;v=iPhone10,6;vv=5.13;sv=13.5' } } async function getPickupList() { const { region, models, stores, headers } = CONFIG const requests = [] const url = ({ m, s, region }) => `https://mobileapp.apple.com/mnm/p/${region}/pickup/quote/${m}?partNumber=${m}&store=${s}` models.forEach(m => { stores.forEach(s => { const request = url({ region, m, s }) requests.push(request) }) }) const results = await Promise.all(requests.map(r => { return axios.get(r, { headers: CONFIG.headers }).then(res => res.data) })) const all = results.map(x => x) const available = all .filter(x => x.availabilityStatus !== 'NOT_AVAILABLE') const items = available.map(x => { const model = x.partNumber const name = RegExp('GPS.*(?= Case)', 'g').exec(x.omniture.trackPage.eVar4) || model return { formatted: `${name} - ${x.pickupQuote}`, link: `https://store.apple.com/${region}/xc/product/${model}` } }) return items } function lastUpdate() { const now = new Date() const lastUpdate = now.toLocaleTimeString() return lastUpdate } async function sendEmail(payload) { const url = process.env.EMAILAPI const options = { text: JSON.stringify(payload, null, 2) } try { const { data } = await axios.post(url, options) console.log({ payload, data }) return data } catch (error) { return { error: error.message } } }
exports.endpoint = async function (request, res) { try { const list = await getPickupList() if (list.length > 0) { await sendEmail(list) } res.end(JSON.stringify(list, null, 2)) } catch (err) { res.end(err.message) } }
Loading…

no comments

    sign in to comment