Skip to content

Commit

Permalink
- Added additional cmdline argument to wget call to fix issue #2
Browse files Browse the repository at this point in the history
- Added the ability to provide Url and download location via parameters
- added verbose statements for debugging purposes (only when script is called with "-Verbose")
  • Loading branch information
lazaroblanc committed Mar 26, 2021
1 parent 38294cc commit 7a267f3
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 15 deletions.
44 changes: 29 additions & 15 deletions IndexOfDownloader.ps1
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
[CmdletBinding()]
param (
$Url,
$Path
)

Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName Microsoft.VisualBasic

# Prompt for Url
$title = '"Index of" downloader'
$msg = "Please enter URL"
$Url = [Microsoft.VisualBasic.Interaction]::InputBox($msg, $title, "https://...")
if (-not $Url) {
$title = '"Index of" downloader'
$msg = "Please enter URL"
$Url = [Microsoft.VisualBasic.Interaction]::InputBox($msg, $title, "https://...")
}
"Url: $Url" | Write-Verbose

if ($Url.length -eq 0) { exit }
if (-not [Uri]::IsWellFormedUriString($Url, 1)) {
Expand All @@ -22,6 +31,8 @@ $wgetParams = @(
"--cut-dirs=$NoOfDirsToCut"
"--quiet"
"--show-progress"
"--restrict-file-names=ascii" # mode "nocontrol" unfortunately does not work on Windows so UTF-8 filenames are not possible atm :(
"--local-encoding=utf-8"
)

try { $statusCode = (Invoke-WebRequest -Uri $Url -ErrorAction Stop).StatusCode }
Expand All @@ -41,26 +52,29 @@ while ($statusCode -eq 401) {
}

# Prompt for download location
$folderBrowseDialog = [System.Windows.Forms.FolderBrowserDialog]::new()
$folderBrowseDialog.Description = @"
Select download directory
Downloadverzeichnis auswählen
"@
$buttonPressed = $folderBrowseDialog.ShowDialog()
if ($buttonPressed -eq "Cancel") { exit }
Set-Location $folderBrowseDialog.SelectedPath
if (-not $Path) {
$folderBrowseDialog = [System.Windows.Forms.FolderBrowserDialog]::new()
$folderBrowseDialog.Description = "Select download directory`n`nDownloadverzeichnis auswählen"
$buttonPressed = $folderBrowseDialog.ShowDialog()
if ($buttonPressed -eq "Cancel") { exit }
Set-Location $folderBrowseDialog.SelectedPath
}
else {
Set-Location $Path
}
"Path: $(Get-Location)" | Write-Verbose

# Only download wget if not already found on system
$wget = "wget.exe"
if (-not (Get-Command $wget -ErrorAction SilentlyContinue)) {
$wget = ".\wget.exe"
if (-not (Test-Path "$wget" -ErrorAction SilentlyContinue)) {
if ([Environment]::Is64BitOperatingSystem) { $architecture = "64" }
else { $architecture = "32" }
"Downloading wget" | Write-Verbose
Invoke-WebRequest -Uri "https://eternallybored.org/misc/wget/1.20.3/$architecture/wget.exe" -OutFile ".\$wget"
$cleanupWget = $true
$wget = ".\$wget"
}

"Running wget" | Write-Verbose
& $wget $wgetParams --execute robots=off --reject index.htm* $Url

if ($cleanupWget) { $wget | Remove-Item }
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ Now I can just give them this script with a URL to where the downloads are *et-v

1. I've used *reasonable* arguments for `wget` in the script. There currently is no need to use different arguments nor is there a way to supply your own arguments to the `wget` instance in this script. Feel free to modify them in the script itself or add a way for you to pass them into the script via a parameter(s). In that case I would welcome a pull request! :) I however do not currently need this functionality.

1. Non-ASCII filenames (like UTF-8) are not supported (wget limitation under Windows, unfortunately nothing I can do). Non-ASCII characters get escaped.

<div align="center">
<hr>
<table>
Expand Down

0 comments on commit 7a267f3

Please sign in to comment.