Hi all,
I've been playing around with the Plex API's using the Invoke-RestMethod cmdlet and the unofficial document around the APIs.
Posting the below to help out others, the code will:
- Get a auth token using supplied credentials
- Connect to a local Plex Media Server
- Get a list of libraries
This list then can be iterated over for a variety of purposes. I'm currently working with it to get a list of duplicate files, pick the best quality one and remove the others for a cleanup of recently mass imported data.
Feel free to post criticisms, feedback and other useful tips!
Mileage may vary, use at own risk, requires PowerShell 4.0+
#'------------------------------------------------------------------------------
#'Get Credentials, convert to Base64 for basic HTML Authentication
#'------------------------------------------------------------------------------
[string]$command = "Get-credential"
Try
{
$cred = Get-Credential
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $cred.GetNetworkCredential().UserName,$cred.GetNetworkCredential().Password)))
}
catch
{
break;
}
#'------------------------------------------------------------------------------
#'Invoke REST API
#'------------------------------------------------------------------------------
[string]$command = "Invoke-RestMethod -Uri ""https://plex.tv/users/sign_in.xml""-Method POST -headers @{'Authorization'=(""Basic {0}"" -f $base64AuthInfo);'X-Plex-Client-Identifier'=""TestApp"";'X-Plex-Product'=""TestScript"";'X-Plex-Version'=""V1.00"";'X-Plex-Username'=$cred.GetNetworkCredential().UserName;}"
Try
{
[array]$data = Invoke-RestMethod `
-Uri "https://plex.tv/users/sign_in.xml"`
-Method POST `
-headers @{
'Authorization'=("Basic {0}" -f $base64AuthInfo);
'X-Plex-Client-Identifier'="PowerShell-Test";
'X-Plex-Product'='PowerShell-Test';
'X-Plex-Version'="V0.01";
'X-Plex-Username'=$cred.GetNetworkCredential().UserName;
}
$authToken = $data.user.authenticationToken
"Executed: $command"
}
catch
{
"Executing: $command"
break;
}
[string]$localPlexAddr = "10.0.0.6"
[string]$plexRESTAddr = "library/sections"
#'------------------------------------------------------------------------------
#'Invoke REST API
#'------------------------------------------------------------------------------
[string]$command = "Invoke-RestMethod -Uri ""http://$localPlexAddr`:32400/$plexRESTAddr"" -Method POST -headers @{'Authorization'=(""Basic {0}"" -f $base64AuthInfo);'X-Plex-Client-Identifier'=""TestApp"";'X-Plex-Product'=""TestScript"";'X-Plex-Version'=""V1.00"";'X-Plex-Username'=$cred.GetNetworkCredential().UserName;'X-Plex-Token'=$authToken}"
Try
{
[array]$data = Invoke-WebRequest `
-Uri "http://$localPlexAddr`:32400/$plexRESTAddr" `
-Method GET `
-Headers @{
'Authorization'=("Basic {0}" -f $base64AuthInfo);
'X-Plex-Client-Identifier'="PowerShell-Test";
'X-Plex-Product'='PowerShell-Test';
'X-Plex-Version'="V0.01";
'X-Plex-Username'=$cred.GetNetworkCredential().UserName;
'X-Plex-Token'=$authToken
}
"Executed: $command"
}
catch
{
"Executing: $command"
break;
}
[xml]$apiContent = $data.Content
[array]$plexLibraries = $apiContent.MediaContainer.Directory