Skip to content

Instantly share code, notes, and snippets.

@5nyper
Created October 18, 2016 22:11
Show Gist options
  • Save 5nyper/f9b496b861d88722374f0ebf2192fec9 to your computer and use it in GitHub Desktop.
Save 5nyper/f9b496b861d88722374f0ebf2192fec9 to your computer and use it in GitHub Desktop.
function createInsta(email, userid, cb) {
ensureExists(function (error) {
if (error) return cb(error);
getData(function (error, data) {
if (error) return cb(error);
return cb(null, data);
});
});
function ensureExists(cb) {
fs.exists('users/' + email + '/' + userid, function (exists) {
if (!exists) {
fs.mkdir('users/' + email + '/' + userid, function (error) {
if (error) return cb(error);
fs.readFile("data/data.json", "utf-8",function (error, contents) {
if (error) return cb(error);
fs.writeFile('users/' + email + '/' + userid + '/instagram.json', contents, function (error) {
if (error) return cb(error);
console.log("insta folder created");
return cb();
});
});
});
} else {
return cb();
}
}
function getData (cb) {
console.log("Initializing Data")
fs.readFile('users/' + email + '/' + userid + '/instagram.json', "utf-8", function (error, data) {
if (error) return cb(error);
data = JSON.parse(data);
request(("https://instagram.com/" + userid + "/?__a=1"), function(error, response, body) {
if (error) return cb(error);
var res = JSON.parse(body);
result = res.user.followed_by.count
data.STARTING_COUNTS[0].DAY = result;
data.STARTING_COUNTS[0].WEEK = result;
data.STARTING_COUNTS[0].MONTH = result;
return cb(null, data);
});
});
}
}
}
Call it like this:
createInsta("xxx@email.com", 3, function (error, data) {
if (error) {
console.log(error);
} else {
console.log("data is ");
console.log(data);
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment