Jump to content

Simple web downloader with progress bar


colombeen
 Share

Recommended Posts

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
Link to comment
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!

 

Link to comment
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.

Link to comment
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.

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

Link to comment
Share on other sites

  • 3 months later...

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


 

Link to comment
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 :)

Link to comment
Share on other sites

Thanks for sharing! :thumbsup:

 

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

Link to comment
Share on other sites

  • 2 weeks later...
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)

Link to comment
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 ;)

Link to comment
Share on other sites

  • 2 weeks later...

@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.

Link to comment
Share on other sites

  • 5 years later...

Today I solve some problem which I had with @AutID code.

My finall code is here:

 

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

Spoiler

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind. 

My contribution (my own projects): * Debenu Quick PDF Library - UDF * Debenu PDF Viewer SDK - UDF * Acrobat Reader - ActiveX Viewer * UDF for PDFCreator v1.x.x * XZip - UDF * AppCompatFlags UDF * CrowdinAPI UDF * _WinMergeCompare2Files() * _JavaExceptionAdd() * _IsBeta() * Writing DPI Awareness App - workaround * _AutoIt_RequiredVersion() * Chilkatsoft.au3 UDF * TeamViewer.au3 UDF * JavaManagement UDF * VIES over SOAP * WinSCP UDF * GHAPI UDF - modest begining - comunication with GitHub REST APIErrorLog.au3 UDF - A logging Library * Include Dependency Tree (Tool for analyzing script relations) * Show_Macro_Values.au3 *

 

My contribution to others projects or UDF based on  others projects: * _sql.au3 UDF  * POP3.au3 UDF *  RTF Printer - UDF * XML.au3 UDF * ADO.au3 UDF SMTP Mailer UDF * Dual Monitor resolution detection * * 2GUI on Dual Monitor System * _SciLexer.au3 UDF * SciTE - Lexer for console pane

Useful links: * Forum Rules * Forum etiquette *  Forum Information and FAQs * How to post code on the forum * AutoIt Online Documentation * AutoIt Online Beta Documentation * SciTE4AutoIt3 getting started * Convert text blocks to AutoIt code * Games made in Autoit * Programming related sites * Polish AutoIt Tutorial * DllCall Code Generator * 

Wiki: Expand your knowledge - AutoIt Wiki * Collection of User Defined Functions * How to use HelpFile * Good coding practices in AutoIt * 

OpenOffice/LibreOffice/XLS Related: WriterDemo.au3 * XLS/MDB from scratch with ADOX

IE Related:  * How to use IE.au3  UDF with  AutoIt v3.3.14.x * Why isn't Autoit able to click a Javascript Dialog? * Clicking javascript button with no ID * IE document >> save as MHT file * IETab Switcher (by LarsJ ) * HTML Entities * _IEquerySelectorAll() (by uncommon) * IE in TaskSchedulerIE Embedded Control Versioning (use IE9+ and HTML5 in a GUI) * PDF Related:How to get reference to PDF object embeded in IE * IE on Windows 11

I encourage you to read: * Global Vars * Best Coding Practices * Please explain code used in Help file for several File functions * OOP-like approach in AutoIt * UDF-Spec Questions *  EXAMPLE: How To Catch ConsoleWrite() output to a file or to CMD *

I also encourage you to check awesome @trancexx code:  * Create COM objects from modules without any demand on user to register anything. * Another COM object registering stuffOnHungApp handlerAvoid "AutoIt Error" message box in unknown errors  * HTML editor

winhttp.au3 related : * https://www.autoitscript.com/forum/topic/206771-winhttpau3-download-problem-youre-speaking-plain-http-to-an-ssl-enabled-server-port/

"Homo sum; humani nil a me alienum puto" - Publius Terentius Afer
"Program are meant to be read by humans and only incidentally for computers and execute" - Donald Knuth, "The Art of Computer Programming"
:naughty:  :ranting:, be  :) and       \\//_.

Anticipating Errors :  "Any program that accepts data from a user must include code to validate that data before sending it to the data store. You cannot rely on the data store, ...., or even your programming language to notify you of problems. You must check every byte entered by your users, making sure that data is the correct type for its field and that required fields are not empty."

Signature last update: 2023-04-24

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...