# PUCKS Medya Yardımcısı 2.0 - Windows PowerShell # Kullanım: PowerShell -ExecutionPolicy Bypass -File .\giftag_medya.ps1 param( [string]$EspUrl = $(if ($env:PUCKS_ESP_URL) { $env:PUCKS_ESP_URL } else { $env:GIFTAG_ESP_URL }) ) $ErrorActionPreference = "Stop" $SID = "desktop_windows" $LastIdentity = "" $LastPlaying = $null function Normalize-EspUrl([string]$url) { if ([string]::IsNullOrWhiteSpace($url)) { return "" } $u = $url.Trim() if (-not $u.StartsWith("http://") -and -not $u.StartsWith("https://")) { $u = "http://$u" } return $u.TrimEnd("/") } function Test-Pucks([string]$url) { try { Invoke-RestMethod -Uri "$url/api/now_playing_status" -TimeoutSec 2 | Out-Null return $true } catch { try { Invoke-RestMethod -Uri "$url/api/info" -TimeoutSec 2 | Out-Null return $true } catch { return $false } } } function Resolve-Pucks { $candidates = @( (Normalize-EspUrl $EspUrl), (Normalize-EspUrl "http://pucks.local"), (Normalize-EspUrl "http://giftag.local"), (Normalize-EspUrl "http://192.168.4.1") ) | Where-Object { $_ } | Select-Object -Unique foreach ($candidate in $candidates) { if (Test-Pucks $candidate) { return $candidate } } Write-Host "PUCKS bulunamadi. Cihaz URL/IP adresini girin (ornek: http://192.168.1.50)." -ForegroundColor Yellow $manual = Normalize-EspUrl (Read-Host "PUCKS URL") if ($manual -and (Test-Pucks $manual)) { return $manual } throw "PUCKS cihazina baglanilamadi." } function Await-WinRt($operation, [Type]$resultType) { $asTask = ([System.WindowsRuntimeSystemExtensions].GetMethods() | Where-Object { $_.Name -eq "AsTask" -and $_.IsGenericMethodDefinition -and $_.GetParameters().Count -eq 1 } | Select-Object -First 1).MakeGenericMethod($resultType) $task = $asTask.Invoke($null, @($operation)) return $task.GetAwaiter().GetResult() } Add-Type -AssemblyName System.Runtime.WindowsRuntime | Out-Null [Windows.Media.Control.GlobalSystemMediaTransportControlsSessionManager, Windows.Media.Control, ContentType = WindowsRuntime] | Out-Null [Windows.Media.Control.GlobalSystemMediaTransportControlsSessionMediaProperties, Windows.Media.Control, ContentType = WindowsRuntime] | Out-Null $BaseUrl = Resolve-Pucks Write-Host "PUCKS Medya Yardimcisi 2.0 calisiyor: $BaseUrl" -ForegroundColor Green Write-Host "Bu pencere acik kaldigi surece Windows medya bilgisi cihaza aktarilir." -ForegroundColor DarkGray $manager = Await-WinRt ` ([Windows.Media.Control.GlobalSystemMediaTransportControlsSessionManager]::RequestAsync()) ` ([Windows.Media.Control.GlobalSystemMediaTransportControlsSessionManager]) while ($true) { try { $session = $manager.GetCurrentSession() if ($null -eq $session) { Start-Sleep -Seconds 2 continue } $props = Await-WinRt $session.TryGetMediaPropertiesAsync() ` ([Windows.Media.Control.GlobalSystemMediaTransportControlsSessionMediaProperties]) $timeline = $session.GetTimelineProperties() $playback = $session.GetPlaybackInfo() $title = [string]$props.Title if ([string]::IsNullOrWhiteSpace($title)) { Start-Sleep -Seconds 2 continue } $artist = [string]$props.Artist $album = [string]$props.AlbumTitle $positionMs = [int64]$timeline.Position.TotalMilliseconds $durationMs = [int64]$timeline.EndTime.TotalMilliseconds $isPlaying = ($playback.PlaybackStatus.ToString() -eq "Playing") $identity = "$title|$artist|$album|$durationMs" $action = if ($identity -ne $LastIdentity) { "track" } elseif ($null -ne $LastPlaying -and $isPlaying -ne $LastPlaying) { if ($isPlaying) { "play" } else { "pause" } } else { "tick" } $body = @{ source_id = $SID source_name = "PUCKS Windows Yardimcisi" action = $action title = $title artist = $artist album = $album position = [Math]::Max(0, $positionMs) duration = [Math]::Max(0, $durationMs) paused = (-not $isPlaying) speed = $(if ($isPlaying) { 1.0 } else { 0.0 }) media_key = $identity } | ConvertTo-Json -Depth 4 -Compress Invoke-RestMethod -Uri "$BaseUrl/api/now_playing" -Method Post -ContentType "application/json; charset=utf-8" -Body $body -TimeoutSec 4 | Out-Null $LastIdentity = $identity $LastPlaying = $isPlaying } catch { Write-Host ("PUCKS gonderim hatasi: " + $_.Exception.Message) -ForegroundColor DarkYellow } Start-Sleep -Seconds 2 }