-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun-ripme.ps1
165 lines (110 loc) · 3.48 KB
/
run-ripme.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
param (
[array]$Urls,
[string]$ResultFile = ".\result\$(Get-Date -Format 'MM-dd-yyyy_HHmmss').csv",
[string]$CsvFile,
[int]$CooldownTimer = 300
)
#
# Settings
#
$StorageFolder = ($env:USERPROFILE + "\Downloads\RipMe")
$JarFile = Join-Path -Path $StorageFolder -ChildPath "ripme.jar"
$Threads = 5 # Thread Performance
$MaxJobTime = 10 # Job Timeout in Minutes
# Creating Folder If Missing
if (!(Test-Path -PathType Container $StorageFolder)) {
Write-Host "Creating directory: $StorageFolder"
New-Item -ItemType Directory -Path $StorageFolder
}
#
# Checks
#
# Parameter Check
if(($Urls -and $CsvFile) -or (!($Urls) -and !($CsvFile))){
Write-Error "Parameters error! You must use one parameter." -ErrorAction Stop
}
# CSV File Actually Exists
if($CsvFile){
if(!(Test-Path -Path $CsvFile)){
Write-Error "Invalid CSV Path!" -ErrorAction Stop
}
}
#
# CSV Handling
#
if($CsvFile){
$Urls = Import-Csv -Path $CsvFile -Header URLs |
Select-Object -ExpandProperty URLs
}
#
# Functions
#
# RipMe Function
function Start-RipMe {
param(
$Url
)
# Initial Start Message
Write-Host "Starting job for URL: $Url" -ForegroundColor Blue
# Running as a job for monitoring
$DownloadJob = Start-Job -ScriptBlock {
param(
$JarFile,
$StorageFolder,
$Url,
$Threads
)
java -jar $JarFile --ripsdirectory $StorageFolder --url $Url --threads $Threads --skip404
} -ArgumentList $JarFile, $StorageFolder, $Url, $Threads
# Monitor: This will loop every minute until the job stops or the Max Job Time has been reached.
$JobMonitorCounter = $null # Resetting counter
while ($DownloadJob.State -eq 'Running'){
Start-Sleep -Seconds 60
if($JobMonitorCounter -ge $MaxJobTime){
Write-Host "Job has exceeded it's threshold. Terminating job...." -ForegroundColor DarkRed
Stop-Job -Job $DownloadJob
}else{
$JobMonitorCounter++
Write-Host "- Still processing... $JobMonitorCounter minute(s) have elapsed" -ForegroundColor DarkGray
}
}
# Providing result
if ($DownloadJob.State -eq 'Completed') {
Write-Host "- The job has completed successfully!" -ForegroundColor DarkGreen
$FinalStatus = "Completed"
} elseif ($DownloadJob.State -eq 'Stopped') {
Write-Host "- The job was terminated due to lack of progress." -ForegroundColor DarkRed
$FinalStatus = "Terminated"
} else {
Write-Host "- The job ended in state: $($DownloadJob.State)" -ForegroundColor DarkRed
$FinalStatus = "Error"
}
# Cleanup the job
Remove-Job -Job $DownloadJob
Return $FinalStatus
}
# Variables required for cooldown counting
$BatchCounter = 0
$BatchLastItem = $Urls.Count
# Creating Array for Batch Results
$BatchResult = @()
# Looping through URLs and starting the functions
$Urls | ForEach-Object {
$result = $null
$result = Start-RipMe -Url $_
$BatchCounter++
# Checking if a cooldown is required based on timer
if($BatchCounter -lt $BatchLastItem){
Write-Host "- Cooldown required, please wait!"
Start-Sleep -Seconds $CooldownTimer
}else{
Write-Host "- Cooldown is not required."
}
$BatchResult += [PSCustomObject]@{
Job = $_
Result = $result
}
}
$BatchResult | Export-Csv -Path $ResultFile -NoTypeInformation
return $BatchResult
Write-Host "Script has ended."