Created
March 22, 2016 21:14
-
-
Save formigone/606f2c554da4ce181029 to your computer and use it in GitHub Desktop.
Fetch N pages of active users from Twitch.tv
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
const https = require('https'); | |
// This being a dirty prototype, I'm not concerned about checking for min/max values for this... | |
const maxPages = process.argv[2] || 3; | |
function getStreams(pages, offset, data, cb){ | |
// Offset could be too far. First response would include total streams (res._total). It also has links to next page... | |
const req = https.get({ hostname: 'api.twitch.tv', path: '/kraken/streams?stream_type=live&offset=' + offset }, function(res){ | |
var body = ''; | |
res.on('data', function(data) { | |
body += data; | |
}); | |
res.on('end', function(){ | |
const json = JSON.parse(body); | |
const activeStreams = json.streams.map(function(stream){ | |
return { | |
channel: stream.channel.display_name, | |
channelId: stream.channel._id, | |
channelUrl: stream.channel.url | |
}; | |
}); | |
data = data.concat(activeStreams); | |
if (pages > 1) { | |
return getStreams(pages - 1, offset + 25, data, cb); | |
} else { | |
cb(null, data); | |
} | |
}); | |
}); | |
req.on('error', function (error){ | |
cb(error); | |
}); | |
req.end(); | |
} | |
getStreams(maxPages, 0, [], function(err, data){ | |
if (err) { | |
console.log('=(', err); | |
} | |
console.log(data); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment