Skip to content

Instantly share code, notes, and snippets.

@Philmist
Created February 17, 2021 14:07
Show Gist options
  • Save Philmist/331671fd6a74083ad93325397c6aee4b to your computer and use it in GitHub Desktop.
Save Philmist/331671fd6a74083ad93325397c6aee4b to your computer and use it in GitHub Desktop.
foobar2000が再生してる曲を書きだしたファイルをPOSTするだけのスクリプト
Param([string]$FileFilter = "*.*")
function Start-Changed-Watcher() {
Param (
[string]$Path,
[String]$Filter = "*.*"
)
$watcher = New-Object System.IO.FileSystemWatcher
$watcher.Path = Resolve-Path $Path
$watcher.Filter = $Filter
$watcher.IncludeSubdirectories = $false
$watcher.InternalBufferSize = 8192
$watcher.NotifyFilter = [System.IO.NotifyFilters]::LastWrite
$watcher.EnableRaisingEvents = $true
$file_notified_block = {
Param($sender, $ev)
Write-Host "DISABLE WATCHER"
$sender.EnableRaisingEvents = $false
$Name = ($ev.FullPath).ToString()
$Content = Get-Content $Name
Write-Host "NAME: " $Name
Write-Host $Content
$fn = "@" + $Name
curl -d $fn -H "Content-Type: text/plain" -m 2 http://192.168.0.100:9090/philmist-bundle/nowplaying
Write-Host "ENABLE WATCHER"
$sender.EnableRaisingEvents = $true
}
$si = ("UserFileChanged_" + $Path + "_" + $Filter)
$ret = Register-ObjectEvent -InputObject $watcher -EventName Changed -SourceIdentifier $si -Action $file_notified_block
return {
Unregister-Event $si
Remove-Job $si
}.GetNewClosure()
}
$leaf = Split-Path -Leaf $FileFilter
$parent = Split-Path -Parent $FileFilter
$watcher_job = Start-Changed-Watcher $parent $leaf
Write-Host "Start Watching... PRESS CTRL-C TO STOP"
try {
while($true) {
Start-Sleep -milliseconds 100
}
} finally {
Write-Host "Exit"
&$watcher_job
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment