-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
134 lines (116 loc) · 4.12 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
var spawn = require('child_process').spawn;
var colors = require("colors");
var request = require("request");
var rl = require("readline");
var formatter = require("./formatter");
function handleInput(question, options, callback) {
var readline = require('readline');
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question(question + " ", function (answer) {
rl.close();
if (answer === 'g')
getGames();
else if (answer === 'f')
getFeatured();
else if (answer === 'm')
showMainMenu();
else
callback(options[parseInt(answer)][0]);
});
}
function getFeatured() {
request({
url: "https://api.twitch.tv/kraken/streams/featured"
}, function(err, response, body) {
var json = JSON.parse(body);
var featured = json.featured;
var streamers = [];
for (var i = 0; i < featured.length; i++) {
var stream = featured[i].stream;
var channel = stream.channel;
var channel_name = channel.display_name;
var channel_status = channel.status.slice(0, 40);
var viewers = stream.viewers;
streamers[i] = [channel_name, channel_status, viewers];
}
var col = [colors.red, colors.lightgrey, colors.blue, colors.yellow];
formatter.table(streamers, {"colors":col, "delimeter_color":colors.red});
openStreams(streamers);
});
}
function getGames() {
var games = [];
request({
url: "https://api.twitch.tv/kraken/games/top?limit=25&offset=0",
}, function (err, response, body) {
var json = JSON.parse(body);
var top = json.top;
for (var i = 0; i < top.length; i++) {
var info = top[i];
var game_info = info.game;
var game_name = game_info.name;
var game_viewers = info.viewers;
games[i] = [game_name, game_viewers];
}
var col = [colors.red, colors.lightgrey, colors.yellow];
formatter.table(games, {"colors":col, "delimeter_color":colors.red});
openGame(games);
});
}
function getStreamsForGame(gamename) {
request({
url: "https://api.twitch.tv/kraken/streams?game="+gamename
}, function(err, response, body) {
var json = JSON.parse(body);
var streams = json.streams;
var streamers = [];
for (var i = 0; i < streams.length; i++) {
var channel = streams[i].channel;
var channel_name = channel.display_name;
var channel_status = channel.status.slice(0, 40);
var viewers = streams[i].viewers;
streamers[i] = [channel_name, channel_status, viewers];
}
var col = [colors.red, colors.lightgrey, colors.blue, colors.yellow];
formatter.table(streamers, {"colors":col, "delimeter_color":colors.red});
openStreams(gamename, streamers);
});
}
function showMainMenu() {
var games = "Games (g)";
var featured = "Featured (f)";
var col = [colors.red, colors.lightgrey];
var options = [[games], [featured]];
formatter.table(options, {colors: col});
handleInput("Open Menu (m)?", options, function(answer) {
if (answer === games)
getGames();
else if (answer === featured)
getFeatured();
else
showMainMenu();
});
}
function openStreams(gamename, streams) {
cb = function(stream) {
const stream_url = 'http://www.twitch.tv/' + stream;
const livestreamer = spawn('livestreamer', [stream_url, 'best']);
livestreamer.on('close', (code) => {
getStreamsForGame(gamename);
});
livestreamer.on('error', (code) => {
const mpv = spawn('mpv', ['--fs', 'http://www.twitch.tv/' + stream]);
mpv.on('close', (code) => {
getStreamsForGame(gamename);
});
});
};
handleInput("Open stream number?", streams, cb);
}
function openGame(games) {
handleInput("Open game number?", games, getStreamsForGame);
}
showMainMenu();