Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const req = require('request');
- const irc = require('irc-connect');
- const GAME = process.env.GAME;
- const USERNAME = process.env.USERNAME;
- const TOKEN = process.env.TOKEN;
- if (!GAME || !USERNAME) {
- throw 'Game and Twitch username required';
- }
- const nowTs = +new Date();
- const filename = GAME + '-' + nowTs + '.txt';
- const ircOptions = {
- //[port] if not provided defaults to 6667 (or if secure, 6697)
- port: 6667,
- //[secure] can be true/false or 'semi' for lazy CA checking (self-signed, obscure CA, etc)
- secure: false,
- //[nick] is the desired nickname, if not provided one will be generated (you can always use nick() later)
- nick: USERNAME,
- pass: TOKEN
- };
- const getStreams = (offset = 0) => {
- req.get(
- 'https://api.twitch.tv/kraken/streams/?game=' + GAME + '&stream_type=live&language=en&offset=' + offset + '&limit=50', {
- headers: { 'client-id': 'e0slzqu26m4tokzkc0vcj9t87vk9lx', 'content-type': 'application/json' }
- }, (error, respoonse, data) => {
- const res = JSON.parse(data);
- const streams = res.streams
- .filter((stream) => stream.viewers <= 50)
- .map((stream) => stream.channel.name);
- if (streams.length) {
- streams.forEach((stream) => {
- console.log('Connecting to ' + stream);
- irc.connect('irc.chat.twitch.tv', ircOptions)
- //include some plugins
- // .use(irc.pong, irc.names, irc.motd)
- //fires when the servers sends the welcome message (RPL_WELCOME)
- .on('welcome', function (msg) {
- console.log(msg);
- // this.nick(USERNAME, TOKEN, (err) => {
- // console.log('There was a problem setting your NICK:', err);
- // });
- })
- //fires after the server confirms password
- .on('identified', function (nick) {
- this.send('JOIN #node.js');
- })
- //fires only when YOUR nick changes
- .on('nick', function (nick) {
- console.log('Your nick is now:', nick);
- })
- .on('NOTICE', function (event) {
- console.log('NOTICE:', event.params[1]);
- })
- .on('JOIN', function (event) {
- console.log(event.nick, 'joined');
- })
- .on('PRIVMSG', function (event) {
- var params = event.params;
- console.log('message from: '+event.nick, 'to: '+params[0], params[1]);
- })
- //from the `names` plugin.
- .on('names', function (cname, names) {
- console.log(cname, names);
- })
- //from the `motd` plugin.
- .on('motd', function (event) {
- console.log(this.motd);
- console.log(this.support);
- })
- });
- }
- const timeout = streams.length ? 15100 : 0;
- if (offset < res._total) {
- setTimeout(() => {
- getStreams(offset + 50);
- }, timeout);
- } else {
- console.log('Finished writing ' + filename);
- }
- }
- );
- }
- getStreams();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement