-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAddToRealDebrid.ps1
109 lines (93 loc) · 3.76 KB
/
AddToRealDebrid.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# Define the Real-Debrid API endpoints
$baseURL = "https://api.real-debrid.com/rest/1.0"
$addTorrentEndpoint = "$baseURL/torrents/addTorrent"
$listTorrentsEndpoint = "$baseURL/torrents"
$selectFilesEndpoint = "$baseURL/torrents/selectFiles/"
# Your Real-Debrid API Key
$apiKey = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
# Folder containing torrent files
$torrentsFolder = "C:\Apps\AddToRealDebrid\torrents"
# Function to add a torrent file to Real-Debrid
function AddTorrentToRealDebrid($torrentFilePath) {
# Prepare headers
$headers = @{
"Authorization" = "Bearer $apiKey"
}
# Read the torrent file content as byte array
$torrentContent = [System.IO.File]::ReadAllBytes($torrentFilePath)
# Send PUT request to Real-Debrid API to add the torrent
try {
$response = Invoke-RestMethod -Uri $addTorrentEndpoint -Method Put -Headers $headers -ContentType "application/octet-stream" -Body $torrentContent
Write-Host "Torrent added successfully: $($response.filename)"
return $response
} catch {
Write-Host "Failed to add torrent: $_"
return $null
}
}
# Function to get torrent details, including the list of files
function GetTorrentDetails($torrentId) {
# Prepare headers
$headers = @{
"Authorization" = "Bearer $apiKey"
}
# Send GET request to Real-Debrid API to get torrent details
try {
$response = Invoke-RestMethod -Uri ($listTorrentsEndpoint + "/info/$torrentId") -Method Get -Headers $headers
Write-Host "Torrent details for torrent ID: $torrentId"
Write-Host "Response: $response"
if ($response.torrent.files) {
Write-Host "Files found in torrent:"
foreach ($file in $response.torrent.files) {
Write-Host "File: $($file.filename)"
}
} else {
Write-Host "No files found in torrent."
}
} catch {
Write-Host "Failed to get torrent information for torrent $($torrentId): $_"
}
}
# Function to select all files for a torrent
function SelectAllFilesInTorrent($torrentId) {
# Prepare headers
$headers = @{
"Authorization" = "Bearer $apiKey"
}
# Prepare payload to select all files
$payload = @{
"files" = "*"
}
# Send POST request to Real-Debrid API to select all files in the torrent
try {
$response = Invoke-RestMethod -Uri ($selectFilesEndpoint + "/$torrentId") -Method Post -Headers $headers -ContentType "application/json" -Body ($payload | ConvertTo-Json)
Write-Host "All files selected for torrent: $torrentId"
return $true
} catch {
Write-Host "Failed to select all files for torrent $($torrentId): $($_.Exception.Message)"
if ($_.Exception.Response -ne $null -and $_.Exception.Response.StatusCode -eq "BadRequest") {
$responseContent = $_.Exception.Response.Content
Write-Host "Response content: $responseContent"
}
return $false
}
}
# Function to add a torrent file to Real-Debrid and select all files
function AddTorrentAndSelectAllFiles($torrentFilePath) {
# Add torrent file to Real-Debrid
$torrentInfo = AddTorrentToRealDebrid $torrentFilePath
if ($torrentInfo) {
$torrentId = $torrentInfo.id
Write-Host "Torrent added successfully: $($torrentInfo.filename)"
# Get torrent details (list of files)
GetTorrentDetails $torrentId
# Select all files within the torrent
SelectAllFilesInTorrent $torrentId
}
}
# Get all torrent files in the folder
$torrentFiles = Get-ChildItem -Path $torrentsFolder -Filter *.torrent
# Add each torrent file to Real-Debrid and select all files
foreach ($torrentFile in $torrentFiles) {
AddTorrentAndSelectAllFiles $torrentFile.FullName
}