Created
February 17, 2015 02:40
-
-
Save rvagg/9977d2bb8a562387279f to your computer and use it in GitHub Desktop.
Nexus 64GB Check
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
| // check if a Nexus 64GB is available | |
| const urls = { | |
| 'White 64GB' : 'https://play.google.com/store/devices/details/Nexus_6_64_GB_Cloud_White?id=nexus_6_white_64gb&psid=3&iphc=true' | |
| //, 'White 32GB' : 'https://play.google.com/store/devices/details/Nexus_6_32_GB_Cloud_White?id=nexus_6_white_32gb&psid=3&iphc=true' | |
| , 'Blue 64GB' : 'https://play.google.com/store/devices/details/Nexus_6_64_GB_Midnight_Blue?id=nexus_6_blue_64gb&psid=3&iphc=true' | |
| //, 'Blue 32GB' : 'https://play.google.com/store/devices/details/Nexus_6_32_GB_Midnight_Blue?id=nexus_6_blue_32gb&psid=3&iphc=true' | |
| } | |
| const hyperquest = require('hyperquest') | |
| , bl = require('bl') | |
| , cheerio = require('cheerio') | |
| , map = require('map-async') | |
| , mandrill = require('mandrill-api/mandrill') // postmark is probably a better choice here | |
| , email = process.env.MANDRILL_KEY && new mandrill.Mandrill(process.env.MANDRILL_KEY) | |
| function checkModel (model, callback) { | |
| hyperquest.get(urls[model]) | |
| .pipe(bl(function (err, _data) { | |
| if (err) | |
| return callback(err) | |
| var $ = cheerio.load(_data) | |
| , available = $('[data-availability]').attr('data-availability') == 'AVAILABLE' | |
| , pricematch = available && $('.buy-button-container span').text().match(/\w\$[\d\.]+/i) | |
| , price = pricematch && pricematch[0] | |
| if (available && !price) | |
| return callback(new Error(`Derp, something's up with the page for ${model}`)) | |
| callback(null, { model: model, price: available && price }) | |
| })) | |
| } | |
| function done (err, availability) { | |
| if (err) | |
| throw err | |
| var msgs = availability.map(function (a) { | |
| return (`Nexus ${a.model} is ` + (a.price ? `available for ${a.price}` : 'not available')) | |
| }) | |
| // if no MANDRILL_KEY env var set, dump to stdout | |
| if (!email) | |
| return console.log(msgs.join('\n')) | |
| // don't send email if nothing available | |
| if (!availability.some(function (a) { return a.price })) | |
| return | |
| var message = { | |
| html : msgs.join('<br>') | |
| , subject : 'Nexus availability' | |
| , from_email : 'rod@vagg.org' | |
| , from_name : 'Rod Vagg' | |
| , to: [{ | |
| email : 'rod@vagg.org' | |
| , name : 'Rod Vagg' | |
| , type : 'to' | |
| }] | |
| } | |
| email.messages.send({ message: message }, function (result) { | |
| if (result[0].status != 'sent') | |
| throw new Error('Email not sent! [' + result[0].status + ']') | |
| }) | |
| } | |
| map(Object.keys(urls), checkModel, done) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment