# PiSignage API Credentials and Endpoint $piSignageAPIUrl = "https://cybertek.pisignage.com/api/session" $email = "cole@cybertek.systems" $password = "(OB4,k6@[{8}t#?M0}FD" $youtubeApiKey = "AIzaSyAw6-9hHTAVh_2HI_iqIy2HOBh6gXcFbXs" # Replace with your actual YouTube API key # Define Player Groups $playerGroups = @("Gregory", "Colome", "Winner") # Define YouTube Channels $channels = @( @{name="WION"; youtube_channel_id="UC_gUM8rL-Lrg6O3adPW9K1g"}, @{name="Celebrate"; youtube_channel_id="UCT7rpbRfATyFzrRLhRFMgLA"}, @{name="Winner"; youtube_channel_id="UCi_NFDGibun9FAi9AxaFYOg"}, @{name="Gregory"; youtube_channel_id="UCtVXpTODGQpXmUhuYyojChQ"}, @{name="Colome"; youtube_channel_id="UCWCjf47gUD7eyCesddHWdCA"}, @{name="Burke"; youtube_channel_id="UC7d8OoqxxSOKnW_HYzcaRhQ"} ) # Function to Authenticate and Retrieve API Token function Get-ApiToken { $payload = @{ email = $email password = $password getToken = "True" } | ConvertTo-Json $headers = @{ 'Content-Type' = 'application/json' } try { $response = Invoke-RestMethod -Uri $piSignageAPIUrl -Method Post -Headers $headers -Body $payload return $response.token } catch { Write-Output "Error obtaining API token: $_" return $null } } # Function to Check YouTube Channel for Live Broadcast function Check-YoutubeChannel { param ( [string]$channelId, [string]$apiKey ) $endpoint = "https://www.googleapis.com/youtube/v3/search" $params = @{ part = "id" type = "video" eventType = "live" maxResults = 1 channelId = $channelId key = $apiKey } try { $response = Invoke-RestMethod -Uri "$endpoint?$(($params.GetEnumerator() | ForEach-Object { "$($_.Key)=$($_.Value)" }) -join "&")" -Method Get return $response } catch { Write-Output "Error checking YouTube channel: $_" return $null } } # Function to Create a New Livestream Asset on PiSignage function New-LivestreamAsset { param ( [string]$liveBroadcastUrl, [string]$apiToken ) $assetData = @{ name = "YouTube Livestream" filename = $liveBroadcastUrl assetType = "video" } $headers = @{ 'x-access-token' = $apiToken 'Content-Type' = 'application/json' } try { $response = Invoke-RestMethod -Uri "https://cybertek.pisignage.com/api/assets" -Method Post -Headers $headers -Body ($assetData | ConvertTo-Json) return $response._id } catch { Write-Output "Error creating livestream asset: $_" return $null } } # Function to Schedule the Livestream on Specified Groups function Schedule-Livestream { param ( [string]$assetId, [string]$apiToken ) $headers = @{ 'x-access-token' = $apiToken 'Content-Type' = 'application/json' } foreach ($group in $playerGroups) { $scheduleData = @{ playlist = @($assetId) playerGroup = $group } try { Invoke-RestMethod -Uri "https://cybertek.pisignage.com/api/groups/$group/schedule" -Method Post -Headers $headers -Body ($scheduleData | ConvertTo-Json) Write-Output "Livestream scheduled for group: $group" } catch { Write-Output ("Error scheduling livestream for group: " + $group) Write-Output ("Error details: " + $_.Exception.Message) } } } # Function to Remove Livestream Asset and Revert to Default Playlist function Cleanup-Livestream { param ( [string]$assetId, [string]$apiToken ) $headers = @{ 'x-access-token' = $apiToken 'Content-Type' = 'application/json' } try { Invoke-RestMethod -Uri "https://cybertek.pisignage.com/api/assets/$assetId" -Method Delete -Headers $headers Write-Output "Livestream asset removed." } catch { Write-Output "Error deleting livestream asset: $_" } foreach ($group in $playerGroups) { try { Invoke-RestMethod -Uri "https://cybertek.pisignage.com/api/groups/$group/revert" -Method Post -Headers $headers Write-Output "Reverted to default playlist for group: $group" } catch { Write-Output "Error reverting group $group to default playlist: $_" } } } # Main Code - Check Each Channel and Schedule Livestream if Found $apiToken = Get-ApiToken if (-not $apiToken) { Write-Output "Unable to proceed without API token." exit } foreach ($channel in $channels) { $channelId = $channel.youtube_channel_id $response = Check-YoutubeChannel -channelId $channelId -apiKey $youtubeApiKey if ($response -and $response.items.Count -gt 0) { $videoId = $response.items[0].id.videoId $liveBroadcastUrl = "https://www.youtube.com/watch?v=$videoId" # Schedule on PiSignage if not already scheduled $assetId = New-LivestreamAsset -liveBroadcastUrl $liveBroadcastUrl -apiToken $apiToken if ($assetId) { Schedule-Livestream -assetId $assetId -apiToken $apiToken # Periodic check for livestream end while ($true) { Start-Sleep -Seconds 60 $response = Check-YoutubeChannel -channelId $channelId -apiKey $youtubeApiKey if ($response.items.Count -eq 0) { Cleanup-Livestream -assetId $assetId -apiToken $apiToken Write-Output "Livestream ended for channel $($channel.name). Cleaned up." break } } } else { Write-Output "Failed to create livestream asset for channel $($channel.name)." } } else { Write-Output "No live broadcast currently playing for channel $($channel.name)." } }