From 2be4fba53b722926e0b4ca79966031594f6c9e1f Mon Sep 17 00:00:00 2001 From: Redsandro Media Date: Mon, 7 Jan 2013 16:26:02 +0100 Subject: [PATCH] Get dynamic download url using RegEx --- src/helpers/chocolateyInstaller.psm1 | 2 +- .../functions/Get-FilenameFromRegex.ps1 | 45 +++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 src/helpers/functions/Get-FilenameFromRegex.ps1 diff --git a/src/helpers/chocolateyInstaller.psm1 b/src/helpers/chocolateyInstaller.psm1 index d04b2c9..6aa39f5 100644 --- a/src/helpers/chocolateyInstaller.psm1 +++ b/src/helpers/chocolateyInstaller.psm1 @@ -9,4 +9,4 @@ Resolve-Path $helpersPath\functions\*.ps1 | ? { -not ($_.ProviderPath.Contains(".Tests.")) } | % { . $_.ProviderPath } -Export-ModuleMember -Function Start-ChocolateyProcessAsAdmin, Install-ChocolateyPackage, Uninstall-ChocolateyPackage, Install-ChocolateyZipPackage, Install-ChocolateyPowershellCommand, Get-ChocolateyWebFile, Install-ChocolateyInstallPackage, Get-ChocolateyUnzip, Write-ChocolateySuccess, Write-ChocolateyFailure, Install-ChocolateyPath, Install-ChocolateyDesktopLink, Install-ChocolateyPinnedTaskBarItem, Install-ChocolateyExplorerMenuItem, Install-ChocolateyFileAssociation, Install-ChocolateyEnvironmentVariable, Write-Host, Write-Debug, Write-Error, Update-SessionEnvironment, Install-ChocolateyVsixPackage \ No newline at end of file +Export-ModuleMember -Function Start-ChocolateyProcessAsAdmin, Install-ChocolateyPackage, Uninstall-ChocolateyPackage, Install-ChocolateyZipPackage, Install-ChocolateyPowershellCommand, Get-ChocolateyWebFile, Install-ChocolateyInstallPackage, Get-ChocolateyUnzip, Get-FilenameFromRegex, Write-ChocolateySuccess, Write-ChocolateyFailure, Install-ChocolateyPath, Install-ChocolateyDesktopLink, Install-ChocolateyPinnedTaskBarItem, Install-ChocolateyExplorerMenuItem, Install-ChocolateyFileAssociation, Install-ChocolateyEnvironmentVariable, Write-Host, Write-Debug, Write-Error, Update-SessionEnvironment, Install-ChocolateyVsixPackage diff --git a/src/helpers/functions/Get-FilenameFromRegex.ps1 b/src/helpers/functions/Get-FilenameFromRegex.ps1 new file mode 100644 index 0000000..ff06581 --- /dev/null +++ b/src/helpers/functions/Get-FilenameFromRegex.ps1 @@ -0,0 +1,45 @@ +# Some files have a changing element/hash in the download path, making an automated download harder. +# This file gets the correct download url from a download page with a regular expression. +# Good for e.g. Foobar2000, Rename Master, etc +# +# Usage: Get-FilenameFromRegex download_page find replace +# Examples: +# Get-FilenameFromRegex "http://www.joejoesoft.com/vcms/hot/userupload/8/files/rmv303.zip" '/cms/file.php\?f=userupload/8/files/rmv303.zip&c=([\w\d]+)' 'http://www.joejoesoft.com/sim/$1/userupload/8/files/rmv303.zip' +# +# Remember to escape regex characters, like the question mark in the querystring + +function Get-FilenameFromRegex { + param( + [string]$source_url, + [string]$find, + [string]$replace + ) + + try { + + ## Example # $source_url = "http://www.joejoesoft.com/vcms/hot/userupload/8/files/rmv303.zip" + ## Example # $find = '/cms/file.php\?f=userupload/8/files/rmv303.zip&c=([\w\d]+)' + ## Example # $replace = 'http://www.joejoesoft.com/sim/$1/userupload/8/files/rmv303.zip' + + # Download and open download page + $tempfile = "chocolatey_temp_download.html" + $wc = new-object system.net.webclient + $wc.UseDefaultCredentials = $true + $wc.downloadfile($source_url, $tempfile) + $source = Get-Content $tempfile + + $nothing = ''+$source -cmatch $find # Get the matches, prevent output + # Replace Match-references with previous matches: $1 $2 -> $matches[1] $matches[2] + #$download_url = $replace -creplace "\$`(\d+)",$matches[$1] + # This one is not working :( + # I have a temporary fix that only allows ONE match. However, in most cases we only need one match. + $download_url = $replace -creplace "\$`(\d+)",$matches[1] + + return $download_url + + } catch { + $errorMessage = "Could not get Regex from download page." + Write-Error $errorMessage + throw $errorMessage + } +}