iNNOKENTIY21
Silver Member | Редактировать | Профиль | Сообщение | Цитировать | Сообщить модератору
Код: function SourceForgeDownload { #Requires -Version 5.1 [CmdletBinding(SupportsShouldProcess)] param ( # Ссылка на загрузку с sourceforge.net [Parameter(Mandatory)] [ValidateScript( { ($_.Host -eq 'sourceforge.net') -and ($_.Segments[1] -eq 'projects/') -and ($_.Segments.Count -ge 3) })] [uri] $Url, # Полное имя папки для сохранения [Parameter(Mandatory)] [ValidateScript( { Test-Path -Path $_ })] [string] $Path ) $ErrorActionPreference = "Stop" if (!$BaseUriProject) { $ConstantBaseUriProjectParam = @{ Name = 'BaseUriProject' Value = ([uri] "https://sourceforge.net/projects/") Description = "Базовый адрес проектов на 'https://sourceforge.net/'" Option = 'Constant' } Set-Variable @ConstantBaseUriProjectParam } $ProjectUri = [uri]::new($BaseUriProject, ([uri] $Url).Segments[2]) $Responce = Invoke-WebRequest -uri $ProjectUri -UseBasicParsing if ($Responce.StatusCode -ne 200) { Throw "{0} - {1}" -f $Responce.StatusCode, $Responce.StatusDescription } $UserAgent = "Wget" $Field = "class" $Match = "button download big-text green" $Link = $Responce.Links | Where-Object $Field -Match $Match -ErrorAction SilentlyContinue $APIProjectUri = [uri]::new($ProjectUri, $Link.href) $Name = $link.title -replace "Download " -replace " from SourceForge.+" $File = Join-Path -Path $Path -ChildPath $Name $IWRParams = @{ UserAgent = $UserAgent Uri = $APIProjectUri Method = "Head" UseBasicParsing = $true } $Responce = Invoke-WebRequest @IWRParams if ($Responce.StatusCode -ne 200) { Throw "{0} - {1}" -f $Responce.StatusCode, $Responce.StatusDescription } $Properties = [ordered] @{ Url = $Url Uri = $APIProjectUri Type = [string] $Responce.Headers."Content-Type" Size = [long] $Responce.Headers."Content-Length" Modified = [datetime] $Responce.Headers."Last-Modified" Path = $Path Name = $Name File = $File } New-Object -TypeName PSCustomObject -Property $Properties if ([System.IO.File]::Exists($File)) { $Size = [System.IO.FileInfo]::new($File).Length Throw "Файл: {0} [{1} byte] - уже существует!" -f $File, $Size } $IWRParams = @{ UserAgent = $Useragent Uri = $APIProjectUri OutFile = $File UseBasicParsing = $true } Invoke-WebRequest @IWRParams } |
|