Tags

,


I’ve been looking at the Twitch API and as a result, here’s a little function you can use to get info about the streams you follow – see which ones are live and obtain info on them. The function accepts your Twitch username as the mandatory string parameter but optionally an array of strings as well which are usernames of other channels you don’t follow but want to check on similarly. This is my first play with Invoke-WebRequest and also with JSON.

function Get-TwitchStreams {
    [CmdletBinding()]
    Param(
        [Parameter(Mandatory=$true,Position=0,ValueFromPipeline=$true,
                    HelpMessage='Enter the name of the Twitch account whose followed streams you wish to query.')]
        [ValidateLength(4,30)]
        [string]$TwitchUsername,

        [Parameter(Mandatory=$false,Position=1)]
        [string[]]$ExtraChannels
    )

    try {
        $channelFollowsResponse = Invoke-WebRequest -Uri "https://api.twitch.tv/kraken/users/$TwitchUsername/follows/channels?limit=100" -Method Get
    } catch {
        return $null
    }
    $channelFollowsJson = ConvertFrom-Json -InputObject $channelFollowsResponse
    $channelNames = $channelFollowsJson.follows.channel.name
    if ($ExtraChannels -ne $null) {
        $channelNames += $ExtraChannels
    }
    $channelNames | ForEach-Object {
        $channelName = $_
        $props = [ordered]@{
            "ChannelName" = $channelName;
            "Streaming" = $false;
            "DisplayName" = $null;
            "Bio" = $null;
            "Game" = $null;
            "Status" = $null;
            "Viewers" = $null;
            "Views" = $null;
            "Followers" = $null;
            "Logo" = $null;
            "Preview" = $null
        }
        $userExists = $true
        try {
            $userResponse = Invoke-WebRequest -Uri "https://api.twitch.tv/kraken/users/$channelName" -Method Get
        } catch {
            $userExists = $false
            $props["DisplayName"] = "DOES NOT EXIST"
        }
        if ($userExists) {
            $userJson = ConvertFrom-Json -InputObject $userResponse.Content
            $props["DisplayName"] = $userJson.display_name
            $props["Bio"] = $userJson.bio
            $props["Logo"] = $userJson.logo
            $streamResponse = Invoke-WebRequest -Uri "https://api.twitch.tv/kraken/streams/$channelName" -Method Get
            $streamJson = ConvertFrom-Json -InputObject $streamResponse.Content
            if ($streamJson.stream -ne $null) {
                $props["Streaming"] = $true
                $props["Game"] = $streamJson.stream.channel.game
                $props["Status"] = $streamJson.stream.channel.status
                $props["Viewers"] = $streamJson.stream.viewers
                $props["Views"] = $streamJson.stream.channel.views
                $props["Followers"] = $streamJson.stream.channel.followers
                $props["Preview"] = $streamJson.stream.preview.large
            }
        }
        $obj = New-Object -TypeName PSObject -Property $props
        Write-Output $obj
    }
}
Advertisements