Created
February 6, 2017 20:19
-
-
Save ecaron/1e4851492d58db66ef4c1ef53010234a 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
| 'use strict'; | |
| if (!process.env.API_KEY) { | |
| console.warn('API_KEY must be defined as an environment variable'); | |
| process.exit(); | |
| } | |
| var bby = require('bestbuy')(process.env.API_KEY); | |
| var async = require('async'); | |
| var _ = require('lodash'); | |
| var startTime = new Date(); | |
| var foundProducts = 0; | |
| var queryParams = {show: 'salePrice,name,url', pageSize: 100, cursorMark: '*'}; | |
| var hasMoreProducts = true; | |
| async.whilst( | |
| function () { return hasMoreProducts; }, | |
| function (callback) { | |
| bby.products('', queryParams, function(err, data) { | |
| if (err) return callback(err); | |
| if (data.total === 0 || queryParams.cursorMark === data.nextCursorMark) { | |
| hasMoreProducts = false; | |
| } else { | |
| queryParams.cursorMark = data.nextCursorMark; | |
| _.each(data.products, function(product) { | |
| foundProducts++; | |
| }); | |
| console.log('[%dms] Retreived %d products', new Date() - startTime, foundProducts); | |
| } | |
| return callback(); | |
| }); | |
| }, | |
| function (err) { | |
| if (err) throw err; | |
| else console.log('All done!'); | |
| } | |
| ); |
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
| { | |
| "name": "cursor-sample", | |
| "version": "0.0.1", | |
| "description": "", | |
| "main": "index.js", | |
| "scripts": { | |
| "test": "echo \"Error: no test specified\" && exit 1" | |
| }, | |
| "author": "", | |
| "license": "MIT", | |
| "dependencies": { | |
| "async": "^2.1.4", | |
| "bestbuy": "^1.0.0", | |
| "lodash": "^4.17.3" | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment