-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodio_downloader.ps1
More file actions
358 lines (328 loc) · 10 KB
/
Copy pathmodio_downloader.ps1
File metadata and controls
358 lines (328 loc) · 10 KB
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
$IsDotSourced = $MyInvocation.InvocationName -eq '.' -or $MyInvocation.Line -eq ''
$DefaultTextColor = (Get-Host).UI.RawUI.ForegroundColor
$SearchAttemptsUsed = 0
#region General use
function Confirm-EmptyVariable($Variable)
{
return $null -eq $Variable -or $Variable -eq ''
}
function Set-Title()
{
$Host.UI.RawUI.WindowTitle = 'Mod.IO Downloader'
}
function Exit-WithMessageAndPause()
{
Write-Host "The script will now exit."
pause
if ($IsDotSourced)
{
return
}
exit
}
function Test-RequiredModule([string]$Module)
{
if (Get-Module -ListAvailable -Name $Module)
{
return
}
Write-Host "The module " -NoNewline
Write-Host "$Module" -ForegroundColor Green -NoNewline
Write-Host " does not exist!"
Write-Host "This is needed for the script to run. Do you want to install it?"
Write-Host "Type " -NoNewline
Write-Host "Y " -ForegroundColor Yellow -NoNewline
Write-Host "or " -NoNewline
Write-Host "N" -ForegroundColor Yellow -NoNewline
Write-Host ", then press " -NoNewline
Write-Host "ENTER" -ForegroundColor Cyan -NoNewline
Write-Host ": " -NoNewline
$Prompt = Read-Host
if ($Prompt -ne "y")
{
Exit-WithMessageAndPause
}
$OldErrorActionPreference = $ErrorActionPreference
Install-Module $Module -Scope CurrentUser
if (!$?)
{
Write-Host "An error occurred! " -ForegroundColor Red -NoNewline
Write-Host " You may need to run PowerShell in administrator mode to install the module ""$Module""."
pause
exit
}
else
{
Write-Host "Done! Module """ -NoNewline
Write-Host "$Module" -ForegroundColor Green -NoNewline
Write-Host """ should have been installed for the current user!"
pause
}
$ErrorActionPreference = $OldErrorActionPreference
Clear-Host
}
function Get-ScriptConfigOptions()
{
[system.gc]::Collect()
$FileName = $PSCommandPath.Split("\")[-1]
$FileNameNoext = $FileName.SubString(0, $FileName.Length - 4)
$Global:ScriptConfigFileName = "$FileNameNoExt.conf"
if (!(Test-Path $Global:ScriptConfigFileName))
{
$DownloadsFolderLocation = (New-Object -ComObject Shell.Application).NameSpace('shell:Downloads').Self.Path
New-Item $Global:ScriptConfigFileName | Out-Null
Set-Content $Global:ScriptConfigFileName "ApiKey=put-api-key-here`nDownloadLocation=$DownloadsFolderLocation`nModSearchAttempts=5"
}
foreach ($Line in (Get-Content $Global:ScriptConfigFileName))
{
if (Confirm-EmptyVariable $Line)
{
continue
}
$LineSplit = $Line.Split("=")
$OptionName = $LineSplit[0]
$OptionResult = $LineSplit[1].Split("#")[0].Trim() # The extra functions remove the comments found in the conf file by default
Set-Variable $OptionName $OptionResult -Scope Global
if ($OptionName -eq "ApiKey" -and $OptionResult -eq "put-api-key-here")
{
Write-Color "ERROR! ","The ","default value ","for the API key is still in the config file! Please change it to your own." Red,$DefaultTextColor,Yellow,$DefaultTextColor
Exit-WithMessageAndPause
return
}
if ($OptionName -eq "DownloadLocation")
{
if (!(Test-Path $Global:DownloadLocation))
{
Write-Color "ERROR! ","The download location ",$Global:DownloadLocation," is invalid! Please provide a path to an existing folder, or make the folder at the path provided." Red,$DefaultTextColor,Yellow,$DefaultTextColor
Exit-WithMessageAndPause
return
}
}
if ($IsDotSourced)
{
Write-Host "The config option ""$OptionName"" is ""$OptionResult"""
}
}
}
function Start-YesOrNoPrompt([string]$Question = 'Agree?', [string]$YesDescription = 'Yes', [string]$NoDescription = 'No', [string]$DefaultOptionStr = 'Yes')
{
switch ($DefaultOptionStr)
{
'Yes'
{
$DefaultOptionInt = 0
}
'No'
{
$DefaultOptionInt = 1
}
}
$Choices = @(
[System.Management.Automation.Host.ChoiceDescription]::New("&Yes", $YesDescription)
[System.Management.Automation.Host.ChoiceDescription]::New("&No", $NoDescription)
)
return ![bool]$Host.UI.PromptForChoice('', $Question, $Choices, $DefaultOptionInt)
}
#endregion
#region Downloading process
function Test-ApiKey()
{
Write-Color "Testing the provided ","API key","..." $DefaultTextColor,Yellow,$DefaultTextColor
try
{
Invoke-RestMethod -Method Get -Uri "https://api.mod.io/v1/games?api_key=$Global:ApiKey" | Out-Null
}
catch
{
if ($null -eq $_.ErrorDetails.Message)
{
Write-Color "The provided API key is ","valid","!" $DefaultTextColor,Green,$DefaultTextColor
return
}
$ErrorCode = (($_.ErrorDetails.Message) | ConvertFrom-Json).error.error_ref
switch ($ErrorCode)
{
{$_ -in 11000..11002}
{
Write-Color "ERROR! ","Your API key is " Red,$DefaultTextColor -NoNewLine
}
11000
{
Write-Color "missing","! Please provide your own API key." Red,$DefaultTextColor
}
11001
{
Write-Color "malformed","! Please make sure you put in your API key correctly." Red,$DefaultTextColor
}
11002
{
Write-Color "invalid","! Please provide a new API key for your account." Red,$DefaultTextColor
}
default
{
Write-Color "ERROR! ","The error code is ",$ErrorCode Red,$DefaultTextColor,Yellow
}
}
Exit-WithMessageAndPause
return
}
Write-Color "The provided API key is ","valid","!" $DefaultTextColor,Green,$DefaultTextColor
return
}
function Get-URLs()
{
[system.gc]::Collect()
$Global:UrlSet = Read-Host "Paste in the URLs of the mod(s) you'd like to download. If you have multiple URLs, split each one with a comma and no spaces"
if ($UrlSet.Contains(",")) # Multiple URLs provided
{
$Global:UrlArray = $UrlSet.Split(",")
$Global:IsUrlSetMultiple = $true
$Global:IsUrlSetMultiple > $null
$HasValidUrl = $false
foreach ($Url in $Global:UrlArray)
{
if ($Url.Contains("mod.io"))
{
$HasValidUrl = $true
break
}
}
if (!$HasValidUrl)
{
Clear-Host
Write-Host "No mod.io URLs provided!" -ForegroundColor Red
Get-URLs
}
}
else
{
$Global:IsUrlSetMultiple = $false
if (!$UrlSet.Contains("mod.io"))
{
Clear-Host
Write-Host "That was not a valid mod.io URL!" -ForegroundColor Red
Get-URLs
}
else
{
Clear-Host
$Global:Url = $Global:UrlSet
}
}
}
function Get-GameNameIdFromUrl([string]$Url)
{
return $Url.Split("/")[4]
}
function Get-ModNameIdFromUrl([string]$Url)
{
return $Url.Split("/")[-1]
}
function Get-GameIdFromUrl([string]$Url)
{
[system.gc]::Collect()
Write-Color "Getting the ","game ID","..." $DefaultTextColor,Green,$DefaultTextColor
$ModIoData = (Invoke-RestMethod -Method Get -Uri "https://api.mod.io/v1/games?api_key=$ApiKey").data
if (Confirm-EmptyVariable $ModIoData)
{
Write-Color "ERROR! ","Could not get mod.io data! The API key provided may no longer work!" Red,$DefaultTextColor
Exit-WithMessageAndPause
return
}
foreach ($SetOfData in $ModIoData)
{
if ($SetOfData.name_id -eq (Get-GameNameIdFromUrl($Url)))
{
return $SetOfData.id
}
}
Write-Color "Could not get the ","game ID ","from the URL ",$Url,"!" $DefaultTextColor,Green,$DefaultTextColor,Yellow,$DefaultTextColor
Exit-WithMessageAndPause
return
}
function Get-ModIdFromUrlAndGameId([string]$Url, [int]$GameId, [int]$ResultsOffset = 0)
{
[system.gc]::Collect()
$ModNameId = Get-ModNameIdFromUrl $Url
$CurrentAttemptNum = $Global:SearchAttemptsUsed + 1
Write-Color "Searching mod.io for the mod ID (","Attempt $CurrentAttemptNum",")" $DefaultTextColor,Yellow,$DefaultTextColor
$ModIoGameData = (Invoke-RestMethod -Method Get -Uri "https://api.mod.io/v1/games/$GameId/mods?api_key=$ApiKey&_offset=$ResultsOffset&_q=$ModNameId").data
foreach ($SetOfData in $ModIoGameData)
{
if ($SetOfData.profile_url -eq $Url)
{
return $SetOfData.id
}
}
$Global:SearchAttemptsUsed += 1
if ($Global:SearchAttemptsUsed -eq $Global:ModSearchAttempts)
{
Write-Color "Could not get the ","mod ID ","from the URL ",$Url," in ",$Global:ModSearchAttempts," attempts!" $DefaultTextColor,Green,$DefaultTextColor,Yellow,$DefaultTextColor,Yellow,$DefaultTextColor
Write-Color "Continue searching for another ",$Global:ModSearchAttempts," attempts?" $DefaultTextColor,Green,$DefaultTextColor
switch (Start-YesOrNoPrompt -Question '' -YesDescription "Continue searching for 10 more attempts." -NoDescription "Do not continue searching, and exit the script.")
{
$false
{
Exit-WithMessageAndPause
return
}
$true
{
$Global:SearchAttemptsUsed = 0
}
}
}
Get-ModIdFromUrl $Url $GameId ($ResultsOffset + $ModIoGameData.Length)
}
function Get-ModZipFileFromUrl([string]$Url)
{
[system.gc]::Collect()
Write-Color "Processing ",$Url,"..." $DefaultTextColor,Green,$DefaultTextColor
$GameId = Get-GameIdFromUrl $Url
if (Confirm-EmptyVariable $GameId)
{
return
}
$ModId = Get-ModIdFromUrlAndGameId $Url $GameId
$ModData = (Invoke-RestMethod -Method Get -Uri "https://api.mod.io/v1/games/$GameId/mods/$ModId`?api_key=$ApiKey")
$DownloadUrl = $ModData.modfile.download.binary_url
$DownloadFileName = $ModData.modfile.filename
if (Test-Path "$Global:DownloadLocation\$DownloadFileName")
{
Write-Color "WARNING! ","The file ",$DownloadFileName," has already been downloaded!" Yellow,$DefaultTextColor,Green,$DefaultTextColor
switch (Start-YesOrNoPrompt -Question 'Do you want to replace the file?' -YesDescription 'The file will be removed before being downloaded.' -NoDescription 'The script will skip this file.')
{
$true
{
Remove-Item "$Global:DownloadLocation\$DownloadFileName"
}
$false
{
return
}
}
}
Write-Color "Downloading ",$DownloadFileName," to ",$Global:DownloadLocation,"..." $DefaultTextColor,Green,$DefaultTextColor,Green,$DefaultTextColor
Invoke-WebRequest $DownloadUrl -OutFile "$Global:DownloadLocation\$DownloadFileName"
}
#endregion
if (!$IsDotSourced)
{
Set-Title
Test-RequiredModule "PSWriteColor"
Get-ScriptConfigOptions
Test-ApiKey
Get-URLs
if ($Global:IsUrlSetMultiple)
{
foreach ($Url in $Global:UrlArray)
{
Get-ModZipFileFromUrl $Url
}
Write-Host "Done!" -ForegroundColor Green
Exit-WithMessageAndPause
}
Get-ModZipFileFromUrl $Global:Url
Write-Host "Done!" -ForegroundColor Green
Exit-WithMessageAndPause
}