colombeen

Simple web downloader with progress bar

13 posts in this topic

#1 ·  Posted (edited)

Hi guys, I needed a simple function to download files and show the progress of the download.

I commented a bit (don't know if i should have added more)

You can provide the url to the file, the wanted file name, the visible name for in the download progress UI, where the file should be downloaded to, if the progressbar should be hidden at the end of the function or if it should remain visible, how long the last message (completed/failed) should show and last but not least, a title for the progress window.

At the end of the function you either get the full path of the download file or false when it fails with @error set and @extended set with the error code of the download.

#include <InetConstants.au3>

Func _webDownloader($sSourceURL, $sTargetName, $sVisibleName, $sTargetDir = @TempDir, $bProgressOff = True, $iEndMsgTime = 2000, $sDownloaderTitle = "MyDownloader")
    ; Declare some general vars
    Local $iMBbytes = 1048576

    ; If the target directory doesn't exist -> create the dir
    If Not FileExists($sTargetDir) Then DirCreate($sTargetDir)

    ; Get download and target info
    Local $sTargetPath = $sTargetDir & "\" & $sTargetName
    Local $iFileSize = InetGetSize($sSourceURL)
    Local $hFileDownload = InetGet($sSourceURL, $sTargetPath, $INET_LOCALCACHE, $INET_DOWNLOADBACKGROUND)

    ; Show progress UI
    ProgressOn($sDownloaderTitle, "Downloading " & $sVisibleName)

    ; Keep checking until download completed
    Do
        Sleep(250)

        ; Set vars
        Local $iDLPercentage = Round(InetGetInfo($hFileDownload, $INET_DOWNLOADREAD) * 100 / $iFileSize, 0)
        Local $iDLBytes = Round(InetGetInfo($hFileDownload, $INET_DOWNLOADREAD) / $iMBbytes, 2)
        Local $iDLTotalBytes = Round($iFileSize / $iMBbytes, 2)

        ; Update progress UI
        If IsNumber($iDLBytes) And $iDLBytes >= 0 Then
            ProgressSet($iDLPercentage, $iDLPercentage & "% - Downloaded " & $iDLBytes & " MB of " & $iDLTotalBytes & " MB")
        Else
            ProgressSet(0, "Downloading '" & $sVisibleName & "'")
        EndIf
    Until InetGetInfo($hFileDownload, $INET_DOWNLOADCOMPLETE)

    ; If the download was successfull, return the target location
    If InetGetInfo($hFileDownload, $INET_DOWNLOADSUCCESS) Then
        ProgressSet(100, "Downloading '" & $sVisibleName & "' completed")
        If $bProgressOff Then
            Sleep($iEndMsgTime)
            ProgressOff()
        EndIf
        Return $sTargetPath
    ; If the download failed, set @error and return False
    Else
        Local $errorCode = InetGetInfo($hFileDownload, $INET_DOWNLOADERROR)
        ProgressSet(0, "Downloading '" & $sVisibleName & "' failed." & @CRLF & "Error code: " & $errorCode)
        If $bProgressOff Then
            Sleep($iEndMsgTime)
            ProgressOff()
        EndIf
        SetError(1, $errorCode, False)
    EndIf
EndFunc   ;==>_webDownloader

Let me know what you think :)

The test in my example is done with the installer for java 8 update 65

Example usage :

$url = "http://javadl.sun.com/webapps/download/AutoDL?BundleId=111687"
$file = "Java_8_Update_65.exe"
$name = "Java 8 Update 65"
$dir = @TempDir & "\MyDownloader"
$installcommand = " /s STATIC=Disable AUTO_UPDATE=Disable WEB_JAVA=Enable WEB_JAVA_SECURITY_LEVEL=H WEB_ANALYTICS=Disable EULA=Enable REBOOT=Disable SPONSORS=Disable"

$test = _webDownloader($url, $file, $name, $dir, False)

If $test Then
    ProgressSet(100, "Running silent installation...", "Installing " & $name)
    $exitCode = RunWait($test & $installcommand)
    If $exitCode = 0 Then ProgressSet(100, "Installation completed")
    If $exitCode <> 0 Then ProgressSet(0, "Installation failed" & @CRLF & "Exit code: " & $exitCode)
    Sleep(3000)
    ProgressOff()
    FileDelete($test)
Else
    ProgressOff()
EndIf

Greetz

_webDownloader.au3

Edited by colombeen
New version with some small changes
1 person likes this

Share this post


Link to post
Share on other sites



Made some edits and added the function as a file to download.

Comments wanted!!! so that I might be able to make it better :)

Share this post


Link to post
Share on other sites

Made some more little changes. nothing to fancy :)

I guess nobody is interested in this kind of thing since there aren't any replies/comments

Share this post


Link to post
Share on other sites

I'll offer a thank you.  It works as advertised on Win7 and I'm glad to have it available.  It's just the sort of thing scripting is a natural for.  That said, I would guess that not many people have need at the moment.  But it will be here when they do.

Good work!

 

Share this post


Link to post
Share on other sites

I'll offer a thank you.  It works as advertised on Win7 and I'm glad to have it available.  It's just the sort of thing scripting is a natural for.  That said, I would guess that not many people have need at the moment.  But it will be here when they do.

Good work!

 

Thanks for the reply qwert. Yeah must be that most of this community doesn't need this atm.

I'll keep updating my first post when i make more changes.

1 person likes this

Share this post


Link to post
Share on other sites

@colembeen .. I would not worry about not getting feedback. There is too much "if likes < n then failure = 1" culture around.

I am quite sure that there are many who used all / parts of your script, and did not find time / need / whatever to write a thank you.

 

I guess I could find a good use of you script at some point, so from here also a wave of the flag, hurray and THANK YOU.

But don't give up. Remember there are LOTS of scripts being posted here every day. Getting lots of feedback is like getting 1 million YT views. Does not mean what you did is bad though.

1 person likes this

I am just a hobby programmer, and nothing great to publish right now.

Share this post


Link to post
Share on other sites

Hey, Colombeen!

I am reviewing your function and I like it a lot. I am new to AutoIt, so it is helping me to look at things a little differently.

One thing I am not quite wrapping my head around, though, is the $bProgressOff variable's use. You initialize the boolean to true but do not do anything to set it again. Later in your script, after the do...until loop you check the boolean with an If. Well, since it has not been changed, it should always be true. Then, once the If reads it as true, you use a sleep for $iEndMsgTime.

So, my question is, what is the point of the boolean and the EndMsgTime integer?

Thanks!



The snippet I am referring to.

If $bProgressOff Then
            Sleep($iEndMsgTime)
            ProgressOff()
        EndIf


 

Share this post


Link to post
Share on other sites
On 24-2-2016 at 4:47 PM, ericbartha said:

Hey, Colombeen!

I am reviewing your function and I like it a lot. I am new to AutoIt, so it is helping me to look at things a little differently.

One thing I am not quite wrapping my head around, though, is the $bProgressOff variable's use. You initialize the boolean to true but do not do anything to set it again. Later in your script, after the do...until loop you check the boolean with an If. Well, since it has not been changed, it should always be true. Then, once the If reads it as true, you use a sleep for $iEndMsgTime.

So, my question is, what is the point of the boolean and the EndMsgTime integer?

Thanks!



The snippet I am referring to.

If $bProgressOff Then
            Sleep($iEndMsgTime)
            ProgressOff()
        EndIf


 

Hi :)

The point of the ProgressOff is to enable you to interact with the function after dowloading a file.

In the function it's "True" by default, so when you leave it like that, it just shows the progress for the amount of time set and then it disappears.

When you set it to "False" you could use it to say something like installing in the background, ... like in my example.

I hope that explains it enough.

Greetz :)

Share this post


Link to post
Share on other sites

Thanks for sharing! :thumbsup:

 


AutoIt.4.Life Clubrooms - Life is like a Donut (secret key)

Make sure brain is in gear before opening mouth!
Remember, what is not said, can be just as important as what is said.

Spoiler

What is the Secret Key? Life is like a Donut

If I put effort into communication, I expect you to read properly & fully, or just not comment.
Ignoring those who try to divert conversation with irrelevancies.
If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it.
I'm only big and bad, to those who have an over-active imagination.

I may have the Artistic Liesense ;) to disagree with you. TheSaint's Toolbox (be advised many downloads are not working due to ISP screwup with my storage)

userbar.png

Share this post


Link to post
Share on other sites

Hey, could you please explain what you would use this for? I guess it might protect you against a malicious website? 

Share this post


Link to post
Share on other sites
On ‎1‎/‎03‎/‎2016 at 11:12 AM, iloveyou said:

Hey, could you please explain what you would use this for? I guess it might protect you against a malicious website? 

I use this to download files without the need for a user to interact with a specific website etc. Like this they see that something is happening without the risk of a user error.

you provide the path and the script downloads the file to the provided location.

You could use this for downloading default software or for retrieving a config file you need etc, ...

(sorry for my late response, was a little busy the last few days)

Share this post


Link to post
Share on other sites

Thanks for sharing. :drool:

Let me share with you another example using WinHTTP.au3 > https://www.autoitscript.com/forum/topic/84133-winhttp-functions/

A friend gave this code to me a while ago while helping me.
 

#include "WinHttp.au3"

; Download some gif
;~ http://33.media.tumblr.com/dd3ffab90cc338666f192fd86f6a4f8f/tumblr_n0pefhIpss1swyb6ao1_500.gif

; Initialize and get session handle
$hOpen = _WinHttpOpen()
; Get connection handle
$hConnect = _WinHttpConnect($hOpen, "http://33.media.tumblr.com")
; Specify the reguest
$hRequest = _WinHttpOpenRequest($hConnect, Default, "dd3ffab90cc338666f192fd86f6a4f8f/tumblr_n0pefhIpss1swyb6ao1_500.gif")

; Send request
_WinHttpSendRequest($hRequest)

; Wait for the response
_WinHttpReceiveResponse($hRequest)

;~ ConsoleWrite(_WinHttpQueryHeaders($hRequest) & @CRLF)

ProgressOn("Downloading", "In Progress...")
Progress(_WinHttpQueryHeaders($hRequest, $WINHTTP_QUERY_CONTENT_LENGTH))

Local $sData
; Check if there is data available...
If _WinHttpQueryDataAvailable($hRequest) Then
    While 1
        $sChunk = _WinHttpReadData_Ex($hRequest, Default, Default, Default, Progress)
        If @error Then ExitLoop
        $sData &= $sChunk
        Sleep(20)
    WEnd
Else
    MsgBox(48, "Error", "Site is experiencing problems (or you).")
EndIf

Sleep(1000)
ProgressOff()

; Close handles
_WinHttpCloseHandle($hRequest)
_WinHttpCloseHandle($hConnect)
_WinHttpCloseHandle($hOpen)

; Do whatever with data, write to some file or whatnot. I'll just print it to console here:
ConsoleWrite($sData & @CRLF)
Local $hFile = FileOpen(@DesktopDir & "\test.gif", 26)
FileWrite($hFile, $sData)
FileClose($hFile)




Func Progress($iSizeAll, $iSizeChunk = 0)
    Local Static $iMax, $iCurrentSize
    If $iSizeAll Then $iMax = $iSizeAll
    $iCurrentSize += $iSizeChunk
    Local $iPercent = Round($iCurrentSize / $iMax * 100, 0)
    ProgressSet($iPercent, $iPercent & " %")
EndFunc


Func _WinHttpReadData_Ex($hRequest, $iMode = Default, $iNumberOfBytesToRead = Default, $pBuffer = Default, $vFunc = Default)
    __WinHttpDefault($iMode, 0)
    __WinHttpDefault($iNumberOfBytesToRead, 8192)
    __WinHttpDefault($vFunc, 0)
    Local $tBuffer, $vOutOnError = ""
    If $iMode = 2 Then $vOutOnError = Binary($vOutOnError)
    Switch $iMode
        Case 1, 2
            If $pBuffer And $pBuffer <> Default Then
                $tBuffer = DllStructCreate("byte[" & $iNumberOfBytesToRead & "]", $pBuffer)
            Else
                $tBuffer = DllStructCreate("byte[" & $iNumberOfBytesToRead & "]")
            EndIf
        Case Else
            $iMode = 0
            If $pBuffer And $pBuffer <> Default Then
                $tBuffer = DllStructCreate("char[" & $iNumberOfBytesToRead & "]", $pBuffer)
            Else
                $tBuffer = DllStructCreate("char[" & $iNumberOfBytesToRead & "]")
            EndIf
    EndSwitch
    Local $sReadType = "dword*"
    If BitAND(_WinHttpQueryOption(_WinHttpQueryOption(_WinHttpQueryOption($hRequest, $WINHTTP_OPTION_PARENT_HANDLE), $WINHTTP_OPTION_PARENT_HANDLE), $WINHTTP_OPTION_CONTEXT_VALUE), $WINHTTP_FLAG_ASYNC) Then $sReadType = "ptr"
    Local $aCall = DllCall($hWINHTTPDLL__WINHTTP, "bool", "WinHttpReadData", _
            "handle", $hRequest, _
            "struct*", $tBuffer, _
            "dword", $iNumberOfBytesToRead, _
            $sReadType, 0)
    If @error Or Not $aCall[0] Then Return SetError(1, 0, "")
    If Not $aCall[4] Then Return SetError(-1, 0, $vOutOnError)
    If IsFunc($vFunc) Then $vFunc(0, $aCall[4])
    If $aCall[4] < $iNumberOfBytesToRead Then
        Switch $iMode
            Case 0
                Return SetExtended($aCall[4], StringLeft(DllStructGetData($tBuffer, 1), $aCall[4]))
            Case 1
                Return SetExtended($aCall[4], BinaryToString(BinaryMid(DllStructGetData($tBuffer, 1), 1, $aCall[4]), 4))
            Case 2
                Return SetExtended($aCall[4], BinaryMid(DllStructGetData($tBuffer, 1), 1, $aCall[4]))
        EndSwitch
    Else
        Switch $iMode
            Case 0, 2
                Return SetExtended($aCall[4], DllStructGetData($tBuffer, 1))
            Case 1
                Return SetExtended($aCall[4], BinaryToString(DllStructGetData($tBuffer, 1), 4))
        EndSwitch
    EndIf
EndFunc

Now this is how you download a file with progessbar ;)

1 person likes this

Share this post


Link to post
Share on other sites

@iloveyou; this would not necessarily protect you against a malicious site. If the file you are targeting contains the malicious content, it would be downloaded all the same. I think something like this would be most useful automating the first time set up of a new node. You know you always need to install Chrome, and that file will likely always be in the same place, under the same name, so you could automate the retrieval and install of it.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now