skip to Main Content

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?

2

Answers


  1. Chosen as BEST ANSWER

    My Code:

                $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
                    
                    $dispositionHeader = $Response.Headers['Content-Disposition']
                    $disposition = [System.Net.Mime.ContentDisposition]::new($dispositionHeader)
                    
                    $fileName = $disposition.FileName
                    $response.Close()
                } catch {
                    "Error: $_"
                }
                Invoke-WebRequest -URI $redirectedURL -OutFile ".APKs$fileName"
        
    

  2. 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
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search