Advertisement
Guest User

plex_update.ps1 FINAL V3.0

a guest
Feb 17th, 2024
601
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.91 KB | Source Code | 0 0
  1. Add-Type -AssemblyName System.Web
  2.  
  3. # Plex server details - EDIT BELOW
  4. $plexUrl = "http://plex-ip:32400"
  5. $plexToken = "your-plex-token"
  6.  
  7. # Replace with your mount - EDIT BELOW
  8. $mount = "Z:"
  9.  
  10. $path = $args[2]
  11.  
  12. # Set how many times you want the script to retry if the folder has not yet been added - EDIT BELOW
  13. $retryAmount = 30
  14.  
  15. # Function to URL encode a string
  16. function UrlEncode($value) {
  17. [System.Web.HttpUtility]::UrlEncode($value, [System.Text.Encoding]::UTF8)
  18. }
  19.  
  20. # Example path to a log - EDIT BELOW
  21. Start-Transcript -Path "C:\Path\To\zurg-testing\logs\plex_update.log"
  22.  
  23. # Function to trigger library update for a specific folder
  24. function UpdateFolder($retries) {
  25. $section_ids = (Invoke-WebRequest -Uri "$plexUrl/library/sections" -Headers @{"X-Plex-Token" = $plexToken} -UseBasicParsing -Method Get).Content |
  26. Select-Xml -XPath "//Directory/@key" |
  27. ForEach-Object { $_.Node.Value }
  28.  
  29. Write-Host "IDs: $section_ids"
  30. $fullPath = Join-Path -Path $mount -ChildPath $path
  31. Write-Host "Path: $fullPath"
  32. $encodedPath = UrlEncode $fullPath
  33.  
  34. if (Test-Path -LiteralPath $fullPath) {
  35. Write-Host "Path exists"
  36. # Trigger the library update for the specific folder
  37. foreach ($section_id in $section_ids) {
  38. $final_url = "${plexUrl}/library/sections/${section_id}/refresh?path=${encodedPath}&X-Plex-Token=${plexToken}"
  39.  
  40. Write-Host "Encoded argument: $encodedPath"
  41. Write-Host "Section ID: $section_id"
  42. Write-Host "Final URL: $final_url"
  43.  
  44. $request = Invoke-WebRequest -Uri $final_url -UseBasicParsing -Method Get
  45.  
  46. Write-Host $request
  47.  
  48. Write-Host "Partial refresh request successful for: $($path)"
  49. }
  50. } else {
  51. if (!$retries -eq 0) {
  52. $retries--
  53. Write-Host "Retries: $retries"
  54. Write-Host "Path not found. Trying again..."
  55. Start-Sleep -Seconds 1
  56. UpdateFolder $retries
  57. } else {
  58. Write-Host "The path does not exist."
  59. }
  60. }
  61. }
  62.  
  63. # Function to trigger library update for a specific folder
  64. function UpdateFolderLast5($folder, $directory) {
  65. $section_ids = (Invoke-WebRequest -Uri "$plexUrl/library/sections" -Headers @{"X-Plex-Token" = $plexToken} -UseBasicParsing -Method Get).Content |
  66. Select-Xml -XPath "//Directory/@key" |
  67. ForEach-Object { $_.Node.Value }
  68.  
  69. Write-Host "IDs: $section_ids"
  70. $fullPath = Join-Path -Path $directory -ChildPath $folder.Name
  71. Write-Host "Path: $fullPath"
  72. $encodedPath = UrlEncode $fullPath
  73.  
  74. try {
  75. # Trigger the library update for the specific folder
  76. foreach ($section_id in $section_ids) {
  77. $final_url = "${plexUrl}/library/sections/${section_id}/refresh?path=${encodedPath}&X-Plex-Token=${plexToken}"
  78.  
  79. Write-Host "Encoded argument: $encodedPath"
  80. Write-Host "Section ID: $section_id"
  81. Write-Host "Final URL: $final_url"
  82.  
  83. # Trigger the library update for the specific folder
  84. $request = Invoke-WebRequest -Uri $final_url -UseBasicParsing -Method Get
  85.  
  86. Write-Host $request
  87.  
  88. Write-Host "Partial refresh request successful for: $($path)"
  89. }
  90. } catch {
  91. Write-Host "Error refreshing: $($folder.FullName)"
  92. Write-Host "Error details: $_"
  93. }
  94. }
  95.  
  96. # Function to update folders within the last 5 minutes
  97. function UpdateFoldersWithinLast5Minutes($directories, $retries) {
  98. $startTime = (Get-Date).AddMinutes(-5)
  99. $foundNewItem = $false
  100.  
  101. foreach ($directory in $directories) {
  102. $folders = Get-ChildItem -Path $directory -Directory | Where-Object { $_.LastWriteTime -gt $startTime }
  103.  
  104. if ($folders.Count -gt 0) {
  105. $foundNewItem = $true
  106. Write-Host "Folders found in $directory modified within the last 5 minutes:"
  107.  
  108. foreach ($folder in $folders) {
  109. UpdateFolderLast5 $folder $directory
  110. }
  111. } else {
  112. Write-Host "No folders found in $directory modified within the last 5 minutes."
  113. }
  114. }
  115.  
  116. if (!$foundNewItem) {
  117. if (!$retries -eq 0) {
  118. $retries--
  119. Write-Host "Retries: $retries"
  120. Write-Host "Trying again..."
  121. Start-Sleep -Seconds 1
  122. UpdateFoldersWithinLast5Minutes $directories $retries
  123. }
  124. }
  125. }
  126.  
  127. # Example usage - REPLACE WITH YOUR DIRECTORIES
  128. $directoriesToUpdate = @("Z:\movies","Z:\anime","Z:\shows","Z:\movies4k","Z:\shows4k")
  129.  
  130. if ($args.length -gt 4) {
  131. Write-Host "Update within last 5 minutes"
  132. UpdateFoldersWithinLast5Minutes $directoriesToUpdate $retryAmount
  133. }
  134. else {
  135. Write-Host "Normal method"
  136. if ($args[2].StartsWith("__all__")) {
  137. Write-Host "Path starts with '__all__'."
  138. $path = $args[3]
  139. }
  140.  
  141. UpdateFolder $retryAmount
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement