diff --git a/pisignage.ps1 b/pisignage.ps1 new file mode 100644 index 0000000..c121657 --- /dev/null +++ b/pisignage.ps1 @@ -0,0 +1,29 @@ +# Define PiSignage API endpoint and credentials +$piSignageAPIUrl = "https://cybertek.pisignage.com/api/session" +$email = "cole@cybertek.systems" +$password = "(OB4,k6@[{8}t#?M0}FD" + +# Convert payload to JSON format +$payload = @{ + email = $email + password = $password + getToken = "True" +} | ConvertTo-Json + +# Set content type to JSON +$headers = @{ + 'Content-Type' = 'application/json' +} + +# Send the POST request to retrieve the token +$response = Invoke-RestMethod -Uri $piSignageAPIUrl -Method Post -Headers $headers -Body $payload + +# Extract the token from the response +$apiToken = $response.token + +# Add token to headers for future requests +$headers['x-access-token'] = $apiToken + +# Display token for verification +Write-Output "API Token: $apiToken" +Write-Output "Headers with token: $headers" diff --git a/youtube_2_pisignage_main.ps1 b/youtube_2_pisignage_main.ps1 new file mode 100644 index 0000000..9aa56eb --- /dev/null +++ b/youtube_2_pisignage_main.ps1 @@ -0,0 +1,187 @@ +# 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)." + } +} diff --git a/youtubetest.ps1 b/youtubetest.ps1 new file mode 100644 index 0000000..6c2d3cf --- /dev/null +++ b/youtubetest.ps1 @@ -0,0 +1,30 @@ +# Define your YouTube API key and a test channel ID +$youtubeApiKey = "AIzaSyAw6-9hHTAVh_2HI_iqIy2HOBh6gXcFbXs" # Replace with the actual API key +$channelId = "UC_gUM8rL-Lrg6O3adPW9K1g" # Example channel ID for testing + +# Define the endpoint and parameters +$endpoint = "https://www.googleapis.com/youtube/v3/search" +$params = @{ + part = "snippet" + channelId = $channelId + type = "video" + eventType = "live" + maxResults = 1 + key = $youtubeApiKey +} + +# Construct the query string manually and prepend the endpoint +$queryString = ($params.GetEnumerator() | ForEach-Object { "$($_.Key)=$($_.Value)" }) -join "&" +$fullUrl = "${endpoint}?${queryString}" + +# Output the full URL for debugging +Write-Output "Full URL: $fullUrl" + +try { + # Send the request + $response = Invoke-RestMethod -Uri $fullUrl -Method Get + Write-Output "YouTube API Response Items:" + Write-Output $response.items | Format-List +} catch { + Write-Output "YouTube API Error: " + $_.Exception.Message +}