DanGough / Nevergreen

This module is an alternative to Evergreen, and allows you to find the latest version and download URL for various Windows apps. Evergreen uses API queries to obtain its data whereas this module is more focussed on web scraping. This is more prone to breaking when websites are changed, hence the name.
The Unlicense
71 stars 16 forks source link

[New App Request] PDFsam (On Proposals list) #90

Open AScott-WWF opened 2 days ago

AScott-WWF commented 2 days ago

This will return results for PDFsam Basic, Enhanced or Visual in exe, msi or zip format N.B. exe files do not resolve to a filename per se, but download from presented url will succeed provided an output filename is supplied, e.g:

(get-nevergreenApp -Name PDFsam | Where-Object {($_.Type -eq 'exe') -and ($_.Version -eq '7.0')}).Uri
https://downloadenhanced7.pdfsam.org/download.ashx?productcode=pdfsam$params=configld=4bf2c0be-8a73-4735-8bf6-b68bfe282ff6&uid=1007261&cmp=pdfsam_enhanced&ref=pdfsam.org/in-app&wid=6848

iwr ((get-nevergreenApp -Name PDFsam | Where-Object {($_.Type -eq 'exe') -and ($_.Version -eq '7.0')}).Uri) -OutFile .\Downloads\PDFsamenhanced7.exe

Script:

# Get-PDFsam.ps1

# Define AppName
$AppName = "PDFsam"

$ReleaseUrl = "https://pdfsam.org/downloads/"

Write-Verbose "Obtaining $($AppName) Release Versions from $($ReleaseUrl)...`n"

# Main script to fetch and process links
$AppVersions = @(
    @{AppName = "$($AppName) Basic"; Architecture = 'multi'; Type = 'exe'; Url = 'https://pdfsam.org/download-pdfsam-basic/'; Pattern = 'Windows downloader'; VersionPattern = 'Download PDFsam Basic v(\d+\.\d+\.\d+)'}
    @{AppName = "$($AppName) Basic"; Architecture = 'multi'; Type = 'msi'; Url = 'https://pdfsam.org/download-pdfsam-basic/'; Pattern = '\.msi'; VersionPattern = 'Download.PDFsam.Basic.v(\d+\.\d+\.\d+)'}
    @{AppName = "$($AppName) Basic"; Architecture = 'multi'; Type = 'zip'; Url = 'https://pdfsam.org/download-pdfsam-basic/'; Pattern = 'windows\.zip'; VersionPattern = 'Download.PDFsam.Basic.v(\d+\.\d+\.\d+)'}
    @{AppName = "$($AppName) Enhanced"; Architecture = 'multi'; Type = 'exe'; Url = 'https://pdfsam.org/download-pdfsam-enhanced/'; Pattern = 'Download PDFsam Enhanced'; VersionPattern = '>(\d+\.\d+)</div>'}
    @{AppName = "$($AppName) Visual"; Architecture = 'x64'; Type = 'msi'; Url = 'https://pdfsam.org/download-pdfsam-visual/'; Pattern = '_x64\.msi'; VersionPattern = '<h3> Download PDFsam Visual v(\d+\.\d+\.\d+)</h3>'}
    @{AppName = "$($AppName) Visual"; Architecture = 'x64'; Type = 'zip'; Url = 'https://pdfsam.org/download-pdfsam-visual/'; Pattern = '_x64\.zip'; VersionPattern = '<h3> Download PDFsam Visual v(\d+\.\d+\.\d+)</h3>'}
    @{AppName = "$($AppName) Visual"; Architecture = 'x86'; Type = 'msi'; Url = 'https://pdfsam.org/download-pdfsam-visual/'; Pattern = '_ia32\.msi'; VersionPattern = '<h3> Download PDFsam Visual v(\d+\.\d+\.\d+)</h3>'}
    @{AppName = "$($AppName) Visual"; Architecture = 'x86'; Type = 'zip'; Url = 'https://pdfsam.org/download-pdfsam-visual/'; Pattern = '_ia32\.zip'; VersionPattern = '<h3> Download PDFsam Visual v(\d+\.\d+\.\d+)</h3>'}
)

foreach ($AppVersion in $AppVersions) {
    $SearchCount = 1 # This may need increasing as future versions are released

    #Build each link with File Type specific versions
    if ($($AppVersion.Type -ne 'exe')) {
        $URL = Get-Link -Uri $($AppVersion.Url) -MatchProperty href -Pattern $AppVersion.Pattern
        [version]$Version = Get-Version -String $URL
    } else {
        $URL = (Resolve-Uri -Uri (Get-Link -Uri $($AppVersion.Url) -MatchProperty OuterHTML -Pattern $AppVersion.Pattern)).Uri
        [version]$Version = Get-Version -Uri $($AppVersion.Url) -Pattern $($AppVersion.VersionPattern)
    }
    do {
        if ($URL) {
            New-NevergreenApp -Name $($AppVersion.AppName) -Version $($Version) -Uri $($URL) -Architecture $($AppVersion.Architecture) -Type $($AppVersion.Type)
            break
        }

        $SearchCount--
    } until ($SearchCount -eq 0)

    if ($SearchCount -eq 0) {
        Write-Warning "Could not find release for $($AppName) $($AppVersion.Type)"
    }
}

$InstallInstructionsUrl = "https://pdfsam.org/silent-install/"
$ReleaseNotesUrl = "https://github.com/torakiki/pdfsam/releases"
Write-Verbose "$($AppName) Release notes are available here: $($ReleaseNotesUrl)"
Write-Verbose "$($AppName) Install instructions are available here: $($InstallInstructionsUrl)"