Created
December 17, 2016 20:21
-
-
Save Mariusio/3a7179480f227daf51135b77635017ee to your computer and use it in GitHub Desktop.
This file contains 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
var args = require('minimist')(process.argv.slice(2)); | |
var username = args.username; | |
var timeframe = args.timeframe; | |
var moment = require('moment'); | |
var InstagramPosts, streamOfPosts; | |
InstagramPosts = require('instagram-screen-scrape').InstagramPosts; | |
streamOfPosts = new InstagramPosts({ | |
username: username | |
}); | |
var now = moment(); | |
var oneHourAgo = moment().subtract(1, 'hours'); | |
var twentyFourHoursAgo = moment().subtract(24, 'hours'); | |
var res = []; | |
streamOfPosts.on('data', function(post) { | |
if (timeframe === 'all') { | |
res.push(post); | |
} else if (timeframe === 'lastHour') { | |
if (moment(new Date(post.time * 1000)).isBetween(oneHourAgo, now)) { | |
res.push(post); | |
} else { | |
console.log('reached end'); | |
streamOfPosts.destroy(); | |
return res; | |
} | |
} else if (timeframe === 'last24Hours') { | |
if (moment(new Date(post.time * 1000)).isBetween(twentyFourHoursAgo, now)) { | |
res.push(post); | |
console.log('post added'); | |
} else { | |
console.log('reached end'); | |
streamOfPosts.destroy(); | |
return res; | |
} | |
} | |
}); | |
streamOfPosts.on('end', function() { | |
console.log(res.length); | |
}); | |
streamOfPosts.on('close', function() { | |
console.log('Stream closed'); | |
}); | |
// output | |
node userFeed.js --username avalango --timeframe last24Hours | |
post added | |
post added | |
post added | |
post added | |
post added | |
post added | |
reached end | |
reached end | |
reached end | |
Stream closed | |
reached end | |
reached end | |
reached end | |
reached end | |
reached end | |
reached end | |
reached end | |
reached end | |
reached end | |
reached end | |
reached end | |
6 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment