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
77 stars 18 forks source link

[New App request] PostgreSQL #121

Open AScott-WWF opened 1 month ago

AScott-WWF commented 1 month ago

Seen a request for PostgreSQL to be included with Evergreen (https://github.com/aaronparker/evergreen/issues/747), but Aaron has been unable to find a source, so I have quickly knocked up this script which queries the downloads page: https://www.enterprisedb.com/downloads/postgres-postgresql-downloads It's not the best code I'll admit (partly due to the webpage layout)- But I'm sure with time it could be improved!

# Get-PostgreSQL.ps1

$AppName = "PostgreSQL"

# Define the URL of the PostgreSQL Downloads page
$Releaseurl = "https://www.enterprisedb.com/downloads/postgres-postgresql-downloads"

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

# Use Invoke-WebRequest to get the HTML content
$response = Invoke-WebRequest -Uri $Releaseurl

# Load the HTML content
$htmlContent = $response.Content

# Use regex to extract the links for Windows x86-64 and x86-32
$pattern = '<tr.*?>.*?<td.*?>(.*?)<\/td>.*?<td.*?>(.*?)<\/td>.*?<td.*?>(.*?)<\/td>.*?<td.*?>(.*?)<\/td>.*?<td.*?>(.*?)<\/td>.*?<td.*?>(.*?)<\/td>.*?<\/tr>'
$patternmatches = [regex]::Matches($htmlContent, $pattern)

# Initialize an array to hold the links
$links = @()

# Loop through each match
foreach ($match in $patternmatches) {
    # Extract the PostgreSQL Version from table (in case version can not be obtained from URL)
    $PostgreSQLVersion = $match.Groups[1].Value
    # Extract the href links for Windows x86-64 and x86-32
    $x64Link = $match.Groups[5].Value -replace '.*?href="(.*?)".*', '$1'
    $x32Link = $match.Groups[6].Value -replace '.*?href="(.*?)".*', '$1'

    # Add the links to the array if they exist
    if ($x64Link) {
        $Arch = "x64"
        $SearchCount = 1 # This may need increasing as future versions are released
        if ($x64Link -like "*getfile.jsp?fileid=*"){
            $URL = (Resolve-Uri -Uri $x64Link).Uri
            $Version = Get-Version -String $URL -ReplaceWithDot
        } elseif ($x64Link -like "/postgres-postgresql-downloads"){
            $URL = Set-UriPrefix -Uri $x64Link -Prefix 'https://www.enterprisedb.com'
        } else {
            $URL = $x64Link
            $Version = Get-Version -String $URL -ReplaceWithDot
        }
        if ($null -eq $Version) {
            $Version = $PostgreSQLVersion
        }
        Write-Verbose "Version $($Version) 64-bit download: $($x64Link)"
        do {
            if ($URL) {
                New-NevergreenApp -Name $($AppName) -Architecture $($Arch) -Version $Version -Uri $($URL) -Type "exe"
                break
            }

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

        if ($SearchCount -eq 0) {
            Write-Warning "Could not find release for $($AppName) $($Version) $($Arch)"
        }
    }
    $URL = $null
    $Version = $null
    $Arch = $null

    if ($x32Link) {
        $Arch = "x86"
        $SearchCount = 1 # This may need increasing as future versions are released
        if ($x32Link -ne "<span>Not supported</span>"){
            if ($x32Link -like "*getfile.jsp?fileid=*"){
                $URL = (Resolve-Uri -Uri $x32Link).Uri
                $Version = Get-Version -String $URL -ReplaceWithDot
            } elseif ($x32Link -like "/postgres-postgresql-downloads"){
                $URL = Set-UriPrefix -Uri $x32Link -Prefix 'https://www.enterprisedb.com'
            } else {
                $URL = $x32Link
                $Version = Get-Version -String $URL -ReplaceWithDot
            }
        }
        if ($null -eq $Version) {
            $Version = $PostgreSQLVersion
        }
        Write-Verbose "Version $($Version) 32-bit download: $($x32Link)"
        do {
            if ($URL) {
                New-NevergreenApp -Name $($AppName) -Architecture $($Arch) -Version $($Version) -Uri $($URL) -Type "exe"
                break
            }

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

        if ($SearchCount -eq 0) {
            Write-Warning "Could not find release for $($AppName) $($Version) $($Arch)"
        }
    }
    $URL = $null
    $Version = $null
    $Arch = $null
}
bmitchell76 commented 2 weeks ago

@DanGough, will this be added to Nevergreen?

DanGough commented 2 weeks ago

Happy to add this eventually. However my plans are currently:

I should have some time to get on with this later in October.