Skip to content

Instantly share code, notes, and snippets.

@kimboslice99
Last active February 4, 2024 04:51
Show Gist options
  • Save kimboslice99/78d75a26b9e03d92ab11b7bcb07a01fc to your computer and use it in GitHub Desktop.
Save kimboslice99/78d75a26b9e03d92ab11b7bcb07a01fc to your computer and use it in GitHub Desktop.
Set QBT to alternative speeds while a stream is active in Jellyfin or Emby
# Read configuration from JSON file, run once to generate config.json
$configFile = "$PSScriptRoot/config.json"
if (-not (Test-Path $configFile)) {
$configlayout = @{
"jfAddress"="http://127.0.0.1:8096";
"jfKey"="abcdefg12345";
"qbtAddress"="http://127.0.0.1:8080";
"QBusername"="username";
"QBpassword"="password";
}
$configlayout | ConvertTo-Json | Out-File "$PSScriptRoot/config.json.sample"
Write-Host "Config file not found!" -ForegroundColor Red
Exit 1
}
$config = Get-Content -Raw -Path $configFile | ConvertFrom-Json
# Function to check play state of a media server
function CheckPlayState($Address, $ApiKey) {
try {
# Set up the headers
$headers = @{
"Authorization" = "Mediabrowser Token=$ApiKey"
}
# Make the API request
$sessions = Invoke-RestMethod -UseBasicParsing -Uri $Address'/emby/Sessions' -Method 'GET' -ContentType "application/json" -Headers $headers
$result = $false
foreach ($session in $sessions) {
if ($session.PlayState -and $session.PlayState.CanSeek -eq $true -and $session.PlayState.IsPaused -eq $false) {
$result = $true
break
}
}
return $result
} catch {
Write-Host "Couldn't connect to $Address! Error: $_"
return $false
}
}
# Function to set speed limits in qBittorrent
function SetSpeedLimits($qbtAddress, $username, $password, $speedMode) {
try {
$loginResult = Invoke-WebRequest -UseBasicParsing -Uri "$qbtAddress/api/v2/auth/login" -Method 'POST' -Body "username=$username&password=$password" -Headers @{'Referrer' = "$qbtAddress/"}
$loginCookie = $loginResult.Headers['Set-Cookie'] -replace "SID=([^;]+).*", '$1'
$session = [Microsoft.PowerShell.Commands.WebRequestSession]::new()
$cookie = [System.Net.Cookie]::new('SID', $loginCookie)
$session.Cookies.Add("$qbtAddress/", $cookie)
$speedLimitsUrl = "$qbtAddress/api/v2/transfer/speedLimitsMode"
$currentSpeedMode = Invoke-WebRequest -UseBasicParsing -Uri $speedLimitsUrl -Method 'GET' -WebSession $session | Select-Object -Expand Content
if ($currentSpeedMode -ne $speedMode) {
if ($speedMode -eq 1) {
$mode = 'Alternate speed rates enabled'
} else {
$mode = 'Standard speed rates enabled'
}
Write-Host $mode
Invoke-WebRequest -UseBasicParsing -Uri "$qbtAddress/api/v2/transfer/toggleSpeedLimitsMode" -Method 'POST' -WebSession $session | Out-Null
}
}
catch {
Write-Host "An error occurred: $_"
}
}
# Extract configuration values
$jfAddress = $config.jfAddress
$jfKey = $config.jfKey
# QB login
$qbtAddress = $config.qbtAddress
$QBusername = $config.QBusername
$QBpassword = $config.QBpassword
# give jellyfin high priority
# I handle this with Service2Task but it could also be done here
#Get-Process -Name "Jellyfin" | ForEach-Object { $_.PriorityClass = 'High' }
# Main loop
while ($true) {
# sleep for a bit
Start-Sleep -Seconds 30
$jfResult = CheckPlayState $jfAddress $jfKey
if ($jfResult) {
Write-Host "Active streams found"
SetSpeedLimits $qbtAddress $QBusername $QBpassword 1
} else {
Write-Host "No active streams"
SetSpeedLimits $qbtAddress $QBusername $QBpassword 0
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment