Skip to content
This repository has been archived by the owner on Feb 19, 2019. It is now read-only.

Get dynamic download url using RegEx #227

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/helpers/chocolateyInstaller.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -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
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
45 changes: 45 additions & 0 deletions src/helpers/functions/Get-FilenameFromRegex.ps1
Original file line number Diff line number Diff line change
@@ -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
}
}