47 lines
1.8 KiB
Lua
47 lines
1.8 KiB
Lua
|
|
-- "Что играет" — query Windows SMTC.
|
|||
|
|
-- Requires Windows 10 1803+ (Media Session API).
|
|||
|
|
local ps = [[
|
|||
|
|
[Windows.Media.Control.GlobalSystemMediaTransportControlsSessionManager,Windows.Media.Control,ContentType=WindowsRuntime] | Out-Null
|
|||
|
|
$mgr = [Windows.Media.Control.GlobalSystemMediaTransportControlsSessionManager]::RequestAsync()
|
|||
|
|
while ($mgr.Status -eq 0) { Start-Sleep -Milliseconds 50 }
|
|||
|
|
$session = $mgr.GetResults().GetCurrentSession()
|
|||
|
|
if (-not $session) { Write-Output 'NONE'; exit }
|
|||
|
|
$propsTask = $session.TryGetMediaPropertiesAsync()
|
|||
|
|
while ($propsTask.Status -eq 0) { Start-Sleep -Milliseconds 50 }
|
|||
|
|
$props = $propsTask.GetResults()
|
|||
|
|
$artist = $props.Artist
|
|||
|
|
$title = $props.Title
|
|||
|
|
if ([string]::IsNullOrEmpty($artist) -and [string]::IsNullOrEmpty($title)) {
|
|||
|
|
Write-Output 'EMPTY'
|
|||
|
|
exit
|
|||
|
|
}
|
|||
|
|
Write-Output ("{0}|{1}" -f $artist, $title)
|
|||
|
|
]]
|
|||
|
|
|
|||
|
|
local res = jarvis.system.exec(string.format(
|
|||
|
|
'powershell -NoProfile -ExecutionPolicy Bypass -Command "%s"',
|
|||
|
|
ps:gsub('"', '\\"'):gsub("\r?\n", "; ")
|
|||
|
|
))
|
|||
|
|
|
|||
|
|
if not res.success then
|
|||
|
|
return jarvis.cmd.error("Не получилось получить данные.")
|
|||
|
|
end
|
|||
|
|
|
|||
|
|
local out = (res.stdout or ""):gsub("^%s+", ""):gsub("%s+$", "")
|
|||
|
|
if out == "NONE" then
|
|||
|
|
return jarvis.cmd.not_found("Сейчас ничего не играет.")
|
|||
|
|
end
|
|||
|
|
if out == "EMPTY" or out == "|" then
|
|||
|
|
return jarvis.cmd.not_found("Что-то играет, но без названия.")
|
|||
|
|
end
|
|||
|
|
|
|||
|
|
local artist, title = out:match("^([^|]*)|(.*)$")
|
|||
|
|
if not title or title == "" then
|
|||
|
|
return jarvis.cmd.not_found("Трек без названия.")
|
|||
|
|
end
|
|||
|
|
|
|||
|
|
if artist and artist ~= "" then
|
|||
|
|
return jarvis.cmd.ok(string.format("Сейчас играет: %s — %s.", artist, title))
|
|||
|
|
end
|
|||
|
|
return jarvis.cmd.ok(string.format("Сейчас играет: %s.", title))
|