Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
node-cratejoy sample
var _ = require('lodash');
var cratejoyAPI = require('node-cratejoy');
var cratejoy = new cratejoyAPI('{API Id Here}', '{API Key Here}');
var fs = require('fs');
// var now = new Date();
// var msPerHr = 60 * 60 * 1000;
// var twoHoursAgo = new Date(now - 2 * msPerHr);
//Get all subscriptions
// var getAllSubscriptions = function(callback){
// var subscriptions = [];
// var addSubscriptions = function(err, results, body){
// if(err) throw err;
// if(body.results) subscriptions = subscriptions.concat(body.results);
// if(body.next){
// var page = body.next.split('page=')[1];
// cratejoy.getSubscriptions({status:'active', limit:100, page: page}, addSubscriptions);
// }
// }
// cratejoy.getSubscriptions({status:'active', limit:100}, addSubscriptions)
// }
// var requestsPerSecond = 20;
// var getAllCustomers = function (callback) {
// var pages = 0;
// var pagesProcessed = 0;
// var pageSize = 50;
// var customers = {};
// var handleCustomers = function(err, results, body){
// if (err) throw err;
// var page = 0;
// if(body.next) page = parseInt(body.next.split('page=')[1])-1;
// if(body.prev) page = parseInt(body.prev.split('page=')[1])+1;
// console.log('processing page '+page);
// if (body.results) {
// _.forEach(body.results, function (customer) {
// customers[customer.id+''] = customer;
// });
// }
// pagesProcessed++;
// if(pagesProcessed == pages) callback(customers);
// }
// var getPage = function(page){
// var waitTime = Math.round(page * (1000/requestsPerSecond));
// setTimeout(function(){
// console.log('requesting page '+page);
// cratejoy.getCustomers({ limit: pageSize, page: page }, handleCustomers);
// }, waitTime);
// }
// cratejoy.getCustomers({ limit: 1 }, function(err, results, body){
// pages = Math.ceil(body.count/pageSize)+1;
// console.log(pages);
// for(var i=0; i<pages;i++) getPage(i);
// });
// }
// getAllCustomers(function(customers){
// console.log(Object.keys(customers).length);
// fs.writeFileSync('customers.v3.json', JSON.stringify(customers, null, 4));
// })
// fs.readFile('customers.v2.json', function(err, json){
// var customers = JSON.parse(json);
// console.log(Object.keys(customers).length);
// })
// cratejoy.toggleProduct(10141909, true, function(err, results, body){
// if(err) throw err;
// cratejoy.getProduct(10141909, function(err, results, body){
// console.log(body.visible);
// })
// })
// cratejoy.getSubscription(18712113, function(err, results, body){
// console.log(JSON.stringify(body, null, 4));
// })
// cratejoy.getOrders({limit:1}, function(err, results, body){
// console.log(JSON.stringify(body, null, 4));
// })
// cratejoy.getProduct(10141909, function(err, results, body){
// console.log(JSON.stringify(body, null, 4));
// })
var getAllPages = function(method, options, callback){
var results = [];
var handleResults = function(err, res, body){
results = results.concat(body.results);
if(body.next != null){
options.page = body.next.match(/page\=(.+)/)[1];
cratejoy[method](options, handleResults);
}else{
callback(results);
}
}
cratejoy[method](options, handleResults);
}
getAllPages('getProducts', {}, function(products){
var problems = [];
products.forEach(function(product, i){
//Find Problem
if(product == null || product.variants == null) return;
for(var vi in product.variants){
var variant = product.variants[vi];
if(variant.name.indexOf(':') == -1) continue;
problems.push({
id: product.id,
variant_id: variant.id,
old_name: variant.name,
new_name: variant.name.replace(":","")
});
}
})
fs.writeFileSync('products.json', JSON.stringify(problems, null, 4));
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment