-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtwitch.ps1
192 lines (162 loc) · 5.64 KB
/
twitch.ps1
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#------------------------------------------------------------------------------
# This script will check all twitch channels added to the channels.txt file
# and list those that are live. You can then choose which you want to watch
# and livestreamer does the rest.
#
# Require:
# PowerShell 4 (https://www.microsoft.com/en-us/download/details.aspx?id=40855)
# livestreamer (http://docs.livestreamer.io/)
# Optional:
# HexChat (https://hexchat.github.io/)
#------------------------------------------------------------------------------
# DO NOT CHANGE THIS
#------------------------------------------------------------------------------
$url = "http://www.twitch.tv/";
$apiUrl = "https://api.twitch.tv/kraken";
$streamsUrl = $apiUrl + "/streams";
$followingUrl = $apiUrl + "/users/{0:S}/follows/channels?limit=100";
#------------------------------------------------------------------------------
#------------------------------------------------------------------------------
# Mandatory Settings - Required but they can be changed to your preference
#------------------------------------------------------------------------------
# Set quality to use: see livestreamer documentation
$quality = "best";
# To check channels that you have followed set username
$twitchUsername = "spaceogre";
# Otherwise you can add channels you want checked to a file called
# "channels.txt" and put it in the same folder as this script.
#------------------------------------------------------------------------------
#------------------------------------------------------------------------------
# Optional Settings - Comment out the options you don't want
#------------------------------------------------------------------------------
# To not show the start menu set this to $false, the script will then default
# to show your followed channels.
$showStartMenu = $true;
# Add path to HexChat if you want to connect to the selected channels chat.
# HexChat needs to be configured for irc.twitch.tv. Server name: Twitch
$hexChatPath = "C:\Program Files\HexChat\hexchat.exe";
#------------------------------------------------------------------------------
#------------------------------------------------------------------------------
# Functions
#------------------------------------------------------------------------------
function main{
cls;
if($showStartMenu){
write-host "TwitchTV Script - Start Menu";
printHR;
write-host "[0] - Following";
write-host "[1] - Enter Channel Name";
printHR;
write-host "";
$choice = chooseNumberOrExit;
if(!$choice -Or ($choice -eq 0)){
cls;
following;
}
elseif($choice -eq 1){
cls;
$channelName = Read-Host -Prompt "Enter Channel name";
write-host "";
startChannel($channelName);
}
}
else{
following;
}
}
function checkRunAgain{
$choice = Read-Host -Prompt "Run script again? [Y/N]";
if($choice -like "yes" -Or ($choice -like "y")){
main;
}
}
function following{
$channels = getChannels;
write-host "Channels that are live"
printHR;
$liveChannels = getLiveChannels $channels;
printHR;
write-host "";
chooseChannel $liveChannels;
printHR;
write-host "";
}
function getChannels{
if($twitchUsername -eq $null -Or $twitchUsername -eq ""){
return getChannelsFromFile;
}
else{
return getChannelsFromUsername;
}
}
function getChannelsFromFile{
if(!(Test-Path "channels.txt")){
write-host "channels.txt is missing!";
write-host "Add it to the same folder as the script, ";
write-host "with one channel per line.";
Exit;
}
return Get-Content "channels.txt";
}
function getChannelsFromUsername{
$channels = {@()}.Invoke();
$follows = (Invoke-RestMethod $($followingUrl -f $twitchUsername)).follows;
foreach($channel in $follows){
$channels.Add($channel.channel.name) | Out-Null;
}
return $channels;
}
function getLiveChannels($channels){
$streams = (Invoke-RestMethod $($streamsUrl + "?channel=" + $($channels -join ','))).streams;
$liveChannels = {@()}.Invoke();
$i = 0;
foreach ($stream in $streams){
write-host $("[" + $i + "] - " + $stream.channel.display_name + " - " + $stream.game);
write-host $("`t`t" + $stream.channel.status) -foregroundcolor "yellow";
$liveChannels.add($stream.channel.display_name) | Out-Null;
$i++;
}
if($liveChannels.Count -eq 0){
write-host "No channels are live, exiting...";
write-host "";
Exit;
}
return $liveChannels;
}
function chooseChannel($liveChannels){
DO {
$choice = chooseNumberOrExit;
if($choice -le $($liveChannels.Count - 1)){
startChannel($liveChannels[$choice].ToLower());
}
else {
write-host "Not a valid choice";
write-host "";
}
} While ($true)
}
function startChannel($channelName){
if($hexChatPath -ne $null -And (Test-Path $hexChatPath)){
& $hexChatPath $("irc://Twitch/#" + $channelName)
}
write-host "Livestreamer output: ";
printHR;
& "livestreamer" $($url + $channelName) $quality
break;
}
function chooseNumberOrExit{
$choice = Read-Host -Prompt "Choose one of the above numbers (0 is default) or type [e]xit";
write-host "";
if($choice -like "exit" -Or ($choice -like "e")){
Exit;
}
return $choice;
}
function printHR{
write-host "------------------------------------------------------------------";
}
#------------------------------------------------------------------------------
# Run the script
#------------------------------------------------------------------------------
main;
checkRunAgain;