Skip to content

Instantly share code, notes, and snippets.

@micalevisk
Last active February 1, 2018 16:35
Show Gist options
  • Save micalevisk/d4a84a9fc8b157c9b4f375f59014da3f to your computer and use it in GitHub Desktop.
Save micalevisk/d4a84a9fc8b157c9b4f375f59014da3f to your computer and use it in GitHub Desktop.
simple JS script which use Twitch.tv API v3
#!/bin/env node
// USE: ./getInfoTwitchTv.js <channelname>
'use strict';
if(process.argv.length < 2) return;
var channel = process.argv[2].trim();
if(!channel) return;
var request = require('request');
const URL = 'https://api.twitch.tv/kraken/streams/' + channel;
var options = {
url: URL,
qs: { client_id: '<CLIENT ID>' }
};
function callback(error, response, body) {
if (!error && response.statusCode == 200) {
var infoStream = JSON.parse(body).stream;
if(infoStream == null) return;
var infoChannel = infoStream.channel;
var info = {
"title": infoChannel.status ,
"game": infoStream.game ,
"viewers": infoStream.viewers ,
"followers": infoChannel.followers ,
"user": infoChannel.name ,
"partner": infoChannel.partner
};
console.log( JSON.stringify(info, null, ' ') );
}
}
request(options, callback);
{
"name": "info_twitchtv",
"version": "1.0.0",
"description": "Show channel informations from Twitch Tv",
"main": "getInfoTwitchTv.js",
"author": "Micael Levi",
"license": "ISC",
"dependencies":{
"request": "latest"
}
}
@micalevisk
Copy link
Author

micalevisk commented Jan 29, 2017

$ npm install
$ chmod +x getInfoTwitchTv.js

get client_id: https://www.twitch.tv/kraken/oauth2/clients/new

@micalevisk
Copy link
Author

micalevisk commented Feb 1, 2017

output example

{
  "title": "(PC 21:9) Day 32 - Road to Twitch partnership",
  "game": "APB Reloaded",
  "viewers": 97,
  "followers": 7566,
  "user": "mydopefish",
  "partner": false
}

on bash with cURL and jq

curl -s \
	--header 'Accept: application/vnd.twitchtv.v3+json' \
	--header 'Client-ID: <CLIENT ID>' \
	--request GET "https://api.twitch.tv/kraken/streams/${channel}/" |
	jq '.stream' |
	jq '{ "title": .channel.status , "game": .game , "viewers": .viewers , "followers": .channel.followers , "user": .channel.name , "partner": .channel.partner }'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment