Search code examples
windowspowershelldownloadinvoke-webrequest

Downloading latest version of file from website / Getting latest download Link from button (Windows 10)


I need to download the newest version of a bunch of files. That works pretty straight forward using direct links.

The links I need to use look something like this: https://d.apkpure.com/b/APK/org.telegram.messenger?version=latest It's the link of the download button, which generates a new direct download link every time.

In the web browser this link will download the latest Telegram .apk. I can not use the direct download link in my script since it will change, and the link to the button will result in a html file.

I tried it using curl, Invoke-Webrequest and DownloadFile(). It works seamlessly using the direct download links generated when using a web browser. I'd like to generate this download link using a script.

How do I use this download button to always get the latest file/latest download link?


Solution

  • To get final redirected url use this script:

    $URL= "https://d.apkpure.com/b/APK/org.telegram.messenger?version=latest"
    $request = [System.Net.WebRequest]::Create($URL)
    $request.AllowAutoRedirect=$true
    $request.UserAgent = 'Mozilla/5.0 (Windows NT; Windows NT 10.0; en-US) AppleWebKit/534.6 (KHTML, like Gecko) Chrome/7.0.500.0 Safari/534.6' #helps with difficult pages...
    
    try{
        $response = $request.GetResponse()
        $redirectedURL = $response.ResponseUri.AbsoluteUri
        $response.Close()
    } catch {
        "Error: $_"
    }
    

    Then you can download via Invoke-WebRequest

    Invoke-WebRequest -URI $redirectedURL -OutFile ".\a.apk" -user