Created
April 18, 2016 02:03
-
-
Save kwliou/b4820ec2c9c73b593f528f1dbad4ccf5 to your computer and use it in GitHub Desktop.
Search Twitch VOD chat replay Node.js script
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
/* jshint esversion: 6 */ | |
/* | |
* Usage: node search_twitch_chat.js <numeric_video_id> "<keyword_regex>" | |
* the video id is in the URL e.g. https://www.twitch.tv/username/v/VIDEO_ID | |
* if you have minimist installed, you can pass --start <unix_time_seconds> --tick <wait_per_request_ms> | |
*/ | |
() => { | |
'use strict'; | |
let http = require('https'); | |
let argv = {_: process.argv.slice(2)}; | |
try { | |
argv = require('minimist')(process.argv.slice(2)); | |
} catch (e) { | |
console.error('\x1b[33mWarning: minimist is not installed.\x1b[39m'); | |
} | |
const CHAT_CHUNK_DUR = 30; | |
const REQUEST_TICK = argv.tick || 40; | |
let videoId = argv._[0]; | |
let keyword = argv._[1]; | |
let keywordRx = new RegExp(`(?:^|\\W)(?:${keyword})(?=$|\\W)`, 'gi'); | |
let reqInfo = videoId => ({ | |
host: 'api.twitch.tv', | |
path: `/kraken/videos/v${videoId}` | |
}); | |
let reqChat = (start, videoId) => ({ | |
host: 'rechat.twitch.tv', | |
path: `/rechat-messages?start=${start}&video_id=v${videoId}` | |
}); | |
makeRequest(reqInfo(videoId), json => { | |
let createdAt = new Date(json.created_at).getTime()/1000; | |
let start = argv.start || createdAt; | |
let end = createdAt + json.length; | |
console.log(new Date(start * 1000).toLocaleString() + | |
' to ' + | |
new Date(end * 1000).toLocaleTimeString()); | |
console.log(`${json.channel.display_name} playing ${json.game} (${json.title.trim()})`); | |
console.log(`Searching for keyword: \x1b[94m"${keyword}"\x1b[39m\n`); | |
let loop = setInterval(() => { | |
if (start > end) { | |
clearInterval(loop); | |
return; | |
} | |
start += CHAT_CHUNK_DUR; | |
scrape(start); | |
}, REQUEST_TICK); | |
}); | |
function makeRequest(settings, callback) { | |
let chunked = ''; | |
let req = http.request(settings) | |
.on('error', console.error) | |
.on('response', resp => resp | |
.on('data', data => chunked += data.toString()) | |
.on('end', () => chunked && callback(JSON.parse(chunked)))); | |
req.end(); | |
} | |
function scrape(offset) { | |
makeRequest(reqChat(offset, videoId), json => { | |
if (json.data) { | |
for (let msg of json.data) { | |
let attrs = msg.attributes; | |
var record = attrs.from + ': ' + attrs.message; | |
if (keywordRx.test(record)) { | |
var coloredMsg = record.replace(keywordRx, '\x1b[94m$&\x1b[39m'); | |
console.log(`\x1b[90m${new Date(attrs.timestamp).toLocaleTimeString()}\x1b[39m ${coloredMsg}`); | |
} | |
} | |
} | |
}); | |
} | |
}(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment