This repository has been archived by the owner on Sep 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTwitchStreamLister.psm1
183 lines (156 loc) · 5.11 KB
/
TwitchStreamLister.psm1
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
<#
.SYNOPSIS
TwitchStreamLister, a helping hand for StreamLink users.
.DESCRIPTION
Help can be found in README.md
TwitchStreamLister is available under The MIT License (MIT). See included LICENSE.
Copyright (c) 2014 Manuel "ElectronicWar" Kroeber <[email protected]>
.NOTES
Author: Manuel "ElectronicWar" Kroeber <[email protected]>
Version: r5 (2016-11-16)
License: MIT
.LINK
https://github.com/ElectronicWar/twitchstreamlister
#>
#requires -Version 4.0
Add-Type -AssemblyName System.Web
function Invoke-TwitchApi {
Param (
[Parameter(Mandatory=$True)]
[String]$commandUrl
)
$clientID = "sw19g2vrz9nkugaviab9xbk6w7ehxxh"
$userAgent = "PowerShell StreamLister"
$game = [System.Web.HttpUtility]::UrlEncode($gameName);
$apiUrl = "https://api.twitch.tv/kraken"
$headers = @{
"Client-ID" = $ClientID;
"Accept" = "application/vnd.twitchtv.v5+json"
}
$requestUrl = $apiUrl + $commandUrl
$apiResult = Invoke-RestMethod -Uri $requestUrl -Method Get -UserAgent $userAgent -Headers $headers
if ($apiResult) {
return $apiResult
} else {
Write-Warning "Sorry, but there was a problem with the last twitch.tv API call."
return
}
}
function Invoke-TwitchGetStreams {
Param (
[Parameter(Mandatory=$True)]
[String]$gameName
)
$game = [System.Web.HttpUtility]::UrlEncode($gameName);
$command = "/streams?game=" + $game
Write-Host ("Retrieving top 25 '" + $gameName + "' streams...")
$gameStreams = Invoke-TwitchApi -commandUrl $command
if ($gameStreams) {
return $gameStreams.streams
} else {
return
}
}
function Invoke-TwitchGetTopGames {
$game = [System.Web.HttpUtility]::UrlEncode($gameName);
$command = "/games/top?limit=10"
Write-Host ("Retrieving top live games...")
$topGames = Invoke-TwitchApi -commandUrl $command
if ($topGames) {
return $topGames.top
$topGames.top
} else {
return
}
}
function Invoke-TwitchListStreams {
Param (
[Parameter(Mandatory=$True)]
[PSObject]$streamList
)
$nameLen = 0
foreach ($stream in $streamList) {
$len = $stream.channel.display_name.Length
if ($len -gt $nameLen) { $nameLen = $len }
}
$nameLen++
$streamNum = 0
foreach ($stream in $streamList) {
$streamNum++
Write-Host -NoNewLine ($streamNum.ToString().PadLeft(2, "0") + ": ")
Write-Host -NoNewLine -ForegroundColor "Green" ($stream.channel.display_name.PadRight($nameLen, " "))
Write-Host $stream.viewers.ToString().PadLeft(6, " ") "Viewers"
}
}
function Invoke-TwitchListGames {
Param (
[Parameter(Mandatory=$True)]
[PSObject]$gameList
)
$gameLen = 0
foreach ($game in $gameList) {
$len = $game.game.name.Length
if ($len -gt $gameLen) { $gameLen = $len }
}
$gameLen++
$gameNum = 0
foreach ($game in $gameList) {
$gameNum++
Write-Host -NoNewLine ($gameNum.ToString().PadLeft(2, "0") + ": ")
Write-Host -NoNewLine -ForegroundColor "Green" ($game.game.name.PadRight($gameLen, " "))
Write-Host $game.viewers.ToString().PadLeft(6, " ") "Viewers in" $game.channels "Channels"
}
}
function Invoke-TwitchWatchStream {
Param (
[Parameter(Mandatory=$True)]
[PSObject]$streamList,
[Parameter(Mandatory=$True)]
[Int32]$streamNumber,
[Parameter(Mandatory=$False)]
[String]$quality = "best"
)
if ($streamNumber -gt 0) {
Write-Host (
"Starting stream #" + $streamNumber +
" (" + $streamList[$streamNumber-1].channel.display_name +
") with StreamLink using " +
$quality + " quality setting."
)
streamlink $streamList[$streamNumber-1].channel.url $quality
} else {
Write-Host "Invalid stream number."
}
}
function Watch-TwitchStreams {
if($PSVersionTable.PSVersion.Major -lt 4) {
Write-Error "TwitchStreamLister requires PowerShell 4.0 or better; you have version $($Host.Version)"
return
}
$games = Invoke-TwitchGetTopGames
if (!$games) {
Write-Warning "Something went wrong, please try again."
return
}
Invoke-TwitchListGames($games)
[Int32]$gameNumber = Read-Host "Enter game number to list it's top channels. 0 to exit"
$gameNumber--
if ($gameNumber -lt 0) {
Write-Host "Exiting. Thanks for using!"
return
}
$streams = Invoke-TwitchGetStreams -game ($games[$gameNumber].game.name)
if ($streams) {
Invoke-TwitchListStreams -streamList $streams
[Int32]$streamNumber = Read-Host "Enter stream number to watch. 0 to exit"
if ($streamNumber -lt 0) {
Write-Host "Exiting. Thanks for using!"
return
}
Invoke-TwitchWatchStream -streamList $streams -streamNumber $streamNumber -quality "best"
Watch-TwitchStreams
} else {
Write-Warning "Unable to retrieve live stream list, please try again."
return
}
}