Skip to content

Commit

Permalink
Merge pull request #82 from fatbob01/patch-8
Browse files Browse the repository at this point in the history
Update plex_update.ps1
  • Loading branch information
yowmamasita authored Feb 16, 2024
2 parents d80bc93 + 84ab8b3 commit 3f1cb8d
Showing 1 changed file with 87 additions and 17 deletions.
104 changes: 87 additions & 17 deletions plex_update.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,15 @@ $plexToken = "your-plex-token"
$mount = "Z:"

$path = $args[2]

# Set how many times you want the script to retry if the folder has not yet been added
$retryAmount = 30

# Function to URL encode a string
function UrlEncode($value) {
[System.Web.HttpUtility]::UrlEncode($value)
[System.Web.HttpUtility]::UrlEncode($value, [System.Text.Encoding]::UTF8)
}

# Example path to a log - EDIT BELOW
Start-Transcript -Path "C:\Zurg\zurg-testing\logs\plex_update.log"
Start-Transcript -Path "C:\Path\To\zurg-testing\plex_update.log"

# Function to trigger library update for a specific folder
function UpdateFolder($retries) {
Expand All @@ -27,24 +25,25 @@ function UpdateFolder($retries) {
ForEach-Object { $_.Node.Value }

Write-Host "IDs: $section_ids"
Write-Host "Path: $ $mount/$path"
$encodedPath = UrlEncode("$mount/$path")
$fullPath = Join-Path -Path $mount -ChildPath $path
Write-Host "Path: $fullPath"
$encodedPath = UrlEncode $fullPath

if (Test-Path $mount/$path) {
if (Test-Path -LiteralPath $fullPath) {
Write-Host "Path exists"
# Trigger the library update for the specific folder
foreach ($section_id in $section_ids) {
$final_url = "${plexUrl}/library/sections/${section_id}/refresh?path=${encodedPath}&X-Plex-Token=${plexToken}"
$final_url = "${plexUrl}/library/sections/${section_id}/refresh?path=${encodedPath}&X-Plex-Token=${plexToken}"

Write-Host "Encoded argument: $encodedPath"
Write-Host "Section ID: $section_id"
Write-Host "Final URL: $final_url"
Write-Host "Encoded argument: $encodedPath"
Write-Host "Section ID: $section_id"
Write-Host "Final URL: $final_url"

$request = Invoke-WebRequest -Uri $final_url -UseBasicParsing -Method Get
$request = Invoke-WebRequest -Uri $final_url -UseBasicParsing -Method Get

Write-Host $request
Write-Host $request

Write-Host "Partial refresh request successful for: $($path)"
Write-Host "Partial refresh request successful for: $($path)"
}
} else {
if (!$retries -eq 0) {
Expand All @@ -53,11 +52,82 @@ function UpdateFolder($retries) {
Write-Host "Path not found. Trying again..."
Start-Sleep -Seconds 1
UpdateFolder $retries
}
else {
} else {
Write-Host "The path does not exist."
}
}
}

UpdateFolder $retryAmount
# Function to trigger library update for a specific folder
function UpdateFolderLast5($folder, $directory) {
$section_ids = (Invoke-WebRequest -Uri "$plexUrl/library/sections" -Headers @{"X-Plex-Token" = $plexToken} -UseBasicParsing -Method Get).Content |
Select-Xml -XPath "//Directory/@key" |
ForEach-Object { $_.Node.Value }

Write-Host "IDs: $section_ids"
$fullPath = Join-Path -Path $directory -ChildPath $folder.Name
Write-Host "Path: $fullPath"
$encodedPath = UrlEncode $fullPath

try {
# Trigger the library update for the specific folder
foreach ($section_id in $section_ids) {
$final_url = "${plexUrl}/library/sections/${section_id}/refresh?path=${encodedPath}&X-Plex-Token=${plexToken}"

Write-Host "Encoded argument: $encodedPath"
Write-Host "Section ID: $section_id"
Write-Host "Final URL: $final_url"

# Trigger the library update for the specific folder
$request = Invoke-WebRequest -Uri $final_url -UseBasicParsing -Method Get

Write-Host $request

Write-Host "Partial refresh request successful for: $($path)"
}
} catch {
Write-Host "Error refreshing: $($folder.FullName)"
Write-Host "Error details: $_"
}
}

# Function to update folders within the last 5 minutes
function UpdateFoldersWithinLast5Minutes($directories) {
$startTime = (Get-Date).AddMinutes(-5)

Start-Sleep -Seconds 5

foreach ($directory in $directories) {
$folders = Get-ChildItem -Path $directory -Directory | Where-Object { $_.LastWriteTime -gt $startTime }

if ($folders.Count -gt 0) {
Write-Host "Folders found in $directory modified within the last 5 minutes:"

# Introduce a 10-second delay before triggering the library update for each folder
Start-Sleep -Seconds 10

foreach ($folder in $folders) {
UpdateFolderLast5 $folder $directory
}
} else {
Write-Host "No folders found in $directory modified within the last 5 minutes."
}
}
}

# Example usage - REPLACE WITH YOUR DIRECTORIES
$directoriesToUpdate = @("Z:\movies","Z:\anime","Z:\shows","Z:\movies4k","Z:\shows4k")

if ($args.length -gt 4) {
Write-Host "Update within last 5 minutes"
UpdateFoldersWithinLast5Minutes $directoriesToUpdate $retryAmount
}
else {
Write-Host "Normal method"
if ($args[2].StartsWith("__all__")) {
Write-Host "Path starts with '__all__'."
$path = $args[3]
}

UpdateFolder $retryAmount
}

0 comments on commit 3f1cb8d

Please sign in to comment.