Advertisement
Guest User

Untitled

a guest
Nov 1st, 2018
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.09 KB | None | 0 0
  1. const req = require('request');
  2. const irc = require('irc-connect');
  3.  
  4. const GAME = process.env.GAME;
  5. const USERNAME = process.env.USERNAME;
  6. const TOKEN = process.env.TOKEN;
  7.  
  8. if (!GAME || !USERNAME) {
  9. throw 'Game and Twitch username required';
  10. }
  11.  
  12. const nowTs = +new Date();
  13. const filename = GAME + '-' + nowTs + '.txt';
  14.  
  15. const ircOptions = {
  16. //[port] if not provided defaults to 6667 (or if secure, 6697)
  17. port: 6667,
  18. //[secure] can be true/false or 'semi' for lazy CA checking (self-signed, obscure CA, etc)
  19. secure: false,
  20. //[nick] is the desired nickname, if not provided one will be generated (you can always use nick() later)
  21. nick: USERNAME,
  22. pass: TOKEN
  23. };
  24.  
  25. const getStreams = (offset = 0) => {
  26. req.get(
  27. 'https://api.twitch.tv/kraken/streams/?game=' + GAME + '&stream_type=live&language=en&offset=' + offset + '&limit=50', {
  28. headers: { 'client-id': 'e0slzqu26m4tokzkc0vcj9t87vk9lx', 'content-type': 'application/json' }
  29. }, (error, respoonse, data) => {
  30. const res = JSON.parse(data);
  31. const streams = res.streams
  32. .filter((stream) => stream.viewers <= 50)
  33. .map((stream) => stream.channel.name);
  34.  
  35. if (streams.length) {
  36. streams.forEach((stream) => {
  37. console.log('Connecting to ' + stream);
  38. irc.connect('irc.chat.twitch.tv', ircOptions)
  39. //include some plugins
  40. // .use(irc.pong, irc.names, irc.motd)
  41. //fires when the servers sends the welcome message (RPL_WELCOME)
  42. .on('welcome', function (msg) {
  43. console.log(msg);
  44.  
  45. // this.nick(USERNAME, TOKEN, (err) => {
  46. // console.log('There was a problem setting your NICK:', err);
  47. // });
  48. })
  49. //fires after the server confirms password
  50. .on('identified', function (nick) {
  51. this.send('JOIN #node.js');
  52. })
  53. //fires only when YOUR nick changes
  54. .on('nick', function (nick) {
  55. console.log('Your nick is now:', nick);
  56. })
  57. .on('NOTICE', function (event) {
  58. console.log('NOTICE:', event.params[1]);
  59. })
  60. .on('JOIN', function (event) {
  61. console.log(event.nick, 'joined');
  62. })
  63. .on('PRIVMSG', function (event) {
  64. var params = event.params;
  65. console.log('message from: '+event.nick, 'to: '+params[0], params[1]);
  66. })
  67. //from the `names` plugin.
  68. .on('names', function (cname, names) {
  69. console.log(cname, names);
  70. })
  71. //from the `motd` plugin.
  72. .on('motd', function (event) {
  73. console.log(this.motd);
  74. console.log(this.support);
  75. })
  76. });
  77. }
  78.  
  79. const timeout = streams.length ? 15100 : 0;
  80.  
  81. if (offset < res._total) {
  82. setTimeout(() => {
  83. getStreams(offset + 50);
  84. }, timeout);
  85. } else {
  86. console.log('Finished writing ' + filename);
  87. }
  88. }
  89. );
  90. }
  91.  
  92. getStreams();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement