Skip to content

Instantly share code, notes, and snippets.

@brettmillerb
Created January 11, 2019 16:04
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brettmillerb/de44294739266900c43a6a4d547f7edc to your computer and use it in GitHub Desktop.
Save brettmillerb/de44294739266900c43a6a4d547f7edc to your computer and use it in GitHub Desktop.
Twitch User Events
function Get-TwitchEvent {
[CmdletBinding(DefaultParameterSetName = 'Standard')]
param (
[Parameter(Mandatory,
ParameterSetName = 'Standard')]
[string]
$UserName,
[Parameter(Mandatory,
ParameterSetName = 'Pipeline',
ValueFromPipeline,
ValueFromPipelineByPropertyName)]
[psobject]
$InputObject,
[switch]
$ShowArchive,
$Token = $Script:Token
)
process {
foreach ($user in $InputObject) {
$streamsUri = "{0}/streams?user_login={1}" -f $Script:Uri, $user.UserName
$archiveUri = "{0}/videos?user_id={1}" -f $Script:Uri, $user.UserID
$streamsResults = Invoke-RestMethod -Uri $streamsUri -Headers $script:Headers
$archiveResults = Invoke-RestMethod -Uri $archiveUri -Headers $script:Headers
if ($streamsResults.data) {
"Streaming Live Now"
[PSCustomObject]@{
UserName = $streamsResults.data.user_name
StreamTitle = $streamsResults.data.title
ViewerCount = $streamsResults.data.viewer_count
StartedAt = $streamsResults.data.started_at
}
}
else {
"No Live Stream Available"
}
if ($ShowArchive) {
"Archive Videos"
$archiveResults.data | ForEach-Object {
[PSCustomObject]@{
UserName = $_.user_name
StreamTitle = $_.title
Url = $_.url
}
}
}
}
}
}
function Get-TwitchUser {
param (
[string]
$UserName,
$Token = $Script:Token
)
$usersUri = "{0}/users/?login={1}" -f $script:Uri, $UserName
try {
Invoke-RestMethod -Uri $usersUri -Method Get -Headers $Script:Headers |
Select-Object -ExpandProperty Data | ForEach-Object {
[PSCustomObject]@{
UserId = $_.id
UserName = $_.display_name
Description = $_.description
ViewCount = $_.view_count
}
}
}
catch {
$_
}
}
function Set-TwitchConfiguration {
[CmdletBinding()]
param (
[string]
$Token
)
$Script:Token = $Token
$Script:Uri = "https://api.twitch.tv/helix"
$Script:Headers = @{"Client-ID" = $Token}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment