# GifTag Medya Helper - Windows PowerShell # Usage: PowerShell -ExecutionPolicy Bypass -File .\giftag_medya.ps1 param( [string]$EspUrl = $env:GIFTAG_ESP_URL ) $ErrorActionPreference = "SilentlyContinue" $SID = "pc_ps" $LastKey = "" 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-GifTag([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-GifTag { $candidates = @( (Normalize-EspUrl $EspUrl), (Normalize-EspUrl "http://giftag.local"), (Normalize-EspUrl "http://192.168.4.1") ) | Where-Object { $_ } | Select-Object -Unique foreach ($candidate in $candidates) { if (Test-GifTag $candidate) { return $candidate } } Write-Host "GifTag ESP bulunamadi. ESP URL/IP girin (ornek: http://192.168.1.50)." -ForegroundColor Yellow $manual = Normalize-EspUrl (Read-Host "ESP URL") if ($manual -and (Test-GifTag $manual)) { return $manual } throw "GifTag ESP'ye 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-GifTag Write-Host "GifTag Medya helper calisiyor: $BaseUrl" -ForegroundColor Green $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") $key = "$title|$artist|$album|$durationMs|$isPlaying" $body = @{ source_id = $SID source_name = "GifTag Windows Helper" title = $title artist = $artist album = $album position_ms = [Math]::Max(0, $positionMs) duration_ms = [Math]::Max(0, $durationMs) is_playing = $isPlaying media_key = $key } | ConvertTo-Json -Depth 4 -Compress $endpoint = if ($key -ne $LastKey) { "/api/now_playing" } else { "/api/now_playing_tick" } Invoke-RestMethod -Uri "$BaseUrl$endpoint" -Method Post -ContentType "application/json" -Body $body -TimeoutSec 3 | Out-Null $LastKey = $key } catch { Write-Host ("GifTag gonderim hatasi: " + $_.Exception.Message) -ForegroundColor DarkYellow } Start-Sleep -Seconds 2 }