· 6 years ago · Feb 10, 2020, 04:26 AM
1Param(
2 [string]$category,
3 [string]$singleFilePath,
4 [string]$filePath,
5 [int]$numFiles,
6 [string]$TorrName,
7 [string]$infoHash
8)
9
10$logPath = "C:\Users\YOURUSERNAME\Documents\sondl.txt"
11$rarlogPath = "C:\Users\YOURUSERNAME\Documents\SonDLUnrarLogs\" + $TorrName + ".txt"
12$boxRoot = "G:\Boxes\" #boxpath goes here (temp extract location)
13
14$sonarrUrl = "http://localhost:8085/sonarr/api/command"
15$sonarrAPI = "api key goes here"
16$sonarrCat = "sonarr"
17
18$radarrUrl = "http://localhost:8086/radarr/api/command"
19$radarrAPI = "api key goes here"
20$radarrCat = "radarr"
21
22Start-Sleep -s 3
23
24$RarCheck = Get-ChildItem $filePath -name -recurse *.rar
25#$checkRar = Test-Path $filePath\*.rar
26If ($numFiles -gt 1 -Or $RarCheck -ne $NULL)
27{
28 If ($category -eq $sonarrCat -or $category -eq $radarrCat)
29 {
30 $extPath = $boxRoot + $category + "\" + $TorrName
31
32 while ((Get-Process -Name unrar -ErrorAction SilentlyContinue) -ne $null)
33 {
34 echo ($TorrName + " Sleeping for unrar for 2 secs") | Out-File -Append $logPath
35 Start-Sleep -s 2
36
37 }
38
39 $torrentDrive = Split-Path $filePath -qualifier
40 cmd /c mkdir $extPath
41
42 $ExtractCheck = $NULL
43 $retry = 0
44 while ($ExtractCheck -eq $NULL -and $retry -lt 10) {
45 if ($retry -gt 0)
46 {
47 echo ($TorrName + " Retry Attempt: " + $retry) | Out-File -Append $logPath
48 }
49 $cmdexec = $torrentDrive + " && cd """ + $filePath + """ && ""C:\Program Files\WinRAR\unrar.exe"" e *.rar -o+ """ + $extPath + """"
50 echo $cmdexec | Out-File -Append $logPath
51 cmd /c $cmdexec | Out-File -Append $rarlogPath
52
53 $dir = dir $filepath | ?{$_.PSISContainer}
54
55 foreach ($d in $dir) {
56 $RarCheck = Get-ChildItem $d.FullName -name -recurse *.rar
57 if ($RarCheck -ne $NULL)
58 {
59 $cmdexec = $torrentDrive + " && cd """ + $d.FullName + """ && ""C:\Program Files\WinRAR\unrar.exe"" e *.rar -o+ """ + $extPath + """"
60 echo $cmdexec | Out-File -Append $logPath
61 cmd /c $cmdexec | Out-File -Append $rarlogPath
62 }
63 }
64 Start-Sleep -s 2
65
66 $cmdexec = "dir /s $extPath"
67 cmd /c $cmdexec | Out-File -Append $logPath
68 $ExtractCheck = Get-ChildItem $extPath -name *.*
69 $retry++
70 }
71
72 If ($category -eq $sonarrCat)
73 {
74 $url = $sonarrUrl
75 $apikey = $sonarrAPI
76 $scanType = "DownloadedEpisodesScan"
77 }
78 If ($category -eq $radarrCat)
79 {
80 $url = $radarrUrl
81 $apikey = $radarrAPI
82 $scanType = "DownloadedMoviesScan"
83 }
84 $body = @{
85 path = $extPath
86 name = $scanType
87 downloadClientId = $infoHash.ToUpper()
88 importMode = "Move"
89 }
90 $json = (ConvertTo-Json -depth 5 $body)
91 Invoke-WebRequest -TimeoutSec 5 -Uri $url -Method POST -Body $json -ContentType 'application/json' -Headers @{"X-Api-Key"=$apikey} #| Select-Object -Expand RawContent
92 #echo (ConvertFrom-Json $json) | Out-File -Append $logPath
93 echo $json | Out-File -Append $logPath
94
95 }
96}